realpath,readlink -f: coreutils compat, closes 11021
function old new delta xmalloc_realpath_coreutils - 121 +121 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
@ -122,3 +122,33 @@ char* FAST_FUNC xmalloc_realpath(const char *path)
|
||||
return xstrdup(realpath(path, buf));
|
||||
#endif
|
||||
}
|
||||
|
||||
char* FAST_FUNC xmalloc_realpath_coreutils(const char *path)
|
||||
{
|
||||
char *buf;
|
||||
|
||||
errno = 0;
|
||||
buf = xmalloc_realpath(path);
|
||||
/*
|
||||
* There is one case when "readlink -f" and
|
||||
* "realpath" from coreutils succeed,
|
||||
* even though file does not exist, such as:
|
||||
* /tmp/file_does_not_exist
|
||||
* (the directory must exist).
|
||||
*/
|
||||
if (!buf && errno == ENOENT) {
|
||||
char *last_slash = strrchr(path, '/');
|
||||
if (last_slash) {
|
||||
*last_slash++ = '\0';
|
||||
buf = xmalloc_realpath(path);
|
||||
if (buf) {
|
||||
unsigned len = strlen(buf);
|
||||
buf = xrealloc(buf, len + strlen(last_slash) + 2);
|
||||
buf[len++] = '/';
|
||||
strcpy(buf + len, last_slash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
Reference in New Issue
Block a user