whereis.c revision 99821
1/*
2 * Copyright � 2002, J�rg Wunsch
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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 */
25
26/*
27 * 4.3BSD UI-compatible whereis(1) utility.  Rewritten from scratch
28 * since the original 4.3BSD version suffers legal problems that
29 * prevent it from being redistributed, and since the 4.4BSD version
30 * was pretty inferior in functionality.
31 */
32
33#include <sys/types.h>
34
35__FBSDID("$FreeBSD: head/usr.bin/whereis/whereis.c 99821 2002-07-11 21:20:54Z joerg $");
36
37#include <sys/stat.h>
38#include <sys/sysctl.h>
39
40#include <dirent.h>
41#include <err.h>
42#include <errno.h>
43#include <regex.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <sysexits.h>
48#include <unistd.h>
49
50#include "pathnames.h"
51
52typedef const char *ccharp;
53
54int opt_b, opt_m, opt_q, opt_s, opt_u, opt_x;
55ccharp *bindirs, *mandirs, *sourcedirs;
56char **query;
57
58const char *sourcepath = PATH_SOURCES;
59
60char	*colonify(ccharp *);
61int	 contains(ccharp *, const char *);
62void	 decolonify(char *, ccharp **, int *);
63void	 defaults(void);
64void	 scanopts(int, char **);
65void	 usage(void);
66
67/*
68 * Throughout this program, a number of strings are dynamically
69 * allocated but never freed.  Their memory is written to when
70 * splitting the strings into string lists which will later be
71 * processed.  Since it's important that those string lists remain
72 * valid even after the functions allocating the memory returned,
73 * those functions cannot free them.  They could be freed only at end
74 * of main(), which is pretty pointless anyway.
75 *
76 * The overall amount of memory to be allocated for processing the
77 * strings is not expected to exceed a few kilobytes.  For that
78 * reason, allocation can usually always be assumed to succeed (within
79 * a virtual memory environment), thus we simply bail out using
80 * abort(3) in case of an allocation failure.
81 */
82
83void
84usage(void)
85{
86	errx(EX_USAGE,
87	     "usage: whereis [-bmqsux] [-BMS dir... -f] name ...");
88}
89
90/*
91 * Scan options passed to program.
92 *
93 * Note that the -B/-M/-S options expect a list of directory
94 * names that must be terminated with -f.
95 */
96void
97scanopts(int argc, char **argv)
98{
99	int c, i, opt_f;
100	ccharp **dirlist;
101
102	opt_f = 0;
103	while ((c = getopt(argc, argv, "BMSbfmqsux")) != -1)
104		switch (c) {
105		case 'B':
106			dirlist = &bindirs;
107			goto dolist;
108
109		case 'M':
110			dirlist = &mandirs;
111			goto dolist;
112
113		case 'S':
114			dirlist = &sourcedirs;
115		  dolist:
116			i = 0;
117			while (optind < argc &&
118			       strcmp(argv[optind], "-f") != 0 &&
119			       strcmp(argv[optind], "-B") != 0 &&
120			       strcmp(argv[optind], "-M") != 0 &&
121			       strcmp(argv[optind], "-S") != 0) {
122				*dirlist = realloc(*dirlist,
123						   (i + 2) * sizeof(char *));
124				if (*dirlist == NULL)
125					abort();
126				(*dirlist)[i] = argv[optind];
127				i++;
128				optind++;
129			}
130			(*dirlist)[i] = NULL;
131			break;
132
133		case 'b':
134			opt_b = 1;
135			break;
136
137		case 'f':
138			goto breakout;
139
140		case 'm':
141			opt_m = 1;
142			break;
143
144		case 'q':
145			opt_q = 1;
146			break;
147
148		case 's':
149			opt_s = 1;
150			break;
151
152		case 'u':
153			opt_u = 1;
154			break;
155
156		case 'x':
157			opt_x = 1;
158			break;
159
160		default:
161			usage();
162		}
163  breakout:
164	if (optind == argc)
165		usage();
166	query = argv + optind;
167}
168
169/*
170 * Find out whether string `s' is contained in list `cpp'.
171 */
172int
173contains(ccharp *cpp, const char *s)
174{
175	ccharp cp;
176
177	if (cpp == NULL)
178		return (0);
179
180	while ((cp = *cpp) != NULL) {
181		if (strcmp(cp, s) == 0)
182			return (1);
183		cpp++;
184	}
185	return (0);
186}
187
188/*
189 * Split string `s' at colons, and pass it to the string list pointed
190 * to by `cppp' (which has `*ip' elements).  Note that the original
191 * string is modified by replacing the colon with a NUL byte.  The
192 * partial string is only added if it has a length greater than 0, and
193 * if it's not already contained in the string list.
194 */
195void
196decolonify(char *s, ccharp **cppp, int *ip)
197{
198	char *cp;
199
200	while ((cp = strchr(s, ':')), *s != '\0') {
201		if (cp)
202			*cp = '\0';
203		if (strlen(s) && !contains(*cppp, s)) {
204			*cppp = realloc(*cppp, (*ip + 2) * sizeof(char *));
205			if (cppp == NULL)
206				abort();
207			(*cppp)[*ip] = s;
208			(*cppp)[*ip + 1] = NULL;
209			(*ip)++;
210		}
211		if (cp)
212			s = cp + 1;
213		else
214			break;
215	}
216}
217
218/*
219 * Join string list `cpp' into a colon-separated string.
220 */
221char *
222colonify(ccharp *cpp)
223{
224	size_t s;
225	char *cp;
226	int i;
227
228	if (cpp == NULL)
229		return (0);
230
231	for (s = 0, i = 0; cpp[i] != NULL; i++)
232		s += strlen(cpp[i]) + 1;
233	if ((cp = malloc(s + 1)) == NULL)
234		abort();
235	for (i = 0, *cp = '\0'; cpp[i] != NULL; i++) {
236		strcat(cp, cpp[i]);
237		strcat(cp, ":");
238	}
239	cp[s - 1] = '\0';		/* eliminate last colon */
240
241	return (cp);
242}
243
244/*
245 * Provide defaults for all options and directory lists.
246 */
247void
248defaults(void)
249{
250	size_t s;
251	char *b, buf[BUFSIZ], *cp;
252	int nele;
253	FILE *p;
254	DIR *dir;
255	struct stat sb;
256	struct dirent *dirp;
257
258	/* default to -bms if none has been specified */
259	if (!opt_b && !opt_m && !opt_s)
260		opt_b = opt_m = opt_s = 1;
261
262	/* -b defaults to default path + /usr/libexec + user's path */
263	if (!bindirs) {
264		if (sysctlbyname("user.cs_path", (void *)NULL, &s,
265				 (void *)NULL, 0) == -1)
266			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
267		if ((b = malloc(s + 1)) == NULL)
268			abort();
269		if (sysctlbyname("user.cs_path", b, &s, (void *)NULL, 0) == -1)
270			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
271		nele = 0;
272		decolonify(b, &bindirs, &nele);
273		bindirs = realloc(bindirs, (nele + 2) * sizeof(char *));
274		if (bindirs == NULL)
275			abort();
276		bindirs[nele++] = "/usr/libexec";
277		bindirs[nele] = NULL;
278		if ((cp = getenv("PATH")) != NULL) {
279			/* don't destroy the original environment... */
280			if ((b = malloc(strlen(cp) + 1)) == NULL)
281				abort();
282			strcpy(b, cp);
283			decolonify(b, &bindirs, &nele);
284		}
285	}
286
287	/* -m defaults to $(manpath) */
288	if (!mandirs) {
289		if ((p = popen(MANPATHCMD, "r")) == NULL)
290			err(EX_OSERR, "cannot execute manpath command");
291		if (fgets(buf, BUFSIZ - 1, p) == NULL ||
292		    pclose(p))
293			err(EX_OSERR, "error processing manpath results");
294		if ((b = strchr(buf, '\n')) != NULL)
295			*b = '\0';
296		if ((b = malloc(strlen(buf) + 1)) == NULL)
297			abort();
298		strcpy(b, buf);
299		nele = 0;
300		decolonify(b, &mandirs, &nele);
301	}
302
303	/* -s defaults to precompiled list, plus subdirs of /usr/ports */
304	if (!sourcedirs) {
305		if ((b = malloc(strlen(sourcepath) + 1)) == NULL)
306			abort();
307		strcpy(b, sourcepath);
308		nele = 0;
309		decolonify(b, &sourcedirs, &nele);
310
311		if (stat(PATH_PORTS, &sb) == -1) {
312			if (errno == ENOENT)
313				/* no /usr/ports, we are done */
314				return;
315			err(EX_OSERR, "stat(" PATH_PORTS ")");
316		}
317		if ((sb.st_mode & S_IFMT) != S_IFDIR)
318			/* /usr/ports is not a directory, ignore */
319			return;
320		if (access(PATH_PORTS, R_OK | X_OK) != 0)
321			return;
322		if ((dir = opendir(PATH_PORTS)) == NULL)
323			err(EX_OSERR, "opendir" PATH_PORTS ")");
324		while ((dirp = readdir(dir)) != NULL) {
325			if (dirp->d_name[0] == '.' ||
326			    strcmp(dirp->d_name, "CVS") == 0)
327				/* ignore dot entries and CVS subdir */
328				continue;
329			if ((b = malloc(sizeof PATH_PORTS + 1 + dirp->d_namlen))
330			    == NULL)
331				abort();
332			strcpy(b, PATH_PORTS);
333			strcat(b, "/");
334			strcat(b, dirp->d_name);
335			if (stat(b, &sb) == -1 ||
336			    (sb.st_mode & S_IFMT) != S_IFDIR ||
337			    access(b, R_OK | X_OK) != 0) {
338				free(b);
339				continue;
340			}
341			sourcedirs = realloc(sourcedirs,
342					     (nele + 2) * sizeof(char *));
343			if (sourcedirs == NULL)
344				abort();
345			sourcedirs[nele++] = b;
346			sourcedirs[nele] = NULL;
347		}
348		closedir(dir);
349	}
350}
351
352int
353main(int argc, char **argv)
354{
355	int unusual, i, printed;
356	char *bin, buf[BUFSIZ], *cp, *cp2, *man, *name, *src;
357	ccharp *dp;
358	size_t s;
359	struct stat sb;
360	regex_t re, re2;
361	regmatch_t matches[2];
362	regoff_t rlen;
363	FILE *p;
364
365	scanopts(argc, argv);
366	defaults();
367
368	if (mandirs == NULL)
369		opt_m = 0;
370	if (bindirs == NULL)
371		opt_b = 0;
372	if (sourcedirs == NULL)
373		opt_s = 0;
374	if (opt_m + opt_b + opt_s == 0)
375		errx(EX_DATAERR, "no directories to search");
376
377	if (opt_m) {
378		setenv("MANPATH", colonify(mandirs), 1);
379		if ((i = regcomp(&re, MANWHEREISMATCH, REG_EXTENDED)) != 0) {
380			regerror(i, &re, buf, BUFSIZ - 1);
381			errx(EX_UNAVAILABLE, "regcomp(%s) failed: %s",
382			     MANWHEREISMATCH, buf);
383		}
384	}
385
386	for (; (name = *query) != NULL; query++) {
387		/* strip leading path name component */
388		if ((cp = strrchr(name, '/')) != NULL)
389			name = cp + 1;
390		/* strip SCCS or RCS suffix/prefix */
391		if (strlen(name) > 2 && strncmp(name, "s.", 2) == 0)
392			name += 2;
393		if ((s = strlen(name)) > 2 && strcmp(name + s - 2, ",v") == 0)
394			name[s - 2] = '\0';
395		/* compression suffix */
396		s = strlen(name);
397		if (s > 2 &&
398		    (strcmp(name + s - 2, ".z") == 0 ||
399		     strcmp(name + s - 2, ".Z") == 0))
400			name[s - 2] = '\0';
401		else if (s > 3 &&
402			 strcmp(name + s - 3, ".gz") == 0)
403			name[s - 3] = '\0';
404		else if (s > 4 &&
405			 strcmp(name + s - 4, ".bz2") == 0)
406			name[s - 4] = '\0';
407
408		unusual = 0;
409		bin = man = src = NULL;
410		s = strlen(name);
411
412		if (opt_b) {
413			/*
414			 * Binaries have to match exactly, and must be regular
415			 * executable files.
416			 */
417			unusual++;
418			for (dp = bindirs; *dp != NULL; dp++) {
419				cp = malloc(strlen(*dp) + 1 + s + 1);
420				if (cp == NULL)
421					abort();
422				strcpy(cp, *dp);
423				strcat(cp, "/");
424				strcat(cp, name);
425				if (stat(cp, &sb) == 0 &&
426				    (sb.st_mode & S_IFMT) == S_IFREG &&
427				    (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
428				    != 0) {
429					unusual--;
430					bin = cp;
431					break;
432				}
433				free(cp);
434			}
435		}
436
437		if (opt_m) {
438			/*
439			 * Ask the man command to perform the search for us.
440			 */
441			unusual++;
442			cp = malloc(sizeof MANWHEREISCMD - 2 + s);
443			if (cp == NULL)
444				abort();
445			sprintf(cp, MANWHEREISCMD, name);
446			if ((p = popen(cp, "r")) != NULL &&
447			    fgets(buf, BUFSIZ - 1, p) != NULL &&
448			    pclose(p) == 0) {
449				unusual--;
450				if ((cp2 = strchr(buf, '\n')) != NULL)
451					*cp2 = '\0';
452				if (regexec(&re, buf, 2, matches, 0) == 0 &&
453				    (rlen = matches[1].rm_eo - matches[1].rm_so)
454				    > 0) {
455					/*
456					 * man -w found compressed
457					 * page, need to pick up
458					 * source page name.
459					 */
460					cp2 = malloc(rlen + 1);
461					if (cp2 == NULL)
462						abort();
463					memcpy(cp2, buf + matches[1].rm_so,
464					       rlen);
465					cp2[rlen] = '\0';
466					man = cp2;
467				} else {
468					/*
469					 * man -w found plain source
470					 * page, use it.
471					 */
472					s = strlen(buf);
473					cp2 = malloc(s + 1);
474					if (cp2 == NULL)
475						abort();
476					strcpy(cp2, buf);
477					man = cp2;
478				}
479			}
480			free(cp);
481		}
482
483		if (opt_s) {
484			/*
485			 * Sources match if a subdir with the exact
486			 * name is found.
487			 */
488			unusual++;
489			for (dp = sourcedirs; *dp != NULL; dp++) {
490				cp = malloc(strlen(*dp) + 1 + s + 1);
491				if (cp == NULL)
492					abort();
493				strcpy(cp, *dp);
494				strcat(cp, "/");
495				strcat(cp, name);
496				if (stat(cp, &sb) == 0 &&
497				    (sb.st_mode & S_IFMT) == S_IFDIR) {
498					unusual--;
499					src = cp;
500					break;
501				}
502				free(cp);
503			}
504			/*
505			 * If still not found, ask locate to search it
506			 * for us.  This will find sources for things
507			 * like lpr that are well hidden in the
508			 * /usr/src tree, but takes a lot longer.
509			 * Thus, option -x (`expensive') prevents this
510			 * search.
511			 *
512			 * Do only match locate output that starts
513			 * with one of our source directories, and at
514			 * least one further level of subdirectories.
515			 */
516			if (opt_x || src)
517				goto done_sources;
518
519			cp = malloc(sizeof LOCATECMD - 2 + s);
520			if (cp == NULL)
521				abort();
522			sprintf(cp, LOCATECMD, name);
523			if ((p = popen(cp, "r")) == NULL)
524				goto done_sources;
525			while (src == NULL &&
526			       (fgets(buf, BUFSIZ - 1, p)) != NULL) {
527				if ((cp2 = strchr(buf, '\n')) != NULL)
528					*cp2 = '\0';
529				for (dp = sourcedirs;
530				     src == NULL && *dp != NULL;
531				     dp++) {
532					cp2 = malloc(strlen(*dp) + 9);
533					if (cp2 == NULL)
534						abort();
535					strcpy(cp2, "^");
536					strcat(cp2, *dp);
537					strcat(cp2, "/[^/]+/");
538					if ((i = regcomp(&re2, cp2,
539							 REG_EXTENDED|REG_NOSUB))
540					    != 0) {
541						regerror(i, &re, buf,
542							 BUFSIZ - 1);
543						errx(EX_UNAVAILABLE,
544						     "regcomp(%s) failed: %s",
545						     cp2, buf);
546					}
547					free(cp2);
548					if (regexec(&re2, buf, 0,
549						    (regmatch_t *)NULL, 0)
550					    == 0) {
551						unusual--;
552						src = buf;
553					}
554					regfree(&re2);
555				}
556			}
557			pclose(p);
558			free(cp);
559		}
560	  done_sources:
561
562		if (opt_u && !unusual)
563			continue;
564
565		printed = 0;
566		if (!opt_q) {
567			printf("%s:", name);
568			printed++;
569		}
570		if (bin) {
571			if (printed++)
572				putchar(' ');
573			fputs(bin, stdout);
574		}
575		if (man) {
576			if (printed++)
577				putchar(' ');
578			fputs(man, stdout);
579		}
580		if (src) {
581			if (printed++)
582				putchar(' ');
583			fputs(src, stdout);
584		}
585		if (printed)
586			putchar('\n');
587	}
588
589	if (opt_m)
590		regfree(&re);
591
592	return (0);
593}
594