]> git.cworth.org Git - tar/blob - gnu/readlink.c
upstream: Fix extraction of device nodes.
[tar] / gnu / readlink.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Stub for readlink().
4    Copyright (C) 2003-2007, 2009-2010 Free Software 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 #include <config.h>
20
21 /* Specification.  */
22 #include <unistd.h>
23
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/stat.h>
27
28 #if !HAVE_READLINK
29
30 /* readlink() substitute for systems that don't have a readlink() function,
31    such as DJGPP 2.03 and mingw32.  */
32
33 ssize_t
34 readlink (const char *name, char *buf _GL_UNUSED,
35           size_t bufsize _GL_UNUSED)
36 {
37   struct stat statbuf;
38
39   /* In general we should use lstat() here, not stat().  But on platforms
40      without symbolic links, lstat() - if it exists - would be equivalent to
41      stat(), therefore we can use stat().  This saves us a configure check.  */
42   if (stat (name, &statbuf) >= 0)
43     errno = EINVAL;
44   return -1;
45 }
46
47 #else /* HAVE_READLINK */
48
49 # undef readlink
50
51 /* readlink() wrapper that uses correct types, for systems like cygwin
52    1.5.x where readlink returns int, and which rejects trailing slash,
53    for Solaris 9.  */
54
55 ssize_t
56 rpl_readlink (const char *name, char *buf, size_t bufsize)
57 {
58 # if READLINK_TRAILING_SLASH_BUG
59   size_t len = strlen (name);
60   if (len && name[len - 1] == '/')
61     {
62       /* Even if name without the slash is a symlink to a directory,
63          both lstat() and stat() must resolve the trailing slash to
64          the directory rather than the symlink.  We can therefore
65          safely use stat() to distinguish between EINVAL and
66          ENOTDIR/ENOENT, avoiding extra overhead of rpl_lstat().  */
67       struct stat st;
68       if (stat (name, &st) == 0)
69         errno = EINVAL;
70       return -1;
71     }
72 # endif /* READLINK_TRAILING_SLASH_BUG */
73   return readlink (name, buf, bufsize);
74 }
75
76 #endif /* HAVE_READLINK */