Blame lib/lstat.c

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