Blame gnulib/fstat.c

Packit 23ab03
/* fstat() replacement.
Packit 23ab03
   Copyright (C) 2011-2016 Free Software Foundation, Inc.
Packit 23ab03
Packit 23ab03
   This program is free software: you can redistribute it and/or modify
Packit 23ab03
   it under the terms of the GNU General Public License as published by
Packit 23ab03
   the Free Software Foundation; either version 3 of the License, or
Packit 23ab03
   (at your option) any later version.
Packit 23ab03
Packit 23ab03
   This program is distributed in the hope that it will be useful,
Packit 23ab03
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 23ab03
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 23ab03
   GNU General Public License for more details.
Packit 23ab03
Packit 23ab03
   You should have received a copy of the GNU General Public License
Packit 23ab03
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit 23ab03
Packit 23ab03
/* If the user's config.h happens to include <sys/stat.h>, let it include only
Packit 23ab03
   the system's <sys/stat.h> here, so that orig_fstat doesn't recurse to
Packit 23ab03
   rpl_fstat.  */
Packit 23ab03
#define __need_system_sys_stat_h
Packit 23ab03
#include <config.h>
Packit 23ab03
Packit 23ab03
/* Get the original definition of fstat.  It might be defined as a macro.  */
Packit 23ab03
#include <sys/types.h>
Packit 23ab03
#include <sys/stat.h>
Packit 23ab03
#if _GL_WINDOWS_64_BIT_ST_SIZE
Packit 23ab03
# undef stat /* avoid warning on mingw64 with _FILE_OFFSET_BITS=64 */
Packit 23ab03
# define stat _stati64
Packit 23ab03
# undef fstat /* avoid warning on mingw64 with _FILE_OFFSET_BITS=64 */
Packit 23ab03
# define fstat _fstati64
Packit 23ab03
#endif
Packit 23ab03
#undef __need_system_sys_stat_h
Packit 23ab03
Packit 23ab03
static int
Packit 23ab03
orig_fstat (int fd, struct stat *buf)
Packit 23ab03
{
Packit 23ab03
  return fstat (fd, buf);
Packit 23ab03
}
Packit 23ab03
Packit 23ab03
/* Specification.  */
Packit 23ab03
/* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
Packit 23ab03
   eliminates this include because of the preliminary #include <sys/stat.h>
Packit 23ab03
   above.  */
Packit 23ab03
#include "sys/stat.h"
Packit 23ab03
Packit 23ab03
#include <errno.h>
Packit 23ab03
#include <unistd.h>
Packit 23ab03
Packit 23ab03
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 23ab03
# include "msvc-inval.h"
Packit 23ab03
#endif
Packit 23ab03
Packit 23ab03
#if HAVE_MSVC_INVALID_PARAMETER_HANDLER
Packit 23ab03
static int
Packit 23ab03
fstat_nothrow (int fd, struct stat *buf)
Packit 23ab03
{
Packit 23ab03
  int result;
Packit 23ab03
Packit 23ab03
  TRY_MSVC_INVAL
Packit 23ab03
    {
Packit 23ab03
      result = orig_fstat (fd, buf);
Packit 23ab03
    }
Packit 23ab03
  CATCH_MSVC_INVAL
Packit 23ab03
    {
Packit 23ab03
      result = -1;
Packit 23ab03
      errno = EBADF;
Packit 23ab03
    }
Packit 23ab03
  DONE_MSVC_INVAL;
Packit 23ab03
Packit 23ab03
  return result;
Packit 23ab03
}
Packit 23ab03
#else
Packit 23ab03
# define fstat_nothrow orig_fstat
Packit 23ab03
#endif
Packit 23ab03
Packit 23ab03
int
Packit 23ab03
rpl_fstat (int fd, struct stat *buf)
Packit 23ab03
{
Packit 23ab03
#if REPLACE_FCHDIR && REPLACE_OPEN_DIRECTORY
Packit 23ab03
  /* Handle the case when rpl_open() used a dummy file descriptor to work
Packit 23ab03
     around an open() that can't normally visit directories.  */
Packit 23ab03
  const char *name = _gl_directory_name (fd);
Packit 23ab03
  if (name != NULL)
Packit 23ab03
    return stat (name, buf);
Packit 23ab03
#endif
Packit 23ab03
Packit 23ab03
  return fstat_nothrow (fd, buf);
Packit 23ab03
}