bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: $HOME does not get abbreviated in prompt \w output when there is a t


From: Libor Pechacek
Subject: Re: $HOME does not get abbreviated in prompt \w output when there is a trailing slash
Date: Fri, 23 May 2014 10:32:01 +0200
User-agent: Mutt/1.5.17 (2007-11-01)

On Fri 16-05-14 09:13:34, Chet Ramey wrote:
> On 5/16/14, 5:22 AM, Libor Pechacek wrote:
> 
> > Bash Version: 4.2
> > Patch Level: 46
> > Release Status: release
> > 
> > Description:
> >     Bash prompt always shows full path in prompt instead of tilde
> >     abbreviation when $HOME ends with slash.
> 
> This came up in March, 2012:
> 
> http://lists.gnu.org/archive/html/bug-bash/2012-03/msg00055.html
> 
> My opinion that this is not a bug in bash hasn't changed.  There are a
> number of easy ways to remove this trailing slash.

Proposed patch:

>From da53c262cb7c356a09e5786f01e6c8c8d7301940 Mon Sep 17 00:00:00 2001
From: Libor Pechacek <address@hidden>
Date: Fri, 23 May 2014 09:30:49 +0200
Subject: [PATCH] Make \w and \W tolerate trailing slash in $HOME

Currently \w and \W abbreviate $HOME into tilde only when it ends with a
character different from slash.  This patch makes them tolerate trailing
slashes.
---
 general.c | 5 ++++-
 parse.y   | 8 ++++++--
 y.tab.c   | 6 +++++-
 3 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/general.c b/general.c
index 087689eb74cc..de6093b57995 100644
--- a/general.c
+++ b/general.c
@@ -699,7 +699,10 @@ polite_directory_format (name)
   int l;
 
   home = get_string_value ("HOME");
-  l = home ? strlen (home) : 0;
+
+  /* remove trailing slashes from $HOME before comparisons */
+  for (l = home ? strlen (home) : 0; l > 1 && home[l-1] == '/'; l--);
+
   if (l > 1 && strncmp (home, name, l) == 0 && (!name[l] || name[l] == '/'))
     {
       strncpy (tdir + 1, name + l, sizeof(tdir) - 2);
diff --git a/parse.y b/parse.y
index 91bf3bf649bd..2abfceb1b6ee 100644
--- a/parse.y
+++ b/parse.y
@@ -5384,7 +5384,7 @@ decode_prompt_string (string)
              {
                /* Use the value of PWD because it is much more efficient. */
                char t_string[PATH_MAX];
-               int tlen;
+               int tlen, l;
 
                temp = get_string_value ("PWD");
 
@@ -5415,7 +5415,11 @@ decode_prompt_string (string)
 #define ROOT_PATH(x)   ((x)[0] == '/' && (x)[1] == 0)
 #define DOUBLE_SLASH_ROOT(x)   ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
                /* Abbreviate \W as ~ if $PWD == $HOME */
-               if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || 
STREQ (t, t_string) == 0))
+               /* remove trailing slashes from $HOME before comparisons */
+               t = get_string_value ("HOME");
+               for (l = t ? strlen (t) : 0; l > 1 && t[l-1] == '/'; l--);
+
+               if (c == 'W' && ((t == 0) || STREQN (t, t_string, l) == 0))
                  {
                    if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT 
(t_string) == 0)
                      {
diff --git a/y.tab.c b/y.tab.c
index 80fe9308398e..d107e5b8dec5 100644
--- a/y.tab.c
+++ b/y.tab.c
@@ -7727,7 +7727,11 @@ decode_prompt_string (string)
 #define ROOT_PATH(x)   ((x)[0] == '/' && (x)[1] == 0)
 #define DOUBLE_SLASH_ROOT(x)   ((x)[0] == '/' && (x)[1] == '/' && (x)[2] == 0)
                /* Abbreviate \W as ~ if $PWD == $HOME */
-               if (c == 'W' && (((t = get_string_value ("HOME")) == 0) || 
STREQ (t, t_string) == 0))
+               /* remove trailing slashes from $HOME before comparisons */
+               t = get_string_value ("HOME");
+               for (l = t ? strlen (t) : 0; l > 1 && t[l-1] == '/'; l--);
+
+               if (c == 'W' && ((t == 0) || STREQN (t, t_string, l) == 0))
                  {
                    if (ROOT_PATH (t_string) == 0 && DOUBLE_SLASH_ROOT 
(t_string) == 0)
                      {
-- 
1.7.12.4




reply via email to

[Prev in Thread] Current Thread [Next in Thread]