changeset 176:07533cabeede

Patch from Charlie Shepherd to add basename and dirname. (Fixed up to apply.)
author Rob Landley <rob@landley.net>
date Tue, 27 Nov 2007 00:57:42 -0600
parents 318fc45fe7bf
children 00bbb7f65b6f
files toys/Config.in toys/basename.c toys/dirname.c toys/toylist.h
diffstat 4 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/toys/Config.in	Sat Nov 24 22:05:29 2007 -0600
+++ b/toys/Config.in	Tue Nov 27 00:57:42 2007 -0600
@@ -11,6 +11,14 @@
 	  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 @@
 
 	  -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toys/basename.c	Tue Nov 27 00:57:42 2007 -0600
@@ -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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/toys/dirname.c	Tue Nov 27 00:57:42 2007 -0600
@@ -0,0 +1,8 @@
+#include "toys.h"
+#include <libgen.h>
+
+int dirname_main(void)
+{
+	puts(dirname(toys.optargs[0]));
+	return 0;
+}
--- a/toys/toylist.h	Sat Nov 24 22:05:29 2007 -0600
+++ b/toys/toylist.h	Tue Nov 27 00:57:42 2007 -0600
@@ -105,11 +105,13 @@
 
 // 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))