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=-1.0 required=5.0 tests=DKIM_ADSP_CUSTOM_MED, FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 15708 invoked from network); 26 Jun 2020 17:33:32 -0000 Received: from ns1.primenet.com.au (HELO primenet.com.au) (203.24.36.2) by inbox.vuxu.org with ESMTPUTF8; 26 Jun 2020 17:33:32 -0000 Received: (qmail 27913 invoked by alias); 26 Jun 2020 17:33:26 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: List-Unsubscribe: Sender: zsh-workers@zsh.org X-Seq: 46127 Received: (qmail 25216 invoked by uid 1010); 26 Jun 2020 17:33:26 -0000 X-Qmail-Scanner-Diagnostics: from mail-ot1-f68.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.102.3/25850. spamassassin: 3.4.4. Clear:RC:0(209.85.210.68):SA:0(-1.7/5.0):. Processed in 1.972372 secs); 26 Jun 2020 17:33:26 -0000 X-Envelope-From: phy1729@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.210.68 as permitted sender) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mail-followup-to :references:mime-version:content-disposition:in-reply-to; bh=/o3eikUKdOen1aSyZnsRILz12HkLCqazvwNWjLjfO+A=; b=tRjL/MFth24G9zUmRg5djsO9R4gZ0IjmV2USnoJEKz823VPKCYYsHcGCzdZGxSCOqO aEcYuOlSaNu/Kocjn6msRRYCcJ6pn7iBFgZw1NMlVjy5Jx5G0E/ExNhF0DNH2V17QNQa ApPha+nefJHc3VzNdPIPKQpG1YGsohb61G7v8QsX32C6vvEblNL/QJjRxlaTKR7FngQC m46Vjda9Y9JWBM5PpW4YntPkO7M5P16Gf0Jn2yQPn4QgFz9TP1ZU1ckAiW5P1T/YwZmn yLXggDoLczhkIV25hN8M9fdt6f3a5oNPanhBXjPEBtACbAD+cqKG5Jhrsrp9aR+VT7hw m0Cw== X-Gm-Message-State: AOAM532qaUBV3HtjN5aXA9BZRRjLKGtUKEfkyPHdCeluYV6bk9MoF6r0 3F3sCwsEg3rpw8BgM0kf1dgP1aEhHzo= X-Google-Smtp-Source: ABdhPJwq1z65JfUGvVGyMmd+xYt1HJAnXhTEtVLNx77mZPfWb1RSu4b67WiM/m/SiIXs1kiIZJOYdQ== X-Received: by 2002:a05:6830:18c8:: with SMTP id v8mr3408151ote.119.1593192771303; Fri, 26 Jun 2020 10:32:51 -0700 (PDT) Date: Fri, 26 Jun 2020 12:32:48 -0500 From: Matthew Martin To: zsh-workers@zsh.org Subject: Re: cd does not update $PWD in a timely fashion. Message-ID: <20200626173248.GA31933@CptOrmolo.darkstar> Mail-Followup-To: zsh-workers@zsh.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Fri, Jun 26, 2020 at 07:24:21PM +0200, Han Boetes wrote: > I had this program changing the name of the current working directory and > noticed cd .; echo $PWD didn't pick up the changes. Was first discussed in IRC, so I had a head start on the patch. bin_cd may update pwd after calling cd_new_pwd which calls set_pwd_env. Since bin_cd is the only caller of cd_new_pwd, I think it's safe to move that code into cd_new_pwd prior to the oldpwd, pwd, new_pwd shift. This should also call the chpwd hook with the right value. Seems to fix the issue locally. I know Daniel will rightfully ask for a new test, so I'll work on that tonight. diff --git a/Src/builtin.c b/Src/builtin.c index 770930579..ff84ce936 100644 --- a/Src/builtin.c +++ b/Src/builtin.c @@ -841,7 +841,6 @@ int bin_cd(char *nam, char **argv, Options ops, int func) { LinkNode dir; - struct stat st1, st2; if (isset(RESTRICTED)) { zwarnnam(nam, "restricted"); @@ -860,23 +859,6 @@ bin_cd(char *nam, char **argv, Options ops, int func) } cd_new_pwd(func, dir, OPT_ISSET(ops, 'q')); - if (stat(unmeta(pwd), &st1) < 0) { - setjobpwd(); - zsfree(pwd); - pwd = NULL; - pwd = metafy(zgetcwd(), -1, META_DUP); - } else if (stat(".", &st2) < 0) { - if (chdir(unmeta(pwd)) < 0) - zwarn("unable to chdir(%s): %e", pwd, errno); - } else if (st1.st_ino != st2.st_ino || st1.st_dev != st2.st_dev) { - if (chasinglinks) { - setjobpwd(); - zsfree(pwd); - pwd = NULL; - pwd = metafy(zgetcwd(), -1, META_DUP); - } else if (chdir(unmeta(pwd)) < 0) - zwarn("unable to chdir(%s): %e", pwd, errno); - } unqueue_signals(); return 0; } @@ -1210,6 +1192,7 @@ static void cd_new_pwd(int func, LinkNode dir, int quiet) { char *new_pwd, *s; + struct stat st1, st2; int dirstacksize; if (func == BIN_PUSHD) @@ -1239,6 +1222,22 @@ cd_new_pwd(int func, LinkNode dir, int quiet) } } + if (stat(unmeta(new_pwd), &st1) < 0) { + zsfree(new_pwd); + new_pwd = NULL; + new_pwd = metafy(zgetcwd(), -1, META_DUP); + } else if (stat(".", &st2) < 0) { + if (chdir(unmeta(new_pwd)) < 0) + zwarn("unable to chdir(%s): %e", new_pwd, errno); + } else if (st1.st_ino != st2.st_ino || st1.st_dev != st2.st_dev) { + if (chasinglinks) { + zsfree(new_pwd); + new_pwd = NULL; + new_pwd = metafy(zgetcwd(), -1, META_DUP); + } else if (chdir(unmeta(new_pwd)) < 0) + zwarn("unable to chdir(%s): %e", new_pwd, errno); + } + /* shift around the pwd variables, to make oldpwd and pwd relate to the current (i.e. new) pwd */ zsfree(oldpwd);