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
4 Copyright (C) 1997-2002 Free Software Foundation, Inc.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* written by Jim Meyering */
24 #include <sys/types.h>
37 # ifdef STAT_MACROS_BROKEN
43 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
49 # ifndef HAVE_DECL_FREE
50 "this configure-time declaration test was not run"
58 /* lstat works differently on Linux and Solaris systems. POSIX (see
59 `pathname resolution' in the glossary) requires that programs like `ls'
60 take into consideration the fact that FILE has a trailing slash when
61 FILE is a symbolic link. On Linux systems, the lstat function already
62 has the desired semantics (in treating `lstat("symlink/",sbuf)' just like
63 `lstat("symlink/.",sbuf)', but on Solaris it does not.
65 If FILE has a trailing slash and specifies a symbolic link,
66 then append a `.' to FILE and call lstat a second time. */
69 slash_aware_lstat (const char *file
, struct stat
*sbuf
)
74 int lstat_result
= lstat (file
, sbuf
);
76 if (lstat_result
!= 0 || !S_ISLNK (sbuf
->st_mode
))
80 if (file
[len
- 1] != '/')
83 /* FILE refers to a symbolic link and the name ends with a slash.
84 Append a `.' to FILE and repeat the lstat call. */
86 /* Add one for the `.' we'll append, and one more for the trailing NUL. */
87 new_file
= xmalloc (len
+ 1 + 1);
88 memcpy (new_file
, file
, len
);
90 new_file
[len
+ 1] = 0;
92 lstat_result
= lstat (new_file
, sbuf
);
99 /* This is a wrapper for stat/lstat.
100 If FILE is the empty string, fail with errno == ENOENT.
101 Otherwise, return the result of calling the real stat/lstat.
103 This works around the bug in some systems whereby stat/lstat succeeds when
104 given the zero-length file name argument. The stat/lstat from SunOS4.1.4
107 /* This function also provides a version of lstat with consistent semantics
108 when FILE specifies a symbolic link and has a trailing slash. */
111 # define rpl_xstat rpl_lstat
112 # define xstat_return_val(F, S) slash_aware_lstat (F, S)
114 # define rpl_xstat rpl_stat
115 # define xstat_return_val(F, S) stat (F, S)
119 rpl_xstat (const char *file
, struct stat
*sbuf
)
121 if (file
&& *file
== 0)
127 return xstat_return_val (file
, sbuf
);