xreadlink: code shrink
[oweals/busybox.git] / libbb / xreadlink.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  *  xreadlink.c - safe implementation of readlink.
4  *  Returns a NULL on failure...
5  */
6
7 #include "libbb.h"
8
9 /*
10  * NOTE: This function returns a malloced char* that you will have to free
11  * yourself. You have been warned.
12  */
13 char *xmalloc_readlink(const char *path)
14 {
15         enum { GROWBY = 80 }; /* how large we will grow strings by */
16
17         char *buf = NULL;
18         int bufsize = 0, readsize = 0;
19
20         do {
21                 bufsize += GROWBY;
22                 buf = xrealloc(buf, bufsize);
23                 readsize = readlink(path, buf, bufsize);
24                 if (readsize == -1) {
25                         free(buf);
26                         return NULL;
27                 }
28         } while (bufsize < readsize + 1);
29
30         buf[readsize] = '\0';
31
32         return buf;
33 }
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  */
42 char *xmalloc_readlink_follow(const char *path)
43 {
44         char *buf;
45         char *lpc;
46         char *linkpath;
47         int bufsize;
48         int looping = MAXSYMLINKS + 1;
49
50         linkpath = xstrdup(path);
51         goto jump_in;
52
53         while (1) {
54                 if (!--looping) {
55                         free(linkpath);
56                         free(buf);
57                         return NULL;
58                 }
59                 linkpath = xmalloc_readlink(buf);
60                 if (!linkpath) {
61                         if (errno == EINVAL) /* not a symlink */
62                                 return buf;
63                         free(buf);
64                         return NULL;
65                 } 
66                 if (linkpath[0] != '/') {
67                         bufsize += strlen(linkpath);
68                         buf = xrealloc(buf, bufsize);
69                         lpc = bb_get_last_path_component_strip(buf);
70                         strcpy(lpc, linkpath);
71                         free(linkpath);
72                 } else {
73                         free(buf);
74  jump_in:
75                         buf = linkpath;
76                         bufsize = strlen(buf) + 1;
77                 }
78         }
79 }
80
81 char *xmalloc_readlink_or_warn(const char *path)
82 {
83         char *buf = xmalloc_readlink(path);
84         if (!buf) {
85                 /* EINVAL => "file: Invalid argument" => puzzled user */
86                 bb_error_msg("%s: cannot read link (not a symlink?)", path);
87         }
88         return buf;
89 }
90
91 /* UNUSED */
92 #if 0
93 char *xmalloc_realpath(const char *path)
94 {
95 #if defined(__GLIBC__) && !defined(__UCLIBC__)
96         /* glibc provides a non-standard extension */
97         return realpath(path, NULL);
98 #else
99         char buf[PATH_MAX+1];
100
101         /* on error returns NULL (xstrdup(NULL) ==NULL) */
102         return xstrdup(realpath(path, buf));
103 #endif
104 }
105 #endif