annotate lib/stat.c @ 4060:bf15ee79dcc2

* stat.c: New file. Contents mostly from xstat.in. Rework so that it may serve to define rpl_lstat, too.
author Jim Meyering <jim@meyering.net>
date Wed, 04 Dec 2002 10:11:15 +0000
parents
children 91433623b923
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
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
2 given the zero-length file name argument. The stat/lstat from SunOS4.1.4
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
3 has this bug.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
4 Copyright (C) 1997-2002 Free Software Foundation, Inc.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
5
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
6 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
7 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
8 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
9 any later version.
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
10
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
11 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
12 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
13 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
14 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
15
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
16 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
17 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
18 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
19
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
20 /* written by Jim Meyering */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
21
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
22 #include <config.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
23
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
24 #include <sys/types.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
25 #include <sys/stat.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
26 #include <errno.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
27 #ifndef errno
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
28 extern int errno;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
29 #endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
30 #ifdef LSTAT
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
31 # include <string.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
32
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
33 # if HAVE_STDLIB_H
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
34 # include <stdlib.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
35 # endif
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
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
49 # ifndef HAVE_DECL_FREE
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50 "this configure-time declaration test was not run"
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
51 # endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
52 # if !HAVE_DECL_FREE
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53 void free ();
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
54 # endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
55
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
56 char *xmalloc ();
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 /* 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
59 `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
60 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
61 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
62 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
63 `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
64
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65 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
66 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
67
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
68 static int
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
69 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
70 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
71 size_t len;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
72 char *new_file;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
73
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
74 int lstat_result = lstat (file, sbuf);
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 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
77 return lstat_result;
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 len = strlen (file);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
80 if (file[len - 1] != '/')
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
81 return lstat_result;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
82
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
83 /* 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
84 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
85
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
86 /* 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
87 new_file = xmalloc (len + 1 + 1);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
88 memcpy (new_file, file, len);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
89 new_file[len] = '.';
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
90 new_file[len + 1] = 0;
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 lstat_result = lstat (new_file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
93 free (new_file);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
94
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
95 return lstat_result;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
96 }
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
97 #endif /* LSTAT */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
98
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
99 /* 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
100 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
101 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
102
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
103 This works around the bug in some systems whereby stat/lstat succeeds when
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
104 given the zero-length file name argument. The stat/lstat from SunOS4.1.4
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
105 has this bug. */
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
106
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
107 /* 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
108 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
109
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
110 #ifdef LSTAT
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
111 # define rpl_xstat rpl_lstat
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) slash_aware_lstat (F, S)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
113 #else
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
114 # define rpl_xstat rpl_stat
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
115 # 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
116 #endif
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 int
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
119 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
120 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
121 if (file && *file == 0)
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 errno = ENOENT;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
124 return -1;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
125 }
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
126
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
127 return xstat_return_val (file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
128 }