Blame gnulib/lib/lstat.c

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