diff options
author | Rob Landley <rob@landley.net> | 2007-11-27 00:57:42 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2007-11-27 00:57:42 -0600 |
commit | c15b3edf55ebd915b234e2ee6280a19874a052f9 (patch) | |
tree | 43d6c9c5bce6f6b7200ebb0aa84479248d1596b5 /toys | |
parent | 57389ec1d8f5b32e5de97f0f557e5f5c5b7054bb (diff) | |
download | toybox-c15b3edf55ebd915b234e2ee6280a19874a052f9.tar.gz |
Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
Diffstat (limited to 'toys')
-rw-r--r-- | toys/Config.in | 16 | ||||
-rw-r--r-- | toys/basename.c | 15 | ||||
-rw-r--r-- | toys/dirname.c | 8 | ||||
-rw-r--r-- | toys/toylist.h | 2 |
4 files changed, 41 insertions, 0 deletions
diff --git a/toys/Config.in b/toys/Config.in index 04640c58..34c31107 100644 --- a/toys/Config.in +++ b/toys/Config.in @@ -11,6 +11,14 @@ config TOYBOX With no arguments, shows available commands. First argument is name of a command to run, followed by any arguments to that command. +config BASENAME + bool "basename" + default y + help + usage: basename path + + Print the part of path after the last slash. + config BZCAT bool "bzcat" default n @@ -68,6 +76,14 @@ config DF_PEDANTIC -k Sets units back to 1024 bytes (the default without -P) +config DIRNAME + bool "dirname" + default y + help + usage: dirname path + + Print the part of path up to the last slash. + config DMESG bool "dmesg" default y diff --git a/toys/basename.c b/toys/basename.c new file mode 100644 index 00000000..2ea6882c --- /dev/null +++ b/toys/basename.c @@ -0,0 +1,15 @@ +#include "toys.h" + +int basename_main(void) +{ + char *name = basename(toys.optargs[0]); + if (toys.optargs[1]) { + int slen = strlen(toys.optargs[1]); + int name_len = strlen(name); + if (slen < name_len) + if (!strcmp(name+name_len-slen, toys.optargs[1])) + *(name+name_len-slen) = '\0'; + } + puts(name); + return 0; +} diff --git a/toys/dirname.c b/toys/dirname.c new file mode 100644 index 00000000..454a5d67 --- /dev/null +++ b/toys/dirname.c @@ -0,0 +1,8 @@ +#include "toys.h" +#include <libgen.h> + +int dirname_main(void) +{ + puts(dirname(toys.optargs[0])); + return 0; +} diff --git a/toys/toylist.h b/toys/toylist.h index a55a56a6..87bffed7 100644 --- a/toys/toylist.h +++ b/toys/toylist.h @@ -105,11 +105,13 @@ NEWTOY(toybox, NULL, 0) // The rest of these are alphabetical, for binary search. +USE_BASENAME(NEWTOY(basename, "<1>2", TOYFLAG_BIN)) USE_BZCAT(NEWTOY(bzcat, "", TOYFLAG_USR|TOYFLAG_BIN)) USE_CATV(NEWTOY(catv, "vte", TOYFLAG_USR|TOYFLAG_BIN)) USE_COUNT(NEWTOY(count, "", TOYFLAG_USR|TOYFLAG_BIN)) USE_TOYSH(NEWTOY(cd, NULL, TOYFLAG_NOFORK)) USE_DF(NEWTOY(df, "Pkt*a", TOYFLAG_USR|TOYFLAG_SBIN)) +USE_DIRNAME(NEWTOY(dirname, "<1>1", TOYFLAG_BIN)) USE_DMESG(NEWTOY(dmesg, "s#n#c", TOYFLAG_BIN)) USE_ECHO(NEWTOY(echo, "+en", TOYFLAG_BIN)) USE_TOYSH(NEWTOY(exit, NULL, TOYFLAG_NOFORK)) |