git: 3963483523a4 - main - whereis(1): Simplify code

From: Fernando Apesteguía <fernape_at_FreeBSD.org>
Date: Sat, 26 Sep 2026 16:07:06 UTC
The branch main has been updated by fernape:

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

commit 3963483523a422e8644ded8d079f82949fd5a8fb
Author:     Fernando Apesteguía <fernape@FreeBSD.org>
AuthorDate: 2026-09-06 16:30:03 +0000
Commit:     Fernando Apesteguía <fernape@FreeBSD.org>
CommitDate: 2026-09-26 16:05:53 +0000

    whereis(1): Simplify code
    
    Simplify the way we build paths.
    Avoid decolonification of source paths.
    Remove gnu directories and add non-tracked ones.
    
    Fix a memory leak while here.
    
    Approved by:            ngie@
    Differential Revision:  https://reviews.freebsd.org/D59846
---
 usr.bin/whereis/pathnames.h | 36 ++++++++++++++++++++++++++----------
 usr.bin/whereis/whereis.c   | 39 +++++++++++----------------------------
 2 files changed, 37 insertions(+), 38 deletions(-)

diff --git a/usr.bin/whereis/pathnames.h b/usr.bin/whereis/pathnames.h
index b7ac47ca93e9..07de74c63463 100644
--- a/usr.bin/whereis/pathnames.h
+++ b/usr.bin/whereis/pathnames.h
@@ -25,20 +25,36 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <stdio.h>
+
 /* Where to look for libexec */
 #define PATH_LIBEXEC "/usr/libexec"
 
 /* Where to look for sources. */
-#define PATH_SOURCES					\
-"/usr/src/bin:/usr/src/usr.bin:/usr/src/sbin:"		\
-"/usr/src/usr.sbin:/usr/src/libexec:"			\
-"/usr/src/gnu/bin:/usr/src/gnu/usr.bin:"		\
-"/usr/src/gnu/sbin:/usr/src/gnu/usr.sbin:"		\
-"/usr/src/contrib:"					\
-"/usr/src/secure/bin:/usr/src/secure/usr.bin:"		\
-"/usr/src/secure/sbin:/usr/src/secure/usr.sbin:"	\
-"/usr/src/secure/libexec:/usr/src/crypto:"		\
-"/usr/src/games"
+static const char *sourcepaths[] = {
+	"/usr/src/bin",
+	"/usr/src/cddl/usr.bin",
+	"/usr/src/cddl/usr.sbin",
+	"/usr/src/contrib",
+	"/usr/src/crypto",
+	"/usr/src/games",
+	"/usr/src/kerberos5/libexec",
+	"/usr/src/kerberos5/usr.bin",
+	"/usr/src/kerberos5/usr.sbin",
+	"/usr/src/krb5/libexec",
+	"/usr/src/krb5/usr.bin",
+	"/usr/src/krb5/usr.sbin",
+	"/usr/src/libexec",
+	"/usr/src/sbin",
+	"/usr/src/secure/bin",
+	"/usr/src/secure/libexec",
+	"/usr/src/secure/sbin",
+	"/usr/src/secure/usr.bin",
+	"/usr/src/secure/usr.sbin",
+	"/usr/src/usr.bin",
+	"/usr/src/usr.sbin",
+	NULL
+};
 
 /* Each subdirectory of PATH_PORTS will be appended to PATH_SOURCES. */
 #define PATH_PORTS "/usr/ports"
diff --git a/usr.bin/whereis/whereis.c b/usr.bin/whereis/whereis.c
index 21b7c11cfb02..5f8aeb1cdbc1 100644
--- a/usr.bin/whereis/whereis.c
+++ b/usr.bin/whereis/whereis.c
@@ -59,8 +59,6 @@ static int opt_a, opt_b, opt_m, opt_q, opt_s, opt_u, opt_x;
 static ccharp *bindirs, *mandirs, *sourcedirs;
 static char **query;
 
