Blame lib/fstat.c

Packit Service fdd496
/* fstat() replacement.
Packit Service fdd496
   Copyright (C) 2011-2017 Free Software Foundation, Inc.
Packit Service fdd496
Packit Service fdd496
   This program is free software: you can redistribute it and/or modify
Packit Service fdd496
   it under the terms of the GNU General Public License as published by
Packit Service fdd496
   the Free Software Foundation; either version 3 of the License, or
Packit Service fdd496
   (at your option) any later version.
Packit Service fdd496
Packit Service fdd496
   This program is distributed in the hope that it will be useful,
Packit Service fdd496
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fdd496
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service fdd496
   GNU General Public License for more details.
Packit Service fdd496
Packit Service fdd496
   You should have received a copy of the GNU General Public License
Packit Service fdd496
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
Packit Service fdd496
Packit Service fdd496
/* If the user's config.h happens to include <sys/stat.h>, let it include only
Packit Service fdd496
   the system's <sys/stat.h> here, so that orig_fstat doesn't recurse to
Packit Service fdd496
   rpl_fstat.  */
Packit Service fdd496
#define __need_system_sys_stat_h
Packit Service fdd496
#include <config.h>
Packit Service fdd496
Packit Service fdd496
/* Get the original definition of fstat.  It might be defined as a macro.  */
Packit Service fdd496
#include <sys/types.h>
Packit Service fdd496
#include <sys/stat.h>
Packit Service fdd496
#undef __need_system_sys_stat_h
Packit Service fdd496
Packit Service fdd496
#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
Packit Service fdd496
# define WINDOWS_NATIVE
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#if !defined WINDOWS_NATIVE
Packit Service fdd496
Packit Service fdd496
static int
Packit Service fdd496
orig_fstat (int fd, struct stat *buf)
Packit Service fdd496
{
Packit Service fdd496
  return fstat (fd, buf);
Packit Service fdd496
}
Packit Service fdd496
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
/* Specification.  */
Packit Service fdd496
/* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
Packit Service fdd496
   eliminates this include because of the preliminary #include <sys/stat.h>
Packit Service fdd496
   above.  */
Packit Service fdd496
#include "sys/stat.h"
Packit Service fdd496
Packit Service fdd496
#include <errno.h>
Packit Service fdd496
#include <unistd.h>
Packit Service fdd496
#ifdef WINDOWS_NATIVE
Packit Service fdd496
# define WIN32_LEAN_AND_MEAN
Packit Service fdd496
# include <windows.h>
Packit Service fdd496
# if GNULIB_MSVC_NOTHROW
Packit Service fdd496
#  include "msvc-nothrow.h"
Packit Service fdd496
# else
Packit Service fdd496
#  include <io.h>
Packit Service fdd496
# endif
Packit Service fdd496
# include "stat-w32.h"
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
int
Packit Service fdd496
rpl_fstat (int fd, struct stat *buf)
Packit Service fdd496
{
Packit Service fdd496
#if REPLACE_FCHDIR && REPLACE_OPEN_DIRECTORY
Packit Service fdd496
  /* Handle the case when rpl_open() used a dummy file descriptor to work
Packit Service fdd496
     around an open() that can't normally visit directories.  */
Packit Service fdd496
  const char *name = _gl_directory_name (fd);
Packit Service fdd496
  if (name != NULL)
Packit Service fdd496
    return stat (name, buf);
Packit Service fdd496
#endif
Packit Service fdd496
Packit Service fdd496
#ifdef WINDOWS_NATIVE
Packit Service fdd496
  /* Fill the fields ourselves, because the original fstat function returns
Packit Service fdd496
     values for st_atime, st_mtime, st_ctime that depend on the current time
Packit Service fdd496
     zone.  See
Packit Service fdd496
     <https://lists.gnu.org/archive/html/bug-gnulib/2017-04/msg00134.html>  */
Packit Service fdd496
  HANDLE h = (HANDLE) _get_osfhandle (fd);
Packit Service fdd496
Packit Service fdd496
  if (h == INVALID_HANDLE_VALUE)
Packit Service fdd496
    {
Packit Service fdd496
      errno = EBADF;
Packit Service fdd496
      return -1;
Packit Service fdd496
    }
Packit Service fdd496
  return _gl_fstat_by_handle (h, NULL, buf);
Packit Service fdd496
#else
Packit Service fdd496
  return orig_fstat (fd, buf);
Packit Service fdd496
#endif
Packit Service fdd496
}