Imported Upstream version 1.3.14
[debian/gzip] / lib / lstat.c
1 /* Work around a bug of lstat on some systems
2
3    Copyright (C) 1997-1999, 2000-2006, 2008-2009 Free Software
4    Foundation, Inc.
5
6    This program is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19 /* written by Jim Meyering */
20
21 #include <config.h>
22
23 #if !HAVE_LSTAT
24 /* On systems that lack symlinks, our replacement <sys/stat.h> already
25    defined lstat as stat, so there is nothing further to do other than
26    avoid an empty file.  */
27 typedef int dummy;
28 #else /* HAVE_LSTAT */
29
30 /* Get the original definition of lstat.  It might be defined as a macro.  */
31 # define __need_system_sys_stat_h
32 # include <sys/types.h>
33 # include <sys/stat.h>
34 # undef __need_system_sys_stat_h
35
36 static inline int
37 orig_lstat (const char *filename, struct stat *buf)
38 {
39   return lstat (filename, buf);
40 }
41
42 /* Specification.  */
43 # include <sys/stat.h>
44
45 # include <string.h>
46 # include <errno.h>
47
48 /* lstat works differently on Linux and Solaris systems.  POSIX (see
49    `pathname resolution' in the glossary) requires that programs like
50    `ls' take into consideration the fact that FILE has a trailing slash
51    when FILE is a symbolic link.  On Linux and Solaris 10 systems, the
52    lstat function already has the desired semantics (in treating
53    `lstat ("symlink/", sbuf)' just like `lstat ("symlink/.", sbuf)',
54    but on Solaris 9 and earlier it does not.
55
56    If FILE has a trailing slash and specifies a symbolic link,
57    then use stat() to get more info on the referent of FILE.
58    If the referent is a non-directory, then set errno to ENOTDIR
59    and return -1.  Otherwise, return stat's result.  */
60
61 int
62 rpl_lstat (const char *file, struct stat *sbuf)
63 {
64   size_t len;
65   int lstat_result = orig_lstat (file, sbuf);
66
67   if (lstat_result != 0)
68     return lstat_result;
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   len = strlen (file);
76   if (file[len - 1] != '/' || S_ISDIR (sbuf->st_mode))
77     return 0;
78
79   /* At this point, a trailing slash is only permitted on
80      symlink-to-dir; but it should have found information on the
81      directory, not the symlink.  Call stat() to get info about the
82      link's referent.  Our replacement stat guarantees valid results,
83      even if the symlink is not pointing to a directory.  */
84   if (!S_ISLNK (sbuf->st_mode))
85     {
86       errno = ENOTDIR;
87       return -1;
88     }
89   return stat (file, sbuf);
90 }
91
92 #endif /* HAVE_LSTAT */