zsh-workers
 help / color / mirror / code / Atom feed
* [PATCH] cd: Append 2nd argument to CWD if 1st is empty
@ 2023-12-01  3:11 Julian Prein
  2023-12-01  9:21 ` Roman Perepelitsa
  0 siblings, 1 reply; 7+ messages in thread
From: Julian Prein @ 2023-12-01  3:11 UTC (permalink / raw)
  To: zsh-workers


[-- Attachment #1.1: Type: text/plain, Size: 1948 bytes --]

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@protonmail.com>
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 #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 249 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-12-01 18:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-01  3:11 [PATCH] cd: Append 2nd argument to CWD if 1st is empty Julian Prein
2023-12-01  9:21 ` Roman Perepelitsa
2023-12-01 13:32   ` Julian Prein
2023-12-01 13:51     ` Mikael Magnusson
2023-12-01 15:05       ` Julian Prein
2023-12-01 18:21     ` Lawrence Velázquez
2023-12-01 18:27       ` Roman Perepelitsa

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).