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
This commit is contained in:
Fernando Apesteguía committed 2026-09-26 18:05:53 +02:00
1 parent ce0268cc1e
commit 3963483523
2 files changed
+37 -38

No files matched your search

+26 -10
View File
@@ -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"
+11 -28
View File
@@ -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) {