lstat: fix Solaris 9 bug
[gnulib.git] / 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 /* Get the original definition of lstat.  It might be defined as a macro.  */
24 #define __need_system_sys_stat_h
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #undef __need_system_sys_stat_h
28
29 static inline int
30 orig_lstat (const char *filename, struct stat *buf)
31 {
32   return lstat (filename, buf);
33 }
34
35 /* Specification.  */
36 #include <sys/stat.h>
37
38 #include <string.h>
39 #include <errno.h>
40
41 /* lstat works differently on Linux and Solaris systems.  POSIX (see
42    `pathname resolution' in the glossary) requires that programs like
43    `ls' take into consideration the fact that FILE has a trailing slash
44    when FILE is a symbolic link.  On Linux and Solaris 10 systems, the
45    lstat function already has the desired semantics (in treating
46    `lstat ("symlink/", sbuf)' just like `lstat ("symlink/.", sbuf)',
47    but on Solaris 9 and earlier it does not.
48
49    If FILE has a trailing slash and specifies a symbolic link,
50    then use stat() to get more info on the referent of FILE.
51    If the referent is a non-directory, then set errno to ENOTDIR
52    and return -1.  Otherwise, return stat's result.  */
53
54 int
55 rpl_lstat (const char *file, struct stat *sbuf)
56 {
57   size_t len;
58   int lstat_result = orig_lstat (file, sbuf);
59
60   if (lstat_result != 0)
61     return lstat_result;
62
63   /* This replacement file can blindly check against '/' rather than
64      using the ISSLASH macro, because all platforms with '\\' either
65      lack symlinks (mingw) or have working lstat (cygwin) and thus do
66      not compile this file.  0 len should have already been filtered
67      out above, with a failure return of ENOENT.  */
68   len = strlen (file);
69   if (file[len - 1] != '/' || S_ISDIR (sbuf->st_mode))
70     return 0;
71
72   /* At this point, a trailing slash is only permitted on
73      symlink-to-dir; but it should have found information on the
74      directory, not the symlink.  Call stat() to get info about the
75      link's referent.  Our replacement stat guarantees valid results,
76      even if the symlink is not pointing to a directory.  */
77   if (!S_ISLNK (sbuf->st_mode))
78     {
79       errno = ENOTDIR;
80       return -1;
81     }
82   return stat (file, sbuf);
83 }