1 /* Work around the bug in some systems whereby stat/lstat succeeds when
2 given the zero-length file name argument. The stat/lstat from SunOS4.1.4
3 has this bug. Also work around a deficiency in Solaris systems (up to at
4 least Solaris5.9) regarding the semantics of `lstat ("symlink/", sbuf).'
5 Copyright (C) 1997-2003 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 2, or (at your option)
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, write to the Free Software Foundation,
19 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* written by Jim Meyering */
25 #include <sys/types.h>
31 #if defined LSTAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
38 # ifdef STAT_MACROS_BROKEN
44 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
50 # ifndef HAVE_DECL_FREE
51 "this configure-time declaration test was not run"
59 /* lstat works differently on Linux and Solaris systems. POSIX (see
60 `pathname resolution' in the glossary) requires that programs like `ls'
61 take into consideration the fact that FILE has a trailing slash when
62 FILE is a symbolic link. On Linux systems, the lstat function already
63 has the desired semantics (in treating `lstat("symlink/",sbuf)' just like
64 `lstat("symlink/.",sbuf)', but on Solaris it does not.
66 If FILE has a trailing slash and specifies a symbolic link,
67 then append a `.' to FILE and call lstat a second time. */
70 slash_aware_lstat (const char *file, struct stat *sbuf)
75 int lstat_result = lstat (file, sbuf);
77 if (lstat_result != 0 || !S_ISLNK (sbuf->st_mode))
81 if (file[len - 1] != '/')
84 /* FILE refers to a symbolic link and the name ends with a slash.
85 Append a `.' to FILE and repeat the lstat call. */
87 /* Add one for the `.' we'll append, and one more for the trailing NUL. */
88 new_file = xmalloc (len + 1 + 1);
89 memcpy (new_file, file, len);
91 new_file[len + 1] = 0;
93 lstat_result = lstat (new_file, sbuf);
98 #endif /* LSTAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK */
100 /* This is a wrapper for stat/lstat.
101 If FILE is the empty string, fail with errno == ENOENT.
102 Otherwise, return the result of calling the real stat/lstat.
104 This works around the bug in some systems whereby stat/lstat succeeds when
105 given the zero-length file name argument. The stat/lstat from SunOS4.1.4
108 /* This function also provides a version of lstat with consistent semantics
109 when FILE specifies a symbolic link and has a trailing slash. */
112 # define rpl_xstat rpl_lstat
113 # if ! LSTAT_FOLLOWS_SLASHED_SYMLINK
114 # define xstat_return_val(F, S) slash_aware_lstat (F, S)
116 # define xstat_return_val(F, S) lstat (F, S)
119 # define rpl_xstat rpl_stat
120 # define xstat_return_val(F, S) stat (F, S)
124 rpl_xstat (const char *file, struct stat *sbuf)
126 if (file && *file == 0)
132 return xstat_return_val (file, sbuf);