2006-07-02 19:47:05 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2001-04-30 18:17:00 +00:00
|
|
|
/*
|
2001-05-07 17:48:28 +00:00
|
|
|
* xreadlink.c - safe implementation of readlink.
|
|
|
|
* Returns a NULL on failure...
|
2001-04-30 18:17:00 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-04 17:59:59 +00:00
|
|
|
#include "libbb.h"
|
2001-04-30 18:17:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: This function returns a malloced char* that you will have to free
|
|
|
|
* yourself. You have been warned.
|
|
|
|
*/
|
2007-09-05 11:30:34 +00:00
|
|
|
char *xmalloc_readlink(const char *path)
|
2006-01-25 00:08:53 +00:00
|
|
|
{
|
2006-03-10 19:22:06 +00:00
|
|
|
enum { GROWBY = 80 }; /* how large we will grow strings by */
|
2001-04-30 18:17:00 +00:00
|
|
|
|
2004-03-15 08:29:22 +00:00
|
|
|
char *buf = NULL;
|
2001-04-30 18:17:00 +00:00
|
|
|
int bufsize = 0, readsize = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
buf = xrealloc(buf, bufsize += GROWBY);
|
2007-09-05 11:30:34 +00:00
|
|
|
readsize = readlink(path, buf, bufsize);
|
2001-05-07 17:48:28 +00:00
|
|
|
if (readsize == -1) {
|
2004-08-11 03:50:30 +00:00
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2001-05-07 17:48:28 +00:00
|
|
|
}
|
2007-09-05 11:30:34 +00:00
|
|
|
} while (bufsize < readsize + 1);
|
2001-04-30 18:17:00 +00:00
|
|
|
|
|
|
|
buf[readsize] = '\0';
|
|
|
|
|
|
|
|
return buf;
|
2004-03-15 08:29:22 +00:00
|
|
|
}
|
2007-01-04 17:59:59 +00:00
|
|
|
|
2007-09-05 11:30:34 +00:00
|
|
|
char *xmalloc_readlink_or_warn(const char *path)
|
|
|
|
{
|
|
|
|
char *buf = xmalloc_readlink(path);
|
|
|
|
if (!buf) {
|
|
|
|
/* EINVAL => "file: Invalid argument" => puzzled user */
|
|
|
|
bb_error_msg("%s: cannot read link (not a symlink?)", path);
|
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* UNUSED */
|
|
|
|
#if 0
|
2007-01-04 17:59:59 +00:00
|
|
|
char *xmalloc_realpath(const char *path)
|
|
|
|
{
|
2007-01-24 22:02:01 +00:00
|
|
|
#if defined(__GLIBC__) && !defined(__UCLIBC__)
|
2007-01-04 17:59:59 +00:00
|
|
|
/* glibc provides a non-standard extension */
|
|
|
|
return realpath(path, NULL);
|
|
|
|
#else
|
|
|
|
char buf[PATH_MAX+1];
|
|
|
|
|
|
|
|
/* on error returns NULL (xstrdup(NULL) ==NULL) */
|
|
|
|
return xstrdup(realpath(path, buf));
|
|
|
|
#endif
|
|
|
|
}
|
2007-09-05 11:30:34 +00:00
|
|
|
#endif
|