From nobody Mon Dec 18 05:54:26 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4StptV6R5Bz53vyw; Mon, 18 Dec 2023 05:54:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4StptV629Vz4CwR; Mon, 18 Dec 2023 05:54:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702878866; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DGWKpBKqKk44wNnjPy5Wm/1r53BTZJ+P+UmWC5lRQxk=; b=UPGUxZoOkpg2Et9eqDNWMhy8f8t7v+7YOpBv1k7MMWNIMeTckMRRkcKDT3Zj4V18s/OAHJ F6/G/1+1fJTQ+xxZxSY7GYFcyRt2jsPPUZA7WfrOw8hC8byCWbUZLKFrxHLGLFw53JCiSy kN2Em/QtzR1H2zS/P5W6XjdPaMZuvWkM27cFsWBP31yBmslHzKbBpxRNTQDxW2UreaReum xyuwRAysyP5b50cF5GQl2qhJ4zBM1rb4ZfthZVT5YH/2IRVdmV0KGYhu9z5HgxWBTDI/ex YkWw9qJ1T3tplqw3LaDOTnnxbszqBsAFLsT0DANFfd/9pojgh86xzh5TE6ZuWA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1702878866; a=rsa-sha256; cv=none; b=Bt3fJU0BaF0Hp6FByG9IOlib57ZaZ+nrD3Nste4MZsETFBb5QHlgdZ822YHlPWKcB17NR+ R4xfGUrbaQVFlIC0TOrZvba1y61qBrYfnLuTDaoTGbnY5xm3iSzTz6sQiVN0XgqqfTu9fi eGF3YPXYyMavazQXuMJr0XRjyBSsHX1vsWxc8raj5R5aUqtv922naunZve1TVcjYtTa2N8 Xe31TiONT7rLG6FLaclFjDQhIpNnUpvUxF8fpgQ3aEBx9XGJEfj1m5Sy1AdzG9IOyVXn55 lyP87kFe4w/ziv1t+j+1ZKzTiqwsuUST8utppSM6N4JuG9GxXJhjaFtXrLqzEA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1702878866; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=DGWKpBKqKk44wNnjPy5Wm/1r53BTZJ+P+UmWC5lRQxk=; b=rcltzNG2PZWkMvwEk5v4C/nXrznZTWcHiDdlR8BvCxVMF0oIREFzNwl+rYVqS4+oZALIHP L83zirRqd2Qm5KwdnCY1Xzhh6TULbep6T05+mf0zphlPrl1g8QAEGI6UPJ3obsf//87klX 8jugAdp6PuehCvC1lhPtiMqVpthU4gQ9BGIKH3QXLXtFN+501vYOii1PzmrFBemNLsdSSj xqBvnXX1FKG670YFTEbnR+COEN5MCx9fUuC1iIWVDXAMyx8WUj7YehyGQOSmd3T6bY4NWq Jm5O+hwFDjJDXpkwoc2fPWfjtGHi0VP5DNlVCK1OxT29oFiKkJqhmKiLRTjytg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4StptV51Htz1840; Mon, 18 Dec 2023 05:54:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 3BI5sQss047972; Mon, 18 Dec 2023 05:54:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 3BI5sQkX047969; Mon, 18 Dec 2023 05:54:26 GMT (envelope-from git) Date: Mon, 18 Dec 2023 05:54:26 GMT Message-Id: <202312180554.3BI5sQkX047969@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Kyle Evans Subject: git: 0a82cd4f101c - main - calendar: correct the search order for files List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kevans X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0a82cd4f101c5eb8533a0049aaa3f06f005cf8af Auto-Submitted: auto-generated The branch main has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=0a82cd4f101c5eb8533a0049aaa3f06f005cf8af commit 0a82cd4f101c5eb8533a0049aaa3f06f005cf8af Author: Kyle Evans AuthorDate: 2023-12-18 05:53:03 +0000 Commit: Kyle Evans CommitDate: 2023-12-18 05:53:51 +0000 calendar: correct the search order for files Include files that don't begin with a '/' are documented to search the current directory, then /usr/share/calendar. This hasn't been accurate for years, since e061f95e7b9b ("Rework calendar(1) parser") rewrote a lot of this. Stash off the cwd before we do any chdir()ing around and use that to honor the same order we'll follow for the -f flag. This may result in an extra lookup that will fail for the initial calendar file, but I don't think it's worth the complexity to avoid it. While we're here, fix the documentation to just reference the order described in FILES so that we only need to keep it up to date in one place. Reviewed by: bapt Sponsored by: Klara, Inc. Differential Revision: https://reviews.freebsd.org/D42278 --- usr.bin/calendar/calendar.1 | 7 +++---- usr.bin/calendar/io.c | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/usr.bin/calendar/calendar.1 b/usr.bin/calendar/calendar.1 index af368475e4d5..edb3aca48c92 100644 --- a/usr.bin/calendar/calendar.1 +++ b/usr.bin/calendar/calendar.1 @@ -25,7 +25,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd July 31, 2022 +.Dd December 17, 2023 .Dt CALENDAR 1 .Os .Sh NAME @@ -213,9 +213,8 @@ succeeding lines. .Pp If the shared file is not referenced by a full pathname, .Nm -searches in the current (or home) directory first, and then in the -directory -.Pa /usr/share/calendar . +searches in the same order of precedence described in +.Sx FILES . .Pp Blank lines and text protected by the C comment syntax .Ql /* ... */ diff --git a/usr.bin/calendar/io.c b/usr.bin/calendar/io.c index 978027895dfe..17cd5b852c09 100644 --- a/usr.bin/calendar/io.c +++ b/usr.bin/calendar/io.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -102,9 +103,11 @@ trimlr(char **buf) static FILE * cal_fopen(const char *file) { + static int cwdfd = -1; FILE *fp; char *home = getenv("HOME"); unsigned int i; + int fd; struct stat sb; static bool warned = false; static char calendarhome[MAXPATHLEN]; @@ -114,6 +117,34 @@ cal_fopen(const char *file) return (NULL); } + /* + * On -a runs, we would have done a chdir() earlier on, but we also + * shouldn't have used the initial cwd anyways lest we bring + * unpredictable behavior upon us. + */ + if (!doall && cwdfd == -1) { + cwdfd = open(".", O_DIRECTORY | O_PATH); + if (cwdfd == -1) + err(1, "open(cwd)"); + } + + /* + * Check $PWD first as documented. + */ + if (cwdfd != -1) { + if ((fd = openat(cwdfd, file, O_RDONLY)) != -1) { + if ((fp = fdopen(fd, "r")) == NULL) + err(1, "fdopen(%s)", file); + + cal_home = NULL; + cal_dir = NULL; + cal_file = file; + return (fp); + } else if (errno != ENOENT && errno != ENAMETOOLONG) { + err(1, "open(%s)", file); + } + } + if (chdir(home) != 0) { warnx("Cannot enter home directory \"%s\"", home); return (NULL);