1 /* vi: set sw=4 ts=4: */
3 * xreadlink.c - safe implementation of readlink.
4 * Returns a NULL on failure...
10 * NOTE: This function returns a malloced char* that you will have to free
11 * yourself. You have been warned.
13 char *xmalloc_readlink(const char *path)
15 enum { GROWBY = 80 }; /* how large we will grow strings by */
18 int bufsize = 0, readsize = 0;
21 buf = xrealloc(buf, bufsize += GROWBY);
22 readsize = readlink(path, buf, bufsize);
27 } while (bufsize < readsize + 1);
34 char *xmalloc_readlink_or_warn(const char *path)
36 char *buf = xmalloc_readlink(path);
38 /* EINVAL => "file: Invalid argument" => puzzled user */
39 bb_error_msg("%s: cannot read link (not a symlink?)", path);
46 char *xmalloc_realpath(const char *path)
48 #if defined(__GLIBC__) && !defined(__UCLIBC__)
49 /* glibc provides a non-standard extension */
50 return realpath(path, NULL);
54 /* on error returns NULL (xstrdup(NULL) ==NULL) */
55 return xstrdup(realpath(path, buf));