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 {
|
2007-11-02 23:31:10 +00:00
|
|
|
bufsize += GROWBY;
|
|
|
|
buf = xrealloc(buf, bufsize);
|
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-11-08 01:11:41 +00:00
|
|
|
/*
|
|
|
|
* this routine is not the same as realpath(), which canonicalizes
|
|
|
|
* the given path completely. this routine only follows trailing
|
|
|
|
* symlinks until a real file is reached, and returns its name.
|
|
|
|
* intermediate symlinks are not expanded. as above, a malloced
|
|
|
|
* char* is returned, which must be freed.
|
|
|
|
*/
|
|
|
|
char *xmalloc_readlink_follow(const char *path)
|
|
|
|
{
|
2007-11-08 17:40:23 +00:00
|
|
|
char *buf;
|
|
|
|
char *lpc;
|
|
|
|
char *linkpath;
|
2007-11-08 01:11:41 +00:00
|
|
|
int bufsize;
|
2007-11-08 17:40:23 +00:00
|
|
|
int looping = MAXSYMLINKS + 1;
|
2007-11-08 01:11:41 +00:00
|
|
|
|
2007-11-08 17:40:23 +00:00
|
|
|
linkpath = xstrdup(path);
|
|
|
|
goto jump_in;
|
2007-11-08 01:11:41 +00:00
|
|
|
|
2007-11-08 17:40:23 +00:00
|
|
|
while (1) {
|
|
|
|
if (!--looping) {
|
|
|
|
free(linkpath);
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-08 01:11:41 +00:00
|
|
|
linkpath = xmalloc_readlink(buf);
|
|
|
|
if (!linkpath) {
|
|
|
|
if (errno == EINVAL) /* not a symlink */
|
|
|
|
return buf;
|
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-08 17:40:23 +00:00
|
|
|
if (linkpath[0] != '/') {
|
2007-11-08 01:11:41 +00:00
|
|
|
bufsize += strlen(linkpath);
|
|
|
|
buf = xrealloc(buf, bufsize);
|
|
|
|
lpc = bb_get_last_path_component_strip(buf);
|
|
|
|
strcpy(lpc, linkpath);
|
|
|
|
free(linkpath);
|
2007-11-08 17:40:23 +00:00
|
|
|
} else {
|
|
|
|
free(buf);
|
|
|
|
jump_in:
|
|
|
|
buf = linkpath;
|
|
|
|
bufsize = strlen(buf) + 1;
|
2007-11-08 01:11:41 +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
|