diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-05-10 05:00:31 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-05-10 05:00:31 +0000 |
commit | ac130e1dca289c431c43b6efee4b3d9f2b367c87 (patch) | |
tree | 380b189440ddc169cd4d9435147d8991da08aab2 | |
parent | 0a027e6880762bfe24ffda94e5872710820ecc9d (diff) | |
download | busybox-w32-ac130e1dca289c431c43b6efee4b3d9f2b367c87.tar.gz busybox-w32-ac130e1dca289c431c43b6efee4b3d9f2b367c87.tar.bz2 busybox-w32-ac130e1dca289c431c43b6efee4b3d9f2b367c87.zip |
Add suffix stripping support to basename
-Erik
-rw-r--r-- | Changelog | 12 | ||||
-rw-r--r-- | basename.c | 22 | ||||
-rw-r--r-- | coreutils/basename.c | 22 | ||||
-rw-r--r-- | docs/busybox.pod | 9 |
4 files changed, 47 insertions, 18 deletions
@@ -15,15 +15,17 @@ | |||
15 | does force anyway | 15 | does force anyway |
16 | * tail can now accept -<num> commands (e.g. -10) for better | 16 | * tail can now accept -<num> commands (e.g. -10) for better |
17 | compatibility with the standard tail command | 17 | compatibility with the standard tail command |
18 | * added a simple id implementation; doesn't support supp. groups yet | 18 | * added a simple id implementation; doesn't support sup. groups yet |
19 | * logname used getlogin(3) which uses utmp under the hood. Now it behaves. | 19 | * logname used getlogin(3) which uses utmp. Now it doesn't. |
20 | * whoami used getpwuid(3) which uses libc NSS. Now it behaves. | 20 | * whoami used getpwuid(3) which uses libc NSS. Now it behaves. |
21 | * Due to the license change, I can now use minix code. Minux tr replaces | 21 | * Due to the license change, I can now use minix code. Minux tr |
22 | the BSD derived tr, saving 4k and eliminating bsearch(3) from the | 22 | replaces the BSD derived tr, saving 4k and eliminating bsearch(3) |
23 | list of used Libc symbols. | 23 | from the list of used Libc symbols. |
24 | * Add support for "noatime" and "nodiratime" mount flags to mount. | 24 | * Add support for "noatime" and "nodiratime" mount flags to mount. |
25 | * Changed 'umount -f' to mean force, and actually use umount2. | 25 | * Changed 'umount -f' to mean force, and actually use umount2. |
26 | * Changed 'umount -l' to mean "Do not free loop device". | 26 | * Changed 'umount -l' to mean "Do not free loop device". |
27 | * Fixed basename to support stripping of suffixes. Patch thanks | ||
28 | to xiong jianxin <jxiong@uiuc.edu> | ||
27 | * More doc updates | 29 | * More doc updates |
28 | 30 | ||
29 | -Erik | 31 | -Erik |
diff --git a/basename.c b/basename.c index efd07e272..10ae76188 100644 --- a/basename.c +++ b/basename.c | |||
@@ -26,12 +26,14 @@ | |||
26 | 26 | ||
27 | extern int basename_main(int argc, char **argv) | 27 | extern int basename_main(int argc, char **argv) |
28 | { | 28 | { |
29 | char* s, *s1; | 29 | int m, n; |
30 | char *s, *s1; | ||
30 | 31 | ||
31 | if ((argc < 2) || (**(argv + 1) == '-')) { | 32 | if ((argc < 2) || (**(argv + 1) == '-')) { |
32 | usage("basename [FILE ...]\n" | 33 | usage("basename FILE [SUFFIX]\n" |
33 | #ifndef BB_FEATURE_TRIVIAL_HELP | 34 | #ifndef BB_FEATURE_TRIVIAL_HELP |
34 | "\nStrips directory path and suffixes from FILE(s).\n" | 35 | "\nStrips directory path and suffixes from FILE.\n" |
36 | "If specified, also removes any trailing SUFFIX.\n" | ||
35 | #endif | 37 | #endif |
36 | ); | 38 | ); |
37 | } | 39 | } |
@@ -40,10 +42,20 @@ extern int basename_main(int argc, char **argv) | |||
40 | s1=*argv+strlen(*argv)-1; | 42 | s1=*argv+strlen(*argv)-1; |
41 | while (s1 && *s1 == '/') { | 43 | while (s1 && *s1 == '/') { |
42 | *s1 = '\0'; | 44 | *s1 = '\0'; |
43 | s1=*argv+strlen(*argv)-1; | 45 | s1--; |
44 | } | 46 | } |
45 | s = strrchr(*argv, '/'); | 47 | s = strrchr(*argv, '/'); |
46 | printf("%s\n", (s)? s + 1 : *argv); | 48 | if (s==NULL) s=*argv; |
49 | else s++; | ||
50 | |||
51 | if (argc>2) { | ||
52 | argv++; | ||
53 | n = strlen(*argv); | ||
54 | m = strlen(s); | ||
55 | if (m>=n && strncmp(s+m-n, *argv, n)==0) | ||
56 | s[m-n] = '\0'; | ||
57 | } | ||
58 | printf("%s\n", s); | ||
47 | exit(TRUE); | 59 | exit(TRUE); |
48 | } | 60 | } |
49 | 61 | ||
diff --git a/coreutils/basename.c b/coreutils/basename.c index efd07e272..10ae76188 100644 --- a/coreutils/basename.c +++ b/coreutils/basename.c | |||
@@ -26,12 +26,14 @@ | |||
26 | 26 | ||
27 | extern int basename_main(int argc, char **argv) | 27 | extern int basename_main(int argc, char **argv) |
28 | { | 28 | { |
29 | char* s, *s1; | 29 | int m, n; |
30 | char *s, *s1; | ||
30 | 31 | ||
31 | if ((argc < 2) || (**(argv + 1) == '-')) { | 32 | if ((argc < 2) || (**(argv + 1) == '-')) { |
32 | usage("basename [FILE ...]\n" | 33 | usage("basename FILE [SUFFIX]\n" |
33 | #ifndef BB_FEATURE_TRIVIAL_HELP | 34 | #ifndef BB_FEATURE_TRIVIAL_HELP |
34 | "\nStrips directory path and suffixes from FILE(s).\n" | 35 | "\nStrips directory path and suffixes from FILE.\n" |
36 | "If specified, also removes any trailing SUFFIX.\n" | ||
35 | #endif | 37 | #endif |
36 | ); | 38 | ); |
37 | } | 39 | } |
@@ -40,10 +42,20 @@ extern int basename_main(int argc, char **argv) | |||
40 | s1=*argv+strlen(*argv)-1; | 42 | s1=*argv+strlen(*argv)-1; |
41 | while (s1 && *s1 == '/') { | 43 | while (s1 && *s1 == '/') { |
42 | *s1 = '\0'; | 44 | *s1 = '\0'; |
43 | s1=*argv+strlen(*argv)-1; | 45 | s1--; |
44 | } | 46 | } |
45 | s = strrchr(*argv, '/'); | 47 | s = strrchr(*argv, '/'); |
46 | printf("%s\n", (s)? s + 1 : *argv); | 48 | if (s==NULL) s=*argv; |
49 | else s++; | ||
50 | |||
51 | if (argc>2) { | ||
52 | argv++; | ||
53 | n = strlen(*argv); | ||
54 | m = strlen(s); | ||
55 | if (m>=n && strncmp(s+m-n, *argv, n)==0) | ||
56 | s[m-n] = '\0'; | ||
57 | } | ||
58 | printf("%s\n", s); | ||
47 | exit(TRUE); | 59 | exit(TRUE); |
48 | } | 60 | } |
49 | 61 | ||
diff --git a/docs/busybox.pod b/docs/busybox.pod index 6a18a0499..ea14459ef 100644 --- a/docs/busybox.pod +++ b/docs/busybox.pod | |||
@@ -71,9 +71,10 @@ uname, uniq, update, uptime, usleep, wc, whoami, yes, zcat, [ | |||
71 | 71 | ||
72 | =item basename | 72 | =item basename |
73 | 73 | ||
74 | Usage: basename [file ...] | 74 | Usage: basename FILE [SUFFIX] |
75 | 75 | ||
76 | Strips directory path and suffixes from FILE(s). | 76 | Strips directory path and suffixes from FILE. |
77 | If specified, also removes any trailing SUFFIX. | ||
77 | 78 | ||
78 | Example: | 79 | Example: |
79 | 80 | ||
@@ -81,6 +82,8 @@ Example: | |||
81 | foo | 82 | foo |
82 | $ basename /usr/local/bin/ | 83 | $ basename /usr/local/bin/ |
83 | bin | 84 | bin |
85 | $ basename /foo/bar.txt .txt | ||
86 | bar | ||
84 | 87 | ||
85 | ------------------------------- | 88 | ------------------------------- |
86 | 89 | ||
@@ -1878,4 +1881,4 @@ Enrique Zanardi <ezanardi@ull.es> | |||
1878 | 1881 | ||
1879 | =cut | 1882 | =cut |
1880 | 1883 | ||
1881 | # $Id: busybox.pod,v 1.28 2000/05/05 19:49:33 erik Exp $ | 1884 | # $Id: busybox.pod,v 1.29 2000/05/10 05:00:31 erik Exp $ |