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 29575 invoked from network); 10 Feb 2022 20:27:38 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 10 Feb 2022 20:27:38 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1644524858; b=ADRReiFQEhsl0yob11OdVJQTfK/JFnoNNXn2Q+uhA3RtekqrssuUkBhT6XwkSt4g/UZkZOVEHz 9ivuQk918b4wdB36iCB9U07LDcmANVoE6c4ckG8rnS3xoZYXcSsyrbTXEdmbvNyFdWWlBPkyE9 429ChxkeSsXzpkrQ4HsiYa8Dd2tYi991pgPDYWS1YTBEYVs56dbVVO6USwBkN/+LKIaXr9bL6d iOxeqb2KLU5LHoVliKunZTC+i3nPv/bMzVAxwWzzFCnIxWyrWXg1t1+cCT2vWd39mNXim8ro4/ cKsJBYGjTcrxs551IoM74t2wHwRGUdmUegK30YTKRjlEQw==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (relay9-d.mail.gandi.net) smtp.remote-ip=217.70.183.199; 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=1644524858; bh=1pGrZR8EmnyAWBAohdzcBzax62w45Ezw+tpeFEzA+zM=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:In-Reply-To:Content-Type:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:DKIM-Signature; b=CGrEV35Ojfop4oR7fkdzleE+cbViu4gSXLGQ+gckGhsGbm20dDPfoinkPF80smCbTAZJElrlUG e8+4varCaDK2b+CyFdktDHetG9InREdtOkil8+/AIva355Q4KwHXnmiPFN/XiiKxCghQteCXEG g6NieNaJpJ3Dam5PbD45ug2Q3iQsfOd9SVFzNtyGmCVItwSMc2mjT1Qjh3fYzYg0tDCacrPz/R cApsUkPt9SwSt99HPVnc5PoPLYHQFeSFrukTf4LWZpLn8NWuYigoCklXzAdKZjIvMuxcNOTwaT 2KIHsoqMEu5LZOdcLg19b0Z/0Czu+eSW3Dul9br2ZefJ7Q==; 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:In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID; bh=Ed8q8Un4T/oXfERrgYnXZkGnV4W5weJWSTNTgk+LQYM=; b=sGZiR5BKpQsvrJcUTtn5/pbEWB XpGCyRMb8rligFI8pC32HS974KyDlTm/7a6tE92kHEPOX1Lpin+IIRxzMdKE6F5kXdJaYzq5nqeJo ZNU8SHppYC7J7+hqJivs1XnE1nPtt4NLD/dL3G8l/7EvqsrSTCXCz0RHzJKqx0Kkg+TQk1TKKuyRP zyCXXDVZeMGCrP3fydOx3ZmJzmAgmygatLjBBbpUU2Xh/F8OphEvmG2oZCQZ2kArzWx7na/+4eG/w P2s982822PCGAMhKENoNo+BTpa/TckQmnof/iZK410ZRazOxOKOkUKUAAlEXW62S4sfjvFt6mKQBg xoW8/9fg==; Received: from authenticated user by zero.zsh.org with local id 1nIG2S-000NVL-VV; Thu, 10 Feb 2022 20:27:36 +0000 Authentication-Results: zsh.org; iprev=pass (relay9-d.mail.gandi.net) smtp.remote-ip=217.70.183.199; dmarc=none header.from=chazelas.org; arc=none Received: from relay9-d.mail.gandi.net ([217.70.183.199]:32983) by zero.zsh.org with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) id 1nIG29-000N9V-MF; Thu, 10 Feb 2022 20:27:18 +0000 Received: (Authenticated sender: stephane@chazelas.org) by mail.gandi.net (Postfix) with ESMTPSA id 8012CFF803; Thu, 10 Feb 2022 20:27:16 +0000 (UTC) Date: Thu, 10 Feb 2022 20:27:15 +0000 From: Stephane Chazelas To: Bart Schaefer Cc: Zsh hackers list Subject: Re: can we have an option for cd to do a plain chdir() Message-ID: <20220210202715.56vzsgvuqrnyazmg@chazelas.org> Mail-Followup-To: Bart Schaefer , Zsh hackers list References: <20220206174406.rirklyrkzlx6winw@chazelas.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Seq: 49749 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: 2022-02-09 09:16:46 -0800, Bart Schaefer: > I started looking at this myself a while back but couldn't find a way > to untangle the function dependencies. They're oddly factored and > other parts of the shell depend on some of the bits in the middle. [...] Thanks, that's the impression I had from glancing at the code. But then what about the idea of having an option like -r that bypasses all that and does the simple chdir(). BTW, I've just thought of another way to do the equivalent of a chdir() reliably on Linux provided you have read access to a directory: chdir() { cd /dev/fd/3 3< "$1"; } Beside adding a "cd -r whatever" that just does a chdir(whatever), we could add: - "cd -u 3" which does a fchdir(3) - sysopen -o path -u fd some/path && cd -u "$fd" (or sysopen -pu fd some/path as it's neither a read nor write open) (with -o nofollow, that can help do some safe directory traversal) - sysopen -@ "$fd" -u otherfd some/path for openat() Being able to do fchdir would allow improvements over the less reliable: for dir in $dirs; do cd $dir do-something cd .. done Or for dir in $dirs; do pushd $dir do-something popd done Which we could replace with: savedir() sysopen -po cloexec -u "$1" . chdir_no_follow() { local fd ret sysopen -po nofollow -u fd -- "$1" || return cd -u "$fd"; ret=$? exec {fd}<&- return "$ret" } savedir || exit for dir in $dirs; do chdir_no_follow $dir && do-something cd -u "$fd" done What do you think? -- Stephane