From nobody@FreeBSD.org  Tue Nov 22 09:19:24 2005
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9373B16A41F
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 22 Nov 2005 09:19:24 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 9DD9843D79
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 22 Nov 2005 09:19:19 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id jAM9JJJH057602
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 22 Nov 2005 09:19:19 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id jAM9JJdf057601;
	Tue, 22 Nov 2005 09:19:19 GMT
	(envelope-from nobody)
Message-Id: <200511220919.jAM9JJdf057601@www.freebsd.org>
Date: Tue, 22 Nov 2005 09:19:19 GMT
From: "Dr Balwinder Singh Dheeman <"<bsd@rubyforge.org>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [PATCH] sh missing \u interpolation and bug/fix in \W
X-Send-Pr-Version: www-2.3

>Number:         89410
>Category:       bin
>Synopsis:       [patch] sh(1) missing \u interpolation and bug/fix in \W
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    jh
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Nov 22 09:20:19 GMT 2005
>Closed-Date:    Wed Mar 02 15:38:16 UTC 2011
>Last-Modified:  Wed Mar 02 15:38:16 UTC 2011
>Originator:     Dr Balwinder Singh Dheeman
>Release:        FreeBSD 6.0-RELEASE
>Organization:
Anu's Linux@HOME
>Environment:
FreeBSD cto.sebs.org.in 6.0-RELEASE FreeBSD 6.0-RELEASE #0: Thu Nov  3 09:36:13 UTC 2005     root@x64.samsco.home:/usr/obj/usr/src/sys/GENERIC  i386

Applicable to all base os and, or rescue box.
>Description:
        The code for interpolating \u in $PS1 (prompt sting) is from
        /usr/src/bin/sh/parser.c, which is part of sh binary.

        And interpolation for \W is also buggy and, or incomplete.

>How-To-Repeat:
        At any CLI/shell prompt exec the following:

        /bin/sh -i
        PS1='[\u@\h \W]\$ '

        See whether your /bin/sh is iterpolating \u and \W properly?
        No, I think NOT. Try:

        cd /

        You will see a missing ']$ ' or ']# ' the later if you are
        logged in as root

        Now try:

        cd

        Yor are back in your $HOME, which /bin/sh should interploate as
        a "~" for brivity; but is not in the current parser.c code which
        is linked with binary sh.

        NOTE: The /usr/share/skel/dot.shrc is needed to be patched.

