diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-05-10 09:00:31 +0400 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-05-10 09:00:31 +0400 |
commit | ac130e1dca289c431c43b6efee4b3d9f2b367c87 (patch) | |
tree | 380b189440ddc169cd4d9435147d8991da08aab2 /coreutils/basename.c | |
parent | 0a027e6880762bfe24ffda94e5872710820ecc9d (diff) |
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 @@ extern int basename_main(int argc, char **argv) { - char* s, *s1; + int m, n; + char *s, *s1; if ((argc < 2) || (**(argv + 1) == '-')) { - usage("basename [FILE ...]\n" + usage("basename FILE [SUFFIX]\n" #ifndef BB_FEATURE_TRIVIAL_HELP - "\nStrips directory path and suffixes from FILE(s).\n" + "\nStrips directory path and suffixes from FILE.\n" + "If specified, also removes any trailing SUFFIX.\n" #endif ); } @@ -40,10 +42,20 @@ extern int basename_main(int argc, char **argv) s1=*argv+strlen(*argv)-1; while (s1 && *s1 == '/') { *s1 = '\0'; - s1=*argv+strlen(*argv)-1; + s1--; } s = strrchr(*argv, '/'); - printf("%s\n", (s)? s + 1 : *argv); + if (s==NULL) s=*argv; + else s++; + + if (argc>2) { + argv++; + n = strlen(*argv); + m = strlen(s); + if (m>=n && strncmp(s+m-n, *argv, n)==0) + s[m-n] = '\0'; + } + printf("%s\n", s); exit(TRUE); } |