changeset 177:00bbb7f65b6f

Remove a few bytes from basename and add 'em back (and more) in the help string.
author Rob Landley <rob@landley.net>
date Tue, 27 Nov 2007 01:06:43 -0600
parents 07533cabeede
children 0e94f5f14f08
files toys/Config.in toys/basename.c
diffstat 2 files changed, 13 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/toys/Config.in	Tue Nov 27 00:57:42 2007 -0600
+++ b/toys/Config.in	Tue Nov 27 01:06:43 2007 -0600
@@ -15,9 +15,9 @@
 	bool "basename"
 	default y
 	help
-	  usage: basename path
+	  usage: basename path [suffix]
 
-	  Print the part of path after the last slash.
+	  Print the part of path after the last slash, optionally minus suffix.
 
 config BZCAT
 	bool "bzcat"
--- a/toys/basename.c	Tue Nov 27 00:57:42 2007 -0600
+++ b/toys/basename.c	Tue Nov 27 01:06:43 2007 -0600
@@ -1,14 +1,18 @@
+/* vi: set sw=4 ts=4: */
+/* basename.c - print non-directory portion of path
+ *
+ * See http://www.opengroup.org/onlinepubs/009695399/utilities/basename.html
+ */
+
 #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';
+	char *name = basename(*toys.optargs);
+	char *suffix = toys.optargs[1];
+	if (suffix) {
+		char *end = name+strlen(name)-strlen(suffix);
+		if (end>name && !strcmp(end,suffix)) *end=0;
 	}
 	puts(name);
 	return 0;