aboutsummaryrefslogtreecommitdiffstats
path: root/lib/stat.c
diff options
context:
space:
mode:
authorPaul Eggert2011-02-22 11:30:07 -0800
committerPaul Eggert2011-02-22 11:30:07 -0800
commitae0d725005539d9259efac6a81ff8fdd45eb69a6 (patch)
treec721b530409b355281537abc7b058bdb2697ee01 /lib/stat.c
parent8d40723d1e9661840a67ca7be9c5b073526ab421 (diff)
downloademacs-ae0d725005539d9259efac6a81ff8fdd45eb69a6.tar.gz
emacs-ae0d725005539d9259efac6a81ff8fdd45eb69a6.zip
[ChangeLog]
Work around some portability problems with symlinks. * Makefile.in (GNULIB_MODULES): Add lstat, readlink, symlink. * configure.in (lstat, HAVE_LSTAT): Remove special hack. * lib/lstat.c, lib/readlink.c, lib/stat.c, lib/symlink.c: * m4/dos.m4, m4/lstat.m4, m4/readlink.m4, m4/stat.m4, m4/symlink.m4: New files, automatically generated from gnulib. * aclocal.m4, configure, lib/Makefile.in, lib/gnulib.mk: * lib/stdlib.in.h, m4/gl-comp.m4, m4/stdlib_h.m4: Regenerate. 2011-02-22 Paul Eggert <eggert@cs.ucla.edu> [src/ChangeLog] Work around some portability problems with symlinks. * fileio.c (Frename_file, Fmake_symbolic_link, Ffile_symlink_p): Simplify the code by assuming that the readlink and symlink calls exist, even if they always fail on this host. (Ffile_readable_p): Likewise, for fifos. * config.in: Regenerate.
Diffstat (limited to 'lib/stat.c')
-rw-r--r--lib/stat.c104
1 files changed, 104 insertions, 0 deletions
diff --git a/lib/stat.c b/lib/stat.c
new file mode 100644
index 00000000000..f16d9144afc
--- /dev/null
+++ b/lib/stat.c
@@ -0,0 +1,104 @@
1/* Work around platform bugs in stat.
2 Copyright (C) 2009-2011 Free Software Foundation, Inc.
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17/* written by Eric Blake */
18
19#include <config.h>
20
21/* Get the original definition of stat. It might be defined as a macro. */
22#define __need_system_sys_stat_h
23#include <sys/types.h>
24#include <sys/stat.h>
25#undef __need_system_sys_stat_h
26
27static inline int
28orig_stat (const char *filename, struct stat *buf)
29{
30 return stat (filename, buf);
31}
32
33/* Specification. */
34#include <sys/stat.h>
35
36#include <errno.h>
37#include <limits.h>
38#include <stdbool.h>
39#include <string.h>
40
41/* Store information about NAME into ST. Work around bugs with
42 trailing slashes. Mingw has other bugs (such as st_ino always
43 being 0 on success) which this wrapper does not work around. But
44 at least this implementation provides the ability to emulate fchdir
45 correctly. */
46
47int
48rpl_stat (char const *name, struct stat *st)
49{
50 int result = orig_stat (name, st);
51#if REPLACE_FUNC_STAT_FILE
52 /* Solaris 9 mistakenly succeeds when given a non-directory with a
53 trailing slash. */
54 if (result == 0 && !S_ISDIR (st->st_mode))
55 {
56 size_t len = strlen (name);
57 if (ISSLASH (name[len - 1]))
58 {
59 errno = ENOTDIR;
60 return -1;
61 }
62 }
63#endif /* REPLACE_FUNC_STAT_FILE */
64#if REPLACE_FUNC_STAT_DIR
65 if (result == -1 && errno == ENOENT)
66 {
67 /* Due to mingw's oddities, there are some directories (like
68 c:\) where stat() only succeeds with a trailing slash, and
69 other directories (like c:\windows) where stat() only
70 succeeds without a trailing slash. But we want the two to be
71 synonymous, since chdir() manages either style. Likewise, Mingw also
72 reports ENOENT for names longer than PATH_MAX, when we want
73 ENAMETOOLONG, and for stat("file/"), when we want ENOTDIR.
74 Fortunately, mingw PATH_MAX is small enough for stack
75 allocation. */
76 char fixed_name[PATH_MAX + 1] = {0};
77 size_t len = strlen (name);
78 bool check_dir = false;
79 if (PATH_MAX <= len)
80 errno = ENAMETOOLONG;
81 else if (len)
82 {
83 strcpy (fixed_name, name);
84 if (ISSLASH (fixed_name[len - 1]))
85 {
86 check_dir = true;
87 while (len && ISSLASH (fixed_name[len - 1]))
88 fixed_name[--len] = '\0';
89 if (!len)
90 fixed_name[0] = '/';
91 }
92 else
93 fixed_name[len++] = '/';
94 result = orig_stat (fixed_name, st);
95 if (result == 0 && check_dir && !S_ISDIR (st->st_mode))
96 {
97 result = -1;
98 errno = ENOTDIR;
99 }
100 }
101 }
102#endif /* REPLACE_FUNC_STAT_DIR */
103 return result;
104}