diff options
-rw-r--r-- | coreutils/readlink.c | 2 | ||||
-rw-r--r-- | coreutils/realpath.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 1 | ||||
-rw-r--r-- | libbb/xreadlink.c | 30 |
4 files changed, 33 insertions, 2 deletions
diff --git a/coreutils/readlink.c b/coreutils/readlink.c index b8e327d11..49361cea0 100644 --- a/coreutils/readlink.c +++ b/coreutils/readlink.c | |||
@@ -86,7 +86,7 @@ int readlink_main(int argc UNUSED_PARAM, char **argv) | |||
86 | 86 | ||
87 | /* NOFORK: only one alloc is allowed; must free */ | 87 | /* NOFORK: only one alloc is allowed; must free */ |
88 | if (opt & 1) { /* -f */ | 88 | if (opt & 1) { /* -f */ |
89 | buf = xmalloc_realpath(fname); | 89 | buf = xmalloc_realpath_coreutils(fname); |
90 | } else { | 90 | } else { |
91 | buf = xmalloc_readlink_or_warn(fname); | 91 | buf = xmalloc_readlink_or_warn(fname); |
92 | } | 92 | } |
diff --git a/coreutils/realpath.c b/coreutils/realpath.c index aa878fcd2..43923681c 100644 --- a/coreutils/realpath.c +++ b/coreutils/realpath.c | |||
@@ -38,7 +38,7 @@ int realpath_main(int argc UNUSED_PARAM, char **argv) | |||
38 | 38 | ||
39 | do { | 39 | do { |
40 | /* NOFORK: only one alloc is allowed; must free */ | 40 | /* NOFORK: only one alloc is allowed; must free */ |
41 | char *resolved_path = xmalloc_realpath(*argv); | 41 | char *resolved_path = xmalloc_realpath_coreutils(*argv); |
42 | if (resolved_path != NULL) { | 42 | if (resolved_path != NULL) { |
43 | puts(resolved_path); | 43 | puts(resolved_path); |
44 | free(resolved_path); | 44 | free(resolved_path); |
diff --git a/include/libbb.h b/include/libbb.h index a605c7f03..d4ba031df 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -485,6 +485,7 @@ DIR *xopendir(const char *path) FAST_FUNC; | |||
485 | DIR *warn_opendir(const char *path) FAST_FUNC; | 485 | DIR *warn_opendir(const char *path) FAST_FUNC; |
486 | 486 | ||
487 | char *xmalloc_realpath(const char *path) FAST_FUNC RETURNS_MALLOC; | 487 | char *xmalloc_realpath(const char *path) FAST_FUNC RETURNS_MALLOC; |
488 | char *xmalloc_realpath_coreutils(const char *path) FAST_FUNC RETURNS_MALLOC; | ||
488 | char *xmalloc_readlink(const char *path) FAST_FUNC RETURNS_MALLOC; | 489 | char *xmalloc_readlink(const char *path) FAST_FUNC RETURNS_MALLOC; |
489 | char *xmalloc_readlink_or_warn(const char *path) FAST_FUNC RETURNS_MALLOC; | 490 | char *xmalloc_readlink_or_warn(const char *path) FAST_FUNC RETURNS_MALLOC; |
490 | /* !RETURNS_MALLOC: it's a realloc-like function */ | 491 | /* !RETURNS_MALLOC: it's a realloc-like function */ |
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 | } | ||