"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 2 | /* |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 3 | * xreadlink.c - safe implementation of readlink. |
| 4 | * Returns a NULL on failure... |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 7 | #include "libbb.h" |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 8 | |
| 9 | /* |
| 10 | * NOTE: This function returns a malloced char* that you will have to free |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 11 | * yourself. |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 12 | */ |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 13 | char *xmalloc_readlink(const char *path) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 14 | { |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 15 | enum { GROWBY = 80 }; /* how large we will grow strings by */ |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 16 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 17 | char *buf = NULL; |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 18 | int bufsize = 0, readsize = 0; |
| 19 | |
| 20 | do { |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 21 | bufsize += GROWBY; |
| 22 | buf = xrealloc(buf, bufsize); |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 23 | readsize = readlink(path, buf, bufsize); |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 24 | if (readsize == -1) { |
Glenn L McGrath | 18bbd9b | 2004-08-11 03:50:30 +0000 | [diff] [blame] | 25 | free(buf); |
| 26 | return NULL; |
Eric Andersen | 28355a3 | 2001-05-07 17:48:28 +0000 | [diff] [blame] | 27 | } |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 28 | } while (bufsize < readsize + 1); |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 29 | |
| 30 | buf[readsize] = '\0'; |
| 31 | |
| 32 | return buf; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 33 | } |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 34 | |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 35 | /* |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 36 | * 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 Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 42 | * those at the tail. |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 43 | * A malloced char* is returned, which must be freed by the caller. |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 44 | */ |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 45 | char *xmalloc_follow_symlinks(const char *path) |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 46 | { |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 47 | char *buf; |
| 48 | char *lpc; |
| 49 | char *linkpath; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 50 | int bufsize; |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 51 | int looping = MAXSYMLINKS + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 52 | |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 53 | buf = xstrdup(path); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 54 | goto jump_in; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 56 | while (1) { |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 57 | linkpath = xmalloc_readlink(buf); |
| 58 | if (!linkpath) { |
| 59 | /* not a symlink, or doesn't exist */ |
| 60 | if (errno == EINVAL || errno == ENOENT) |
| 61 | return buf; |
Bernhard Reutner-Fischer | 9bd8d0c | 2007-11-08 21:11:43 +0000 | [diff] [blame] | 62 | goto free_buf_ret_null; |
| 63 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 65 | if (!--looping) { |
| 66 | free(linkpath); |
Denis Vlasenko | d031b20 | 2007-11-10 01:28:19 +0000 | [diff] [blame] | 67 | free_buf_ret_null: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 68 | free(buf); |
| 69 | return NULL; |
| 70 | } |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 71 | |
| 72 | if (*linkpath != '/') { |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 73 | bufsize += strlen(linkpath); |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 74 | buf = xrealloc(buf, bufsize); |
| 75 | lpc = bb_get_last_path_component_strip(buf); |
| 76 | strcpy(lpc, linkpath); |
| 77 | free(linkpath); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 78 | } else { |
| 79 | free(buf); |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 80 | buf = linkpath; |
Paul Fox | 599bbfb | 2007-11-08 20:00:36 +0000 | [diff] [blame] | 81 | jump_in: |
Denis Vlasenko | abbd363 | 2007-11-08 17:40:23 +0000 | [diff] [blame] | 82 | bufsize = strlen(buf) + 1; |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
Paul Fox | 459a2ba | 2007-11-08 01:11:41 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 87 | char *xmalloc_readlink_or_warn(const char *path) |
| 88 | { |
| 89 | char *buf = xmalloc_readlink(path); |
| 90 | if (!buf) { |
| 91 | /* EINVAL => "file: Invalid argument" => puzzled user */ |
| 92 | bb_error_msg("%s: cannot read link (not a symlink?)", path); |
| 93 | } |
| 94 | return buf; |
| 95 | } |
| 96 | |
| 97 | /* UNUSED */ |
| 98 | #if 0 |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 99 | char *xmalloc_realpath(const char *path) |
| 100 | { |
Denis Vlasenko | 218f2f4 | 2007-01-24 22:02:01 +0000 | [diff] [blame] | 101 | #if defined(__GLIBC__) && !defined(__UCLIBC__) |
Denis Vlasenko | a9b60e9 | 2007-01-04 17:59:59 +0000 | [diff] [blame] | 102 | /* glibc provides a non-standard extension */ |
| 103 | return realpath(path, NULL); |
| 104 | #else |
| 105 | char buf[PATH_MAX+1]; |
| 106 | |
| 107 | /* on error returns NULL (xstrdup(NULL) ==NULL) */ |
| 108 | return xstrdup(realpath(path, buf)); |
| 109 | #endif |
| 110 | } |
Denis Vlasenko | beffd43 | 2007-09-05 11:30:34 +0000 | [diff] [blame] | 111 | #endif |