1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Work around a bug of lstat on some systems
5 Copyright (C) 1997-2006, 2008-2013 Free Software Foundation, Inc.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* written by Jim Meyering */
22 /* If the user's config.h happens to include <sys/stat.h>, let it include only
23 the system's <sys/stat.h> here, so that orig_lstat doesn't recurse to
25 #define __need_system_sys_stat_h
29 /* On systems that lack symlinks, our replacement <sys/stat.h> already
30 defined lstat as stat, so there is nothing further to do other than
31 avoid an empty file. */
33 #else /* HAVE_LSTAT */
35 /* Get the original definition of lstat. It might be defined as a macro. */
36 # include <sys/types.h>
37 # include <sys/stat.h>
38 # undef __need_system_sys_stat_h
41 orig_lstat (const char *filename, struct stat *buf)
43 return lstat (filename, buf);
47 /* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
48 eliminates this include because of the preliminary #include <sys/stat.h>
50 # include "sys/stat.h"
55 /* lstat works differently on Linux and Solaris systems. POSIX (see
56 "pathname resolution" in the glossary) requires that programs like
57 'ls' take into consideration the fact that FILE has a trailing slash
58 when FILE is a symbolic link. On Linux and Solaris 10 systems, the
59 lstat function already has the desired semantics (in treating
60 'lstat ("symlink/", sbuf)' just like 'lstat ("symlink/.", sbuf)',
61 but on Solaris 9 and earlier it does not.
63 If FILE has a trailing slash and specifies a symbolic link,
64 then use stat() to get more info on the referent of FILE.
65 If the referent is a non-directory, then set errno to ENOTDIR
66 and return -1. Otherwise, return stat's result. */
69 rpl_lstat (const char *file, struct stat *sbuf)
72 int lstat_result = orig_lstat (file, sbuf);
74 if (lstat_result != 0)
77 /* This replacement file can blindly check against '/' rather than
78 using the ISSLASH macro, because all platforms with '\\' either
79 lack symlinks (mingw) or have working lstat (cygwin) and thus do
80 not compile this file. 0 len should have already been filtered
81 out above, with a failure return of ENOENT. */
83 if (file[len - 1] != '/' || S_ISDIR (sbuf->st_mode))
86 /* At this point, a trailing slash is only permitted on
87 symlink-to-dir; but it should have found information on the
88 directory, not the symlink. Call stat() to get info about the
89 link's referent. Our replacement stat guarantees valid results,
90 even if the symlink is not pointing to a directory. */
91 if (!S_ISLNK (sbuf->st_mode))
96 return stat (file, sbuf);
99 #endif /* HAVE_LSTAT */