annotate lib/readlink.c @ 17935:0ad1f4c9eed5 default tip

tests: support stderr verification with returns_() * tests/init.sh (returns_): Disable tracing for this wrapper function, so that stderr of the wrapped command is unchanged, allowing for verification of the contents.
author Pádraig Brady <P@draigBrady.com>
date Mon, 16 Feb 2015 17:20:39 +0000
parents ab58d4870664
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Stub for readlink().
17848
ab58d4870664 version-etc: new year
Paul Eggert <eggert@cs.ucla.edu>
parents: 17587
diff changeset
2 Copyright (C) 2003-2007, 2009-2015 Free Software Foundation, Inc.
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8199
diff changeset
4 This program is free software: you can redistribute it and/or modify
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8199
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8199
diff changeset
7 (at your option) any later version.
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 GNU General Public License for more details.
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
9309
bbbbbf4cd1c5 Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents: 8199
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16
7304
1c4ed7637c24 Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents: 7024
diff changeset
17 #include <config.h>
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
8199
51d32a83a7df Move more declarations into <unistd.h>.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
19 /* Specification. */
51d32a83a7df Move more declarations into <unistd.h>.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
20 #include <unistd.h>
51d32a83a7df Move more declarations into <unistd.h>.
Bruno Haible <bruno@clisp.org>
parents: 7304
diff changeset
21
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include <errno.h>
12058
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
23 #include <string.h>
4408
84ab196c83d8 Provide the correct errno.
Bruno Haible <bruno@clisp.org>
parents: 4405
diff changeset
24 #include <sys/stat.h>
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #if !HAVE_READLINK
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 /* readlink() substitute for systems that don't have a readlink() function,
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 such as DJGPP 2.03 and mingw32. */
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30
12058
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
31 ssize_t
12482
11bc92a9e2bc gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents: 12059
diff changeset
32 readlink (const char *name, char *buf _GL_UNUSED,
11bc92a9e2bc gnulib-common: prefer _GL_UNUSED over _UNUSED_PARAMETER_
Eric Blake <ebb9@byu.net>
parents: 12059
diff changeset
33 size_t bufsize _GL_UNUSED)
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 {
4408
84ab196c83d8 Provide the correct errno.
Bruno Haible <bruno@clisp.org>
parents: 4405
diff changeset
35 struct stat statbuf;
84ab196c83d8 Provide the correct errno.
Bruno Haible <bruno@clisp.org>
parents: 4405
diff changeset
36
84ab196c83d8 Provide the correct errno.
Bruno Haible <bruno@clisp.org>
parents: 4405
diff changeset
37 /* In general we should use lstat() here, not stat(). But on platforms
12058
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
38 without symbolic links, lstat() - if it exists - would be equivalent to
4408
84ab196c83d8 Provide the correct errno.
Bruno Haible <bruno@clisp.org>
parents: 4405
diff changeset
39 stat(), therefore we can use stat(). This saves us a configure check. */
12058
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
40 if (stat (name, &statbuf) >= 0)
4408
84ab196c83d8 Provide the correct errno.
Bruno Haible <bruno@clisp.org>
parents: 4405
diff changeset
41 errno = EINVAL;
4405
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 return -1;
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 }
55fc591dca07 New module 'readlink'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
12058
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
45 #else /* HAVE_READLINK */
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
46
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
47 # undef readlink
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
48
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
49 /* readlink() wrapper that uses correct types, for systems like cygwin
12059
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
50 1.5.x where readlink returns int, and which rejects trailing slash,
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
51 for Solaris 9. */
12058
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
52
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
53 ssize_t
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
54 rpl_readlink (const char *name, char *buf, size_t bufsize)
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
55 {
12059
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
56 # if READLINK_TRAILING_SLASH_BUG
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
57 size_t len = strlen (name);
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
58 if (len && name[len - 1] == '/')
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
59 {
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
60 /* Even if name without the slash is a symlink to a directory,
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
61 both lstat() and stat() must resolve the trailing slash to
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
62 the directory rather than the symlink. We can therefore
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
63 safely use stat() to distinguish between EINVAL and
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
64 ENOTDIR/ENOENT, avoiding extra overhead of rpl_lstat(). */
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
65 struct stat st;
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
66 if (stat (name, &st) == 0)
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
67 errno = EINVAL;
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
68 return -1;
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
69 }
6babf16a67dd readlink: fix Solaris 9 bug with trailing slash
Eric Blake <ebb9@byu.net>
parents: 12058
diff changeset
70 # endif /* READLINK_TRAILING_SLASH_BUG */
12058
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
71 return readlink (name, buf, bufsize);
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
72 }
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
73
36183b482b71 readlink: fix cygwin 1.5.x bug with return type
Eric Blake <ebb9@byu.net>
parents: 9309
diff changeset
74 #endif /* HAVE_READLINK */