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