summaryrefslogtreecommitdiff
path: root/libbb/xreadlink.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-11-08 17:40:23 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-11-08 17:40:23 +0000
commitabbd363261cf3a9b18e611c524f8103fb68f539b (patch)
tree33545409717701ac3cc40d2a60f80a697835620c /libbb/xreadlink.c
parent53bd4015aa7ec79b8f45edd30555ea7b7235d7ca (diff)
downloadbusybox-w32-abbd363261cf3a9b18e611c524f8103fb68f539b.tar.gz
busybox-w32-abbd363261cf3a9b18e611c524f8103fb68f539b.tar.bz2
busybox-w32-abbd363261cf3a9b18e611c524f8103fb68f539b.zip
xreadlink: code shrink
udhcp: add missing tryagain member to client_config function old new delta xmalloc_readlink_follow 169 154 -15 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-15) Total: -15 bytes
Diffstat (limited to 'libbb/xreadlink.c')
-rw-r--r--libbb/xreadlink.c35
1 files changed, 18 insertions, 17 deletions
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
index 2f6b1e237..f7948cbf3 100644
--- a/libbb/xreadlink.c
+++ b/libbb/xreadlink.c
@@ -41,14 +41,21 @@ char *xmalloc_readlink(const char *path)
41 */ 41 */
42char *xmalloc_readlink_follow(const char *path) 42char *xmalloc_readlink_follow(const char *path)
43{ 43{
44 char *buf = NULL, *lpc, *linkpath; 44 char *buf;
45 char *lpc;
46 char *linkpath;
45 int bufsize; 47 int bufsize;
46 smallint looping = 0; 48 int looping = MAXSYMLINKS + 1;
47 49
48 buf = strdup(path); 50 linkpath = xstrdup(path);
49 bufsize = strlen(path) + 1; 51 goto jump_in;
50 52
51 while(1) { 53 while (1) {
54 if (!--looping) {
55 free(linkpath);
56 free(buf);
57 return NULL;
58 }
52 linkpath = xmalloc_readlink(buf); 59 linkpath = xmalloc_readlink(buf);
53 if (!linkpath) { 60 if (!linkpath) {
54 if (errno == EINVAL) /* not a symlink */ 61 if (errno == EINVAL) /* not a symlink */
@@ -56,25 +63,19 @@ char *xmalloc_readlink_follow(const char *path)
56 free(buf); 63 free(buf);
57 return NULL; 64 return NULL;
58 } 65 }
59 66 if (linkpath[0] != '/') {
60 if (*linkpath == '/') {
61 free(buf);
62 buf = linkpath;
63 bufsize = strlen(linkpath) + 1;
64 } else {
65 bufsize += strlen(linkpath); 67 bufsize += strlen(linkpath);
66 if (looping++ > MAXSYMLINKS) {
67 free(linkpath);
68 free(buf);
69 return NULL;
70 }
71 buf = xrealloc(buf, bufsize); 68 buf = xrealloc(buf, bufsize);
72 lpc = bb_get_last_path_component_strip(buf); 69 lpc = bb_get_last_path_component_strip(buf);
73 strcpy(lpc, linkpath); 70 strcpy(lpc, linkpath);
74 free(linkpath); 71 free(linkpath);
72 } else {
73 free(buf);
74 jump_in:
75 buf = linkpath;
76 bufsize = strlen(buf) + 1;
75 } 77 }
76 } 78 }
77
78} 79}
79 80
80char *xmalloc_readlink_or_warn(const char *path) 81char *xmalloc_readlink_or_warn(const char *path)