Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

[PATCH] cd: Append 2nd argument to CWD if 1st is empty



Hello,

Since this could break some scripts relying on that behavior, the change could
also be put behind an option that is off by default. I first wanted to hear what
the general opinion on this change is, but would happily send a v2, if
requested.

Thanks,
Julian


From fdc9b7401d10d37a927394e782071af933469a20 Mon Sep 17 00:00:00 2001
From: Julian Prein <druckdev@xxxxxxxxxxxxxx>
Date: Thu, 30 Nov 2023 19:12:33 +0100
Subject: [PATCH] cd: Append 2nd argument to CWD if 1st is empty

In cd's second form:

    cd [ -qsLP ] old new

Currently when old is empty, cd will prepend new to the name of the
current directory. As this has very limited use cases (e.g.
`cd '' /proc` when in `/sys/...`), change the behaviour of cd to
**append** new instead.
---
 Doc/Zsh/builtins.yo | 3 ++-
 Src/builtin.c       | 2 ++
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 8f310f6cf591..8694298aaeac 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -287,7 +287,8 @@ directory hash table.
 

 The second form of tt(cd) substitutes the string var(new)
 for the string var(old) in the name of the current directory,
-and tries to change to this new directory.
+and tries to change to this new directory. If var(old) is empty, var(new)
+is appended to the name of the current directory.
 

 The third form of tt(cd) extracts an entry from the directory
 stack, and changes to that directory.  An argument of the form
diff --git a/Src/builtin.c b/Src/builtin.c
index 9e08a1dbce99..dad9ff745a5c 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -921,6 +921,8 @@ cd_get_dest(char *nam, char **argv, int hard, int func)
 	}
 	len1 = strlen(argv[0]);
 	len2 = strlen(argv[1]);
+	if (!len1)
+	    u = pwd + strlen(pwd);
 	len3 = u - pwd;
 	d = (char *)zalloc(len3 + len2 + strlen(u + len1) + 1);
 	strncpy(d, pwd, len3);
-- 

2.42.1

Attachment: signature.asc
Description: OpenPGP digital signature



Messages sorted by: Reverse Date, Date, Thread, Author