.
[coreutils.git] / lib / stat.c
blobf3f373b9583f2b03b2955076e07f9f85c02684e3
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.
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)
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
30 #ifdef LSTAT
31 # include <string.h>
33 # if HAVE_STDLIB_H
34 # include <stdlib.h>
35 # endif
37 # ifdef STAT_MACROS_BROKEN
38 # undef S_ISLNK
39 # endif
41 # ifndef S_ISLNK
42 # ifdef S_IFLNK
43 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
44 # else
45 # define S_ISLNK(m) 0
46 # endif
47 # endif
49 # ifndef HAVE_DECL_FREE
50 "this configure-time declaration test was not run"
51 # endif
52 # if !HAVE_DECL_FREE
53 void free ();
54 # endif
56 char *xmalloc ();
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. */
68 static int
69 slash_aware_lstat (const char *file, struct stat *sbuf)
71 size_t len;
72 char *new_file;
74 int lstat_result = lstat (file, sbuf);
76 if (lstat_result != 0 || !S_ISLNK (sbuf->st_mode))
77 return lstat_result;
79 len = strlen (file);
80 if (file[len - 1] != '/')
81 return lstat_result;
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);
89 new_file[len] = '.';
90 new_file[len + 1] = 0;
92 lstat_result = lstat (new_file, sbuf);
93 free (new_file);
95 return lstat_result;
97 #endif /* LSTAT */
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
105 has this bug. */
107 /* This function also provides a version of lstat with consistent semantics
108 when FILE specifies a symbolic link and has a trailing slash. */
110 #ifdef LSTAT
111 # define rpl_xstat rpl_lstat
112 # define xstat_return_val(F, S) slash_aware_lstat (F, S)
113 #else
114 # define rpl_xstat rpl_stat
115 # define xstat_return_val(F, S) stat (F, S)
116 #endif
119 rpl_xstat (const char *file, struct stat *sbuf)
121 if (file && *file == 0)
123 errno = ENOENT;
124 return -1;
127 return xstat_return_val (file, sbuf);