Magellan Linux

Annotation of /trunk/mkinitrd-magellan/busybox/libbb/xreadlink.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1123 - (hide annotations) (download)
Wed Aug 18 21:56:57 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 2647 byte(s)
-updated to busybox-1.17.1
1 niro 532 /* vi: set sw=4 ts=4: */
2     /*
3 niro 984 * xreadlink.c - safe implementation of readlink.
4     * Returns a NULL on failure...
5     *
6     * Licensed under GPLv2, see file LICENSE in this tarball for details.
7 niro 532 */
8    
9     #include "libbb.h"
10    
11     /*
12     * NOTE: This function returns a malloced char* that you will have to free
13 niro 816 * yourself.
14 niro 532 */
15 niro 816 char* FAST_FUNC xmalloc_readlink(const char *path)
16 niro 532 {
17     enum { GROWBY = 80 }; /* how large we will grow strings by */
18    
19     char *buf = NULL;
20     int bufsize = 0, readsize = 0;
21    
22     do {
23 niro 816 bufsize += GROWBY;
24     buf = xrealloc(buf, bufsize);
25     readsize = readlink(path, buf, bufsize);
26 niro 532 if (readsize == -1) {
27     free(buf);
28     return NULL;
29     }
30 niro 816 } while (bufsize < readsize + 1);
31 niro 532
32     buf[readsize] = '\0';
33    
34     return buf;
35     }
36    
37 niro 816 /*
38     * 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
44     * those at the tail.
45     * A malloced char* is returned, which must be freed by the caller.
46     */
47     char* FAST_FUNC xmalloc_follow_symlinks(const char *path)
48 niro 532 {
49 niro 816 char *buf;
50     char *lpc;
51     char *linkpath;
52     int bufsize;
53     int looping = MAXSYMLINKS + 1;
54    
55     buf = xstrdup(path);
56     goto jump_in;
57    
58     while (1) {
59     linkpath = xmalloc_readlink(buf);
60     if (!linkpath) {
61     /* not a symlink, or doesn't exist */
62     if (errno == EINVAL || errno == ENOENT)
63     return buf;
64     goto free_buf_ret_null;
65     }
66    
67     if (!--looping) {
68     free(linkpath);
69     free_buf_ret_null:
70     free(buf);
71     return NULL;
72     }
73    
74     if (*linkpath != '/') {
75     bufsize += strlen(linkpath);
76     buf = xrealloc(buf, bufsize);
77     lpc = bb_get_last_path_component_strip(buf);
78     strcpy(lpc, linkpath);
79     free(linkpath);
80     } else {
81     free(buf);
82     buf = linkpath;
83     jump_in:
84     bufsize = strlen(buf) + 1;
85     }
86     }
87     }
88    
89     char* FAST_FUNC xmalloc_readlink_or_warn(const char *path)
90     {
91     char *buf = xmalloc_readlink(path);
92     if (!buf) {
93     /* EINVAL => "file: Invalid argument" => puzzled user */
94 niro 984 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);
99 niro 816 }
100     return buf;
101     }
102    
103     char* FAST_FUNC xmalloc_realpath(const char *path)
104     {
105 niro 532 #if defined(__GLIBC__) && !defined(__UCLIBC__)
106     /* glibc provides a non-standard extension */
107 niro 1123 /* new: POSIX.1-2008 specifies this behavior as well */
108 niro 532 return realpath(path, NULL);
109     #else
110     char buf[PATH_MAX+1];
111    
112 niro 1123 /* on error returns NULL (xstrdup(NULL) == NULL) */
113 niro 532 return xstrdup(realpath(path, buf));
114     #endif
115     }