From 747162109fb1c891baf6aafa1ca0410bd08d04a4 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Thu, 24 May 2018 17:29:14 +0200 Subject: realpath,readlink -f: coreutils compat, closes 11021 function old new delta xmalloc_realpath_coreutils - 121 +121 Signed-off-by: Denys Vlasenko --- libbb/xreadlink.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'libbb/xreadlink.c') diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c index 9b62bcc43..6315033bb 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; +} -- cgit v1.2.3