basename: implement -a and -s SUFFIX

function                                             old     new   delta
basename_main                                        145     207     +62
packed_usage                                       33914   33950     +36
.rodata                                           104241  104250      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 107/0)             Total: 107 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2021-09-09 23:45:13 +02:00
parent 7ab9cd2398
commit 0599e0f87b

View File

@ -29,9 +29,11 @@
/* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */ /* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */
//usage:#define basename_trivial_usage //usage:#define basename_trivial_usage
//usage: "FILE [SUFFIX]" //usage: "FILE [SUFFIX] | -a FILE... | -s SUFFIX FILE..."
//usage:#define basename_full_usage "\n\n" //usage:#define basename_full_usage "\n\n"
//usage: "Strip directory path and .SUFFIX from FILE" //usage: "Strip directory path and SUFFIX from FILE\n"
//usage: "\n -a All arguments are FILEs"
//usage: "\n -s SUFFIX Remove SUFFIX (implies -a)"
//usage: //usage:
//usage:#define basename_example_usage //usage:#define basename_example_usage
//usage: "$ basename /usr/local/bin/foo\n" //usage: "$ basename /usr/local/bin/foo\n"
@ -48,31 +50,43 @@
int basename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int basename_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int basename_main(int argc UNUSED_PARAM, char **argv) int basename_main(int argc UNUSED_PARAM, char **argv)
{ {
size_t m, n; unsigned opts;
char *s; const char *suffix = NULL;
if (argv[1] && strcmp(argv[1], "--") == 0) { /* '+': stop at first non-option */
argv++; opts = getopt32(argv, "^+" "as:"
} "\0" "-1" /* At least one argument */
if (!argv[1]) , &suffix
bb_show_usage(); );
argv += optind;
/* It should strip slash: /abc/def/ -> def */ do {
s = bb_get_last_path_component_strip(*++argv); char *s;
size_t m;
m = strlen(s); /* It should strip slash: /abc/def/ -> def */
if (*++argv) { s = bb_get_last_path_component_strip(*argv++);
if (argv[1]) m = strlen(s);
bb_show_usage(); if (!opts) {
n = strlen(*argv); if (*argv) {
if ((m > n) && (strcmp(s+m-n, *argv) == 0)) { suffix = *argv;
m -= n; if (argv[1])
/*s[m] = '\0'; - redundant */ bb_show_usage();
}
} }
} if (suffix) {
size_t n = strlen(suffix);
if ((m > n) && (strcmp(s + m - n, suffix) == 0)) {
m -= n;
/*s[m] = '\0'; - redundant */
}
}
/* puts(s) will do, but we can do without stdio this way: */
s[m++] = '\n';
/* NB: != is correct here: */
if (full_write(STDOUT_FILENO, s, m) != (ssize_t)m)
return EXIT_FAILURE;
} while (opts && *argv);
/* puts(s) will do, but we can do without stdio this way: */ return EXIT_SUCCESS;
s[m++] = '\n';
/* NB: != is correct here: */
return full_write(STDOUT_FILENO, s, m) != (ssize_t)m;
} }