whereis.c revision 141657
199821Sjoerg/*
299821Sjoerg * Copyright � 2002, J�rg Wunsch
399373Sjohan *
499373Sjohan * Redistribution and use in source and binary forms, with or without
599373Sjohan * modification, are permitted provided that the following conditions
699373Sjohan * are met:
799373Sjohan * 1. Redistributions of source code must retain the above copyright
899373Sjohan *    notice, this list of conditions and the following disclaimer.
999373Sjohan * 2. Redistributions in binary form must reproduce the above copyright
1099373Sjohan *    notice, this list of conditions and the following disclaimer in the
1199373Sjohan *    documentation and/or other materials provided with the distribution.
1299373Sjohan *
1399821Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
1499821Sjoerg * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1599821Sjoerg * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
1699821Sjoerg * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
1799821Sjoerg * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
1899821Sjoerg * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
1999821Sjoerg * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2099821Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2199821Sjoerg * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
2299821Sjoerg * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2399821Sjoerg * POSSIBILITY OF SUCH DAMAGE.
2499373Sjohan */
2599373Sjohan
2699821Sjoerg/*
2799821Sjoerg * 4.3BSD UI-compatible whereis(1) utility.  Rewritten from scratch
2899821Sjoerg * since the original 4.3BSD version suffers legal problems that
2999821Sjoerg * prevent it from being redistributed, and since the 4.4BSD version
3099821Sjoerg * was pretty inferior in functionality.
3199821Sjoerg */
3299373Sjohan
3399821Sjoerg#include <sys/types.h>
3499373Sjohan
3599406Sjohan__FBSDID("$FreeBSD: head/usr.bin/whereis/whereis.c 141657 2005-02-10 16:07:23Z ru $");
3699406Sjohan
3799373Sjohan#include <sys/stat.h>
3899373Sjohan#include <sys/sysctl.h>
3999373Sjohan
4099821Sjoerg#include <dirent.h>
4199373Sjohan#include <err.h>
4299373Sjohan#include <errno.h>
43132198Stjr#include <locale.h>
4499821Sjoerg#include <regex.h>
4599373Sjohan#include <stdio.h>
4699373Sjohan#include <stdlib.h>
4799373Sjohan#include <string.h>
4899821Sjoerg#include <sysexits.h>
4999373Sjohan#include <unistd.h>
5099373Sjohan
5199821Sjoerg#include "pathnames.h"
5299373Sjohan
53102246Sjohan#define	NO_BIN_FOUND	1
54102246Sjohan#define	NO_MAN_FOUND	2
55102246Sjohan#define	NO_SRC_FOUND	4
56102246Sjohan
5799821Sjoergtypedef const char *ccharp;
5899821Sjoerg
59102246Sjohanint opt_a, opt_b, opt_m, opt_q, opt_s, opt_u, opt_x;
6099821Sjoergccharp *bindirs, *mandirs, *sourcedirs;
6199821Sjoergchar **query;
6299821Sjoerg
6399821Sjoergconst char *sourcepath = PATH_SOURCES;
6499821Sjoerg
6599821Sjoergchar	*colonify(ccharp *);
6699821Sjoergint	 contains(ccharp *, const char *);
6799821Sjoergvoid	 decolonify(char *, ccharp **, int *);
6899821Sjoergvoid	 defaults(void);
6999821Sjoergvoid	 scanopts(int, char **);
7099821Sjoergvoid	 usage(void);
7199821Sjoerg
7299821Sjoerg/*
7399821Sjoerg * Throughout this program, a number of strings are dynamically
7499821Sjoerg * allocated but never freed.  Their memory is written to when
7599821Sjoerg * splitting the strings into string lists which will later be
7699821Sjoerg * processed.  Since it's important that those string lists remain
7799821Sjoerg * valid even after the functions allocating the memory returned,
7899821Sjoerg * those functions cannot free them.  They could be freed only at end
7999821Sjoerg * of main(), which is pretty pointless anyway.
8099821Sjoerg *
8199821Sjoerg * The overall amount of memory to be allocated for processing the
8299821Sjoerg * strings is not expected to exceed a few kilobytes.  For that
8399821Sjoerg * reason, allocation can usually always be assumed to succeed (within
8499821Sjoerg * a virtual memory environment), thus we simply bail out using
8599821Sjoerg * abort(3) in case of an allocation failure.
8699821Sjoerg */
8799821Sjoerg
8899821Sjoergvoid
8999821Sjoergusage(void)
9099373Sjohan{
91141657Sru	(void)fprintf(stderr,
92141657Sru	     "usage: whereis [-abmqsux] [-BMS dir ... -f] program ...\n");
93141657Sru	exit(EX_USAGE);
9499821Sjoerg}
9599373Sjohan
9699821Sjoerg/*
9799821Sjoerg * Scan options passed to program.
9899821Sjoerg *
9999821Sjoerg * Note that the -B/-M/-S options expect a list of directory
10099821Sjoerg * names that must be terminated with -f.
10199821Sjoerg */
10299821Sjoergvoid
10399821Sjoergscanopts(int argc, char **argv)
10499821Sjoerg{
10599821Sjoerg	int c, i, opt_f;
10699821Sjoerg	ccharp **dirlist;
10799821Sjoerg
10899821Sjoerg	opt_f = 0;
109102246Sjohan	while ((c = getopt(argc, argv, "BMSabfmqsux")) != -1)
11099821Sjoerg		switch (c) {
11199821Sjoerg		case 'B':
11299821Sjoerg			dirlist = &bindirs;
11399821Sjoerg			goto dolist;
11499821Sjoerg
11599821Sjoerg		case 'M':
11699821Sjoerg			dirlist = &mandirs;
11799821Sjoerg			goto dolist;
11899821Sjoerg
11999821Sjoerg		case 'S':
12099821Sjoerg			dirlist = &sourcedirs;
12199821Sjoerg		  dolist:
12299821Sjoerg			i = 0;
123102072Sjohan			*dirlist = realloc(*dirlist, (i + 1) * sizeof(char *));
124102072Sjohan			(*dirlist)[i] = NULL;
12599821Sjoerg			while (optind < argc &&
12699821Sjoerg			       strcmp(argv[optind], "-f") != 0 &&
12799821Sjoerg			       strcmp(argv[optind], "-B") != 0 &&
12899821Sjoerg			       strcmp(argv[optind], "-M") != 0 &&
12999821Sjoerg			       strcmp(argv[optind], "-S") != 0) {
130102072Sjohan				decolonify(argv[optind], dirlist, &i);
13199821Sjoerg				optind++;
13299821Sjoerg			}
13399821Sjoerg			break;
13499821Sjoerg
135102246Sjohan		case 'a':
136102246Sjohan			opt_a = 1;
137102246Sjohan			break;
138102246Sjohan
13999821Sjoerg		case 'b':
14099821Sjoerg			opt_b = 1;
14199821Sjoerg			break;
14299821Sjoerg
14399821Sjoerg		case 'f':
14499821Sjoerg			goto breakout;
14599821Sjoerg
14699821Sjoerg		case 'm':
14799821Sjoerg			opt_m = 1;
14899821Sjoerg			break;
14999821Sjoerg
15099821Sjoerg		case 'q':
15199821Sjoerg			opt_q = 1;
15299821Sjoerg			break;
15399821Sjoerg
15499821Sjoerg		case 's':
15599821Sjoerg			opt_s = 1;
15699821Sjoerg			break;
15799821Sjoerg
15899821Sjoerg		case 'u':
15999821Sjoerg			opt_u = 1;
16099821Sjoerg			break;
16199821Sjoerg
16299821Sjoerg		case 'x':
16399821Sjoerg			opt_x = 1;
16499821Sjoerg			break;
16599821Sjoerg
16699373Sjohan		default:
16799373Sjohan			usage();
16899373Sjohan		}
16999821Sjoerg  breakout:
17099821Sjoerg	if (optind == argc)
17199373Sjohan		usage();
17299821Sjoerg	query = argv + optind;
17399821Sjoerg}
17499373Sjohan
17599821Sjoerg/*
17699821Sjoerg * Find out whether string `s' is contained in list `cpp'.
17799821Sjoerg */
17899821Sjoergint
17999821Sjoergcontains(ccharp *cpp, const char *s)
18099821Sjoerg{
18199821Sjoerg	ccharp cp;
18299821Sjoerg
18399821Sjoerg	if (cpp == NULL)
18499821Sjoerg		return (0);
18599821Sjoerg
18699821Sjoerg	while ((cp = *cpp) != NULL) {
18799821Sjoerg		if (strcmp(cp, s) == 0)
18899821Sjoerg			return (1);
18999821Sjoerg		cpp++;
19099373Sjohan	}
19199821Sjoerg	return (0);
19299821Sjoerg}
19399373Sjohan
19499821Sjoerg/*
19599821Sjoerg * Split string `s' at colons, and pass it to the string list pointed
19699821Sjoerg * to by `cppp' (which has `*ip' elements).  Note that the original
19799821Sjoerg * string is modified by replacing the colon with a NUL byte.  The
19899821Sjoerg * partial string is only added if it has a length greater than 0, and
19999821Sjoerg * if it's not already contained in the string list.
20099821Sjoerg */
20199821Sjoergvoid
20299821Sjoergdecolonify(char *s, ccharp **cppp, int *ip)
20399821Sjoerg{
20499821Sjoerg	char *cp;
20599821Sjoerg
20699821Sjoerg	while ((cp = strchr(s, ':')), *s != '\0') {
20799821Sjoerg		if (cp)
20899821Sjoerg			*cp = '\0';
20999821Sjoerg		if (strlen(s) && !contains(*cppp, s)) {
21099821Sjoerg			*cppp = realloc(*cppp, (*ip + 2) * sizeof(char *));
21199821Sjoerg			if (cppp == NULL)
21299821Sjoerg				abort();
21399821Sjoerg			(*cppp)[*ip] = s;
21499821Sjoerg			(*cppp)[*ip + 1] = NULL;
21599821Sjoerg			(*ip)++;
21699373Sjohan		}
21799821Sjoerg		if (cp)
21899821Sjoerg			s = cp + 1;
21999821Sjoerg		else
22099821Sjoerg			break;
22199821Sjoerg	}
22299821Sjoerg}
22399373Sjohan
22499821Sjoerg/*
22599821Sjoerg * Join string list `cpp' into a colon-separated string.
22699821Sjoerg */
22799821Sjoergchar *
22899821Sjoergcolonify(ccharp *cpp)
22999821Sjoerg{
23099821Sjoerg	size_t s;
23199821Sjoerg	char *cp;
23299821Sjoerg	int i;
23399821Sjoerg
23499821Sjoerg	if (cpp == NULL)
23599821Sjoerg		return (0);
23699821Sjoerg
23799821Sjoerg	for (s = 0, i = 0; cpp[i] != NULL; i++)
23899821Sjoerg		s += strlen(cpp[i]) + 1;
23999821Sjoerg	if ((cp = malloc(s + 1)) == NULL)
24099821Sjoerg		abort();
24199821Sjoerg	for (i = 0, *cp = '\0'; cpp[i] != NULL; i++) {
24299821Sjoerg		strcat(cp, cpp[i]);
24399821Sjoerg		strcat(cp, ":");
24499821Sjoerg	}
24599821Sjoerg	cp[s - 1] = '\0';		/* eliminate last colon */
24699821Sjoerg
24799821Sjoerg	return (cp);
24899373Sjohan}
24999373Sjohan
25099821Sjoerg/*
25199821Sjoerg * Provide defaults for all options and directory lists.
25299821Sjoerg */
25399373Sjohanvoid
25499821Sjoergdefaults(void)
25599373Sjohan{
25699821Sjoerg	size_t s;
25799821Sjoerg	char *b, buf[BUFSIZ], *cp;
25899821Sjoerg	int nele;
25999821Sjoerg	FILE *p;
26099821Sjoerg	DIR *dir;
26199821Sjoerg	struct stat sb;
26299821Sjoerg	struct dirent *dirp;
26399373Sjohan
26499821Sjoerg	/* default to -bms if none has been specified */
26599821Sjoerg	if (!opt_b && !opt_m && !opt_s)
26699821Sjoerg		opt_b = opt_m = opt_s = 1;
26799821Sjoerg
268100608Sjohan	/* -b defaults to default path + /usr/libexec +
269100608Sjohan	 * /usr/games + user's path */
27099821Sjoerg	if (!bindirs) {
27199821Sjoerg		if (sysctlbyname("user.cs_path", (void *)NULL, &s,
27299821Sjoerg				 (void *)NULL, 0) == -1)
27399821Sjoerg			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
27499821Sjoerg		if ((b = malloc(s + 1)) == NULL)
27599821Sjoerg			abort();
27699821Sjoerg		if (sysctlbyname("user.cs_path", b, &s, (void *)NULL, 0) == -1)
27799821Sjoerg			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
27899821Sjoerg		nele = 0;
27999821Sjoerg		decolonify(b, &bindirs, &nele);
280100608Sjohan		bindirs = realloc(bindirs, (nele + 3) * sizeof(char *));
28199821Sjoerg		if (bindirs == NULL)
28299821Sjoerg			abort();
283100691Sjohan		bindirs[nele++] = PATH_LIBEXEC;
284100691Sjohan		bindirs[nele++] = PATH_GAMES;
28599821Sjoerg		bindirs[nele] = NULL;
28699821Sjoerg		if ((cp = getenv("PATH")) != NULL) {
28799821Sjoerg			/* don't destroy the original environment... */
28899821Sjoerg			if ((b = malloc(strlen(cp) + 1)) == NULL)
28999821Sjoerg				abort();
29099821Sjoerg			strcpy(b, cp);
29199821Sjoerg			decolonify(b, &bindirs, &nele);
29299821Sjoerg		}
29399821Sjoerg	}
29499821Sjoerg
29599821Sjoerg	/* -m defaults to $(manpath) */
29699821Sjoerg	if (!mandirs) {
29799821Sjoerg		if ((p = popen(MANPATHCMD, "r")) == NULL)
29899821Sjoerg			err(EX_OSERR, "cannot execute manpath command");
29999821Sjoerg		if (fgets(buf, BUFSIZ - 1, p) == NULL ||
30099821Sjoerg		    pclose(p))
30199821Sjoerg			err(EX_OSERR, "error processing manpath results");
30299821Sjoerg		if ((b = strchr(buf, '\n')) != NULL)
30399821Sjoerg			*b = '\0';
30499821Sjoerg		if ((b = malloc(strlen(buf) + 1)) == NULL)
30599821Sjoerg			abort();
30699821Sjoerg		strcpy(b, buf);
30799821Sjoerg		nele = 0;
30899821Sjoerg		decolonify(b, &mandirs, &nele);
30999821Sjoerg	}
31099821Sjoerg
31199821Sjoerg	/* -s defaults to precompiled list, plus subdirs of /usr/ports */
31299821Sjoerg	if (!sourcedirs) {
31399821Sjoerg		if ((b = malloc(strlen(sourcepath) + 1)) == NULL)
31499821Sjoerg			abort();
31599821Sjoerg		strcpy(b, sourcepath);
31699821Sjoerg		nele = 0;
31799821Sjoerg		decolonify(b, &sourcedirs, &nele);
31899821Sjoerg
31999821Sjoerg		if (stat(PATH_PORTS, &sb) == -1) {
32099821Sjoerg			if (errno == ENOENT)
32199821Sjoerg				/* no /usr/ports, we are done */
32299821Sjoerg				return;
32399821Sjoerg			err(EX_OSERR, "stat(" PATH_PORTS ")");
32499821Sjoerg		}
32599821Sjoerg		if ((sb.st_mode & S_IFMT) != S_IFDIR)
32699821Sjoerg			/* /usr/ports is not a directory, ignore */
32799821Sjoerg			return;
32899821Sjoerg		if (access(PATH_PORTS, R_OK | X_OK) != 0)
32999821Sjoerg			return;
33099821Sjoerg		if ((dir = opendir(PATH_PORTS)) == NULL)
33199821Sjoerg			err(EX_OSERR, "opendir" PATH_PORTS ")");
33299821Sjoerg		while ((dirp = readdir(dir)) != NULL) {
33399821Sjoerg			if (dirp->d_name[0] == '.' ||
33499821Sjoerg			    strcmp(dirp->d_name, "CVS") == 0)
33599821Sjoerg				/* ignore dot entries and CVS subdir */
33699821Sjoerg				continue;
33799821Sjoerg			if ((b = malloc(sizeof PATH_PORTS + 1 + dirp->d_namlen))
33899821Sjoerg			    == NULL)
33999821Sjoerg				abort();
34099821Sjoerg			strcpy(b, PATH_PORTS);
34199821Sjoerg			strcat(b, "/");
34299821Sjoerg			strcat(b, dirp->d_name);
34399821Sjoerg			if (stat(b, &sb) == -1 ||
34499821Sjoerg			    (sb.st_mode & S_IFMT) != S_IFDIR ||
34599821Sjoerg			    access(b, R_OK | X_OK) != 0) {
34699821Sjoerg				free(b);
34799821Sjoerg				continue;
34899821Sjoerg			}
34999821Sjoerg			sourcedirs = realloc(sourcedirs,
35099821Sjoerg					     (nele + 2) * sizeof(char *));
35199821Sjoerg			if (sourcedirs == NULL)
35299821Sjoerg				abort();
35399821Sjoerg			sourcedirs[nele++] = b;
35499821Sjoerg			sourcedirs[nele] = NULL;
35599821Sjoerg		}
35699821Sjoerg		closedir(dir);
35799821Sjoerg	}
35899373Sjohan}
35999821Sjoerg
36099821Sjoergint
36199821Sjoergmain(int argc, char **argv)
36299821Sjoerg{
36399821Sjoerg	int unusual, i, printed;
36499821Sjoerg	char *bin, buf[BUFSIZ], *cp, *cp2, *man, *name, *src;
36599821Sjoerg	ccharp *dp;
366102246Sjohan	size_t nlen, olen, s;
36799821Sjoerg	struct stat sb;
36899821Sjoerg	regex_t re, re2;
36999821Sjoerg	regmatch_t matches[2];
37099821Sjoerg	regoff_t rlen;
37199821Sjoerg	FILE *p;
37299821Sjoerg
373132198Stjr	setlocale(LC_ALL, "");
374132198Stjr
37599821Sjoerg	scanopts(argc, argv);
37699821Sjoerg	defaults();
37799821Sjoerg
37899821Sjoerg	if (mandirs == NULL)
37999821Sjoerg		opt_m = 0;
38099821Sjoerg	if (bindirs == NULL)
38199821Sjoerg		opt_b = 0;
38299821Sjoerg	if (sourcedirs == NULL)
38399821Sjoerg		opt_s = 0;
38499821Sjoerg	if (opt_m + opt_b + opt_s == 0)
38599821Sjoerg		errx(EX_DATAERR, "no directories to search");
38699821Sjoerg
38799821Sjoerg	if (opt_m) {
38899821Sjoerg		setenv("MANPATH", colonify(mandirs), 1);
38999821Sjoerg		if ((i = regcomp(&re, MANWHEREISMATCH, REG_EXTENDED)) != 0) {
39099821Sjoerg			regerror(i, &re, buf, BUFSIZ - 1);
39199821Sjoerg			errx(EX_UNAVAILABLE, "regcomp(%s) failed: %s",
39299821Sjoerg			     MANWHEREISMATCH, buf);
39399821Sjoerg		}
39499821Sjoerg	}
39599821Sjoerg
39699821Sjoerg	for (; (name = *query) != NULL; query++) {
39799821Sjoerg		/* strip leading path name component */
39899821Sjoerg		if ((cp = strrchr(name, '/')) != NULL)
39999821Sjoerg			name = cp + 1;
40099821Sjoerg		/* strip SCCS or RCS suffix/prefix */
40199821Sjoerg		if (strlen(name) > 2 && strncmp(name, "s.", 2) == 0)
40299821Sjoerg			name += 2;
40399821Sjoerg		if ((s = strlen(name)) > 2 && strcmp(name + s - 2, ",v") == 0)
40499821Sjoerg			name[s - 2] = '\0';
40599821Sjoerg		/* compression suffix */
40699821Sjoerg		s = strlen(name);
40799821Sjoerg		if (s > 2 &&
40899821Sjoerg		    (strcmp(name + s - 2, ".z") == 0 ||
40999821Sjoerg		     strcmp(name + s - 2, ".Z") == 0))
41099821Sjoerg			name[s - 2] = '\0';
41199821Sjoerg		else if (s > 3 &&
41299821Sjoerg			 strcmp(name + s - 3, ".gz") == 0)
41399821Sjoerg			name[s - 3] = '\0';
41499821Sjoerg		else if (s > 4 &&
41599821Sjoerg			 strcmp(name + s - 4, ".bz2") == 0)
41699821Sjoerg			name[s - 4] = '\0';
41799821Sjoerg
41899821Sjoerg		unusual = 0;
41999821Sjoerg		bin = man = src = NULL;
42099821Sjoerg		s = strlen(name);
42199821Sjoerg
42299821Sjoerg		if (opt_b) {
42399821Sjoerg			/*
42499821Sjoerg			 * Binaries have to match exactly, and must be regular
42599821Sjoerg			 * executable files.
42699821Sjoerg			 */
427102246Sjohan			unusual = unusual | NO_BIN_FOUND;
42899821Sjoerg			for (dp = bindirs; *dp != NULL; dp++) {
42999821Sjoerg				cp = malloc(strlen(*dp) + 1 + s + 1);
43099821Sjoerg				if (cp == NULL)
43199821Sjoerg					abort();
43299821Sjoerg				strcpy(cp, *dp);
43399821Sjoerg				strcat(cp, "/");
43499821Sjoerg				strcat(cp, name);
43599821Sjoerg				if (stat(cp, &sb) == 0 &&
43699821Sjoerg				    (sb.st_mode & S_IFMT) == S_IFREG &&
43799821Sjoerg				    (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
43899821Sjoerg				    != 0) {
439102246Sjohan					unusual = unusual & ~NO_BIN_FOUND;
440102246Sjohan					if (bin == NULL) {
441102246Sjohan						bin = strdup(cp);
442102246Sjohan					} else {
443102246Sjohan						olen = strlen(bin);
444102246Sjohan						nlen = strlen(cp);
445102246Sjohan						bin = realloc(bin,
446102246Sjohan							      olen + nlen + 2);
447102246Sjohan						if (bin == 0)
448102246Sjohan							abort();
449102246Sjohan						strcat(bin, " ");
450102246Sjohan						strcat(bin, cp);
451102246Sjohan					}
452102246Sjohan					if (!opt_a) {
453102246Sjohan						free(cp);
454102246Sjohan						break;
455102246Sjohan					}
45699821Sjoerg				}
45799821Sjoerg				free(cp);
45899821Sjoerg			}
45999821Sjoerg		}
46099821Sjoerg
46199821Sjoerg		if (opt_m) {
46299821Sjoerg			/*
46399821Sjoerg			 * Ask the man command to perform the search for us.
46499821Sjoerg			 */
465102246Sjohan			unusual = unusual | NO_MAN_FOUND;
466102246Sjohan			if (opt_a)
467102246Sjohan				cp = malloc(sizeof MANWHEREISALLCMD - 2 + s);
468102246Sjohan			else
469102246Sjohan				cp = malloc(sizeof MANWHEREISCMD - 2 + s);
470102246Sjohan
47199821Sjoerg			if (cp == NULL)
47299821Sjoerg				abort();
473102246Sjohan
474102246Sjohan			if (opt_a)
475102246Sjohan				sprintf(cp, MANWHEREISALLCMD, name);
476102246Sjohan			else
477102246Sjohan				sprintf(cp, MANWHEREISCMD, name);
478102246Sjohan
479102246Sjohan			if ((p = popen(cp, "r")) != NULL) {
480102246Sjohan
481102246Sjohan				while (fgets(buf, BUFSIZ - 1, p) != NULL) {
482102246Sjohan					unusual = unusual & ~NO_MAN_FOUND;
483102246Sjohan
484102246Sjohan					if ((cp2 = strchr(buf, '\n')) != NULL)
485102246Sjohan						*cp2 = '\0';
486102246Sjohan					if (regexec(&re, buf, 2,
487102246Sjohan						    matches, 0) == 0 &&
488102246Sjohan					    (rlen = matches[1].rm_eo -
489102246Sjohan					     matches[1].rm_so) > 0) {
490102246Sjohan						/*
491102246Sjohan						 * man -w found formated
492102246Sjohan						 * page, need to pick up
493102246Sjohan						 * source page name.
494102246Sjohan						 */
495102246Sjohan						cp2 = malloc(rlen + 1);
496102246Sjohan						if (cp2 == NULL)
497102246Sjohan							abort();
498102246Sjohan						memcpy(cp2,
499102246Sjohan						       buf + matches[1].rm_so,
500102246Sjohan						       rlen);
501102246Sjohan						cp2[rlen] = '\0';
502102246Sjohan					} else {
503102246Sjohan						/*
504102246Sjohan						 * man -w found plain source
505102246Sjohan						 * page, use it.
506102246Sjohan						 */
507102246Sjohan						s = strlen(buf);
508102246Sjohan						cp2 = malloc(s + 1);
509102246Sjohan						if (cp2 == NULL)
510102246Sjohan							abort();
511102246Sjohan						strcpy(cp2, buf);
512102246Sjohan					}
513102246Sjohan
514102246Sjohan					if (man == NULL) {
515102246Sjohan						man = strdup(cp2);
516102246Sjohan					} else {
517102246Sjohan						olen = strlen(man);
518102246Sjohan						nlen = strlen(cp2);
519102246Sjohan						man = realloc(man,
520102246Sjohan							      olen + nlen + 2);
521102246Sjohan						if (man == 0)
522102246Sjohan							abort();
523102246Sjohan						strcat(man, " ");
524102246Sjohan						strcat(man, cp2);
525102246Sjohan					}
526102246Sjohan
527102246Sjohan					free(cp2);
528102246Sjohan
529102246Sjohan					if (!opt_a)
530102246Sjohan						break;
53199821Sjoerg				}
532102246Sjohan				pclose(p);
533102246Sjohan				free(cp);
53499821Sjoerg			}
53599821Sjoerg		}
53699821Sjoerg
53799821Sjoerg		if (opt_s) {
53899821Sjoerg			/*
53999821Sjoerg			 * Sources match if a subdir with the exact
54099821Sjoerg			 * name is found.
54199821Sjoerg			 */
542102246Sjohan			unusual = unusual | NO_SRC_FOUND;
54399821Sjoerg			for (dp = sourcedirs; *dp != NULL; dp++) {
54499821Sjoerg				cp = malloc(strlen(*dp) + 1 + s + 1);
54599821Sjoerg				if (cp == NULL)
54699821Sjoerg					abort();
54799821Sjoerg				strcpy(cp, *dp);
54899821Sjoerg				strcat(cp, "/");
54999821Sjoerg				strcat(cp, name);
55099821Sjoerg				if (stat(cp, &sb) == 0 &&
55199821Sjoerg				    (sb.st_mode & S_IFMT) == S_IFDIR) {
552102246Sjohan					unusual = unusual & ~NO_SRC_FOUND;
553102246Sjohan					if (src == NULL) {
554102246Sjohan						src = strdup(cp);
555102246Sjohan					} else {
556102246Sjohan						olen = strlen(src);
557102246Sjohan						nlen = strlen(cp);
558102246Sjohan						src = realloc(src,
559102246Sjohan							      olen + nlen + 2);
560102246Sjohan						if (src == 0)
561102246Sjohan							abort();
562102246Sjohan						strcat(src, " ");
563102246Sjohan						strcat(src, cp);
564102246Sjohan					}
565102246Sjohan					if (!opt_a) {
566102246Sjohan						free(cp);
567102246Sjohan						break;
568102246Sjohan					}
56999821Sjoerg				}
57099821Sjoerg				free(cp);
57199821Sjoerg			}
57299821Sjoerg			/*
57399821Sjoerg			 * If still not found, ask locate to search it
57499821Sjoerg			 * for us.  This will find sources for things
57599821Sjoerg			 * like lpr that are well hidden in the
57699821Sjoerg			 * /usr/src tree, but takes a lot longer.
57799821Sjoerg			 * Thus, option -x (`expensive') prevents this
57899821Sjoerg			 * search.
57999821Sjoerg			 *
58099821Sjoerg			 * Do only match locate output that starts
58199821Sjoerg			 * with one of our source directories, and at
58299821Sjoerg			 * least one further level of subdirectories.
58399821Sjoerg			 */
584102246Sjohan			if (opt_x || (src && !opt_a))
58599821Sjoerg				goto done_sources;
58699821Sjoerg
58799821Sjoerg			cp = malloc(sizeof LOCATECMD - 2 + s);
58899821Sjoerg			if (cp == NULL)
58999821Sjoerg				abort();
59099821Sjoerg			sprintf(cp, LOCATECMD, name);
59199821Sjoerg			if ((p = popen(cp, "r")) == NULL)
59299821Sjoerg				goto done_sources;
593102246Sjohan			while ((src == NULL || opt_a) &&
59499821Sjoerg			       (fgets(buf, BUFSIZ - 1, p)) != NULL) {
59599821Sjoerg				if ((cp2 = strchr(buf, '\n')) != NULL)
59699821Sjoerg					*cp2 = '\0';
59799821Sjoerg				for (dp = sourcedirs;
598102246Sjohan				     (src == NULL || opt_a) && *dp != NULL;
59999821Sjoerg				     dp++) {
60099821Sjoerg					cp2 = malloc(strlen(*dp) + 9);
60199821Sjoerg					if (cp2 == NULL)
60299821Sjoerg						abort();
60399821Sjoerg					strcpy(cp2, "^");
60499821Sjoerg					strcat(cp2, *dp);
60599821Sjoerg					strcat(cp2, "/[^/]+/");
60699821Sjoerg					if ((i = regcomp(&re2, cp2,
60799821Sjoerg							 REG_EXTENDED|REG_NOSUB))
60899821Sjoerg					    != 0) {
60999821Sjoerg						regerror(i, &re, buf,
61099821Sjoerg							 BUFSIZ - 1);
61199821Sjoerg						errx(EX_UNAVAILABLE,
61299821Sjoerg						     "regcomp(%s) failed: %s",
61399821Sjoerg						     cp2, buf);
61499821Sjoerg					}
61599821Sjoerg					free(cp2);
61699821Sjoerg					if (regexec(&re2, buf, 0,
61799821Sjoerg						    (regmatch_t *)NULL, 0)
61899821Sjoerg					    == 0) {
619102246Sjohan						unusual = unusual &
620102246Sjohan						          ~NO_SRC_FOUND;
621102246Sjohan						if (src == NULL) {
622102246Sjohan							src = strdup(buf);
623102246Sjohan						} else {
624102246Sjohan							olen = strlen(src);
625102246Sjohan							nlen = strlen(buf);
626102246Sjohan							src = realloc(src,
627102246Sjohan								      olen +
628102246Sjohan								      nlen + 2);
629102246Sjohan							if (src == 0)
630102246Sjohan								abort();
631102246Sjohan							strcat(src, " ");
632102246Sjohan							strcat(src, buf);
633102246Sjohan						}
63499821Sjoerg					}
63599821Sjoerg					regfree(&re2);
63699821Sjoerg				}
63799821Sjoerg			}
63899821Sjoerg			pclose(p);
63999821Sjoerg			free(cp);
64099821Sjoerg		}
64199821Sjoerg	  done_sources:
64299821Sjoerg
64399821Sjoerg		if (opt_u && !unusual)
64499821Sjoerg			continue;
64599821Sjoerg
64699821Sjoerg		printed = 0;
64799821Sjoerg		if (!opt_q) {
64899821Sjoerg			printf("%s:", name);
64999821Sjoerg			printed++;
65099821Sjoerg		}
65199821Sjoerg		if (bin) {
65299821Sjoerg			if (printed++)
65399821Sjoerg				putchar(' ');
65499821Sjoerg			fputs(bin, stdout);
65599821Sjoerg		}
65699821Sjoerg		if (man) {
65799821Sjoerg			if (printed++)
65899821Sjoerg				putchar(' ');
65999821Sjoerg			fputs(man, stdout);
66099821Sjoerg		}
66199821Sjoerg		if (src) {
66299821Sjoerg			if (printed++)
66399821Sjoerg				putchar(' ');
66499821Sjoerg			fputs(src, stdout);
66599821Sjoerg		}
66699821Sjoerg		if (printed)
66799821Sjoerg			putchar('\n');
66899821Sjoerg	}
66999821Sjoerg
67099821Sjoerg	if (opt_m)
67199821Sjoerg		regfree(&re);
67299821Sjoerg
67399821Sjoerg	return (0);
67499821Sjoerg}
675