Blame gettext-tools/gnulib-lib/close.c

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