ff9e9bbe5b4b1721ca367ab57cdac09fab47093c
[gnulib.git] / lib / stat.c
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.
6
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)
10    any later version.
11
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.
16
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.  */
20
21 /* written by Jim Meyering */
22
23 #include <config.h>
24
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <errno.h>
28 #ifndef errno
29 extern int errno;
30 #endif
31 #if defined LSTAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
32 # include <string.h>
33
34 # if HAVE_STDLIB_H
35 #  include <stdlib.h>
36 # endif
37
38 # ifdef STAT_MACROS_BROKEN
39 #  undef S_ISLNK
40 # endif
41
42 # ifndef S_ISLNK
43 #  ifdef S_IFLNK
44 #   define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
45 #  else
46 #   define S_ISLNK(m) 0
47 #  endif
48 # endif
49
50 # ifndef HAVE_DECL_FREE
51 "this configure-time declaration test was not run"
52 # endif
53 # if !HAVE_DECL_FREE
54 void free ();
55 # endif
56
57 # include "xalloc.h"
58
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.
65
66    If FILE has a trailing slash and specifies a symbolic link,
67    then append a `.' to FILE and call lstat a second time.  */
68
69 static int
70 slash_aware_lstat (const char *file, struct stat *sbuf)
71 {
72   size_t len;
73   char *new_file;
74
75   int lstat_result = lstat (file, sbuf);
76
77   if (lstat_result != 0 || !S_ISLNK (sbuf->st_mode))
78     return lstat_result;
79
80   len = strlen (file);
81   if (file[len - 1] != '/')
82     return lstat_result;
83
84   /* FILE refers to a symbolic link and the name ends with a slash.
85      Append a `.' to FILE and repeat the lstat call.  */
86
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);
90   new_file[len] = '.';
91   new_file[len + 1] = 0;
92
93   lstat_result = lstat (new_file, sbuf);
94   free (new_file);
95
96   return lstat_result;
97 }
98 #endif /* LSTAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK */
99
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.
103
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
106    has this bug.  */
107
108 /* This function also provides a version of lstat with consistent semantics
109    when FILE specifies a symbolic link and has a trailing slash.  */
110
111 #ifdef LSTAT
112 # define rpl_xstat rpl_lstat
113 # if ! LSTAT_FOLLOWS_SLASHED_SYMLINK
114 #  define xstat_return_val(F, S) slash_aware_lstat (F, S)
115 # else
116 #  define xstat_return_val(F, S) lstat (F, S)
117 # endif
118 #else
119 # define rpl_xstat rpl_stat
120 # define xstat_return_val(F, S) stat (F, S)
121 #endif
122
123 int
124 rpl_xstat (const char *file, struct stat *sbuf)
125 {
126   if (file && *file == 0)
127     {
128       errno = ENOENT;
129       return -1;
130     }
131
132   return xstat_return_val (file, sbuf);
133 }