aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libbb.h1
-rw-r--r--libbb/xreadlink.c45
2 files changed, 46 insertions, 0 deletions
diff --git a/include/libbb.h b/include/libbb.h
index f79d80d7f..0170085cb 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -260,6 +260,7 @@ DIR *warn_opendir(const char *path);
260 260
261/* UNUSED: char *xmalloc_realpath(const char *path); */ 261/* UNUSED: char *xmalloc_realpath(const char *path); */
262char *xmalloc_readlink(const char *path); 262char *xmalloc_readlink(const char *path);
263char *xmalloc_readlink_follow(const char *path);
263char *xmalloc_readlink_or_warn(const char *path); 264char *xmalloc_readlink_or_warn(const char *path);
264char *xrealloc_getcwd_or_warn(char *cwd); 265char *xrealloc_getcwd_or_warn(char *cwd);
265 266
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
index 98b795f56..2f6b1e237 100644
--- a/libbb/xreadlink.c
+++ b/libbb/xreadlink.c
@@ -32,6 +32,51 @@ char *xmalloc_readlink(const char *path)
32 return buf; 32 return buf;
33} 33}
34 34
35/*
36 * this routine is not the same as realpath(), which canonicalizes
37 * the given path completely. this routine only follows trailing
38 * symlinks until a real file is reached, and returns its name.
39 * intermediate symlinks are not expanded. as above, a malloced
40 * char* is returned, which must be freed.
41 */
42char *xmalloc_readlink_follow(const char *path)
43{
44 char *buf = NULL, *lpc, *linkpath;
45 int bufsize;
46 smallint looping = 0;
47
48 buf = strdup(path);
49 bufsize = strlen(path) + 1;
50
51 while(1) {
52 linkpath = xmalloc_readlink(buf);
53 if (!linkpath) {
54 if (errno == EINVAL) /* not a symlink */
55 return buf;
56 free(buf);
57 return NULL;
58 }
59
60 if (*linkpath == '/') {
61 free(buf);
62 buf = linkpath;
63 bufsize = strlen(linkpath) + 1;
64 } else {
65 bufsize += strlen(linkpath);
66 if (looping++ > MAXSYMLINKS) {
67 free(linkpath);
68 free(buf);
69 return NULL;
70 }
71 buf = xrealloc(buf, bufsize);
72 lpc = bb_get_last_path_component_strip(buf);
73 strcpy(lpc, linkpath);
74 free(linkpath);
75 }
76 }
77
78}
79
35char *xmalloc_readlink_or_warn(const char *path) 80char *xmalloc_readlink_or_warn(const char *path)
36{ 81{
37 char *buf = xmalloc_readlink(path); 82 char *buf = xmalloc_readlink(path);