diff options
Diffstat (limited to 'libbb')
-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 9b62bcc43..6315033bb 100644 --- a/libbb/xreadlink.c +++ b/libbb/xreadlink.c | |||
@@ -122,3 +122,33 @@ char* FAST_FUNC xmalloc_realpath(const char *path) | |||
122 | return xstrdup(realpath(path, buf)); | 122 | return xstrdup(realpath(path, buf)); |
123 | #endif | 123 | #endif |
124 | } | 124 | } |
125 | |||
126 | char* FAST_FUNC xmalloc_realpath_coreutils(const char *path) | ||
127 | { | ||
128 | char *buf; | ||
129 | |||
130 | errno = 0; | ||
131 | buf = xmalloc_realpath(path); | ||
132 | /* | ||
133 | * There is one case when "readlink -f" and | ||
134 | * "realpath" from coreutils succeed, | ||
135 | * even though file does not exist, such as: | ||
136 | * /tmp/file_does_not_exist | ||
137 | * (the directory must exist). | ||
138 | */ | ||
139 | if (!buf && errno == ENOENT) { | ||
140 | char *last_slash = strrchr(path, '/'); | ||
141 | if (last_slash) { | ||
142 | *last_slash++ = '\0'; | ||
143 | buf = xmalloc_realpath(path); | ||
144 | if (buf) { | ||
145 | unsigned len = strlen(buf); | ||
146 | buf = xrealloc(buf, len + strlen(last_slash) + 2); | ||
147 | buf[len++] = '/'; | ||
148 | strcpy(buf + len, last_slash); | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | return buf; | ||
154 | } | ||