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.
|
|
|
|
*/
|
|
|
|
|
2006-03-06 20:47:33 +00:00
|
|
|
char *xreadlink(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);
|
|
|
|
readsize = readlink(path, buf, bufsize); /* 1st try */
|
2001-05-07 17:48:28 +00:00
|
|
|
if (readsize == -1) {
|
2004-08-11 03:50:30 +00:00
|
|
|
bb_perror_msg("%s", path);
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2001-05-07 17:48:28 +00:00
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
}
|
2001-04-30 18:17:00 +00:00
|
|
|
while (bufsize < readsize + 1);
|
|
|
|
|
|
|
|
buf[readsize] = '\0';
|
|
|
|
|
|
|
|
return buf;
|
2004-03-15 08:29:22 +00:00
|
|
|
}
|
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
|
|
|
|
}
|