annotate lib/stat.c @ 5382:72833e3704a0

* getdate.y [!TEST]: Include <stdio.h>, since we use sprintf now. (get_date): Overparenthesize to avoid GCC warning.
author Paul Eggert <eggert@cs.ucla.edu>
date Tue, 02 Nov 2004 19:18:44 +0000
parents a535859efd14
children a48fb0e98c8c
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
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4675
diff changeset
6 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Free
4474
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>
4390
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
30 #if defined LSTAT && ! LSTAT_FOLLOWS_SLASHED_SYMLINK
4675
7135d3d3d962 Remove K&R cruft.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4474
diff changeset
31 # include <stdlib.h>
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
32 # include <string.h>
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
33
5159
a535859efd14 Merge from coreutils.
Paul Eggert <eggert@cs.ucla.edu>
parents: 4675
diff changeset
34 # include "stat-macros.h"
4378
91433623b923 Don't declare xmalloc explicitly.
Jim Meyering <jim@meyering.net>
parents: 4060
diff changeset
35 # include "xalloc.h"
4060
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 /* 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
38 `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
39 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
40 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
41 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
42 `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
43
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
44 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
45 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
46
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
47 static int
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
48 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
49 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
50 size_t len;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
51 char *new_file;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
52
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
53 int lstat_result = lstat (file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
54
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
55 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
56 return lstat_result;
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 len = strlen (file);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
59 if (file[len - 1] != '/')
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
60 return lstat_result;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
61
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
62 /* 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
63 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
64
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
65 /* 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
66 new_file = xmalloc (len + 1 + 1);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
67 memcpy (new_file, file, len);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
68 new_file[len] = '.';
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
69 new_file[len + 1] = 0;
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 lstat_result = lstat (new_file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
72 free (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 return lstat_result;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
75 }
4390
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
76 #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
77
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
78 /* 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
79 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
80 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
81
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
82 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
83 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
84 has this bug. */
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 /* 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
87 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
88
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
89 #ifdef LSTAT
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
90 # 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
91 # 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
92 # 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
93 # else
eee1fc78ec03 [LSTAT]: Compile/use slash_aware_lstat only if it is necessary.
Jim Meyering <jim@meyering.net>
parents: 4378
diff changeset
94 # 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
95 # endif
4060
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
96 #else
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
97 # define rpl_xstat rpl_stat
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
98 # 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
99 #endif
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
100
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
101 int
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
102 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
103 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
104 if (file && *file == 0)
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
105 {
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
106 errno = ENOENT;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
107 return -1;
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
108 }
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 return xstat_return_val (file, sbuf);
bf15ee79dcc2 * stat.c: New file. Contents mostly from xstat.in.
Jim Meyering <jim@meyering.net>
parents:
diff changeset
111 }