>Fix:
        Apply the following patch to /usr/src/bin/sh/parser.c for /bin/sh:

        --- parser.c.orig       Mon Feb 28 18:30:00 2005
        +++ parser.c    Tue Nov 22 10:43:12 2005
        @@ -36,6 +36,9 @@
         #endif
         #endif /* not lint */
         #include <sys/cdefs.h>
        +#include <sys/types.h>
        +#include <pwd.h>
        +
         __FBSDID("$FreeBSD: src/bin/sh/parser.c,v 1.52 2005/03/01 03:35:58 obrien Exp $");

         #include <stdlib.h>
        @@ -1564,8 +1567,11 @@
         getprompt(void *unused __unused)
         {
                static char ps[PROMPTLEN];
        +       static char hd[PROMPTLEN];
                char *fmt;
                int i, j, trim;
        +       struct passwd *pw;
        +       pw = getpwuid(geteuid());

                /*
                 * Select prompt format.
        @@ -1608,6 +1614,17 @@
                                        break;

                                        /*
        +                                * username.
        +                                *
        +                                * \u specifies the username.
        +                                */
        +                       case 'u':
        +                               ps[i] == '\0';
        +                               strcpy(&ps[i], pw->pw_name);
        +                               i += strlen(pw->pw_name) - 1;
        +                               break;
        +
        +                               /*
                                         * Working directory.
                                         *
                                         * \W specifies just the final component,
        @@ -1617,14 +1634,19 @@
                                case 'w':
                                        ps[i] == '\0';
                                        getcwd(&ps[i], PROMPTLEN - i);
        -                               if (*fmt == 'W') {
        -                                       /* Final path component only. */
        -                                       trim = 1;
        -                                       for (j = i; ps[j] != '\0'; j++)
        -                                         if (ps[j] == '/')
        -                                               trim = j + 1;
        -                                       memmove(&ps[i], &ps[trim],
        -                                           j - trim + 1);
        +                               chdir(pw->pw_dir);
        +                               getcwd(hd, PROMPTLEN - i);
        +                               if (strcmp(&ps[i], hd) == NULL)
        +                                   strcpy(&ps[i], "~");
        +                               else
        +                                   chdir(&ps[i]);
        +                               if (*fmt == 'W' && ps[i + 1] != '\0') {
        +                                   /* Final path component only. */
        +                                   trim = 1;
        +                                   for (j = i; ps[j] != '\0'; j++)
        +                                       if (ps[j] == '/')
        +                                           trim = j + 1;
        +                                   memmove(&ps[i], &ps[trim], j - trim + 1);
                                        }
                                        /* Skip to end of path. */
                                        while (ps[i + 1] != '\0')
        @@ -1637,7 +1659,7 @@
                                         * '$' for normal users, '#' for root.
                                         */
                                case '$':
        -                               ps[i] = (geteuid() != 0) ? '$' : '#';
        +                               ps[i] = (pw->pw_uid != 0) ? '$' : '#';
                                        break;

                                        /*

        The following may also be applied to /usr/share/skel/dot.shrc
        though is not that very critical.

        --- dot.shrc.orig       Thu Nov  3 08:11:13 2005
        +++ dot.shrc    Tue Nov 22 08:23:38 2005
        @@ -36,12 +36,8 @@
         # alias rm='rm -i'


        -# # set prompt: ``username@hostname$ ''
        -# PS1="`whoami`@`hostname | sed 's/\..*//'`"
        -# case `id -u` in
        -#      0) PS1="${PS1}# ";;
        -#      *) PS1="${PS1}$ ";;
        -# esac
        +# # set prompt: '[username@hostname cwdtail]$|# '
        +#PS1="[\u@\h \W]\$ "

         # search path for cd(1)
         # CDPATH=.:$HOME

>Release-Note:
>Audit-Trail:

From: Jilles Tjoelker <jilles@stack.nl>
To: bug-followup@FreeBSD.org, bsd@rubyforge.org
Cc:  
Subject: Re: bin/89410: [PATCH] sh(1) missing \u interpolation and bug/fix in \W
Date: Wed, 1 Feb 2006 23:58:10 +0100

 Some comments about the patch:
 - You should not call getpwuid() for every prompt, perhaps use $USER
   instead for the \u expansion
 - \w/\W should use more something like $PWD (leave the symlinks) and
   just compare with $HOME (bash3 does the same). This needs wider
   changes over the code though.
 - The code in HEAD seems to have some other bugfixes since you patched
   it.
 - Your dot.shrc patch looks way too redhatty :P
 
 -- 
 Jilles Tjoelker

From: Jaakko Heinonen <jh@saunalahti.fi>
To: stefanf@FreeBSD.org
Cc: bug-followup@FreeBSD.org
Subject: Re: bin/89410: [PATCH] sh(1) missing \u interpolation and bug/fix
	in \W
Date: Tue, 26 Feb 2008 11:18:16 +0200

 --Nq2Wo0NMKNjxTN9z
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: inline
 
 
 Hi Stefan,
 
 Here's a bug fix isolated from the patch in bin/89410. Could you commit
 this patch if you think it's OK? The patch fixes \W handling in prompt
 string for root direcrory.
 
 Thanks.
 -- 
 Jaakko
 
 --Nq2Wo0NMKNjxTN9z
 Content-Type: text/x-diff; charset=us-ascii
 Content-Disposition: attachment; filename="sh-prompt-W.diff"
 
 PR: bin/89410
 
 before:
 $ PS1="\W \$ "
 jaakko $ cd /
 whoami
 jaakko
 
 after:
 $ PS1="\W \$ "
 jaakko $ cd /
 / $ whoami
 jaakko
 / $ 
 
 Index: parser.c
 ===================================================================
 RCS file: /home/ncvs/src/bin/sh/parser.c,v
 retrieving revision 1.58
 diff -p -u -r1.58 parser.c
 --- parser.c	5 Nov 2006 18:36:05 -0000	1.58
 +++ parser.c	25 Feb 2008 16:52:37 -0000
 @@ -1621,7 +1621,7 @@ getprompt(void *unused __unused)
  			case 'w':
  				ps[i] = '\0';
  				getcwd(&ps[i], PROMPTLEN - i);
 -				if (*fmt == 'W') {
 +				if (*fmt == 'W' && ps[i + 1] != '\0') {
  					/* Final path component only. */
  					trim = 1;
  					for (j = i; ps[j] != '\0'; j++)
 
 --Nq2Wo0NMKNjxTN9z--

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/89410: commit references a PR
Date: Wed, 24 Feb 2010 14:20:10 +0000 (UTC)

 Author: jh
 Date: Wed Feb 24 14:19:56 2010
 New Revision: 204276
 URL: http://svn.freebsd.org/changeset/base/204276
 
 Log:
   Fix expansion of \W in prompt strings when the working directory is "/".
   The prompt string was truncated after \W when the working directory was "/".
   
   PR:		bin/89410
   Submitted by:	Dr Balwinder Singh Dheeman
   MFC after:	1 week
 
 Modified:
   head/bin/sh/parser.c
 
 Modified: head/bin/sh/parser.c
 ==============================================================================
 --- head/bin/sh/parser.c	Wed Feb 24 13:13:29 2010	(r204275)
 +++ head/bin/sh/parser.c	Wed Feb 24 14:19:56 2010	(r204276)
 @@ -1641,7 +1641,7 @@ getprompt(void *unused __unused)
  			case 'w':
  				ps[i] = '\0';
  				getcwd(&ps[i], PROMPTLEN - i);
 -				if (*fmt == 'W') {
 +				if (*fmt == 'W' && ps[i + 1] != '\0') {
  					/* Final path component only. */
  					trim = 1;
  					for (j = i; ps[j] != '\0'; j++)
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/89410: commit references a PR
Date: Wed,  3 Mar 2010 16:06:52 +0000 (UTC)

 Author: jh
 Date: Wed Mar  3 16:06:43 2010
 New Revision: 204637
 URL: http://svn.freebsd.org/changeset/base/204637
 
 Log:
   MFC r204276:
   
   Fix expansion of \W in prompt strings when the working directory is "/".
   The prompt string was truncated after \W when the working directory was "/".
   
   PR:		bin/89410
 
 Modified:
   stable/8/bin/sh/parser.c
 Directory Properties:
   stable/8/bin/sh/   (props changed)
 
 Modified: stable/8/bin/sh/parser.c
 ==============================================================================
 --- stable/8/bin/sh/parser.c	Wed Mar  3 15:43:26 2010	(r204636)
 +++ stable/8/bin/sh/parser.c	Wed Mar  3 16:06:43 2010	(r204637)
 @@ -1631,7 +1631,7 @@ getprompt(void *unused __unused)
  			case 'w':
  				ps[i] = '\0';
  				getcwd(&ps[i], PROMPTLEN - i);
 -				if (*fmt == 'W') {
 +				if (*fmt == 'W' && ps[i + 1] != '\0') {
  					/* Final path component only. */
  					trim = 1;
  					for (j = i; ps[j] != '\0'; j++)
 _______________________________________________
 svn-src-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/svn-src-all
 To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org"
 
State-Changed-From-To: open->patched 
State-Changed-By: eadler 
State-Changed-When: Tue Mar 1 10:14:30 EST 2011 
State-Changed-Why:  
committed in head 

http://www.freebsd.org/cgi/query-pr.cgi?pr=89410 
Responsible-Changed-From-To: freebsd-bugs->jh 
Responsible-Changed-By: eadler 
Responsible-Changed-When: Tue Mar 1 10:17:41 EST 2011 
Responsible-Changed-Why:  


http://www.freebsd.org/cgi/query-pr.cgi?pr=89410 
State-Changed-From-To: patched->closed 
State-Changed-By: jh 
State-Changed-When: Wed Mar 2 15:34:00 UTC 2011 
State-Changed-Why:  
The bugfix part has been committed to head and stable/8. Seems that 
there is no interest to add support for u and $USER may be used 
instead. 

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