Blame gl/fstat.c

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