Bug 21918

Summary: Revision 1.5 provides incomplete fix for 1.4 breakage by DES
Product: Base System Reporter: ak03 <ak03>
Component: binAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me CC: des
Priority: Normal    
Version: 5.0-CURRENT   
Hardware: Any   
OS: Any   
Attachments:
Description Flags
file.diff none

Description ak03 2000-10-11 20:50:01 UTC
	The comment on the old (correct) revision states that:
	  If the suffix operand is present, is not identical to the
          characters remaining in string, and is identical to a suffix
          of the characters remaining in string, the suffix suffix
          shall be removed from string.	
	The new version removes suffix even when suffix is identical 
	to the remaining string itself. That is clearly not the 
	behaviour both comment and basename man page describe.

How-To-Repeat: 
	in sh: 
		% basename filename.ext filename.ext
                
		%

	in ksh:
		% basename filename.ext filename.ext
		filename.ext
		%
Comment 1 Bruce Evans 2000-10-12 06:43:14 UTC
On Wed, 11 Oct 2000 ak03@gte.com wrote:

> >Description:
> 
> 	The comment on the old (correct) revision states that:
> 	  If the suffix operand is present, is not identical to the
>           characters remaining in string, and is identical to a suffix
>           of the characters remaining in string, the suffix suffix
>           shall be removed from string.	
> 	The new version removes suffix even when suffix is identical 
> 	to the remaining string itself. That is clearly not the 
> 	behaviour both comment and basename man page describe.

basename(3) is remarkably feeble compared with basename(1).  Another case
that has changed is `basename ""`.  basename(3) is specified to bogotify
this case by changing "" to ".".

Bruce
Comment 2 des 2000-10-12 09:27:39 UTC
I will commit the following patch later today:

Index: basename.c
===================================================================
RCS file: /home/ncvs/src/usr.bin/basename/basename.c,v
retrieving revision 1.5
diff -u -r1.5 basename.c
--- basename.c	2000/09/06 07:28:02	1.5
+++ basename.c	2000/10/12 08:23:25
@@ -73,8 +73,9 @@
 
 	if ((p = basename(argv[0])) == NULL)
 		err(1, "%s", argv[0]);
-	if (*++argv && (q = strstr(p, *argv)) && strcmp(q, *argv) == 0)
-		*q = '\0';
+	if (*++argv && (q = strchr(p, '\0') - strlen(*argv)) > p &&
+	    strcmp(*argv, q) == 0)
+			*q = '\0';
 	(void)printf("%s\n", p);
 	exit(0);
 }


DES
-- 
Dag-Erling Smorgrav - des@ofug.org
Comment 3 ak03 2000-10-12 15:33:41 UTC
> Why do you use 16 lines of code to fix this when all you need is to
> add p != q to the if test?

  I just restored the code you removed in revision 1.4. It is of
course the matter of personal preference, but the old code IMO 
is much more readable than the && chain you added in recently.
Feel free to apply your patch if you really like it better. 
6 out of 16 lines of my patch are the comment anyway, so it is not
_that_ bad :)

>(And you didn't spot the real bug, either - but it can still be
> fixed by just rewording the if test)

Do you mean the basename(1) and basename(3) inconsistency described
in Bruse Evans message? Or there is something else I am missing?

Should we add something like the following to make FreeBSD basename 
consistent with historic behaviour again?

/*
 * Do not call basename(3) with an empty string because
 * basename(3) is specified to bogotify
 * this case by changing "" to "." and that is
 * not consistent with what basename(1) is supposed to do
 */
if ( argv[0][0]) == '\0') {
	(void)printf("\n");
	exit(0);
}


P.S. All English grammar errors in the above text are mine and
     mine only.
Comment 4 des 2000-10-12 17:10:44 UTC
ak03@gte.com (Alexander N. Kabaev) writes:
> >(And you didn't spot the real bug, either - but it can still be
> > fixed by just rewording the if test)
> Do you mean the basename(1) and basename(3) inconsistency described
> in Bruse Evans message? Or there is something else I am missing?

Something else - 'basename filename.ext.ext .ext' doesn't work the way
it should.

> Should we add something like the following to make FreeBSD basename 
> consistent with historic behaviour again?

Yes.

DES
-- 
Dag-Erling Smorgrav - des@ofug.org
Comment 5 ak03 2000-10-12 17:31:23 UTC
> 
> Something else - 'basename filename.ext.ext .ext' doesn't work the way
> it should.
> 
Ah, I see it now. My patch fixed that bug as well despite the fact that I wasn't
even aware it's presence.

Thanks for prompt response!
----------------------------------
E-Mail: Alexander N. Kabaev <ak03@gte.com>
Date: 12-Oct-00
Time: 12:27:41
----------------------------------
Comment 6 Mike Heffner freebsd_committer freebsd_triage 2001-06-23 06:57:41 UTC
State Changed
From-To: open->closed

Fixed.