blob: 706a3d9fff98f5ac5ecc31f6108551646ae4e2cb [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/*
Eric Andersen28355a32001-05-07 17:48:28 +00003 * xreadlink.c - safe implementation of readlink.
4 * Returns a NULL on failure...
Mark Whitley8a633262001-04-30 18:17:00 +00005 */
6
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +00007#include "libbb.h"
Mark Whitley8a633262001-04-30 18:17:00 +00008
9/*
10 * NOTE: This function returns a malloced char* that you will have to free
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000011 * yourself.
Mark Whitley8a633262001-04-30 18:17:00 +000012 */
Denis Vlasenkobeffd432007-09-05 11:30:34 +000013char *xmalloc_readlink(const char *path)
Tim Rikerc1ef7bd2006-01-25 00:08:53 +000014{
Rob Landleybc68cd12006-03-10 19:22:06 +000015 enum { GROWBY = 80 }; /* how large we will grow strings by */
Mark Whitley8a633262001-04-30 18:17:00 +000016
Eric Andersenc7bda1c2004-03-15 08:29:22 +000017 char *buf = NULL;
Mark Whitley8a633262001-04-30 18:17:00 +000018 int bufsize = 0, readsize = 0;
19
20 do {
Denis Vlasenkob68979a2007-11-02 23:31:10 +000021 bufsize += GROWBY;
22 buf = xrealloc(buf, bufsize);
Denis Vlasenkobeffd432007-09-05 11:30:34 +000023 readsize = readlink(path, buf, bufsize);
Eric Andersen28355a32001-05-07 17:48:28 +000024 if (readsize == -1) {
Glenn L McGrath18bbd9b2004-08-11 03:50:30 +000025 free(buf);
26 return NULL;
Eric Andersen28355a32001-05-07 17:48:28 +000027 }
Denis Vlasenkobeffd432007-09-05 11:30:34 +000028 } while (bufsize < readsize + 1);
Mark Whitley8a633262001-04-30 18:17:00 +000029
30 buf[readsize] = '\0';
31
32 return buf;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000033}
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +000034
Paul Fox459a2ba2007-11-08 01:11:41 +000035/*
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000036 * This routine is not the same as realpath(), which
37 * canonicalizes the given path completely. This routine only
38 * follows trailing symlinks until a real file is reached and
39 * returns its name. If the path ends in a dangling link or if
40 * the target doesn't exist, the path is returned in any case.
41 * Intermediate symlinks in the path are not expanded -- only
Paul Fox599bbfb2007-11-08 20:00:36 +000042 * those at the tail.
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000043 * A malloced char* is returned, which must be freed by the caller.
Paul Fox459a2ba2007-11-08 01:11:41 +000044 */
Paul Fox599bbfb2007-11-08 20:00:36 +000045char *xmalloc_follow_symlinks(const char *path)
Paul Fox459a2ba2007-11-08 01:11:41 +000046{
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000047 char *buf;
48 char *lpc;
49 char *linkpath;
Paul Fox459a2ba2007-11-08 01:11:41 +000050 int bufsize;
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000051 int looping = MAXSYMLINKS + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000052
Paul Fox599bbfb2007-11-08 20:00:36 +000053 buf = xstrdup(path);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000054 goto jump_in;
Paul Fox459a2ba2007-11-08 01:11:41 +000055
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000056 while (1) {
Paul Fox599bbfb2007-11-08 20:00:36 +000057
58 linkpath = xmalloc_readlink(buf);
59 if (!linkpath) {
60 /* not a symlink, or doesn't exist */
61 if (errno == EINVAL || errno == ENOENT)
62 return buf;
Bernhard Reutner-Fischer9bd8d0c2007-11-08 21:11:43 +000063 goto free_buf_ret_null;
64 }
Paul Fox599bbfb2007-11-08 20:00:36 +000065
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000066 if (!--looping) {
67 free(linkpath);
Denis Vlasenkod031b202007-11-10 01:28:19 +000068 free_buf_ret_null:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000069 free(buf);
70 return NULL;
71 }
Paul Fox599bbfb2007-11-08 20:00:36 +000072
73 if (*linkpath != '/') {
Paul Fox459a2ba2007-11-08 01:11:41 +000074 bufsize += strlen(linkpath);
Paul Fox459a2ba2007-11-08 01:11:41 +000075 buf = xrealloc(buf, bufsize);
76 lpc = bb_get_last_path_component_strip(buf);
77 strcpy(lpc, linkpath);
78 free(linkpath);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000079 } else {
80 free(buf);
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000081 buf = linkpath;
Paul Fox599bbfb2007-11-08 20:00:36 +000082 jump_in:
Denis Vlasenkoabbd3632007-11-08 17:40:23 +000083 bufsize = strlen(buf) + 1;
Paul Fox459a2ba2007-11-08 01:11:41 +000084 }
85 }
Paul Fox459a2ba2007-11-08 01:11:41 +000086}
87
Denis Vlasenkobeffd432007-09-05 11:30:34 +000088char *xmalloc_readlink_or_warn(const char *path)
89{
90 char *buf = xmalloc_readlink(path);
91 if (!buf) {
92 /* EINVAL => "file: Invalid argument" => puzzled user */
93 bb_error_msg("%s: cannot read link (not a symlink?)", path);
94 }
95 return buf;
96}
97
98/* UNUSED */
99#if 0
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000100char *xmalloc_realpath(const char *path)
101{
Denis Vlasenko218f2f42007-01-24 22:02:01 +0000102#if defined(__GLIBC__) && !defined(__UCLIBC__)
Denis Vlasenkoa9b60e92007-01-04 17:59:59 +0000103 /* glibc provides a non-standard extension */
104 return realpath(path, NULL);
105#else
106 char buf[PATH_MAX+1];
107
108 /* on error returns NULL (xstrdup(NULL) ==NULL) */
109 return xstrdup(realpath(path, buf));
110#endif
111}
Denis Vlasenkobeffd432007-09-05 11:30:34 +0000112#endif