Add suffix stripping support to basename

-Erik
This commit is contained in:
Erik Andersen 2000-05-10 05:00:31 +00:00
parent 0a027e6880
commit ac130e1dca
4 changed files with 47 additions and 18 deletions

View File

@ -15,15 +15,17 @@
does force anyway does force anyway
* tail can now accept -<num> commands (e.g. -10) for better * tail can now accept -<num> commands (e.g. -10) for better
compatibility with the standard tail command compatibility with the standard tail command
* added a simple id implementation; doesn't support supp. groups yet * added a simple id implementation; doesn't support sup. groups yet
* logname used getlogin(3) which uses utmp under the hood. Now it behaves. * logname used getlogin(3) which uses utmp. Now it doesn't.
* whoami used getpwuid(3) which uses libc NSS. Now it behaves. * whoami used getpwuid(3) which uses libc NSS. Now it behaves.
* Due to the license change, I can now use minix code. Minux tr replaces * Due to the license change, I can now use minix code. Minux tr
the BSD derived tr, saving 4k and eliminating bsearch(3) from the replaces the BSD derived tr, saving 4k and eliminating bsearch(3)
list of used Libc symbols. from the list of used Libc symbols.
* Add support for "noatime" and "nodiratime" mount flags to mount. * Add support for "noatime" and "nodiratime" mount flags to mount.
* Changed 'umount -f' to mean force, and actually use umount2. * Changed 'umount -f' to mean force, and actually use umount2.
* Changed 'umount -l' to mean "Do not free loop device". * Changed 'umount -l' to mean "Do not free loop device".
* Fixed basename to support stripping of suffixes. Patch thanks
to xiong jianxin <jxiong@uiuc.edu>
* More doc updates * More doc updates
-Erik -Erik

View File

@ -26,12 +26,14 @@
extern int basename_main(int argc, char **argv) extern int basename_main(int argc, char **argv)
{ {
char* s, *s1; int m, n;
char *s, *s1;
if ((argc < 2) || (**(argv + 1) == '-')) { if ((argc < 2) || (**(argv + 1) == '-')) {
usage("basename [FILE ...]\n" usage("basename FILE [SUFFIX]\n"
#ifndef BB_FEATURE_TRIVIAL_HELP #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 #endif
); );
} }
@ -40,10 +42,20 @@ extern int basename_main(int argc, char **argv)
s1=*argv+strlen(*argv)-1; s1=*argv+strlen(*argv)-1;
while (s1 && *s1 == '/') { while (s1 && *s1 == '/') {
*s1 = '\0'; *s1 = '\0';
s1=*argv+strlen(*argv)-1; s1--;
} }
s = strrchr(*argv, '/'); 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); exit(TRUE);
} }

View File

@ -26,12 +26,14 @@
extern int basename_main(int argc, char **argv) extern int basename_main(int argc, char **argv)
{ {
char* s, *s1; int m, n;
char *s, *s1;
if ((argc < 2) || (**(argv + 1) == '-')) { if ((argc < 2) || (**(argv + 1) == '-')) {
usage("basename [FILE ...]\n" usage("basename FILE [SUFFIX]\n"
#ifndef BB_FEATURE_TRIVIAL_HELP #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 #endif
); );
} }
@ -40,10 +42,20 @@ extern int basename_main(int argc, char **argv)
s1=*argv+strlen(*argv)-1; s1=*argv+strlen(*argv)-1;
while (s1 && *s1 == '/') { while (s1 && *s1 == '/') {
*s1 = '\0'; *s1 = '\0';
s1=*argv+strlen(*argv)-1; s1--;
} }
s = strrchr(*argv, '/'); 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); exit(TRUE);
} }

View File

@ -71,9 +71,10 @@ uname, uniq, update, uptime, usleep, wc, whoami, yes, zcat, [
=item basename =item basename
Usage: basename [file ...] Usage: basename FILE [SUFFIX]
Strips directory path and suffixes from FILE(s). Strips directory path and suffixes from FILE.
If specified, also removes any trailing SUFFIX.
Example: Example:
@ -81,6 +82,8 @@ Example:
foo foo
$ basename /usr/local/bin/ $ basename /usr/local/bin/
bin bin
$ basename /foo/bar.txt .txt
bar
------------------------------- -------------------------------
@ -1878,4 +1881,4 @@ Enrique Zanardi <ezanardi@ull.es>
=cut =cut
# $Id: busybox.pod,v 1.28 2000/05/05 19:49:33 erik Exp $ # $Id: busybox.pod,v 1.29 2000/05/10 05:00:31 erik Exp $