aboutsummaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/get_last_path_component.c23
-rw-r--r--libbb/xreadlink.c34
2 files changed, 50 insertions, 7 deletions
diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c
index 06033e139..254aabafd 100644
--- a/libbb/get_last_path_component.c
+++ b/libbb/get_last_path_component.c
@@ -23,6 +23,21 @@ const char* FAST_FUNC bb_basename(const char *name)
23 return name; 23 return name;
24} 24}
25 25
26#if ENABLE_PLATFORM_MINGW32
27char *get_last_slash(const char *path)
28{
29 const char *start = path + root_len(path);
30 char *slash = strrchr(start, '/');
31 char *bslash = strrchr(start, '\\');
32
33 if (slash && bslash)
34 slash = MAX(slash, bslash);
35 else if (!slash)
36 slash = bslash;
37 return slash;
38}
39#endif
40
26/* 41/*
27 * "/" -> "/" 42 * "/" -> "/"
28 * "abc" -> "abc" 43 * "abc" -> "abc"
@@ -33,13 +48,7 @@ char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path)
33{ 48{
34#if ENABLE_PLATFORM_MINGW32 49#if ENABLE_PLATFORM_MINGW32
35 const char *start = path + root_len(path); 50 const char *start = path + root_len(path);
36 char *slash = strrchr(start, '/'); 51 char *slash = get_last_slash(path);
37 char *bslash = strrchr(start, '\\');
38
39 if (slash && bslash)
40 slash = MAX(slash, bslash);
41 else if (!slash)
42 slash = bslash;
43 52
44 if (!slash && has_dos_drive_prefix(path) && path[2] != '\0') 53 if (!slash && has_dos_drive_prefix(path) && path[2] != '\0')
45 return (char *)path + 2; 54 return (char *)path + 2;
diff --git a/libbb/xreadlink.c b/libbb/xreadlink.c
index 024ee9047..e6cf90310 100644
--- a/libbb/xreadlink.c
+++ b/libbb/xreadlink.c
@@ -156,7 +156,11 @@ char* FAST_FUNC xmalloc_realpath_coreutils(char *path)
156 * $ realpath symlink 156 * $ realpath symlink
157 * /usr/bin/qwe 157 * /usr/bin/qwe
158 */ 158 */
159#if ENABLE_PLATFORM_MINGW32
160 if (is_relative_path(target)) {
161#else
159 if (target[0] != '/') { 162 if (target[0] != '/') {
163#endif
160 /* 164 /*
161 * $ ln -s target_does_not_exist symlink 165 * $ ln -s target_does_not_exist symlink
162 * $ readlink -f symlink 166 * $ readlink -f symlink
@@ -175,6 +179,35 @@ char* FAST_FUNC xmalloc_realpath_coreutils(char *path)
175 return buf; 179 return buf;
176 } 180 }
177 181
182#if ENABLE_PLATFORM_MINGW32
183 /* ignore leading and trailing slashes */
184 /* but keep leading slashes of UNC path */
185 if (!is_unc_path(path)) {
186 while (is_dir_sep(path[0]) && is_dir_sep(path[1]))
187 ++path;
188 }
189 i = strlen(path) - 1;
190 while (i > 0 && is_dir_sep(path[i]))
191 i--;
192 c = path[i + 1];
193 path[i + 1] = '\0';
194
195 last_slash = get_last_slash(path);
196 if (last_slash == path + root_len(path))
197 buf = xstrdup(path);
198 else if (last_slash) {
199 char c2 = *last_slash;
200 *last_slash = '\0';
201 buf = xmalloc_realpath(path);
202 *last_slash++ = c2;
203 if (buf) {
204 unsigned len = strlen(buf);
205 buf = xrealloc(buf, len + strlen(last_slash) + 2);
206 buf[len++] = c2;
207 strcpy(buf + len, last_slash);
208 }
209 }
210#else
178 /* ignore leading and trailing slashes */ 211 /* ignore leading and trailing slashes */
179 while (path[0] == '/' && path[1] == '/') 212 while (path[0] == '/' && path[1] == '/')
180 ++path; 213 ++path;
@@ -198,6 +231,7 @@ char* FAST_FUNC xmalloc_realpath_coreutils(char *path)
198 strcpy(buf + len, last_slash); 231 strcpy(buf + len, last_slash);
199 } 232 }
200 } 233 }
234#endif
201 path[i + 1] = c; 235 path[i + 1] = c;
202 } 236 }
203 237