Blame lib/lstat.c

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