diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-24 18:27:04 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2007-09-24 18:27:04 +0000 |
commit | 818322b9b19a452d66a07ca69256e2c092f5db5f (patch) | |
tree | 0b34390ac0cd61951bb9dc5b9fd3a226dae4f1ef | |
parent | a7ce207bd82882d6436d256a73c42ca4c8500ff3 (diff) | |
download | busybox-w32-818322b9b19a452d66a07ca69256e2c092f5db5f.tar.gz busybox-w32-818322b9b19a452d66a07ca69256e2c092f5db5f.tar.bz2 busybox-w32-818322b9b19a452d66a07ca69256e2c092f5db5f.zip |
*: kill bb_get_last_path_component, replace with two functions
(one which strips trailing slash and one which does not)
wget: straighten out as a result of above change
text data bss dec hex filename
5056 1 0 5057 13c1 busybox.t4/networking/wget.o
5022 0 0 5022 139e busybox.t5/networking/wget.o
-rw-r--r-- | applets/applets.c | 2 | ||||
-rw-r--r-- | coreutils/basename.c | 3 | ||||
-rw-r--r-- | coreutils/cp.c | 2 | ||||
-rw-r--r-- | coreutils/ln.c | 4 | ||||
-rw-r--r-- | coreutils/mv.c | 2 | ||||
-rw-r--r-- | coreutils/rm.c | 2 | ||||
-rw-r--r-- | include/libbb.h | 12 | ||||
-rw-r--r-- | init/init.c | 5 | ||||
-rw-r--r-- | libbb/get_last_path_component.c | 42 | ||||
-rw-r--r-- | libbb/run_shell.c | 2 | ||||
-rw-r--r-- | modutils/insmod.c | 2 | ||||
-rw-r--r-- | networking/wget.c | 45 | ||||
-rw-r--r-- | shell/lash.c | 5 |
13 files changed, 66 insertions, 62 deletions
diff --git a/applets/applets.c b/applets/applets.c index a1a399cd5..5b7b88a54 100644 --- a/applets/applets.c +++ b/applets/applets.c | |||
@@ -601,7 +601,7 @@ static int busybox_main(char **argv) | |||
601 | } | 601 | } |
602 | /* We support "busybox /a/path/to/applet args..." too. Allows for | 602 | /* We support "busybox /a/path/to/applet args..." too. Allows for |
603 | * "#!/bin/busybox"-style wrappers */ | 603 | * "#!/bin/busybox"-style wrappers */ |
604 | applet_name = bb_get_last_path_component(argv[0]); | 604 | applet_name = bb_get_last_path_component_nostrip(argv[0]); |
605 | run_applet_and_exit(applet_name, argv); | 605 | run_applet_and_exit(applet_name, argv); |
606 | bb_error_msg_and_die("applet not found"); | 606 | bb_error_msg_and_die("applet not found"); |
607 | } | 607 | } |
diff --git a/coreutils/basename.c b/coreutils/basename.c index f59d7a8de..ec1f85bef 100644 --- a/coreutils/basename.c +++ b/coreutils/basename.c | |||
@@ -34,7 +34,8 @@ int basename_main(int argc, char **argv) | |||
34 | bb_show_usage(); | 34 | bb_show_usage(); |
35 | } | 35 | } |
36 | 36 | ||
37 | s = bb_get_last_path_component(*++argv); | 37 | /* It should strip slash: /abc/def/ -> def */ |
38 | s = bb_get_last_path_component_strip(*++argv); | ||
38 | 39 | ||
39 | if (*++argv) { | 40 | if (*++argv) { |
40 | n = strlen(*argv); | 41 | n = strlen(*argv); |
diff --git a/coreutils/cp.c b/coreutils/cp.c index 76dc566b3..889e4604d 100644 --- a/coreutils/cp.c +++ b/coreutils/cp.c | |||
@@ -87,7 +87,7 @@ int cp_main(int argc, char **argv) | |||
87 | } | 87 | } |
88 | 88 | ||
89 | while (1) { | 89 | while (1) { |
90 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); | 90 | dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); |
91 | DO_COPY: | 91 | DO_COPY: |
92 | if (copy_file(*argv, dest, flags) < 0) { | 92 | if (copy_file(*argv, dest, flags) < 0) { |
93 | status = 1; | 93 | status = 1; |
diff --git a/coreutils/ln.c b/coreutils/ln.c index a6499039e..f3c67aa36 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c | |||
@@ -45,7 +45,7 @@ int ln_main(int argc, char **argv) | |||
45 | 45 | ||
46 | if (argc == optind + 1) { | 46 | if (argc == optind + 1) { |
47 | *--argv = last; | 47 | *--argv = last; |
48 | last = bb_get_last_path_component(xstrdup(last)); | 48 | last = bb_get_last_path_component_strip(xstrdup(last)); |
49 | } | 49 | } |
50 | 50 | ||
51 | do { | 51 | do { |
@@ -57,7 +57,7 @@ int ln_main(int argc, char **argv) | |||
57 | NULL) | 57 | NULL) |
58 | ) { | 58 | ) { |
59 | src_name = xstrdup(*argv); | 59 | src_name = xstrdup(*argv); |
60 | src = concat_path_file(src, bb_get_last_path_component(src_name)); | 60 | src = concat_path_file(src, bb_get_last_path_component_strip(src_name)); |
61 | free(src_name); | 61 | free(src_name); |
62 | src_name = src; | 62 | src_name = src; |
63 | } | 63 | } |
diff --git a/coreutils/mv.c b/coreutils/mv.c index 1d2977060..d13f4d54f 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c | |||
@@ -68,7 +68,7 @@ int mv_main(int argc, char **argv) | |||
68 | } | 68 | } |
69 | 69 | ||
70 | do { | 70 | do { |
71 | dest = concat_path_file(last, bb_get_last_path_component(*argv)); | 71 | dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); |
72 | dest_exists = cp_mv_stat(dest, &dest_stat); | 72 | dest_exists = cp_mv_stat(dest, &dest_stat); |
73 | if (dest_exists < 0) { | 73 | if (dest_exists < 0) { |
74 | goto RET_1; | 74 | goto RET_1; |
diff --git a/coreutils/rm.c b/coreutils/rm.c index ba37762a8..a686fc40c 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c | |||
@@ -38,7 +38,7 @@ int rm_main(int argc, char **argv) | |||
38 | 38 | ||
39 | if (*argv != NULL) { | 39 | if (*argv != NULL) { |
40 | do { | 40 | do { |
41 | const char *base = bb_get_last_path_component(*argv); | 41 | const char *base = bb_get_last_path_component_strip(*argv); |
42 | 42 | ||
43 | if (DOT_OR_DOTDOT(base)) { | 43 | if (DOT_OR_DOTDOT(base)) { |
44 | bb_error_msg("cannot remove '.' or '..'"); | 44 | bb_error_msg("cannot remove '.' or '..'"); |
diff --git a/include/libbb.h b/include/libbb.h index e5f03517f..25b2e4489 100644 --- a/include/libbb.h +++ b/include/libbb.h | |||
@@ -236,9 +236,15 @@ extern void bb_copyfd_exact_size(int fd1, int fd2, off_t size); | |||
236 | /* this helper yells "short read!" if param is not -1 */ | 236 | /* this helper yells "short read!" if param is not -1 */ |
237 | extern void complain_copyfd_and_die(off_t sz) ATTRIBUTE_NORETURN; | 237 | extern void complain_copyfd_and_die(off_t sz) ATTRIBUTE_NORETURN; |
238 | extern char bb_process_escape_sequence(const char **ptr); | 238 | extern char bb_process_escape_sequence(const char **ptr); |
239 | /* TODO: sometimes modifies its parameter, which | 239 | /* xxxx_strip version can modify its parameter: |
240 | * makes it rather inconvenient at times: */ | 240 | * "/" -> "/" |
241 | extern char *bb_get_last_path_component(char *path); | 241 | * "abc" -> "abc" |
242 | * "abc/def" -> "def" | ||
243 | * "abc/def/" -> "def" !! | ||
244 | */ | ||
245 | extern char *bb_get_last_path_component_strip(char *path); | ||
246 | /* "abc/def/" -> "" and it never modifies 'path' */ | ||
247 | extern char *bb_get_last_path_component_nostrip(const char *path); | ||
242 | 248 | ||
243 | int ndelay_on(int fd); | 249 | int ndelay_on(int fd); |
244 | int ndelay_off(int fd); | 250 | int ndelay_off(int fd); |
diff --git a/init/init.c b/init/init.c index a196ab3ed..543ec2ea8 100644 --- a/init/init.c +++ b/init/init.c | |||
@@ -436,10 +436,11 @@ static pid_t run(const struct init_action *a) | |||
436 | ++cmdpath; | 436 | ++cmdpath; |
437 | 437 | ||
438 | /* find the last component in the command pathname */ | 438 | /* find the last component in the command pathname */ |
439 | s = bb_get_last_path_component(cmdpath); | 439 | s = bb_get_last_path_component_nostrip(cmdpath); |
440 | 440 | ||
441 | /* make a new argv[0] */ | 441 | /* make a new argv[0] */ |
442 | if ((cmd[0] = malloc(strlen(s) + 2)) == NULL) { | 442 | cmd[0] = malloc(strlen(s) + 2); |
443 | if (cmd[0] == NULL) { | ||
443 | message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted); | 444 | message(L_LOG | L_CONSOLE, bb_msg_memory_exhausted); |
444 | cmd[0] = cmdpath; | 445 | cmd[0] = cmdpath; |
445 | } else { | 446 | } else { |
diff --git a/libbb/get_last_path_component.c b/libbb/get_last_path_component.c index b7bc0e626..0f602157d 100644 --- a/libbb/get_last_path_component.c +++ b/libbb/get_last_path_component.c | |||
@@ -8,25 +8,35 @@ | |||
8 | */ | 8 | */ |
9 | 9 | ||
10 | #include "libbb.h" | 10 | #include "libbb.h" |
11 | 11 | /* | |
12 | char *bb_get_last_path_component(char *path) | 12 | * "/" -> "/" |
13 | * "abc" -> "abc" | ||
14 | * "abc/def" -> "def" | ||
15 | * "abc/def/" -> "" | ||
16 | */ | ||
17 | char *bb_get_last_path_component_nostrip(const char *path) | ||
13 | { | 18 | { |
14 | char *first = path; | 19 | char *slash = strrchr(path, '/'); |
15 | char *last; | 20 | |
21 | if (!slash || (slash == path && !slash[1])) | ||
22 | return (char*)path; | ||
16 | 23 | ||
17 | last = path - 1; | 24 | return slash + 1; |
25 | } | ||
18 | 26 | ||
19 | while (*path) { | 27 | /* |
20 | if ((*path != '/') && (path > ++last)) { | 28 | * "/" -> "/" |
21 | last = first = path; | 29 | * "abc" -> "abc" |
22 | } | 30 | * "abc/def" -> "def" |
23 | ++path; | 31 | * "abc/def/" -> "def" !! |
24 | } | 32 | */ |
33 | char *bb_get_last_path_component_strip(char *path) | ||
34 | { | ||
35 | char *slash = last_char_is(path, '/'); | ||
25 | 36 | ||
26 | if (*first == '/') { | 37 | if (slash) |
27 | last = first; | 38 | while (*slash == '/' && slash != path) |
28 | } | 39 | *slash-- = '\0'; |
29 | last[1] = '\0'; | ||
30 | 40 | ||
31 | return first; | 41 | return bb_get_last_path_component_nostrip(path); |
32 | } | 42 | } |
diff --git a/libbb/run_shell.c b/libbb/run_shell.c index b2b4216f4..239887d85 100644 --- a/libbb/run_shell.c +++ b/libbb/run_shell.c | |||
@@ -67,7 +67,7 @@ void run_shell(const char *shell, int loginshell, const char *command, const cha | |||
67 | 67 | ||
68 | args = xmalloc(sizeof(char*) * (4 + additional_args_cnt)); | 68 | args = xmalloc(sizeof(char*) * (4 + additional_args_cnt)); |
69 | 69 | ||
70 | args[0] = bb_get_last_path_component(xstrdup(shell)); | 70 | args[0] = bb_get_last_path_component_nostrip(xstrdup(shell)); |
71 | 71 | ||
72 | if (loginshell) | 72 | if (loginshell) |
73 | args[0] = xasprintf("-%s", args[0]); | 73 | args[0] = xasprintf("-%s", args[0]); |
diff --git a/modutils/insmod.c b/modutils/insmod.c index 503367298..0baf5d7f2 100644 --- a/modutils/insmod.c +++ b/modutils/insmod.c | |||
@@ -806,7 +806,7 @@ static int check_module_name_match(const char *filename, struct stat *statbuf, | |||
806 | return FALSE; | 806 | return FALSE; |
807 | else { | 807 | else { |
808 | char *tmp, *tmp1 = xstrdup(filename); | 808 | char *tmp, *tmp1 = xstrdup(filename); |
809 | tmp = bb_get_last_path_component(tmp1); | 809 | tmp = bb_get_last_path_component_nostrip(tmp1); |
810 | if (strcmp(tmp, fullname) == 0) { | 810 | if (strcmp(tmp, fullname) == 0) { |
811 | free(tmp1); | 811 | free(tmp1); |
812 | /* Stop searching if we find a match */ | 812 | /* Stop searching if we find a match */ |
diff --git a/networking/wget.c b/networking/wget.c index 5feb539d5..86d6f00ec 100644 --- a/networking/wget.c +++ b/networking/wget.c | |||
@@ -12,11 +12,11 @@ | |||
12 | struct host_info { | 12 | struct host_info { |
13 | // May be used if we ever will want to free() all xstrdup()s... | 13 | // May be used if we ever will want to free() all xstrdup()s... |
14 | /* char *allocated; */ | 14 | /* char *allocated; */ |
15 | char *path; | 15 | const char *path; |
16 | char *user; | 16 | const char *user; |
17 | char *host; | 17 | char *host; |
18 | int port; | 18 | int port; |
19 | smallint is_ftp; | 19 | smallint is_ftp; |
20 | }; | 20 | }; |
21 | 21 | ||
22 | 22 | ||
@@ -318,9 +318,7 @@ static void parse_url(char *src_url, struct host_info *h) | |||
318 | p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p; | 318 | p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p; |
319 | p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p; | 319 | p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p; |
320 | if (!sp) { | 320 | if (!sp) { |
321 | /* must be writable because of bb_get_last_path_component() */ | 321 | h->path = ""; |
322 | static char nullstr[] ALIGN1 = ""; | ||
323 | h->path = nullstr; | ||
324 | } else if (*sp == '/') { | 322 | } else if (*sp == '/') { |
325 | *sp = '\0'; | 323 | *sp = '\0'; |
326 | h->path = sp + 1; | 324 | h->path = sp + 1; |
@@ -328,7 +326,7 @@ static void parse_url(char *src_url, struct host_info *h) | |||
328 | // http://busybox.net?login=john@doe is a valid URL | 326 | // http://busybox.net?login=john@doe is a valid URL |
329 | // memmove converts to: | 327 | // memmove converts to: |
330 | // http:/busybox.nett?login=john@doe... | 328 | // http:/busybox.nett?login=john@doe... |
331 | memmove(h->host-1, h->host, sp - h->host); | 329 | memmove(h->host - 1, h->host, sp - h->host); |
332 | h->host--; | 330 | h->host--; |
333 | sp[-1] = '\0'; | 331 | sp[-1] = '\0'; |
334 | h->path = sp; | 332 | h->path = sp; |
@@ -497,31 +495,20 @@ int wget_main(int argc, char **argv) | |||
497 | } | 495 | } |
498 | } | 496 | } |
499 | 497 | ||
500 | /* Guess an output filename */ | 498 | /* Guess an output filename, if there was no -O FILE */ |
501 | if (!fname_out) { | 499 | if (!fname_out) { |
502 | // Dirty hack. Needed because bb_get_last_path_component | 500 | fname_out = bb_get_last_path_component_nostrip(target.path); |
503 | // will destroy trailing / by storing '\0' in last byte! | 501 | /* handle "wget http://kernel.org//" */ |
504 | if (!last_char_is(target.path, '/')) { | 502 | if (fname_out[0] == '/' || !fname_out[0]) |
505 | fname_out = bb_get_last_path_component(target.path); | ||
506 | #if ENABLE_FEATURE_WGET_STATUSBAR | ||
507 | curfile = fname_out; | ||
508 | #endif | ||
509 | } | ||
510 | if (!fname_out || !fname_out[0]) { | ||
511 | /* bb_get_last_path_component writes | ||
512 | * to last '/' only. We don't have one here... */ | ||
513 | fname_out = (char*)"index.html"; | 503 | fname_out = (char*)"index.html"; |
514 | #if ENABLE_FEATURE_WGET_STATUSBAR | 504 | /* -P DIR is considered only if there was no -O FILE */ |
515 | curfile = fname_out; | 505 | if (dir_prefix) |
516 | #endif | ||
517 | } | ||
518 | if (dir_prefix != NULL) | ||
519 | fname_out = concat_path_file(dir_prefix, fname_out); | 506 | fname_out = concat_path_file(dir_prefix, fname_out); |
507 | } | ||
520 | #if ENABLE_FEATURE_WGET_STATUSBAR | 508 | #if ENABLE_FEATURE_WGET_STATUSBAR |
521 | } else { | 509 | curfile = bb_get_last_path_component_nostrip(fname_out); |
522 | curfile = bb_get_last_path_component(fname_out); | ||
523 | #endif | 510 | #endif |
524 | } | 511 | |
525 | /* Impossible? | 512 | /* Impossible? |
526 | if ((opt & WGET_OPT_CONTINUE) && !fname_out) | 513 | if ((opt & WGET_OPT_CONTINUE) && !fname_out) |
527 | bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */ | 514 | bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */ |
diff --git a/shell/lash.c b/shell/lash.c index d4dba8e63..889fe49d8 100644 --- a/shell/lash.c +++ b/shell/lash.c | |||
@@ -1141,12 +1141,11 @@ static int pseudo_exec(struct child_prog *child) | |||
1141 | } | 1141 | } |
1142 | } | 1142 | } |
1143 | 1143 | ||
1144 | |||
1145 | /* Check if the command matches any busybox internal | 1144 | /* Check if the command matches any busybox internal |
1146 | * commands ("applets") here. Following discussions from | 1145 | * commands ("applets") here. Following discussions from |
1147 | * November 2000 on busybox@busybox.net, don't use | 1146 | * November 2000 on busybox@busybox.net, don't use |
1148 | * bb_get_last_path_component(). This way explicit (with | 1147 | * bb_get_last_path_component_nostrip(). This way explicit |
1149 | * slashes) filenames will never be interpreted as an | 1148 | * (with slashes) filenames will never be interpreted as an |
1150 | * applet, just like with builtins. This way the user can | 1149 | * applet, just like with builtins. This way the user can |
1151 | * override an applet with an explicit filename reference. | 1150 | * override an applet with an explicit filename reference. |
1152 | * The only downside to this change is that an explicit | 1151 | * The only downside to this change is that an explicit |