From vova@express.ru  Mon Dec 17 07:27:01 2001
Return-Path: <vova@express.ru>
Received: from vbook.express.ru (asplinux.ru [195.133.213.194])
	by hub.freebsd.org (Postfix) with ESMTP id 5C16837B416
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 17 Dec 2001 07:27:00 -0800 (PST)
Received: from vova by vbook.express.ru with local (Exim 3.31 #2)
	id 16FzfS-0006UA-00
	for FreeBSD-gnats-submit@freebsd.org; Mon, 17 Dec 2001 18:27:02 +0300
Message-Id: <E16FzfS-0006UA-00@vbook.express.ru>
Date: Mon, 17 Dec 2001 18:27:02 +0300
From: Vladimir B.Grebenschikov <vova@sw.ru>
Sender: "Vladimir B. Grebenschikov" <vova@express.ru>
Reply-To: Vladimir B.Grebenschikov <vova@sw.ru>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: /bin/sh buildin echo command have invalid implimentation of command args parser
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         32935
>Category:       bin
>Synopsis:       /bin/sh buildin echo command have invalid implimentation of command args parser
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Dec 17 07:30:00 PST 2001
>Closed-Date:    Sun Jul 28 03:25:02 PDT 2002
>Last-Modified:  Sun Jul 28 03:25:02 PDT 2002
>Originator:     Vladimir B. Grebenschikov
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
SWsoft
>Environment:
System: FreeBSD vbook.express.ru 5.0-CURRENT FreeBSD 5.0-CURRENT #4: Mon Dec 17 12:45:42 MSK 2001 root@vbook.express.ru:/usr/local/src/sys/i386/compile/VBOOK i386

also same problem in 4.4-RELEASE

>Description:

According to manpage sh(1):

     echo [-en] string
             Print string to the standard output with a newline appended.

             -n      Suppress the output of the trailing newline.

             -e      Process C-style backslash escape sequences.  echo under-
                     stands the following character escapes:

But in realality echo accepts either -n or -e flag but not both simultaneously.


>How-To-Repeat:

$ echo -en  "\tGGG\nJJJ\nccc"
-en \tGGG\nJJJ\nccc
$ 

but:

$ echo -e  "\tGGG\nJJJ\nccc"
        GGG
JJJ
ccc
$

$ echo -n "test"
test$ 

>Fix:

diff -u src/bin/sh/bltin/echo.c:1.9.2.1
src/bin/sh/bltin/echo.c:1.9.2.1.10000.2
--- src/bin/sh/bltin/echo.c:1.9.2.1     Fri Jun 30 00:51:59 2000
+++ src/bin/sh/bltin/echo.c     Mon Dec 17 18:15:23 2001
@@ -34,7 +34,7 @@
  * SUCH DAMAGE.
  *
  *     @(#)echo.c      8.2 (Berkeley) 5/4/95
- * $FreeBSD: src/bin/sh/bltin/echo.c,v 1.9.2.1 2000/06/29 20:51:59 mph Exp
$
+ * $FreeBSD: src/bin/sh/bltin/echo.c,v 1.9.2.1.10000.2 2001/12/17 15:15:23
kmv Exp $
  */
 
 /*
@@ -64,16 +64,28 @@
        ap = argv;
        if (argc)
                ap++;
-       if ((p = *ap) != NULL) {
-               if (equal(p, "-n")) {
-                       nflag++;
-                       ap++;
-               } else if (equal(p, "-e")) {
+       if ((p = *ap) != NULL && *p == '-') {
+               ap++;
+               p++;
+               do {
+                       if (*p == 'n') { 
+                               nflag++;
+                       }
+#ifndef eflag
+                       else if (*p == 'e') {
+                               eflag++;
+                       }
+#endif
+                       else {
+                               nflag = 0;
 #ifndef eflag
-                       eflag++;
+                               eflag = 0;
 #endif
-                       ap++;
-               }
+                               ap--;
+                               break;
+                       }
+                       p++;
+               } while (*p); 
        }
        while ((p = *ap++) != NULL) {
                while ((c = *p++) != '\0') {

--
SWSoft, Moscow
Vladimir B. Grebenschikov vova@sw.ru
>Release-Note:
>Audit-Trail:

From: Sheldon Hearn <sheldonh@starjuice.net>
To: "Vladimir B.Grebenschikov" <vova@sw.ru>
Cc: FreeBSD-gnats-submit@freebsd.org
Subject: Re: bin/32935: /bin/sh buildin echo command have invalid implimentation of command args parser 
Date: Sun, 30 Dec 2001 15:42:53 +0200

 On Mon, 17 Dec 2001 18:27:02 +0300, "Vladimir B.Grebenschikov" wrote:
 
 > >Number:         32935
 > >Category:       bin
 > >Synopsis:       /bin/sh buildin echo command have invalid implimentation of command args parser
 
 I've asked for review on the freebsd-audit mailing list.  Hopefully,
 you'll get feedback soon.
 
 Ciao,
 Sheldon.

From: Mike Heffner <mheffner@novacoxmail.com>
To: Sheldon Hearn <sheldonh@starjuice.net>,
	freebsd-gnats-submit@freebsd.org
Cc: audit@FreeBSD.ORG
Subject: Re: bin/32935: /bin/sh builtin echo command
Date: Mon, 31 Dec 2001 03:05:51 -0500 (EST)

 On 30-Dec-2001 Sheldon Hearn wrote:
 | 
 | Hi folks,
 | 
 | Could someone check out PR bin/32935, which contains a patch to fix
 | ash's builtin echo command so that it accepts the -e and -n options
 | combined?
 | 
 
 Looks ok, except that I think in the '#define eflag 1' case it should
 still accept '-ne' as valid and just ignore the 'e'. How about this:
 
 
 Index: echo.c
 ===================================================================
 RCS file: /home/ncvs/src/bin/sh/bltin/echo.c,v
 retrieving revision 1.10
 diff -u -r1.10 echo.c
 --- echo.c      26 Jun 2000 22:43:30 -0000      1.10
 +++ echo.c      31 Dec 2001 08:00:09 -0000
 @@ -64,16 +64,28 @@
         ap = argv;
         if (argc)
                 ap++;
 -       if ((p = *ap) != NULL) {
 -               if (equal(p, "-n")) {
 -                       nflag++;
 -                       ap++;
 -               } else if (equal(p, "-e")) {
 +       if ((p = *ap) != NULL && *p == '-') {
 +               ap++;
 +               p++;
 +               do {
 +                       if (*p == 'n')
 +                               nflag++;
 +                       else if (*p == 'e')
  #ifndef eflag
 -                       eflag++;
 +                               eflag++;
 +#else
 +                               ;
  #endif
 -                       ap++;
 -               }
 +                       else {
 +                               nflag = 0;
 +#ifndef eflag
 +                               eflag = 0;
 +#endif
 +                               ap--;
 +                               break;
 +                       }
 +               } while (*++p != '\0');
 +                       
         }
         while ((p = *ap++) != NULL) {
                 while ((c = *p++) != '\0') {
 
 
 
 Mike
 
 -- 
   Mike Heffner     <mheffner@[acm.]vt.edu>
   Fredericksburg, VA   <mikeh@FreeBSD.org>
 

From: Sheldon Hearn <sheldonh@starjuice.net>
To: Mike Heffner <mheffner@vt.edu>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/32935: /bin/sh builtin echo command 
Date: Mon, 31 Dec 2001 11:16:45 +0200

 On Mon, 31 Dec 2001 03:05:51 EST, Mike Heffner wrote:
 
 > Looks ok, except that I think in the '#define eflag 1' case it should
 > still accept '-ne' as valid and just ignore the 'e'. How about this:
 
 Thanks, Mike.
 
 But why would you want to accept an option that you don't support?
 
 Personally, I don't think -e support needs to be optional. :-)
 
 Ciao,
 Sheldon.

From: Mike Heffner <mheffner@novacoxmail.com>
To: Sheldon Hearn <sheldonh@starjuice.net>
Cc: freebsd-gnats-submit@freebsd.org
Subject: Re: bin/32935: /bin/sh builtin echo command
Date: Mon, 31 Dec 2001 11:46:20 -0500 (EST)

 On 31-Dec-2001 Sheldon Hearn wrote:
 | 
 | 
 | On Mon, 31 Dec 2001 03:05:51 EST, Mike Heffner wrote:
 | 
 |> Looks ok, except that I think in the '#define eflag 1' case it should
 |> still accept '-ne' as valid and just ignore the 'e'. How about this:
 | 
 | Thanks, Mike.
 | 
 | But why would you want to accept an option that you don't support?
 | 
 | Personally, I don't think -e support needs to be optional. :-)
 | 
 
 Well, I guess I'm not sure as to the context of when the defined eflag
 override is used. IMO, if a shell script is using 'echo -e blah', then it
 shouldn't break on an 'echo' that has been compiled with the eflag
 implicitly enabled. Also, I was just following the original broken code, so
 that the previous test case would work as before. ;)
 
 Mike
 
 -- 
   Mike Heffner     <mheffner@[acm.]vt.edu>
   Fredericksburg, VA   <mikeh@FreeBSD.org>
 
State-Changed-From-To: open->patched 
State-Changed-By: tjr 
State-Changed-When: Fri Jul 19 21:39:26 PDT 2002 
State-Changed-Why:  
Manual page has been updated to indicate that only one of -n and -e may 
be specified. The traditional BSD behaviour will not be changed. Consider 
using printf(1) instead to print escape sequences or omit the trailing 
newline, it is much more consistent between operating systems. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=32935 
State-Changed-From-To: patched->closed 
State-Changed-By: tjr 
State-Changed-When: Sun Jul 28 03:24:27 PDT 2002 
State-Changed-Why:  
Change has been MFC'd. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=32935 
>Unformatted:
