annotate lib/stat.c @ 4739:04758f7475fd

Merge changes from glibc.
author Paul Eggert <eggert@cs.ucla.edu>
date Fri, 26 Sep 2003 07:35:01 +0000
parents 7135d3d3d962
children a535859efd14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
1 /* Work around the bug in some systems whereby stat/lstat succeeds when
4474
f1650b772bb6 Correct SunOS and Solaris version number notation to match Sun's usage.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4390
diff changeset
2 given the zero-length file name argument. The stat/lstat from SunOS 4.1.4
4390
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
3 has this bug. Also work around a deficiency in Solaris systems (up to at
4474
f1650b772bb6 Correct SunOS and Solaris version number notation to match Sun's usage.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4390
diff changeset
4 least Solaris 9) regarding the semantics of `lstat ("symlink/", sbuf).'
f1650b772bb6 Correct SunOS and Solaris version number notation to match Sun's usage.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4390
diff changeset
5
f1650b772bb6 Correct SunOS and Solaris version number notation to match Sun's usage.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4390
diff changeset
6 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003 Free
f1650b772bb6 Correct SunOS and Solaris version number notation to match Sun's usage.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4390
diff changeset
7 Software Foundation, Inc.
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
8
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
9 This program is free software; you can redistribute it and/or modify
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10 it under the terms of the GNU General Public License as published by
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 the Free Software Foundation; either version 2, or (at your option)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
12 any later version.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
13
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
14 This program is distributed in the hope that it will be useful,
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
17 GNU General Public License for more details.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
18
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
19 You should have received a copy of the GNU General Public License
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20 along with this program; if not, write to the Free Software Foundation,
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
21 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
22
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23 /* written by Jim Meyering */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
24
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
25 #include <config.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
26
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
27 #include <sys/types.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
28 #include <sys/stat.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
29 #include <errno.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
30 #ifndef errno
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
31 extern int errno;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
32 #endif
4390
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
33 #if defined LSTAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
4675
7135d3d3d962 Remove K&R cruft.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4474
diff changeset
34 # include <stdlib.h>
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
35 # include <string.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
36
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
37 # ifdef STAT_MACROS_BROKEN
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
38 # undef S_ISLNK
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
39 # endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
40
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
41 # ifndef S_ISLNK
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
42 # ifdef S_IFLNK
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
43 # define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
44 # else
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
45 # define S_ISLNK(m) 0
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
46 # endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
47 # endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
48
4378
91433623b923 Don't declare xmalloc explicitly.
Jim Meyering <jim@meyering.net>
parents: 4060
diff changeset
49 # include "xalloc.h"
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
51 /* lstat works differently on Linux and Solaris systems. POSIX (see
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
52 `pathname resolution' in the glossary) requires that programs like `ls'
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53 take into consideration the fact that FILE has a trailing slash when
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
54 FILE is a symbolic link. On Linux systems, the lstat function already
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
55 has the desired semantics (in treating `lstat("symlink/",sbuf)' just like
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
56 `lstat("symlink/.",sbuf)', but on Solaris it does not.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
57
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
58 If FILE has a trailing slash and specifies a symbolic link,
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
59 then append a `.' to FILE and call lstat a second time. */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
60
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
61 static int
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
62 slash_aware_lstat (const char *file, struct stat *sbuf)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
63 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
64 size_t len;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65 char *new_file;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
66
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
67 int lstat_result = lstat (file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
68
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
69 if (lstat_result != 0 || !S_ISLNK (sbuf->st_mode))
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
70 return lstat_result;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
71
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
72 len = strlen (file);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
73 if (file[len - 1] != '/')
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
74 return lstat_result;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
75
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
76 /* FILE refers to a symbolic link and the name ends with a slash.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
77 Append a `.' to FILE and repeat the lstat call. */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
78
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
79 /* Add one for the `.' we'll append, and one more for the trailing NUL. */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
80 new_file = xmalloc (len + 1 + 1);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
81 memcpy (new_file, file, len);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
82 new_file[len] = '.';
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
83 new_file[len + 1] = 0;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
84
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
85 lstat_result = lstat (new_file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
86 free (new_file);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
87
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
88 return lstat_result;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
89 }
4390
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
90 #endif /* LSTAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK */
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
91
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
92 /* This is a wrapper for stat/lstat.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
93 If FILE is the empty string, fail with errno == ENOENT.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
94 Otherwise, return the result of calling the real stat/lstat.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
95
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
96 This works around the bug in some systems whereby stat/lstat succeeds when
4474
f1650b772bb6 Correct SunOS and Solaris version number notation to match Sun's usage.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4390
diff changeset
97 given the zero-length file name argument. The stat/lstat from SunOS 4.1.4
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
98 has this bug. */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
99
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
100 /* This function also provides a version of lstat with consistent semantics
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
101 when FILE specifies a symbolic link and has a trailing slash. */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
102
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
103 #ifdef LSTAT
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
104 # define rpl_xstat rpl_lstat
4390
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
105 # if ! LSTAT_FOLLOWS_SLASHED_SYMLINK
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
106 # define xstat_return_val(F, S) slash_aware_lstat (F, S)
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
107 # else
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
108 # define xstat_return_val(F, S) lstat (F, S)
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
109 # endif
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
110 #else
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
111 # define rpl_xstat rpl_stat
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
112 # define xstat_return_val(F, S) stat (F, S)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
113 #endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
114
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
115 int
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
116 rpl_xstat (const char *file, struct stat *sbuf)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
117 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
118 if (file && *file == 0)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
119 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
120 errno = ENOENT;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
121 return -1;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
122 }
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
123
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
124 return xstat_return_val (file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
125 }