git: fc002631af3e - main - whereis(1): Respect PORTSDIR variable

From: Fernando Apesteguía <fernape_at_FreeBSD.org>
Date: Sun, 06 Sep 2026 15:28:36 UTC
The branch main has been updated by fernape:

URL: https://cgit.FreeBSD.org/src/commit/?id=fc002631af3e25c65d74ca6869c13b59f1af8689

commit fc002631af3e25c65d74ca6869c13b59f1af8689
Author:     Fernando Apesteguía <fernape@FreeBSD.org>
AuthorDate: 2026-09-04 18:27:34 +0000
Commit:     Fernando Apesteguía <fernape@FreeBSD.org>
CommitDate: 2026-09-06 15:25:59 +0000

    whereis(1): Respect PORTSDIR variable
    
    Respect PORTSDIR variable for those who have the ports collection in a different
    place than /usr/ports. PORTSDIR is a very common variable used in the ports
    framework and in /etc/make.conf among other places.
    
    While here, remove and old reference to CVS.
    
    Reviewed by:    delphij@, ngie@
    Approved by:    ngie@
    Differential Revision:  https://reviews.freebsd.org/D42156
---
 usr.bin/whereis/whereis.1 |  4 +++-
 usr.bin/whereis/whereis.c | 38 +++++++++++++++++++++-----------------
 2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/usr.bin/whereis/whereis.1 b/usr.bin/whereis/whereis.1
index 90922ce61db5..e6e2074acc40 100644
--- a/usr.bin/whereis/whereis.1
+++ b/usr.bin/whereis/whereis.1
@@ -27,7 +27,7 @@
 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd August 22, 2002
+.Dd September 6, 2026
 .Dt WHEREIS 1
 .Os
 .Sh NAME
@@ -71,6 +71,8 @@ Program sources are located in a list of known standard places,
 including all the subdirectories of
 .Pa /usr/src
 and
+.Ev PORTSDIR
+If the environment variable is not defined it defaults to
 .Pa /usr/ports .
 .Pp
 The following options are available:
diff --git a/usr.bin/whereis/whereis.c b/usr.bin/whereis/whereis.c
index 8de56b9cf654..987ff68de44c 100644
--- a/usr.bin/whereis/whereis.c
+++ b/usr.bin/whereis/whereis.c
@@ -253,6 +253,7 @@ defaults(void)
 {
 	size_t s;
 	char *b, buf[BUFSIZ], *cp;
+	const char *path_ports;
 	int nele;
 	FILE *p;
 	DIR *dir;
@@ -305,7 +306,8 @@ defaults(void)
 		decolonify(b, &mandirs, &nele);
 	}
 
-	/* -s defaults to precompiled list, plus subdirs of /usr/ports */
+	/* -s defaults to precompiled list, plus subdirs of
+	 * $PORTSDIR. If not set, it defaults to /usr/ports */
 	if (!sourcedirs) {
 		b = strdup(sourcepath);
 		if (b == NULL)
@@ -313,32 +315,34 @@ defaults(void)
 		nele = 0;
 		decolonify(b, &sourcedirs, &nele);
 
-		if (stat(PATH_PORTS, &sb) == -1) {
+		path_ports = getenv("PORTSDIR");
+		if (!path_ports)
+			path_ports = PATH_PORTS;
+
+		if (stat(path_ports, &sb) == -1) {
 			if (errno == ENOENT)
-				/* no /usr/ports, we are done */
+				/* no ports directory, we are done */
 				return;
-			err(EX_OSERR, "stat(" PATH_PORTS ")");
+			err(EX_OSERR, "stat(%s)", path_ports);
 		}
 		if ((sb.st_mode & S_IFMT) != S_IFDIR)
-			/* /usr/ports is not a directory, ignore */
+			/* path_ports is not a directory, ignore */
 			return;
-		if (access(PATH_PORTS, R_OK | X_OK) != 0)
+		if (access(path_ports, R_OK | X_OK) != 0)
 			return;
-		if ((dir = opendir(PATH_PORTS)) == NULL)
-			err(EX_OSERR, "opendir" PATH_PORTS ")");
+		if ((dir = opendir(path_ports)) == NULL)
+			err(EX_OSERR, "opendir(%s)", path_ports);
 		while ((dirp = readdir(dir)) != NULL) {
 			/*
-			 * Not everything below PATH_PORTS is of
+			 * Not everything below path_ports is of
 			 * interest.  First, all dot files and
 			 * directories (e. g. .snap) can be ignored.
 			 * Also, all subdirectories starting with a
-			 * capital letter are not going to be
+			 * capital letter are not going to
 			 * examined, as they are used for internal
-			 * purposes (Mk, Tools, ...).  This also
-			 * matches a possible CVS subdirectory.
-			 * Finally, the distfiles subdirectory is also
-			 * special, and should not be considered to
-			 * avoid false matches.
+			 * purposes (Mk, Tools, ...).  Finally, the distfiles
+			 * subdirectory is also special, and should not be
+			 * considered to avoid false matches.
 			 */
 			if (dirp->d_name[0] == '.' ||
 			    /*
@@ -350,10 +354,10 @@ defaults(void)
 			    (dirp->d_name[0] >= 'A' && dirp->d_name[0] <= 'Z') ||
 			    strcmp(dirp->d_name, "distfiles") == 0)
 				continue;
-			if ((b = malloc(sizeof PATH_PORTS + 1 + dirp->d_namlen))
+			if ((b = malloc(strlen(path_ports) + 1 + dirp->d_namlen + 1))
 			    == NULL)
 				abort();
-			strcpy(b, PATH_PORTS);
+			strcpy(b, path_ports);
 			strcat(b, "/");
 			strcat(b, dirp->d_name);
 			if (stat(b, &sb) == -1 ||