diff options
author | Mark Whitley <markw@lineo.com> | 2001-04-30 18:17:00 +0000 |
---|---|---|
committer | Mark Whitley <markw@lineo.com> | 2001-04-30 18:17:00 +0000 |
commit | 8a633268ef478a31bd649d582ce07e9c26a4a03a (patch) | |
tree | a90a2bc3c3815f7f0e70f009b9b175736902a848 /libbb/xreadlink.c | |
parent | 6f343944548c2e41fde4a376e805bf322a8e01c9 (diff) | |
download | busybox-w32-8a633268ef478a31bd649d582ce07e9c26a4a03a.tar.gz busybox-w32-8a633268ef478a31bd649d582ce07e9c26a4a03a.tar.bz2 busybox-w32-8a633268ef478a31bd649d582ce07e9c26a4a03a.zip |
Made new xreadlink function for libbb and changed applets to use it instead of
readlink(2).
Diffstat (limited to 'libbb/xreadlink.c')
-rw-r--r-- | libbb/xreadlink.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c new file mode 100644 index 000000000..66f63b883 --- /dev/null +++ b/libbb/xreadlink.c | |||
@@ -0,0 +1,34 @@ | |||
1 | /* | ||
2 | * xreadlink.c - safe implementation of readlink | ||
3 | */ | ||
4 | |||
5 | #include <stdio.h> | ||
6 | |||
7 | /* | ||
8 | * NOTE: This function returns a malloced char* that you will have to free | ||
9 | * yourself. You have been warned. | ||
10 | */ | ||
11 | |||
12 | #include <unistd.h> | ||
13 | #include "libbb.h" | ||
14 | |||
15 | extern char *xreadlink(const char *path) | ||
16 | { | ||
17 | static const int GROWBY = 80; /* how large we will grow strings by */ | ||
18 | |||
19 | char *buf = NULL; | ||
20 | int bufsize = 0, readsize = 0; | ||
21 | |||
22 | do { | ||
23 | buf = xrealloc(buf, bufsize += GROWBY); | ||
24 | readsize = readlink(path, buf, bufsize); /* 1st try */ | ||
25 | if (readsize == -1) | ||
26 | perror_msg("%s:%s", applet_name, path); | ||
27 | } | ||
28 | while (bufsize < readsize + 1); | ||
29 | |||
30 | buf[readsize] = '\0'; | ||
31 | |||
32 | return buf; | ||
33 | } | ||
34 | |||