blob: 0bdf394da278f71f78fa5d27c9cf494e1e9fd70a [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Mark Whitley8a633262001-04-30 18:17:00 +00002/*
Denis Vlasenkodb12d1d2008-12-07 00:52:58 +00003 * xreadlink.c - safe implementation of readlink.
4 * Returns a NULL on failure...
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2, see file LICENSE in this source tree.
Mark Whitley8a633262001-04-30 18:17:00 +00007 */
8
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +00009#include "libbb.h"
Mark Whitley8a633262001-04-30 18:17:00 +000010
11/*
12 * NOTE: This function returns a malloced char* that you will have to free
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000013 * yourself.
Mark Whitley8a633262001-04-30 18:17:00 +000014 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000015char* FAST_FUNC xmalloc_readlink(const char *path)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000016{
Rob Landleybc68cd12006-03-10 19:22:06 +000017 enum { GROWBY = 80 }; /* how large we will grow strings by */
Mark Whitley8a633262001-04-30 18:17:00 +000018
Eric Andersenc7bda1c2004-03-15 08:29:22 +000019 char *buf = NULL;
Mark Whitley8a633262001-04-30 18:17:00 +000020 int bufsize = 0, readsize = 0;
21
22 do {
Denis Vlasenkob68979a2007-11-02 23:31:10 +000023 bufsize += GROWBY;
24 buf = xrealloc(buf, bufsize);
Denis Vlasenkobeffd432007-09-05 11:30:34 +000025 readsize = readlink(path, buf, bufsize);
Eric Andersen28355a32001-05-07 17:48:28 +000026 if (readsize == -1) {
Glenn L McGrath18bbd9b2004-08-11 03:50:30 +000027 free(buf);
28 return NULL;
Eric Andersen28355a32001-05-07 17:48:28 +000029 }
Denis Vlasenkobeffd432007-09-05 11:30:34 +000030 } while (bufsize < readsize + 1);
Mark Whitley8a633262001-04-30 18:17:00 +000031
32 buf[readsize] = '\0';
33
34 return buf;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000035}
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000036
Paul Fox459a2ba2007-11-08 01:11:41 +000037/*
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000038 * This routine is not the same as realpath(), which
39 * canonicalizes the given path completely. This routine only
40 * follows trailing symlinks until a real file is reached and
41 * returns its name. If the path ends in a dangling link or if
42 * the target doesn't exist, the path is returned in any case.
43 * Intermediate symlinks in the path are not expanded -- only
Paul Fox599bbfb2007-11-08 20:00:36 +000044 * those at the tail.
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000045 * A malloced char* is returned, which must be freed by the caller.
Paul Fox459a2ba2007-11-08 01:11:41 +000046 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000047char* FAST_FUNC xmalloc_follow_symlinks(const char *path)
Paul Fox459a2ba2007-11-08 01:11:41 +000048{
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000049 char *buf;
50 char *lpc;
51 char *linkpath;
Paul Fox459a2ba2007-11-08 01:11:41 +000052 int bufsize;
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000053 int looping = MAXSYMLINKS + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000054
Paul Fox599bbfb2007-11-08 20:00:36 +000055 buf = xstrdup(path);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000056 goto jump_in;
Paul Fox459a2ba2007-11-08 01:11:41 +000057
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000058 while (1) {
Paul Fox599bbfb2007-11-08 20:00:36 +000059 linkpath = xmalloc_readlink(buf);
60 if (!linkpath) {
61 /* not a symlink, or doesn't exist */
62 if (errno == EINVAL || errno == ENOENT)
63 return buf;
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000064 goto free_buf_ret_null;
65 }
Paul Fox599bbfb2007-11-08 20:00:36 +000066
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000067 if (!--looping) {
68 free(linkpath);
Denis Vlasenkod031b202007-11-10 01:28:19 +000069 free_buf_ret_null:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000070 free(buf);
71 return NULL;
72 }
Paul Fox599bbfb2007-11-08 20:00:36 +000073
74 if (*linkpath != '/') {
Paul Fox459a2ba2007-11-08 01:11:41 +000075 bufsize += strlen(linkpath);
Paul Fox459a2ba2007-11-08 01:11:41 +000076 buf = xrealloc(buf, bufsize);
77 lpc = bb_get_last_path_component_strip(buf);
78 strcpy(lpc, linkpath);
79 free(linkpath);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000080 } else {
81 free(buf);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000082 buf = linkpath;
Paul Fox599bbfb2007-11-08 20:00:36 +000083 jump_in:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000084 bufsize = strlen(buf) + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000085 }
86 }
Paul Fox459a2ba2007-11-08 01:11:41 +000087}
88
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000089char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
Denis Vlasenkobeffd432007-09-05 11:30:34 +000090{
91 char *buf = xmalloc_readlink(path);
92 if (!buf) {
93 /* EINVAL => "file: Invalid argument" => puzzled user */
Denis Vlasenko3a014b82009-03-21 19:11:23 +000094 const char *errmsg = "not a symlink";
95 int err = errno;
96 if (err != EINVAL)
97 errmsg = strerror(err);
98 bb_error_msg("%s: cannot read link: %s", path, errmsg);
Denis Vlasenkobeffd432007-09-05 11:30:34 +000099 }
100 return buf;
101}
102
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000103char* FAST_FUNC xmalloc_realpath(const char *path)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000104{
Mike Frysingeraf9e70b2013-03-12 11:14:24 -0400105#if defined(__GLIBC__) || \
106 (defined(__UCLIBC__) && UCLIBC_VERSION >= KERNEL_VERSION(0, 9, 31))
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000107 /* glibc provides a non-standard extension */
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100108 /* new: POSIX.1-2008 specifies this behavior as well */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000109 return realpath(path, NULL);
110#else
111 char buf[PATH_MAX+1];
112
Jérémie Koenigfbedacf2010-03-26 19:08:53 +0100113 /* on error returns NULL (xstrdup(NULL) == NULL) */
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000114 return xstrdup(realpath(path, buf));
115#endif
116}