GDB support: add frame annotators and filters
[bpt/guile.git] / lib / lstat.c
CommitLineData
8912421c
LC
1/* Work around a bug of lstat on some systems
2
5e69ceb7 3 Copyright (C) 1997-2006, 2008-2014 Free Software Foundation, Inc.
8912421c
LC
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program 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 <http://www.gnu.org/licenses/>. */
17
18/* written by Jim Meyering */
19
35428fb6
LC
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
8912421c
LC
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. */
8912421c
LC
34# include <sys/types.h>
35# include <sys/stat.h>
36# undef __need_system_sys_stat_h
37
af07e104 38static int
8912421c
LC
39orig_lstat (const char *filename, struct stat *buf)
40{
41 return lstat (filename, buf);
42}
43
44/* Specification. */
35428fb6
LC
45/* Write "sys/stat.h" here, not <sys/stat.h>, otherwise OSF/1 5.1 DTK cc
46 eliminates this include because of the preliminary #include <sys/stat.h>
47 above. */
48# include "sys/stat.h"
8912421c
LC
49
50# include <string.h>
51# include <errno.h>
52
53/* lstat works differently on Linux and Solaris systems. POSIX (see
f0007cad
LC
54 "pathname resolution" in the glossary) requires that programs like
55 'ls' take into consideration the fact that FILE has a trailing slash
8912421c
LC
56 when FILE is a symbolic link. On Linux and Solaris 10 systems, the
57 lstat function already has the desired semantics (in treating
f0007cad 58 'lstat ("symlink/", sbuf)' just like 'lstat ("symlink/.", sbuf)',
8912421c
LC
59 but on Solaris 9 and earlier it does not.
60
61 If FILE has a trailing slash and specifies a symbolic link,
62 then use stat() to get more info on the referent of FILE.
63 If the referent is a non-directory, then set errno to ENOTDIR
64 and return -1. Otherwise, return stat's result. */
65
66int
67rpl_lstat (const char *file, struct stat *sbuf)
68{
69 size_t len;
70 int lstat_result = orig_lstat (file, sbuf);
71
72 if (lstat_result != 0)
73 return lstat_result;
74
75 /* This replacement file can blindly check against '/' rather than
76 using the ISSLASH macro, because all platforms with '\\' either
77 lack symlinks (mingw) or have working lstat (cygwin) and thus do
78 not compile this file. 0 len should have already been filtered
79 out above, with a failure return of ENOENT. */
80 len = strlen (file);
81 if (file[len - 1] != '/' || S_ISDIR (sbuf->st_mode))
82 return 0;
83
84 /* At this point, a trailing slash is only permitted on
85 symlink-to-dir; but it should have found information on the
86 directory, not the symlink. Call stat() to get info about the
87 link's referent. Our replacement stat guarantees valid results,
88 even if the symlink is not pointing to a directory. */
89 if (!S_ISLNK (sbuf->st_mode))
90 {
91 errno = ENOTDIR;
92 return -1;
93 }
94 return stat (file, sbuf);
95}
96
97#endif /* HAVE_LSTAT */