aboutsummaryrefslogtreecommitdiff
path: root/libbb/xreadlink.c
diff options
context:
space:
mode:
Diffstat (limited to 'libbb/xreadlink.c')
-rw-r--r--libbb/xreadlink.c34
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
15extern 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