Add suffix stripping support to basename
authorErik Andersen <andersen@codepoet.org>
Wed, 10 May 2000 05:00:31 +0000 (05:00 -0000)
committerErik Andersen <andersen@codepoet.org>
Wed, 10 May 2000 05:00:31 +0000 (05:00 -0000)
 -Erik

Changelog
basename.c
coreutils/basename.c
docs/busybox.pod

index 78255f9732d113369992afffe753f930f0fde804..8b683ba90cc22bcbceac6e9ae821194e01a85e16 100644 (file)
--- a/Changelog
+++ b/Changelog
                does force anyway
            * tail can now accept -<num> commands (e.g. -10) for better 
                compatibility with the standard tail command
-           * added a simple id implementation; doesn't support supp. groups yet
-       * logname used getlogin(3) which uses utmp under the hood.  Now it behaves
+           * added a simple id implementation; doesn't support sup. groups yet
+       * logname used getlogin(3) which uses utmp.  Now it doesn't
        * 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
-           the BSD derived tr, saving 4k and eliminating bsearch(3) from the
-           list of used Libc symbols.
+       * Due to the license change, I can now use minix code.  Minux tr 
+           replaces the BSD derived tr, saving 4k and eliminating bsearch(3) 
+           from the list of used Libc symbols.
        * Add support for "noatime" and "nodiratime" mount flags to mount.
        * Changed 'umount -f' to mean force, and actually use umount2.
        * 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
 
         -Erik
index efd07e27207f5231bd6b2adfc8c28d46712301f4..10ae761889ec809689a8c24af17dc5598e5fd8b9 100644 (file)
 
 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);
 }
 
index efd07e27207f5231bd6b2adfc8c28d46712301f4..10ae761889ec809689a8c24af17dc5598e5fd8b9 100644 (file)
 
 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);
 }
 
index 6a18a0499329a15636fdaec39870074c45a931de..ea14459ef2ef40788961f9e23cdfc886440eb891 100644 (file)
@@ -71,9 +71,10 @@ uname, uniq, update, uptime, usleep, wc, whoami, yes, zcat, [
 
 =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: 
 
@@ -81,6 +82,8 @@ Example:
        foo
        $ basename /usr/local/bin/
        bin
+       $ basename /foo/bar.txt .txt
+       bar
 
 -------------------------------
 
@@ -1878,4 +1881,4 @@ Enrique Zanardi <ezanardi@ull.es>
 
 =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 $