From 71af85ab3b49f255d219abfa8066a64d8f0ffc13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Compostella?= Date: Sun, 4 Mar 2012 20:00:36 +0100 Subject: [PATCH] basename: add BSD options support * src/basename.c (perform_basename): New function that perform the basename work on a STRING, remove a trailing SUFFIX and output the result. (main): Handle new options. * doc/coreutils.texi (basename invocation): Mention new options. * test/misc/basename: Add new options test cases. * NEWS (New features): Mention it. --- NEWS | 2 + doc/coreutils.texi | 29 ++++++++++++- src/basename.c | 110 ++++++++++++++++++++++++++++++++++---------------- tests/misc/basename | 3 + 4 files changed, 107 insertions(+), 37 deletions(-) diff --git a/NEWS b/NEWS index 8006669..082c5ec 100644 --- a/NEWS +++ b/NEWS @@ -16,6 +16,8 @@ GNU coreutils NEWS -*- outline -*- split now accepts the --additional-suffix option, to append an additional static suffix to output file names. + basename now supports the -a and -s BSD options. + ** Bug fixes mv now lets you move a symlink onto a same-inode destination file that diff --git a/doc/coreutils.texi b/doc/coreutils.texi index 4a4cadb..d513c25 100644 --- a/doc/coreutils.texi +++ b/doc/coreutils.texi @@ -12422,6 +12422,7 @@ This section describes commands that manipulate file names. @example basename @var{name} [@var{suffix}] +basename [@var{option}]... [@var{name}]... @end example If @var{suffix} is specified and is identical to the end of @var{name}, @@ -12445,8 +12446,26 @@ for everything except file names containing a trailing newline. result is @samp{//} on platforms where @var{//} is distinct from @var{/}, and @samp{/} on platforms where there is no difference. -The only options are @option{--help} and @option{--version}. @xref{Common -options}. Options must precede operands. +The program accepts the following options. Also see @ref{Common options}. +Options must precede operands. + +@table @samp + +@item -a +@itemx --multiple +@opindex -a +@opindex --multiple +Treat every argument as a @var{name} as if @command{basename} were +invoked with just one argument. + +@item -s @var{suffix} +@itemx --sufix=@var{suffix} +@opindex -s +@opindex --suffix +Remove a trailing @var{suffix}. If @samp{-s} is specified, +@command{basename} acts as if @samp{-a} was specified too. + +@end table @exitstatus @@ -12458,6 +12477,12 @@ basename /usr/bin/sort # Output "stdio". basename include/stdio.h .h + +# Output "stdio". +basename -s .h include/stdio.h + +# Output "stdio" followed by "stdlib" +basename -a -s .h include/stdio.h include/stdlib.h @end smallexample diff --git a/src/basename.c b/src/basename.c index daa2895..a1d28da 100644 --- a/src/basename.c +++ b/src/basename.c @@ -14,23 +14,12 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ -/* Usage: basename name [suffix] - NAME is a file name; SUFFIX is a suffix to strip from it. - - basename /usr/foo/lossage/functions.l - => functions.l - basename /usr/foo/lossage/functions.l .l - => functions - basename functions.lisp p - => functions.lis */ - #include #include #include #include #include "system.h" -#include "long-options.h" #include "error.h" #include "quote.h" @@ -39,6 +28,15 @@ #define AUTHORS proper_name ("David MacKenzie") +static struct option const longopts[] = +{ + {"multiple", no_argument, NULL, 'a'}, + {"suffix", required_argument, NULL, 's'}, + {GETOPT_HELP_OPTION_DECL}, + {GETOPT_VERSION_OPTION_DECL}, + {NULL, 0, NULL, 0} +}; + void usage (int status) { @@ -48,7 +46,7 @@ usage (int status) { printf (_("\ Usage: %s NAME [SUFFIX]\n\ - or: %s OPTION\n\ + or: %s [OPTION]... [NAME]...\n\ "), program_name, program_name); fputs (_("\ @@ -56,6 +54,12 @@ Print NAME with any leading directory components removed.\n\ If specified, also remove a trailing SUFFIX.\n\ \n\ "), stdout); + + fputs (_("\ + -a, --multiple treat every argument as a NAME as if basename were\n\ + invoked with just one argument.\n\ + -s, --suffix=SUFFIX remove a trailing SUFFIX.\n\ +"), stdout); fputs (HELP_OPTION_DESCRIPTION, stdout); fputs (VERSION_OPTION_DESCRIPTION, stdout); printf (_("\ @@ -63,15 +67,18 @@ If specified, also remove a trailing SUFFIX.\n\ Examples:\n\ %s /usr/bin/sort Output \"sort\".\n\ %s include/stdio.h .h Output \"stdio\".\n\ + %s -s .h include/stdio.h Output \"stdio\".\n\ + %s -a -s .h include/stdio.h include/stdlib.h Output \"stdio\" followed by\ + \"stdlib\".\n\ "), - program_name, program_name); + program_name, program_name, program_name, program_name); emit_ancillary_info (); } exit (status); } /* Remove SUFFIX from the end of NAME if it is there, unless NAME - consists entirely of SUFFIX. */ + consists entirely of SUFFIX. */ static void remove_suffix (char *name, const char *suffix) @@ -89,10 +96,33 @@ remove_suffix (char *name, const char *suffix) *np = '\0'; } +/* Perform the basename operation on STRING. If SUFFIX is non-NULL, remove + the trailing suffix SUFFIX. Finally, output the result string. */ + +static void +perform_basename (const char *string, const char *suffix) +{ + char *name = base_name (string); + strip_trailing_slashes (name); + + /* Per POSIX, 'basename // /' must return '//' on platforms with + distinct //. On platforms with drive letters, this generalizes + to making 'basename c: :' return 'c:'. This rule is captured by + skipping suffix stripping if base_name returned an absolute path + or a drive letter (only possible if name is a file-system + root). */ + if (suffix && IS_RELATIVE_FILE_NAME (name) && ! FILE_SYSTEM_PREFIX_LEN (name)) + remove_suffix (name, suffix); + + puts (name); + free (name); +} + int main (int argc, char **argv) { - char *name; + unsigned char multiple_names = 0; + const char *suffix = NULL; initialize_main (&argc, &argv); set_program_name (argv[0]); @@ -102,10 +132,29 @@ main (int argc, char **argv) atexit (close_stdout); - parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE_NAME, Version, - usage, AUTHORS, (char const *) NULL); - if (getopt_long (argc, argv, "+", NULL, NULL) != -1) - usage (EXIT_FAILURE); + while (true) + { + int c = getopt_long (argc, argv, "as:", longopts, NULL); + + if (c == -1) + break; + + switch (c) + { + case 's': + suffix = optarg; + + case 'a': + multiple_names = 1; + break; + + case_GETOPT_HELP_CHAR; + case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); + + default: + usage (EXIT_FAILURE); + } + } if (argc < optind + 1) { @@ -113,27 +162,18 @@ main (int argc, char **argv) usage (EXIT_FAILURE); } - if (optind + 2 < argc) + if (!multiple_names && optind + 2 < argc) { error (0, 0, _("extra operand %s"), quote (argv[optind + 2])); usage (EXIT_FAILURE); } - name = base_name (argv[optind]); - strip_trailing_slashes (name); - - /* Per POSIX, 'basename // /' must return '//' on platforms with - distinct //. On platforms with drive letters, this generalizes - to making 'basename c: :' return 'c:'. This rule is captured by - skipping suffix stripping if base_name returned an absolute path - or a drive letter (only possible if name is a file-system - root). */ - if (argc == optind + 2 && IS_RELATIVE_FILE_NAME (name) - && ! FILE_SYSTEM_PREFIX_LEN (name)) - remove_suffix (name, argv[optind + 1]); - - puts (name); - free (name); + if (multiple_names) + for (; optind < argc; optind++) + perform_basename (argv[optind], suffix); + else + perform_basename (argv[optind], + optind + 2 == argc ? argv[optind + 1] : NULL); exit (EXIT_SUCCESS); } diff --git a/tests/misc/basename b/tests/misc/basename index fefe58c..5415ca9 100755 --- a/tests/misc/basename +++ b/tests/misc/basename @@ -47,6 +47,9 @@ my @Tests = ['h', qw(///), {OUT => '/'}], ['i', qw(///a///), {OUT => 'a'}], ['j', qw(''), {OUT => ''}], + ['k', qw(aa a), {OUT => 'a'}], + ['l', qw(-a a b), {OUT => "a\nb"}], + ['m', qw(-s a aa ba ab), {OUT => "a\nb\nab"}], ['1', qw(f.s .s), {OUT => 'f'}], ['2', qw(fs s), {OUT => 'f'}], ['3', qw(fs fs), {OUT => 'fs'}], -- 1.7.2.5