From nobody Wed Jan 03 04:48:23 2024 X-Original-To: dev-commits-src-branches@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 4T4cfv4lWwz54cR3; Wed, 3 Jan 2024 04:48:23 +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 4T4cfv3Xrhz3CPW; Wed, 3 Jan 2024 04:48:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1704257303; 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=71W1228l0tufRK5FrkaOgk+6k+feNM5abbU0cl/wQqY=; b=p+uVScyecKDw+xG0OYKAaZCgu9NDq4Xku7QkGKDlvrJouq1sX7ubxVeqLW4g8FOjmUhYSh XoV3s0HTrOE7V/JUHTI/0LQXqmgWt/MlfO4NtqZtRZtoasKqkc6gh7gKen1dGdLBLY/zdG rwkXg+fKY3EopSObLin0tbvnj2hx4/QCR3I8vX87i0ZIRt42PP8/K/opErfUNJkWH14spb myyHYcb8KuS6OLKqHEP7IcI7OgPdF1SSZWw/jmreagLzwCxIOyTuZR6L1QU3y6F1DXvGWd 6BeyZDV7dGT90TBCF9oNPhen/ynigYmWkEBnGn99knmzWfgTnjzYMo21BMUDMQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1704257303; 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=71W1228l0tufRK5FrkaOgk+6k+feNM5abbU0cl/wQqY=; b=l2TvVBwVoF1ChS3S+16X9Hos+A+ZeuuCx9rwnVMXZpCCQ95sInXb58mJFQGBsDYD9Ekfbs vXexPZukReODlFUYKDswLazxeZ7+syw4ugETBPRgQ4/X2zuL5IvBEZ0G4EDmRpGxquseQw 8CRnkUXtkVNluB7hPywODb6eu3+vE8tIjUKM8npeZVfULkeES7+IKoc+0PODTSwUzjmyLi OnjyEVfp011zU09ZijqPdpbVVteo9Ro2ngF7XxMDe9M4+2hhHDhrWU9tA39AX/jMTf33MB kTDALCRGru40XLB54PNjFMQdjF8OV1f1CK9BKy2egQWijm9sgDtU1DaG7TLelA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1704257303; a=rsa-sha256; cv=none; b=KGOiOXUmQY00BGrXGcTglI+2v1CFIDC/i2SLd8qEqRbzgz98BHdG5Vg5GWRzNDr8AA0jWO MBvrQb61uRiTGnf28jwfK8I3sYfwGRaxcPZed9cZGD+Iv62SwpxmPpvpl9breQ/SBRtd5g UoVRGPIaBtKUkG0yOpmq8Sd4X/IC3EGjQuz0hNWp66t3jvcDkfHVa+8oOwk9S07bodXrPl b2cwoYkNGklzqBlzDZMcoUcj0RfIXaBLIKHmLQ+oWc+Q/30dZjYOuatmZ8xNaaKvyPoGRd 27qnGpHKp5fMJPukdmrK9lPlxUj6/L3HYho9qcZQB7ZtPqKhkSpFy67KhZgf5w== 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 4T4cfv2dmbzX0g; Wed, 3 Jan 2024 04:48:23 +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 4034mNkj026392; Wed, 3 Jan 2024 04:48:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 4034mNTr026389; Wed, 3 Jan 2024 04:48:23 GMT (envelope-from git) Date: Wed, 3 Jan 2024 04:48:23 GMT Message-Id: <202401030448.4034mNTr026389@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kyle Evans Subject: git: bcfe05c6b683 - stable/14 - calendar: correct the search order for files List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-branches@freebsd.org X-BeenThere: dev-commits-src-branches@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/stable/14 X-Git-Reftype: branch X-Git-Commit: bcfe05c6b6835d359025fb27f064c6aab537e7b4 Auto-Submitted: auto-generated The branch stable/14 has been updated by kevans: URL: https://cgit.FreeBSD.org/src/commit/?id=bcfe05c6b6835d359025fb27f064c6aab537e7b4 commit bcfe05c6b6835d359025fb27f064c6aab537e7b4 Author: Kyle Evans AuthorDate: 2023-12-18 05:53:03 +0000 Commit: Kyle Evans CommitDate: 2024-01-03 04:47:22 +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. (cherry picked from commit 0a82cd4f101c5eb8533a0049aaa3f06f005cf8af) --- 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 898ef8fb4a3d..3b1e6a420d15 100644 --- a/usr.bin/calendar/calendar.1 +++ b/usr.bin/calendar/calendar.1 @@ -27,7 +27,7 @@ .\" .\" @(#)calendar.1 8.1 (Berkeley) 6/29/93 .\" -.Dd July 31, 2022 +.Dd December 17, 2023 .Dt CALENDAR 1 .Os .Sh NAME @@ -215,9 +215,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 5390882305a0..d407addd9b89 100644 --- a/usr.bin/calendar/io.c +++ b/usr.bin/calendar/io.c @@ -48,6 +48,7 @@ static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; #include #include #include +#include #include #include #include @@ -115,9 +116,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]; @@ -127,6 +130,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);