summaryrefslogtreecommitdiff
path: root/usr.bin/basename
diff options
context:
space:
mode:
authorJuli Mallett <jmallett@FreeBSD.org>2002-06-30 13:40:35 +0000
committerJuli Mallett <jmallett@FreeBSD.org>2002-06-30 13:40:35 +0000
commit549e1e057c2bce88d72a6d93a3850c1a2b62ce00 (patch)
treeee6617dcfe8bf547be6de46033e48aebab30f9d7 /usr.bin/basename
parent492fa19f70e13e6a6d811397a01af22d189e7ed3 (diff)
downloadsrc-test-549e1e057c2bce88d72a6d93a3850c1a2b62ce00.tar.gz
src-test-549e1e057c2bce88d72a6d93a3850c1a2b62ce00.zip
Make it possible to have this (basename(1)) perform basename(3) on multiple
files. The traditional behaviour, 'basename string .suffix', is preserved, however a suffix may now also be specified via a getopt(3) option, -s, such that if it is specified in that way, all string arguments follow. There is also a new flag, -a, which allows a user to say "yes, please basename(3) on all arguments". Update manual to reflect this unobtrusively. Reviewed by: obrien
Notes
Notes: svn path=/head/; revision=99137
Diffstat (limited to 'usr.bin/basename')
-rw-r--r--usr.bin/basename/basename.118
-rw-r--r--usr.bin/basename/basename.c40
2 files changed, 50 insertions, 8 deletions
diff --git a/usr.bin/basename/basename.1 b/usr.bin/basename/basename.1
index 66d06f53924f7..d9feaf9637c19 100644
--- a/usr.bin/basename/basename.1
+++ b/usr.bin/basename/basename.1
@@ -45,6 +45,11 @@
.Nm
.Ar string
.Op Ar suffix
+.Nm
+.Op Fl a
+.Op Fl s Ar suffix
+.Ar string
+.Op Ar ...
.Nm dirname
.Ar string
.Sh DESCRIPTION
@@ -64,6 +69,19 @@ is not stripped if it is identical to the remaining characters in
.Ar string .
The resulting filename is written to the standard output.
A non-existent suffix is ignored.
+If
+.Fl a
+is specified, then every argument is treated as a
+.Ar string
+as if
+.Nm
+were invoked with just one argument.
+If
+.Fl s
+is specified, then the
+.Ar suffix
+is taken as its argument, and all other arguments are treated as a
+.Ar string .
.Pp
The
.Nm dirname
diff --git a/usr.bin/basename/basename.c b/usr.bin/basename/basename.c
index 6d7a873de09ee..9650bdbbb0cb2 100644
--- a/usr.bin/basename/basename.c
+++ b/usr.bin/basename/basename.c
@@ -61,11 +61,22 @@ main(argc, argv)
int argc;
char **argv;
{
- char *p, *q;
- int ch;
+ char *p, *q, *suffix;
+ size_t suffixlen;
+ int aflag, ch;
- while ((ch = getopt(argc, argv, "")) != -1)
+ aflag = 0;
+ suffix = NULL;
+ suffixlen = 0;
+
+ while ((ch = getopt(argc, argv, "as:")) != -1)
switch(ch) {
+ case 'a':
+ aflag = 1;
+ break;
+ case 's':
+ suffix = optarg;
+ break;
case '?':
default:
usage();
@@ -73,7 +84,7 @@ main(argc, argv)
argc -= optind;
argv += optind;
- if (argc != 1 && argc != 2)
+ if (argc < 1)
usage();
if (!*argv[0]) {
@@ -82,10 +93,21 @@ main(argc, argv)
}
if ((p = basename(argv[0])) == NULL)
err(1, "%s", argv[0]);
- if (*++argv && (q = strchr(p, '\0') - strlen(*argv)) > p &&
- strcmp(*argv, q) == 0)
+ if ((suffix == NULL && !aflag) && argc == 2) {
+ suffix = argv[1];
+ argc--;
+ }
+ if (suffix != NULL)
+ suffixlen = strlen(suffix);
+ while (argc--) {
+ if ((p = basename(*argv)) == NULL)
+ err(1, "%s", argv[0]);
+ if (suffixlen && (q = strchr(p, '\0') - suffixlen) > p &&
+ strcmp(suffix, q) == 0)
*q = '\0';
- (void)printf("%s\n", p);
+ argv++;
+ (void)printf("%s\n", p);
+ }
exit(0);
}
@@ -93,6 +115,8 @@ void
usage()
{
- (void)fprintf(stderr, "usage: basename string [suffix]\n");
+ (void)fprintf(stderr,
+"usage: basename string [suffix]\n"
+" basename [-a] [-s suffix] string [...]\n");
exit(1);
}