1 /* Work around the bug in some systems whereby lstat succeeds when
2 given the zero-length file name argument. The lstat from SunOS4.1.4
4 Copyright (C) 1997-2000 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>
31 #ifdef STAT_MACROS_BROKEN
34 #if !defined(S_ISLNK) && defined(S_IFLNK)
35 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
40 /* lstat works different on Linux and Solaris systems. POSIX (see
41 `pathname resolution' in the glossary) requires that programs like `ls'
42 take into consideration the fact that FILE has a trailing slash when
43 FILE is a symbolic link. On Linux systems, the lstat function already
44 has the desired semantics (in treating `lstat("symlink/",sbuf)' just like
45 `lstat("symlink/.",sbuf)', but on Solaris it does not.
47 If FILE has a trailing slash and specifies a symbolic link,
48 then append a `.' to FILE and call lstat a second time. */
51 slash_aware_lstat (const char *file
, struct stat
*sbuf
)
56 int lstat_result
= lstat (file
, sbuf
);
58 if (lstat_result
!= 0 || !S_ISLNK (sbuf
->st_mode
))
62 if (file
[len
- 1] != '/')
65 /* FILE refers to a symbolic link and the name ends with a slash.
66 Append a `.' to FILE and repeat the lstat call. */
68 /* Add one for the `.' we might have to append, and one more
69 for the trailing NUL. */
70 new_file
= xmalloc (len
+ 1 + 1);
71 memcpy (new_file
, file
, len
);
73 new_file
[len
+ 1] = 0;
75 lstat_result
= lstat (new_file
, sbuf
);
81 /* This is a wrapper for lstat(2).
82 If FILE is the empty string, fail with errno == ENOENT.
83 Otherwise, return the result of calling the real lstat.
85 This works around the bug in some systems whereby lstat succeeds when
86 given the zero-length file name argument. The lstat from SunOS4.1.4
89 /* This function also provides a version of lstat with consistent semantics
90 when FILE specifies a symbolic link and has a trailing slash. */
93 rpl_lstat (const char *file
, struct stat
*sbuf
)
95 if (file
&& *file
== 0)
101 return slash_aware_lstat (file
, sbuf
);