*** empty log message ***
[findutils.git] / lib / lstat.c
bloba973f7c04cf6e4a33574db8ceb1d6e3e2a78b981
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
3 has this bug.
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)
9 any later version.
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 */
22 #include <config.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <errno.h>
27 #ifndef errno
28 extern int errno;
29 #endif
31 #ifdef STAT_MACROS_BROKEN
32 # undef S_ISLNK
33 #endif
34 #if !defined(S_ISLNK) && defined(S_IFLNK)
35 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
36 #endif
38 char *xmalloc ();
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. */
50 static int
51 slash_aware_lstat (const char *file, struct stat *sbuf)
53 size_t len;
54 char *new_file;
56 int lstat_result = lstat (file, sbuf);
58 if (lstat_result != 0 || !S_ISLNK (sbuf->st_mode))
59 return lstat_result;
61 len = strlen (file);
62 if (file[len - 1] != '/')
63 return lstat_result;
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);
72 new_file[len] = '.';
73 new_file[len + 1] = 0;
75 lstat_result = lstat (new_file, sbuf);
76 free (new_file);
78 return lstat_result;
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
87 has this bug. */
89 /* This function also provides a version of lstat with consistent semantics
90 when FILE specifies a symbolic link and has a trailing slash. */
92 int
93 rpl_lstat (const char *file, struct stat *sbuf)
95 if (file && *file == 0)
97 errno = ENOENT;
98 return -1;
101 return slash_aware_lstat (file, sbuf);