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 /coreutils/basename.c | |
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
Diffstat (limited to 'coreutils/basename.c')
-rw-r--r-- | coreutils/basename.c | 22 |
1 files changed, 17 insertions, 5 deletions
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 | ||