Bug 21061

Summary: basename(1) ignores suffix argument
Product: Base System Reporter: Atsushi Onoe <onoe>
Component: binAssignee: Dag-Erling Smørgrav <des>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 5.0-CURRENT   
Hardware: Any   
OS: Any   

Description Atsushi Onoe 2000-09-05 15:10:01 UTC
	As a result of recent change to basename(1) simply to use basename(3),
	basename(1) no longer delete suffix from the first argument.

Fix: 

back out rev. 1.4
How-To-Repeat: 
	% basename foo.c .c
	foo.c
	<should be 'foo'>
Comment 1 onoe 2000-09-05 15:15:38 UTC
> From: Atsushi Onoe <onoe@duplo.sm.sony.co.jp>

Sorry, I've mailed with wrong address.

Atsushi Onoe <onoe@sm.sony.co.jp>
Comment 2 Sheldon Hearn freebsd_committer freebsd_triage 2000-09-05 15:28:35 UTC
Responsible Changed
From-To: freebsd-bugs->des

Over to DES, so that he can review the patch I'm about to send.
Comment 3 Sheldon Hearn 2000-09-05 15:29:38 UTC
On Tue, 05 Sep 2000 23:02:41 +0900, Atsushi Onoe wrote:

> >Number:         21061
> >Category:       bin
> >Synopsis:       basename(1) ignores suffix argument

Try this.  The code that DES replaced has not been reinstated verbatim
because of style bugs.

Ciao,
Sheldon.

Index: basename.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/basename/basename.c,v
retrieving revision 1.4
diff -u -d -r1.4 basename.c
--- basename.c	2000/09/03 17:09:41	1.4
+++ basename.c	2000/09/05 14:26:00
@@ -43,6 +43,7 @@
 static const char sccsid[] = "@(#)basename.c	8.4 (Berkeley) 5/4/95";
 #endif /* not lint */
 
+#include <err.h>
 #include <libgen.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -54,7 +55,8 @@
 	int argc;
 	char **argv;
 {
-	int ch;
+	int ch, stringlen, off, suffixlen;
+	char *p;
 
 	while ((ch = getopt(argc, argv, "")) != -1)
 		switch(ch) {
@@ -68,7 +70,19 @@
 	if (argc != 1 && argc != 2)
 		usage();
 
-	(void)printf("%s\n", basename(*argv));
+	if ((p = basename(*argv)) == NULL)
+		err(1, "%s", *argv);
+	if (*++argv) {
+		stringlen = strlen(p);
+		suffixlen = strlen(*argv);
+
+		if (suffixlen < stringlen) {
+			off = stringlen - suffixlen;
+			if (!strcmp(p + off, *argv))
+				p[off] = '\0';
+		}
+	}
+	(void)printf("%s\n", p);
 	exit(0);
 }
Comment 4 Sheldon Hearn freebsd_committer freebsd_triage 2000-09-06 17:59:28 UTC
State Changed
From-To: open->closed

Fixed today: 

1.5       +9 -2      src/usr.bin/basename/basename.c 
1.7       +6 -2      src/usr.bin/dirname/dirname.c