diff options
author | Denys Vlasenko | 2018-05-24 17:29:14 +0200 |
---|---|---|
committer | Denys Vlasenko | 2018-05-24 17:31:00 +0200 |
commit | 747162109fb1c891baf6aafa1ca0410bd08d04a4 (patch) | |
tree | 7215b1a5b94212e207b9c3d7c1426e65c2ce0553 /libbb/xreadlink.c | |
parent | 3f91e662f21083d4febf74fa89ccc50e406cbf6c (diff) | |
download | busybox-747162109fb1c891baf6aafa1ca0410bd08d04a4.zip busybox-747162109fb1c891baf6aafa1ca0410bd08d04a4.tar.gz |
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>
Diffstat (limited to 'libbb/xreadlink.c')
-rw-r--r-- | libbb/xreadlink.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c index 9b62bcc..6315033 100644 --- a/libbb/xreadlink.c +++ b/libbb/xreadlink.c @@ -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; +} |