-static const char *sourcepath = PATH_SOURCES;
-
 static char	*colonify(ccharp *);
 static int	 contains(ccharp *, const char *);
 static void	 decolonify(char *, ccharp **, int *);
@@ -309,11 +307,10 @@ defaults(void)
 	/* -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)
-			abort();
-		nele = 0;
-		decolonify(b, &sourcedirs, &nele);
+		size_t sp_len = strlen(*sourcepaths);
+		sourcedirs = malloc(sp_len * sizeof (char *));
+		for (size_t i = 0; i < sp_len; i++)
+			sourcedirs[i] = sourcepaths[i];
 
 		path_ports = getenv("PORTSDIR");
 		if (path_ports == NULL)
@@ -354,12 +351,8 @@ defaults(void)
 			    (dirp->d_name[0] >= 'A' && dirp->d_name[0] <= 'Z') ||
 			    strcmp(dirp->d_name, "distfiles") == 0)
 				continue;
-			if ((b = malloc(strlen(path_ports) + 1 + dirp->d_namlen + 1))
-			    == NULL)
+			if (asprintf(&b, "%s/%s", path_ports, dirp->d_name) < 0)
 				abort();
-			strcpy(b, path_ports);
-			strcat(b, "/");
-			strcat(b, dirp->d_name);
 			if (stat(b, &sb) == -1 ||
 			    (sb.st_mode & S_IFMT) != S_IFDIR ||
 			    access(b, R_OK | X_OK) != 0) {
@@ -405,7 +398,9 @@ main(int argc, char **argv)
 		errx(EX_DATAERR, "no directories to search");
 
 	if (opt_m) {
-		setenv("MANPATH", colonify(mandirs), 1);
+		char *mp = colonify(mandirs);
+		setenv("MANPATH", mp, 1);
+		free(mp);
 		if ((i = regcomp(&re, MANWHEREISMATCH, REG_EXTENDED)) != 0) {
 			regerror(i, &re, buf, BUFSIZ - 1);
 			errx(EX_UNAVAILABLE, "regcomp(%s) failed: %s",
@@ -446,12 +441,8 @@ main(int argc, char **argv)
 			 */
 			unusual = unusual | NO_BIN_FOUND;
 			for (dp = bindirs; *dp != NULL; dp++) {
-				cp = malloc(strlen(*dp) + 1 + s + 1);
-				if (cp == NULL)
+				if (asprintf(&cp, "%s/%s", *dp, name) < 0)
 					abort();
-				strcpy(cp, *dp);
-				strcat(cp, "/");
-				strcat(cp, name);
 				if (stat(cp, &sb) == 0 &&
 				    (sb.st_mode & S_IFMT) == S_IFREG &&
 				    (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
@@ -559,12 +550,8 @@ main(int argc, char **argv)
 			 */
 			unusual = unusual | NO_SRC_FOUND;
 			for (dp = sourcedirs; *dp != NULL; dp++) {
-				cp = malloc(strlen(*dp) + 1 + s + 1);
-				if (cp == NULL)
+				if (asprintf(&cp, "%s/%s", *dp, name) < 0)
 					abort();
-				strcpy(cp, *dp);
-				strcat(cp, "/");
-				strcat(cp, name);
 				if (stat(cp, &sb) == 0 &&
 				    (sb.st_mode & S_IFMT) == S_IFDIR) {
 					unusual = unusual & ~NO_SRC_FOUND;
@@ -615,12 +602,8 @@ main(int argc, char **argv)
 				for (dp = sourcedirs;
 				     (src == NULL || opt_a) && *dp != NULL;
 				     dp++) {
-					cp2 = malloc(strlen(*dp) + 9);
-					if (cp2 == NULL)
+					if (asprintf(&cp2, "^%s/[^/]+/", *dp) < 0)
 						abort();
-					strcpy(cp2, "^");
-					strcat(cp2, *dp);
-					strcat(cp2, "/[^/]+/");
 					if ((i = regcomp(&re2, cp2,
 							 REG_EXTENDED|REG_NOSUB))
 					    != 0) {