diff options
author | Elliott Hughes <enh@google.com> | 2018-11-12 20:52:29 -0800 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2018-11-13 15:51:44 -0600 |
commit | 457dda2930287e4bb1bbc3c3ff56fcd4e9ffa3de (patch) | |
tree | d57463c7fde2d49203660c002978e76c6f4d4dc3 /toys/posix | |
parent | d54fac979ef9f96a64dcce2e3876b97dfcc5a25d (diff) | |
download | toybox-457dda2930287e4bb1bbc3c3ff56fcd4e9ffa3de.tar.gz |
basename: -s SUFFIX.
AOSP doesn't need -a specifically, but since it's needed for -s we may
as well accept it too.
Diffstat (limited to 'toys/posix')
-rw-r--r-- | toys/posix/basename.c | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/toys/posix/basename.c b/toys/posix/basename.c index 0436bfe7..11b96227 100644 --- a/toys/posix/basename.c +++ b/toys/posix/basename.c @@ -5,25 +5,44 @@ * See http://opengroup.org/onlinepubs/9699919799/utilities/basename.html -USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_USR|TOYFLAG_BIN)) +USE_BASENAME(NEWTOY(basename, "<1as:", TOYFLAG_USR|TOYFLAG_BIN)) config BASENAME bool "basename" default y help - usage: basename string [suffix] + usage: basename [-a] [-s SUFFIX] NAME... | NAME [SUFFIX] - Return non-directory portion of a pathname removing suffix + Return non-directory portion of a pathname removing suffix. + + -a All arguments are names. + -s SUFFIX Remove suffix (implies -a). */ +#define FOR_basename #include "toys.h" +GLOBALS( + char *s; +) + void basename_main(void) { - char *base = basename(*toys.optargs), *suffix = toys.optargs[1]; + char **arg; + + if (toys.optflags&FLAG_s) toys.optflags |= FLAG_a; + + if (!(toys.optflags&FLAG_a)) { + if (toys.optc > 2) error_exit("too many args"); + TT.s = toys.optargs[1]; + toys.optargs[1] = NULL; + } - // chop off the suffix if provided - if (suffix && *suffix && (suffix = strend(base, suffix))) *suffix = 0; + for (arg = toys.optargs; *arg; ++arg) { + char *base = basename(*arg), *p; - puts(base); + // Chop off the suffix if provided. + if (TT.s && *TT.s && (p = strend(base, TT.s))) *p = 0; + puts(base); + } } |