Blame lib/close.c

Packit 8f70b4
/* close replacement.
Packit 8f70b4
   Copyright (C) 2008-2018 Free Software Foundation, Inc.
Packit 8f70b4
Packit 8f70b4
   This program is free software: you can redistribute it and/or modify
Packit 8f70b4
   it under the terms of the GNU General Public License as published by
Packit 8f70b4
   the Free Software Foundation; either version 3 of the License, or
Packit 8f70b4
   (at your option) any later version.
Packit 8f70b4
Packit 8f70b4
   This program is distributed in the hope that it will be useful,
Packit 8f70b4
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 8f70b4
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 8f70b4
   GNU General Public License for more details.
Packit 8f70b4
Packit 8f70b4
   You should have received a copy of the GNU General Public License
Packit 8f70b4
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 8f70b4
Packit 8f70b4
#include <config.h>
Packit 8f70b4
Packit 8f70b4
/* Specification.  */
Packit 8f70b4
#include <unistd.h>
Packit 8f70b4
Packit 8f70b4
#include <errno.h>
Packit 8f70b4
Packit 8f70b4
#include "fd-hook.h"
Packit 8f70b4
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 8f70b4
# include "msvc-inval.h"
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#undef close
Packit 8f70b4
Packit 8f70b4
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 8f70b4
static int
Packit 8f70b4
close_nothrow (int fd)
Packit 8f70b4
{
Packit 8f70b4
  int result;
Packit 8f70b4
Packit 8f70b4
  TRY_MSVC_INVAL
Packit 8f70b4
    {
Packit 8f70b4
      result = close (fd);
Packit 8f70b4
    }
Packit 8f70b4
  CATCH_MSVC_INVAL
Packit 8f70b4
    {
Packit 8f70b4
      result = -1;
Packit 8f70b4
      errno = EBADF;
Packit 8f70b4
    }
Packit 8f70b4
  DONE_MSVC_INVAL;
Packit 8f70b4
Packit 8f70b4
  return result;
Packit 8f70b4
}
Packit 8f70b4
#else
Packit 8f70b4
# define close_nothrow close
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
/* Override close() to call into other gnulib modules.  */
Packit 8f70b4
Packit 8f70b4
int
Packit 8f70b4
rpl_close (int fd)
Packit 8f70b4
{
Packit 8f70b4
#if WINDOWS_SOCKETS
Packit 8f70b4
  int retval = execute_all_close_hooks (close_nothrow, fd);
Packit 8f70b4
#else
Packit 8f70b4
  int retval = close_nothrow (fd);
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
#if REPLACE_FCHDIR
Packit 8f70b4
  if (retval >= 0)
Packit 8f70b4
    _gl_unregister_fd (fd);
Packit 8f70b4
#endif
Packit 8f70b4
Packit 8f70b4
  return retval;
Packit 8f70b4
}