From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,T_SCC_BODY_TEXT_LINE, UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 10982 invoked from network); 6 Feb 2022 17:44:44 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 6 Feb 2022 17:44:44 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1644169484; b=ZjmGQ1szw5oOiBpXuw9fMVn2kA5ARnQ2yVzSCgJT9WuerHz9mtECVyJW6NPHHdyK2HHP9tRhTP PrLMUJK8ElQPNp479S6BLnGJU+Ir+cW6aaxq87J8SmCHwJAZ3xmicpQDGmYKE3zjURX94F/AuU Q8wwEtrd6Bw/NytYpv93s5UFIZKtDidR/kLIG0hXLhTN0qAUoDCQD4xuWJs+3BtC8jawK9P0Ee mZY8HLo7AZjzHh2R7r0+3l+wVVrW5r9VAPoXwN/DbSuWmnGjy8hy61Lap0MYIJOlMrbZx6XpD0 5Yjsv/pDZmt2mLxw58gh2kpGliKBTfDZ0sMJfkTpYuKGvQ==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay3-d.mail.gandi.net) smtp.remote-ip=217.70.183.195; dmarc=none header.from=chazelas.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1644169484; bh=3Nanl42HweWRmd2oHmU6ELiqU0ZbgcPQrMHA3GwgMvE=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:MIME-Version:Message-ID:Subject:To:From:Date: DKIM-Signature; b=eagP5meWH5qeUNACsNyekFqeTfvHFHBiCxdDkPOcgqFi9bLdEFKOSCtDFXgvxBzB9dLHaTQQXU DScx3wI6k8VR+uLnAmPIxp4duD5PgYNyv578ULmbL1jBC7hD4y0Dbm53pRerdKsXQi8gu+vXxP bLMtA1fncGKFXH8vgzbz6KAW4IJHMM1fjQON1CBGyWoHkUDEdx1MPLPjJzrVzuAzeb+2RqcyaO FdVI0gNtk27I8nNjGxHZnCG/xdUMzuZfjfjBbHYqSPEWvvRF8cJ6Pa/pF+zdjfnX+2JQATi6L3 Sk2ZPgyk9/0+/9oQYP/HjvnL3teV8w2zM2moN3Vx7GUdow==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:MIME-Version:Message-ID: Subject:To:From:Date:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=NgJ2keb0D+6riFM5VCiozcz64IK/y1GjD6PUArvuLSg=; b=FT9p7s+WT1qYC0APOALnKMBtGR cz7LdDrAHI+L7LYjuMZonyQ/q9ah6uUj86RjfcwdYZgj2u70yT8H60HRO243vsv2Sbde06Rc1NIZe FpPJ823OARotp0mCP1G3khPuFvhQ5DESlvQEfy8tQZJPnfgpp7vEP+bxUZeeU2vEA9aOONXHQ4rKy pnYP/d956FaDzEVXf/hfsbFYJ/j9vWKeAjabXNzOI55MRCHTy96jHBlJYFx7KvCXgiBLRfwpVQWHS 11uz82O1W0S+zLUESbfQfT7CAMvjUaqVisTwGqkdNKbQPkr7d9ilqRwPL557ljxfZqhSN52tNAB3J HXUWttxQ==; Received: from authenticated user by zero.zsh.org with local id 1nGlac-000PWL-LQ; Sun, 06 Feb 2022 17:44:42 +0000 Authentication-Results: zsh.org; iprev=pass (relay3-d.mail.gandi.net) smtp.remote-ip=217.70.183.195; dmarc=none header.from=chazelas.org; arc=none Received: from relay3-d.mail.gandi.net ([217.70.183.195]:42799) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1nGla4-000PDC-2M; Sun, 06 Feb 2022 17:44:08 +0000 Received: (Authenticated sender: stephane@chazelas.org) by mail.gandi.net (Postfix) with ESMTPSA id DF15660003 for ; Sun, 6 Feb 2022 17:44:06 +0000 (UTC) Date: Sun, 6 Feb 2022 17:44:06 +0000 From: Stephane Chazelas To: Zsh hackers list Subject: can we have an option for cd to do a plain chdir() Message-ID: <20220206174406.rirklyrkzlx6winw@chazelas.org> Mail-Followup-To: Zsh hackers list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Seq: 49744 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: I was surprised to see that after: ln -s /proc/self/cwd/.. link; cd link I ended up two directories up instead of one. strace revealed: $ strace -fe getcwd,readlink,chdir,fchdir zsh -c 'cd link' readlink("/proc/self/fd/0", "/dev/pts/4", 4095) = 10 chdir("/home/chazelas/link") = 0 chdir("/home/chazelas/link") = 0 +++ exited with 0 +++ Not sure why the two chdir()s there. That it prepends $PWD to the path is also a problem if the current working directory has been renamed, but most shells do that without -P. It's better in that case with cd -P or with chaselinks: $ strace -fe getcwd,readlink,chdir,fchdir zsh -c 'cd -P link' readlink("/proc/self/fd/0", "/dev/pts/4", 4095) = 10 chdir("/home/chazelas/link") = 0 readlink("/home", 0x7ffe6ba34f00, 4096) = -1 EINVAL (Invalid argument) readlink("/home/chazelas", 0x7ffe6ba34f00, 4096) = -1 EINVAL (Invalid argument) readlink("/home/chazelas/link", "/proc/self/cwd/..", 4096) = 17 readlink("/proc", 0x7ffe6ba2fe70, 4096) = -1 EINVAL (Invalid argument) readlink("/proc/self", "1000438", 4096) = 7 readlink("/proc/1000438", 0x7ffe6ba2ade0, 4096) = -1 EINVAL (Invalid argument) readlink("/proc/1000438/cwd", "/home", 4096) = 5 readlink("/home", 0x7ffe6ba2ade0, 4096) = -1 EINVAL (Invalid argument) chdir("..") = 0 chdir("/home") = 0 +++ exited with 0 +++ ~$ strace -fe getcwd,readlink,chdir,fchdir zsh -o chaselinks -c 'cd link' readlink("/proc/self/fd/0", "/dev/pts/4", 4095) = 10 readlink("/home", 0x7ffdfc3e0f40, 4096) = -1 EINVAL (Invalid argument) readlink("/home/chazelas", 0x7ffdfc3e0f40, 4096) = -1 EINVAL (Invalid argument) chdir("/home/chazelas/link") = 0 readlink("/home", 0x7ffdfc3df960, 4096) = -1 EINVAL (Invalid argument) readlink("/home/chazelas", 0x7ffdfc3df960, 4096) = -1 EINVAL (Invalid argument) readlink("/home/chazelas/link", "/proc/self/cwd/..", 4096) = 17 readlink("/proc", 0x7ffdfc3da8d0, 4096) = -1 EINVAL (Invalid argument) readlink("/proc/self", "1002598", 4096) = 7 readlink("/proc/1002598", 0x7ffdfc3d5840, 4096) = -1 EINVAL (Invalid argument) readlink("/proc/1002598/cwd", "/home", 4096) = 5 readlink("/home", 0x7ffdfc3d5840, 4096) = -1 EINVAL (Invalid argument) chdir("..") = 0 chdir("/home") = 0 +++ exited with 0 +++ But that sounds a bit overkill and racy. In scripts, you usually want your cd's to do plain chdir()s but there are many things that get in the way: - the need for -P to avoid the logical traversal - the need to -- as usual for avoid problems with dirnames starting with - - CDPATH (it is inherited from the environment but at least when not in POSIX mode, that doesn't take precedence over actual relative path). - The "-" string that is taken as $OLDPWD - Also in zsh, the -n +n ones. - and then there's that special fancy chasing of symlinks So, to do an actual chdir, we need something like: chdir() { case $1 in (/*) cd -P "$1";; ('') print -u2 "not sure out to chdir to an empty string";; (*) cd -P "./$1";; esac } Which addresses the first 5 points, but not the 6th. It would be nice if we could just do for instance cd -r -- $dir which would just a do chdir($dir) and set $PWD to getcwd() if successful. (or have chdir (which atm exits as a builtin for tcsh junkies I suppose) do that and not take any option). What do you think? -- Stephane