summaryrefslogtreecommitdiffstats
path: root/gl/lstat.c
diff options
context:
space:
mode:
Diffstat (limited to 'gl/lstat.c')
-rw-r--r--gl/lstat.c97
1 files changed, 97 insertions, 0 deletions
diff --git a/gl/lstat.c b/gl/lstat.c
new file mode 100644
index 00000000..2495568f
--- /dev/null
+++ b/gl/lstat.c
@@ -0,0 +1,97 @@
1/* Work around a bug of lstat on some systems
2
3 Copyright (C) 1997-2006, 2008-2026 Free Software Foundation, Inc.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18/* written by Jim Meyering */
19
20/* If the user's config.h happens to include <sys/stat.h>, let it include only
21 the system's <sys/stat.h> here, so that orig_lstat doesn't recurse to
22 rpl_lstat. */
23#define __need_system_sys_stat_h
24#include <config.h>
25
26#if !HAVE_LSTAT
27/* On systems that lack symlinks, our replacement <sys/stat.h> already
28 defined lstat as stat, so there is nothing further to do other than
29 avoid an empty file. */
30typedef int dummy;
31#else /* HAVE_LSTAT */
32
33/* Get the original definition of lstat. It might be defined as a macro. */
34# include <sys/types.h>
35# include <sys/stat.h>
36# undef __need_system_sys_stat_h
37
38static int
39orig_lstat (const char *filename, struct stat *buf)
40{
41 return lstat (filename, buf);
42}
43
44/* Specification. */
45# include <sys/stat.h>
46
47# include "stat-time.h"
48
49# include <string.h>
50# include <errno.h>
51
52/* lstat works differently on Linux and Solaris systems. POSIX (see
53 "pathname resolution" in the glossary) requires that programs like
54 'ls' take into consideration the fact that FILE has a trailing slash
55 when FILE is a symbolic link. On Linux and Solaris 10 systems, the
56 lstat function already has the desired semantics (in treating
57 'lstat ("symlink/", sbuf)' just like 'lstat ("symlink/.", sbuf)',
58 but on Solaris 9 and earlier it does not.
59
60 If FILE has a trailing slash and specifies a symbolic link,
61 then use stat() to get more info on the referent of FILE.
62 If the referent is a non-directory, then set errno to ENOTDIR
63 and return -1. Otherwise, return stat's result. */
64
65int
66rpl_lstat (const char *file, struct stat *sbuf)
67{
68 int result = orig_lstat (file, sbuf);
69
70 /* This replacement file can blindly check against '/' rather than
71 using the ISSLASH macro, because all platforms with '\\' either
72 lack symlinks (mingw) or have working lstat (cygwin) and thus do
73 not compile this file. 0 len should have already been filtered
74 out above, with a failure return of ENOENT. */
75 if (result == 0)
76 {
77 if (S_ISDIR (sbuf->st_mode) || file[strlen (file) - 1] != '/')
78 result = stat_time_normalize (result, sbuf);
79 else
80 {
81 /* At this point, a trailing slash is permitted only on
82 symlink-to-dir; but it should have found information on the
83 directory, not the symlink. Call 'stat' to get info about the
84 link's referent. Our replacement stat guarantees valid results,
85 even if the symlink is not pointing to a directory. */
86 if (!S_ISLNK (sbuf->st_mode))
87 {
88 errno = ENOTDIR;
89 return -1;
90 }
91 result = stat (file, sbuf);
92 }
93 }
94 return result;
95}
96
97#endif /* HAVE_LSTAT */