Blame lib/lstat.c

Packit 1ac44c
/* Work around a bug of lstat on some systems
Packit 1ac44c
Packit 1ac44c
   Copyright (C) 1997-2006, 2008-2018 Free Software Foundation, Inc.
Packit 1ac44c
Packit 1ac44c
   This program is free software: you can redistribute it and/or modify
Packit 1ac44c
   it under the terms of the GNU General Public License as published by
Packit 1ac44c
   the Free Software Foundation; either version 3 of the License, or
Packit 1ac44c
   (at your option) any later version.
Packit 1ac44c
Packit 1ac44c
   This program is distributed in the hope that it will be useful,
Packit 1ac44c
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 1ac44c
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 1ac44c
   GNU General Public License for more details.
Packit 1ac44c
Packit 1ac44c
   You should have received a copy of the GNU General Public License
Packit 1ac44c
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit 1ac44c
Packit 1ac44c
/* written by Jim Meyering */
Packit 1ac44c
Packit 1ac44c
/* If the user's config.h happens to include <sys/stat.h>, let it include only
Packit 1ac44c
   the system's <sys/stat.h> here, so that orig_lstat doesn't recurse to
Packit 1ac44c
   rpl_lstat.  */
Packit 1ac44c
#define __need_system_sys_stat_h
Packit 1ac44c
#include <config.h>
Packit 1ac44c
Packit 1ac44c
#if !HAVE_LSTAT
Packit 1ac44c
/* On systems that lack symlinks, our replacement <sys/stat.h> already
Packit 1ac44c
   defined lstat as stat, so there is nothing further to do other than
Packit 1ac44c
   avoid an empty file.  */
Packit 1ac44c
typedef int dummy;
Packit 1ac44c
#else /* HAVE_LSTAT */
Packit 1ac44c
Packit 1ac44c
/* Get the original definition of lstat.  It might be defined as a macro.  */
Packit 1ac44c
# include <sys/types.h>
Packit 1ac44c
# include <sys/stat.h>
Packit 1ac44c
# undef __need_system_sys_stat_h
Packit 1ac44c
Packit 1ac44c
static int
Packit 1ac44c
orig_lstat (const char *filename, struct stat *buf)
Packit 1ac44c
{
Packit 1ac44c
  return lstat (filename, buf);
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
/* Specification.  */
Packit 1ac44c
/* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
Packit 1ac44c
   eliminates this include because of the preliminary #include <sys/stat.h>
Packit 1ac44c
   above.  */
Packit 1ac44c
# include "sys/stat.h"
Packit 1ac44c
Packit 1ac44c
# include "stat-time.h"
Packit 1ac44c
Packit 1ac44c
# include <string.h>
Packit 1ac44c
# include <errno.h>
Packit 1ac44c
Packit 1ac44c
/* lstat works differently on Linux and Solaris systems.  POSIX (see
Packit 1ac44c
   "pathname resolution" in the glossary) requires that programs like
Packit 1ac44c
   'ls' take into consideration the fact that FILE has a trailing slash
Packit 1ac44c
   when FILE is a symbolic link.  On Linux and Solaris 10 systems, the
Packit 1ac44c
   lstat function already has the desired semantics (in treating
Packit 1ac44c
   'lstat ("symlink/", sbuf)' just like 'lstat ("symlink/.", sbuf)',
Packit 1ac44c
   but on Solaris 9 and earlier it does not.
Packit 1ac44c
Packit 1ac44c
   If FILE has a trailing slash and specifies a symbolic link,
Packit 1ac44c
   then use stat() to get more info on the referent of FILE.
Packit 1ac44c
   If the referent is a non-directory, then set errno to ENOTDIR
Packit 1ac44c
   and return -1.  Otherwise, return stat's result.  */
Packit 1ac44c
Packit 1ac44c
int
Packit 1ac44c
rpl_lstat (const char *file, struct stat *sbuf)
Packit 1ac44c
{
Packit 1ac44c
  int result = orig_lstat (file, sbuf);
Packit 1ac44c
Packit 1ac44c
  /* This replacement file can blindly check against '/' rather than
Packit 1ac44c
     using the ISSLASH macro, because all platforms with '\\' either
Packit 1ac44c
     lack symlinks (mingw) or have working lstat (cygwin) and thus do
Packit 1ac44c
     not compile this file.  0 len should have already been filtered
Packit 1ac44c
     out above, with a failure return of ENOENT.  */
Packit 1ac44c
  if (result == 0)
Packit 1ac44c
    {
Packit 1ac44c
      if (S_ISDIR (sbuf->st_mode) || file[strlen (file) - 1] != '/')
Packit 1ac44c
        result = stat_time_normalize (result, sbuf);
Packit 1ac44c
      else
Packit 1ac44c
        {
Packit 1ac44c
          /* At this point, a trailing slash is permitted only on
Packit 1ac44c
             symlink-to-dir; but it should have found information on the
Packit 1ac44c
             directory, not the symlink.  Call 'stat' to get info about the
Packit 1ac44c
             link's referent.  Our replacement stat guarantees valid results,
Packit 1ac44c
             even if the symlink is not pointing to a directory.  */
Packit 1ac44c
          if (!S_ISLNK (sbuf->st_mode))
Packit 1ac44c
            {
Packit 1ac44c
              errno = ENOTDIR;
Packit 1ac44c
              return -1;
Packit 1ac44c
            }
Packit 1ac44c
          result = stat (file, sbuf);
Packit 1ac44c
        }
Packit 1ac44c
    }
Packit 1ac44c
  return result;
Packit 1ac44c
}
Packit 1ac44c
Packit 1ac44c
#endif /* HAVE_LSTAT */