2 * Copyright © 2002, Jörg Wunsch
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
17 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 * POSSIBILITY OF SUCH DAMAGE.
24 * $FreeBSD: src/usr.bin/whereis/whereis.c,v 1.12 2002/08/22 01:50:51 johan Exp $
28 * 4.3BSD UI-compatible whereis(1) utility. Rewritten from scratch
29 * since the original 4.3BSD version suffers legal problems that
30 * prevent it from being redistributed, and since the 4.4BSD version
31 * was pretty inferior in functionality.
34 #include <sys/types.h>
38 #include <sys/sysctl.h>
51 #include "pathnames.h"
53 #define NO_BIN_FOUND 1
54 #define NO_MAN_FOUND 2
55 #define NO_SRC_FOUND 4
57 typedef const char *ccharp;
59 int opt_a, opt_b, opt_m, opt_q, opt_s, opt_u, opt_x;
60 ccharp *bindirs, *mandirs, *sourcedirs;
63 const char *sourcepath = PATH_SOURCES;
65 char *colonify(ccharp *);
66 int contains(ccharp *, const char *);
67 void decolonify(char *, ccharp **, int *);
69 void scanopts(int, char **);
73 * Throughout this program, a number of strings are dynamically
74 * allocated but never freed. Their memory is written to when
75 * splitting the strings into string lists which will later be
76 * processed. Since it's important that those string lists remain
77 * valid even after the functions allocating the memory returned,
78 * those functions cannot free them. They could be freed only at end
79 * of main(), which is pretty pointless anyway.
81 * The overall amount of memory to be allocated for processing the
82 * strings is not expected to exceed a few kilobytes. For that
83 * reason, allocation can usually always be assumed to succeed (within
84 * a virtual memory environment), thus we simply bail out using
85 * abort(3) in case of an allocation failure.
92 "usage: whereis [-abmqsux] [-BMS dir... -f] name ...");
96 * Scan options passed to program.
98 * Note that the -B/-M/-S options expect a list of directory
99 * names that must be terminated with -f.
102 scanopts(int argc, char **argv)
107 while ((c = getopt(argc, argv, "BMSabfmqsux")) != -1)
118 dirlist = &sourcedirs;
121 *dirlist = realloc(*dirlist, (i + 1) * sizeof(char *));
122 (*dirlist)[i] = NULL;
123 while (optind < argc &&
124 strcmp(argv[optind], "-f") != 0 &&
125 strcmp(argv[optind], "-B") != 0 &&
126 strcmp(argv[optind], "-M") != 0 &&
127 strcmp(argv[optind], "-S") != 0) {
128 decolonify(argv[optind], dirlist, &i);
170 query = argv + optind;
174 * Find out whether string `s' is contained in list `cpp'.
177 contains(ccharp *cpp, const char *s)
184 while ((cp = *cpp) != NULL) {
185 if (strcmp(cp, s) == 0)
193 * Split string `s' at colons, and pass it to the string list pointed
194 * to by `cppp' (which has `*ip' elements). Note that the original
195 * string is modified by replacing the colon with a NUL byte. The
196 * partial string is only added if it has a length greater than 0, and
197 * if it's not already contained in the string list.
200 decolonify(char *s, ccharp **cppp, int *ip)
204 while ((cp = strchr(s, ':')), *s != '\0') {
207 if (strlen(s) && !contains(*cppp, s)) {
208 *cppp = realloc(*cppp, (*ip + 2) * sizeof(char *));
212 (*cppp)[*ip + 1] = NULL;
223 * Join string list `cpp' into a colon-separated string.
226 colonify(ccharp *cpp)
235 for (s = 0, i = 0; cpp[i] != NULL; i++)
236 s += strlen(cpp[i]) + 1;
237 if ((cp = malloc(s + 1)) == NULL)
239 for (i = 0, *cp = '\0'; cpp[i] != NULL; i++) {
243 cp[s - 1] = '\0'; /* eliminate last colon */
249 * Provide defaults for all options and directory lists.
255 char *b, buf[BUFSIZ], *cp;
262 /* default to -bms if none has been specified */
263 if (!opt_b && !opt_m && !opt_s)
264 opt_b = opt_m = opt_s = 1;
266 /* -b defaults to default path + /usr/libexec +
267 * /usr/games + user's path */
269 if (sysctlbyname("user.cs_path", NULL, &s, NULL, 0) == -1)
270 err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
271 if ((b = malloc(s + 1)) == NULL)
273 if (sysctlbyname("user.cs_path", b, &s, NULL, 0) == -1)
274 err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
276 decolonify(b, &bindirs, &nele);
277 bindirs = realloc(bindirs, (nele + 3) * sizeof(char *));
280 bindirs[nele++] = PATH_LIBEXEC;
281 bindirs[nele++] = PATH_GAMES;
282 bindirs[nele] = NULL;
283 if ((cp = getenv("PATH")) != NULL) {
284 /* don't destroy the original environment... */
285 if ((b = malloc(strlen(cp) + 1)) == NULL)
288 decolonify(b, &bindirs, &nele);
292 /* -m defaults to $(manpath) */
294 if ((p = popen(MANPATHCMD, "r")) == NULL)
295 err(EX_OSERR, "cannot execute manpath command");
296 if (fgets(buf, BUFSIZ - 1, p) == NULL ||
298 err(EX_OSERR, "error processing manpath results");
299 if ((b = strchr(buf, '\n')) != NULL)
301 if ((b = malloc(strlen(buf) + 1)) == NULL)
305 decolonify(b, &mandirs, &nele);
309 * -s defaults to precompiled list, plus subdirs of /usr/dports and
313 if ((b = malloc(strlen(sourcepath) + 1)) == NULL)
315 strcpy(b, sourcepath);
317 decolonify(b, &sourcedirs, &nele);
319 if (stat(PATH_DPORTS, &sb) == -1) {
321 err(EX_OSERR, "stat(" PATH_DPORTS ")");
323 if ((sb.st_mode & S_IFMT) != S_IFDIR)
324 /* /usr/dports is not a directory, ignore */
326 if (access(PATH_DPORTS, R_OK | X_OK) != 0)
328 if ((dir = opendir(PATH_DPORTS)) == NULL)
329 err(EX_OSERR, "opendir" PATH_DPORTS ")");
330 while ((dirp = readdir(dir)) != NULL) {
331 if (dirp->d_name[0] == '.')
332 /* ignore dot entries */
334 b = malloc(sizeof PATH_DPORTS + 1 +
338 strcpy(b, PATH_DPORTS);
340 strcat(b, dirp->d_name);
341 if (stat(b, &sb) == -1 ||
342 (sb.st_mode & S_IFMT) != S_IFDIR ||
343 access(b, R_OK | X_OK) != 0) {
347 sourcedirs = realloc(sourcedirs,
348 (nele + 2) * sizeof(char *));
349 if (sourcedirs == NULL)
351 sourcedirs[nele++] = b;
352 sourcedirs[nele] = NULL;
356 if (stat(PATH_PKGSRC, &sb) == -1) {
358 /* no /usr/pkgsrc, we are done */
360 err(EX_OSERR, "stat(" PATH_PKGSRC ")");
362 if ((sb.st_mode & S_IFMT) != S_IFDIR)
363 /* /usr/pkgsrc is not a directory, ignore */
365 if (access(PATH_PKGSRC, R_OK | X_OK) != 0)
367 if ((dir = opendir(PATH_PKGSRC)) == NULL)
368 err(EX_OSERR, "opendir" PATH_PKGSRC ")");
369 while ((dirp = readdir(dir)) != NULL) {
370 if (dirp->d_name[0] == '.' ||
371 strcmp(dirp->d_name, "CVS") == 0)
372 /* ignore dot entries and CVS subdir */
374 if ((b = malloc(sizeof PATH_PKGSRC + 1 + dirp->d_namlen))
377 strcpy(b, PATH_PKGSRC);
379 strcat(b, dirp->d_name);
380 if (stat(b, &sb) == -1 ||
381 (sb.st_mode & S_IFMT) != S_IFDIR ||
382 access(b, R_OK | X_OK) != 0) {
386 sourcedirs = realloc(sourcedirs,
387 (nele + 2) * sizeof(char *));
388 if (sourcedirs == NULL)
390 sourcedirs[nele++] = b;
391 sourcedirs[nele] = NULL;
398 main(int argc, char **argv)
400 int unusual, i, printed;
401 char *bin, buf[BUFSIZ], *cp, *cp2, *man, *name, *src;
403 size_t nlen, olen, s;
406 regmatch_t matches[2];
410 setlocale(LC_ALL, "");
411 scanopts(argc, argv);
418 if (sourcedirs == NULL)
420 if (opt_m + opt_b + opt_s == 0)
421 errx(EX_DATAERR, "no directories to search");
424 if (setenv("MANPATH", colonify(mandirs), 1) == -1)
425 err(1, "setenv: cannot set MANPATH=%s", colonify(mandirs));
426 if ((i = regcomp(&re, MANWHEREISMATCH, REG_EXTENDED)) != 0) {
427 regerror(i, &re, buf, BUFSIZ - 1);
428 errx(EX_UNAVAILABLE, "regcomp(%s) failed: %s",
429 MANWHEREISMATCH, buf);
433 for (; (name = *query) != NULL; query++) {
434 /* strip leading path name component */
435 if ((cp = strrchr(name, '/')) != NULL)
437 /* strip SCCS or RCS suffix/prefix */
438 if (strlen(name) > 2 && strncmp(name, "s.", 2) == 0)
440 if ((s = strlen(name)) > 2 && strcmp(name + s - 2, ",v") == 0)
442 /* compression suffix */
445 (strcmp(name + s - 2, ".z") == 0 ||
446 strcmp(name + s - 2, ".Z") == 0))
449 strcmp(name + s - 3, ".gz") == 0)
452 strcmp(name + s - 4, ".bz2") == 0)
456 bin = man = src = NULL;
461 * Binaries have to match exactly, and must be regular
464 unusual = unusual | NO_BIN_FOUND;
465 for (dp = bindirs; *dp != NULL; dp++) {
466 cp = malloc(strlen(*dp) + 1 + s + 1);
472 if (stat(cp, &sb) == 0 &&
473 (sb.st_mode & S_IFMT) == S_IFREG &&
474 (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
476 unusual = unusual & ~NO_BIN_FOUND;
500 * Ask the man command to perform the search for us.
502 unusual = unusual | NO_MAN_FOUND;
504 cp = malloc(sizeof MANWHEREISALLCMD - 2 + s);
506 cp = malloc(sizeof MANWHEREISCMD - 2 + s);
512 sprintf(cp, MANWHEREISALLCMD, name);
514 sprintf(cp, MANWHEREISCMD, name);
516 if ((p = popen(cp, "r")) != NULL) {
518 while (fgets(buf, BUFSIZ - 1, p) != NULL) {
519 unusual = unusual & ~NO_MAN_FOUND;
521 if ((cp2 = strchr(buf, '\n')) != NULL)
523 if (regexec(&re, buf, 2,
525 (rlen = matches[1].rm_eo -
526 matches[1].rm_so) > 0) {
528 * man -w found formated
529 * page, need to pick up
532 cp2 = malloc(rlen + 1);
536 buf + matches[1].rm_so,
541 * man -w found plain source
576 * Sources match if a subdir with the exact
579 unusual = unusual | NO_SRC_FOUND;
580 for (dp = sourcedirs; *dp != NULL; dp++) {
581 cp = malloc(strlen(*dp) + 1 + s + 1);
587 if (stat(cp, &sb) == 0 &&
588 (sb.st_mode & S_IFMT) == S_IFDIR) {
589 unusual = unusual & ~NO_SRC_FOUND;
610 * If still not found, ask locate to search it
611 * for us. This will find sources for things
612 * like lpr that are well hidden in the
613 * /usr/src tree, but takes a lot longer.
614 * Thus, option -x (`expensive') prevents this
617 * Do only match locate output that starts
618 * with one of our source directories, and at
619 * least one further level of subdirectories.
621 if (opt_x || (src && !opt_a))
624 cp = malloc(sizeof LOCATECMD - 2 + s);
627 sprintf(cp, LOCATECMD, name);
628 if ((p = popen(cp, "r")) == NULL)
630 while ((src == NULL || opt_a) &&
631 (fgets(buf, BUFSIZ - 1, p)) != NULL) {
632 if ((cp2 = strchr(buf, '\n')) != NULL)
634 for (dp = sourcedirs;
635 (src == NULL || opt_a) && *dp != NULL;
637 cp2 = malloc(strlen(*dp) + 9);
642 strcat(cp2, "/[^/]+/");
643 if ((i = regcomp(&re2, cp2,
644 REG_EXTENDED|REG_NOSUB))
646 regerror(i, &re, buf,
649 "regcomp(%s) failed: %s",
653 if (regexec(&re2, buf, 0, NULL, 0)
679 if (opt_u && !unusual)