aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorErik Andersen <andersen@codepoet.org>2000-05-10 05:00:31 +0000
committerErik Andersen <andersen@codepoet.org>2000-05-10 05:00:31 +0000
commitac130e1dca289c431c43b6efee4b3d9f2b367c87 (patch)
tree380b189440ddc169cd4d9435147d8991da08aab2
parent0a027e6880762bfe24ffda94e5872710820ecc9d (diff)
downloadbusybox-ac130e1dca289c431c43b6efee4b3d9f2b367c87.tar.gz
Add suffix stripping support to basename
-Erik
-rw-r--r--Changelog12
-rw-r--r--basename.c22
-rw-r--r--coreutils/basename.c22
-rw-r--r--docs/busybox.pod9
4 files changed, 47 insertions, 18 deletions
diff --git a/Changelog b/Changelog
index 78255f973..8b683ba90 100644
--- a/Changelog
+++ b/Changelog
@@ -15,15 +15,17 @@
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
diff --git a/basename.c b/basename.c
index efd07e272..10ae76188 100644
--- a/basename.c
+++ b/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);
}
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);
}
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, [
=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 $