2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2001-04-30 23:47:00 +05:30
|
|
|
/*
|
2001-05-07 23:18:28 +05:30
|
|
|
* xreadlink.c - safe implementation of readlink.
|
|
|
|
* Returns a NULL on failure...
|
2001-04-30 23:47:00 +05:30
|
|
|
*/
|
|
|
|
|
2007-01-04 23:29:59 +05:30
|
|
|
#include "libbb.h"
|
2001-04-30 23:47:00 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* NOTE: This function returns a malloced char* that you will have to free
|
2007-11-09 02:41:43 +05:30
|
|
|
* yourself.
|
2001-04-30 23:47:00 +05:30
|
|
|
*/
|
2007-09-05 17:00:34 +05:30
|
|
|
char *xmalloc_readlink(const char *path)
|
2006-01-25 05:38:53 +05:30
|
|
|
{
|
2006-03-11 00:52:06 +05:30
|
|
|
enum { GROWBY = 80 }; /* how large we will grow strings by */
|
2001-04-30 23:47:00 +05:30
|
|
|
|
2004-03-15 13:59:22 +05:30
|
|
|
char *buf = NULL;
|
2001-04-30 23:47:00 +05:30
|
|
|
int bufsize = 0, readsize = 0;
|
|
|
|
|
|
|
|
do {
|
2007-11-03 05:01:10 +05:30
|
|
|
bufsize += GROWBY;
|
|
|
|
buf = xrealloc(buf, bufsize);
|
2007-09-05 17:00:34 +05:30
|
|
|
readsize = readlink(path, buf, bufsize);
|
2001-05-07 23:18:28 +05:30
|
|
|
if (readsize == -1) {
|
2004-08-11 09:20:30 +05:30
|
|
|
free(buf);
|
|
|
|
return NULL;
|
2001-05-07 23:18:28 +05:30
|
|
|
}
|
2007-09-05 17:00:34 +05:30
|
|
|
} while (bufsize < readsize + 1);
|
2001-04-30 23:47:00 +05:30
|
|
|
|
|
|
|
buf[readsize] = '\0';
|
|
|
|
|
|
|
|
return buf;
|
2004-03-15 13:59:22 +05:30
|
|
|
}
|
2007-01-04 23:29:59 +05:30
|
|
|
|
2007-11-08 06:41:41 +05:30
|
|
|
/*
|
2007-11-09 02:41:43 +05:30
|
|
|
* 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. If the path ends in a dangling link or if
|
|
|
|
* the target doesn't exist, the path is returned in any case.
|
|
|
|
* Intermediate symlinks in the path are not expanded -- only
|
2007-11-09 01:30:36 +05:30
|
|
|
* those at the tail.
|
2007-11-09 02:41:43 +05:30
|
|
|
* A malloced char* is returned, which must be freed by the caller.
|
2007-11-08 06:41:41 +05:30
|
|
|
*/
|
2007-11-09 01:30:36 +05:30
|
|
|
char *xmalloc_follow_symlinks(const char *path)
|
2007-11-08 06:41:41 +05:30
|
|
|
{
|
2007-11-08 23:10:23 +05:30
|
|
|
char *buf;
|
|
|
|
char *lpc;
|
|
|
|
char *linkpath;
|
2007-11-08 06:41:41 +05:30
|
|
|
int bufsize;
|
2007-11-08 23:10:23 +05:30
|
|
|
int looping = MAXSYMLINKS + 1;
|
2007-11-08 06:41:41 +05:30
|
|
|
|
2007-11-09 01:30:36 +05:30
|
|
|
buf = xstrdup(path);
|
2007-11-08 23:10:23 +05:30
|
|
|
goto jump_in;
|
2007-11-08 06:41:41 +05:30
|
|
|
|
2007-11-08 23:10:23 +05:30
|
|
|
while (1) {
|
2007-11-08 06:41:41 +05:30
|
|
|
linkpath = xmalloc_readlink(buf);
|
|
|
|
if (!linkpath) {
|
2007-11-09 01:30:36 +05:30
|
|
|
/* not a symlink, or doesn't exist */
|
|
|
|
if (errno == EINVAL || errno == ENOENT)
|
2007-11-08 06:41:41 +05:30
|
|
|
return buf;
|
2007-11-09 02:41:43 +05:30
|
|
|
goto free_buf_ret_null;
|
|
|
|
}
|
2007-11-09 01:30:36 +05:30
|
|
|
|
|
|
|
if (!--looping) {
|
|
|
|
free(linkpath);
|
2007-11-10 06:58:19 +05:30
|
|
|
free_buf_ret_null:
|
2007-11-09 01:30:36 +05:30
|
|
|
free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*linkpath != '/') {
|
2007-11-08 06:41:41 +05:30
|
|
|
bufsize += strlen(linkpath);
|
|
|
|
buf = xrealloc(buf, bufsize);
|
|
|
|
lpc = bb_get_last_path_component_strip(buf);
|
|
|
|
strcpy(lpc, linkpath);
|
|
|
|
free(linkpath);
|
2007-11-08 23:10:23 +05:30
|
|
|
} else {
|
|
|
|
free(buf);
|
|
|
|
buf = linkpath;
|
2007-11-09 01:30:36 +05:30
|
|
|
jump_in:
|
2007-11-08 23:10:23 +05:30
|
|
|
bufsize = strlen(buf) + 1;
|
2007-11-08 06:41:41 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-05 17:00:34 +05:30
|
|
|
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 23:29:59 +05:30
|
|
|
char *xmalloc_realpath(const char *path)
|
|
|
|
{
|
2007-01-25 03:32:01 +05:30
|
|
|
#if defined(__GLIBC__) && !defined(__UCLIBC__)
|
2007-01-04 23:29:59 +05:30
|
|
|
/* 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 17:00:34 +05:30
|
|
|
#endif
|