xref: /freebsd/usr.bin/whereis/whereis.c (revision 6af83ee0d2941d18880b6aaa2b4facd1d30c6106)
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$");
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 <locale.h>
44 #include <regex.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <sysexits.h>
49 #include <unistd.h>
50 
51 #include "pathnames.h"
52 
53 #define	NO_BIN_FOUND	1
54 #define	NO_MAN_FOUND	2
55 #define	NO_SRC_FOUND	4
56 
57 typedef const char *ccharp;
58 
59 int opt_a, opt_b, opt_m, opt_q, opt_s, opt_u, opt_x;
60 ccharp *bindirs, *mandirs, *sourcedirs;
61 char **query;
62 
63 const char *sourcepath = PATH_SOURCES;
64 
65 char	*colonify(ccharp *);
66 int	 contains(ccharp *, const char *);
67 void	 decolonify(char *, ccharp **, int *);
68 void	 defaults(void);
69 void	 scanopts(int, char **);
70 void	 usage(void);
71 
72 /*
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.
80  *
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.
86  */
87 
88 void
89 usage(void)
90 {
91 	(void)fprintf(stderr,
92 	     "usage: whereis [-abmqsux] [-BMS dir ... -f] program ...\n");
93 	exit(EX_USAGE);
94 }
95 
96 /*
97  * Scan options passed to program.
98  *
99  * Note that the -B/-M/-S options expect a list of directory
100  * names that must be terminated with -f.
101  */
102 void
103 scanopts(int argc, char **argv)
104 {
105 	int c, i, opt_f;
106 	ccharp **dirlist;
107 
108 	opt_f = 0;
109 	while ((c = getopt(argc, argv, "BMSabfmqsux")) != -1)
110 		switch (c) {
111 		case 'B':
112 			dirlist = &bindirs;
113 			goto dolist;
114 
115 		case 'M':
116 			dirlist = &mandirs;
117 			goto dolist;
118 
119 		case 'S':
120 			dirlist = &sourcedirs;
121 		  dolist:
122 			i = 0;
123 			*dirlist = realloc(*dirlist, (i + 1) * sizeof(char *));
124 			(*dirlist)[i] = NULL;
125 			while (optind < argc &&
126 			       strcmp(argv[optind], "-f") != 0 &&
127 			       strcmp(argv[optind], "-B") != 0 &&
128 			       strcmp(argv[optind], "-M") != 0 &&
129 			       strcmp(argv[optind], "-S") != 0) {
130 				decolonify(argv[optind], dirlist, &i);
131 				optind++;
132 			}
133 			break;
134 
135 		case 'a':
136 			opt_a = 1;
137 			break;
138 
139 		case 'b':
140 			opt_b = 1;
141 			break;
142 
143 		case 'f':
144 			goto breakout;
145 
146 		case 'm':
147 			opt_m = 1;
148 			break;
149 
150 		case 'q':
151 			opt_q = 1;
152 			break;
153 
154 		case 's':
155 			opt_s = 1;
156 			break;
157 
158 		case 'u':
159 			opt_u = 1;
160 			break;
161 
162 		case 'x':
163 			opt_x = 1;
164 			break;
165 
166 		default:
167 			usage();
168 		}
169   breakout:
170 	if (optind == argc)
171 		usage();
172 	query = argv + optind;
173 }
174 
175 /*
176  * Find out whether string `s' is contained in list `cpp'.
177  */
178 int
179 contains(ccharp *cpp, const char *s)
180 {
181 	ccharp cp;
182 
183 	if (cpp == NULL)
184 		return (0);
185 
186 	while ((cp = *cpp) != NULL) {
187 		if (strcmp(cp, s) == 0)
188 			return (1);
189 		cpp++;
190 	}
191 	return (0);
192 }
193 
194 /*
195  * Split string `s' at colons, and pass it to the string list pointed
196  * to by `cppp' (which has `*ip' elements).  Note that the original
197  * string is modified by replacing the colon with a NUL byte.  The
198  * partial string is only added if it has a length greater than 0, and
199  * if it's not already contained in the string list.
200  */
201 void
202 decolonify(char *s, ccharp **cppp, int *ip)
203 {
204 	char *cp;
205 
206 	while ((cp = strchr(s, ':')), *s != '\0') {
207 		if (cp)
208 			*cp = '\0';
209 		if (strlen(s) && !contains(*cppp, s)) {
210 			*cppp = realloc(*cppp, (*ip + 2) * sizeof(char *));
211 			if (cppp == NULL)
212 				abort();
213 			(*cppp)[*ip] = s;
214 			(*cppp)[*ip + 1] = NULL;
215 			(*ip)++;
216 		}
217 		if (cp)
218 			s = cp + 1;
219 		else
220 			break;
221 	}
222 }
223 
224 /*
225  * Join string list `cpp' into a colon-separated string.
226  */
227 char *
228 colonify(ccharp *cpp)
229 {
230 	size_t s;
231 	char *cp;
232 	int i;
233 
234 	if (cpp == NULL)
235 		return (0);
236 
237 	for (s = 0, i = 0; cpp[i] != NULL; i++)
238 		s += strlen(cpp[i]) + 1;
239 	if ((cp = malloc(s + 1)) == NULL)
240 		abort();
241 	for (i = 0, *cp = '\0'; cpp[i] != NULL; i++) {
242 		strcat(cp, cpp[i]);
243 		strcat(cp, ":");
244 	}
245 	cp[s - 1] = '\0';		/* eliminate last colon */
246 
247 	return (cp);
248 }
249 
250 /*
251  * Provide defaults for all options and directory lists.
252  */
253 void
254 defaults(void)
255 {
256 	size_t s;
257 	char *b, buf[BUFSIZ], *cp;
258 	int nele;
259 	FILE *p;
260 	DIR *dir;
261 	struct stat sb;
262 	struct dirent *dirp;
263 
264 	/* default to -bms if none has been specified */
265 	if (!opt_b && !opt_m && !opt_s)
266 		opt_b = opt_m = opt_s = 1;
267 
268 	/* -b defaults to default path + /usr/libexec +
269 	 * /usr/games + user's path */
270 	if (!bindirs) {
271 		if (sysctlbyname("user.cs_path", (void *)NULL, &s,
272 				 (void *)NULL, 0) == -1)
273 			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
274 		if ((b = malloc(s + 1)) == NULL)
275 			abort();
276 		if (sysctlbyname("user.cs_path", b, &s, (void *)NULL, 0) == -1)
277 			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
278 		nele = 0;
279 		decolonify(b, &bindirs, &nele);
280 		bindirs = realloc(bindirs, (nele + 3) * sizeof(char *));
281 		if (bindirs == NULL)
282 			abort();
283 		bindirs[nele++] = PATH_LIBEXEC;
284 		bindirs[nele++] = PATH_GAMES;
285 		bindirs[nele] = NULL;
286 		if ((cp = getenv("PATH")) != NULL) {
287 			/* don't destroy the original environment... */
288 			if ((b = malloc(strlen(cp) + 1)) == NULL)
289 				abort();
290 			strcpy(b, cp);
291 			decolonify(b, &bindirs, &nele);
292 		}
293 	}
294 
295 	/* -m defaults to $(manpath) */
296 	if (!mandirs) {
297 		if ((p = popen(MANPATHCMD, "r")) == NULL)
298 			err(EX_OSERR, "cannot execute manpath command");
299 		if (fgets(buf, BUFSIZ - 1, p) == NULL ||
300 		    pclose(p))
301 			err(EX_OSERR, "error processing manpath results");
302 		if ((b = strchr(buf, '\n')) != NULL)
303 			*b = '\0';
304 		if ((b = malloc(strlen(buf) + 1)) == NULL)
305 			abort();
306 		strcpy(b, buf);
307 		nele = 0;
308 		decolonify(b, &mandirs, &nele);
309 	}
310 
311 	/* -s defaults to precompiled list, plus subdirs of /usr/ports */
312 	if (!sourcedirs) {
313 		if ((b = malloc(strlen(sourcepath) + 1)) == NULL)
314 			abort();
315 		strcpy(b, sourcepath);
316 		nele = 0;
317 		decolonify(b, &sourcedirs, &nele);
318 
319 		if (stat(PATH_PORTS, &sb) == -1) {
320 			if (errno == ENOENT)
321 				/* no /usr/ports, we are done */
322 				return;
323 			err(EX_OSERR, "stat(" PATH_PORTS ")");
324 		}
325 		if ((sb.st_mode & S_IFMT) != S_IFDIR)
326 			/* /usr/ports is not a directory, ignore */
327 			return;
328 		if (access(PATH_PORTS, R_OK | X_OK) != 0)
329 			return;
330 		if ((dir = opendir(PATH_PORTS)) == NULL)
331 			err(EX_OSERR, "opendir" PATH_PORTS ")");
332 		while ((dirp = readdir(dir)) != NULL) {
333 			if (dirp->d_name[0] == '.' ||
334 			    strcmp(dirp->d_name, "CVS") == 0)
335 				/* ignore dot entries and CVS subdir */
336 				continue;
337 			if ((b = malloc(sizeof PATH_PORTS + 1 + dirp->d_namlen))
338 			    == NULL)
339 				abort();
340 			strcpy(b, PATH_PORTS);
341 			strcat(b, "/");
342 			strcat(b, dirp->d_name);
343 			if (stat(b, &sb) == -1 ||
344 			    (sb.st_mode & S_IFMT) != S_IFDIR ||
345 			    access(b, R_OK | X_OK) != 0) {
346 				free(b);
347 				continue;
348 			}
349 			sourcedirs = realloc(sourcedirs,
350 					     (nele + 2) * sizeof(char *));
351 			if (sourcedirs == NULL)
352 				abort();
353 			sourcedirs[nele++] = b;
354 			sourcedirs[nele] = NULL;
355 		}
356 		closedir(dir);
357 	}
358 }
359 
360 int
361 main(int argc, char **argv)
362 {
363 	int unusual, i, printed;
364 	char *bin, buf[BUFSIZ], *cp, *cp2, *man, *name, *src;
365 	ccharp *dp;
366 	size_t nlen, olen, s;
367 	struct stat sb;
368 	regex_t re, re2;
369 	regmatch_t matches[2];
370 	regoff_t rlen;
371 	FILE *p;
372 
373 	setlocale(LC_ALL, "");
374 
375 	scanopts(argc, argv);
376 	defaults();
377 
378 	if (mandirs == NULL)
379 		opt_m = 0;
380 	if (bindirs == NULL)
381 		opt_b = 0;
382 	if (sourcedirs == NULL)
383 		opt_s = 0;
384 	if (opt_m + opt_b + opt_s == 0)
385 		errx(EX_DATAERR, "no directories to search");
386 
387 	if (opt_m) {
388 		setenv("MANPATH", colonify(mandirs), 1);
389 		if ((i = regcomp(&re, MANWHEREISMATCH, REG_EXTENDED)) != 0) {
390 			regerror(i, &re, buf, BUFSIZ - 1);
391 			errx(EX_UNAVAILABLE, "regcomp(%s) failed: %s",
392 			     MANWHEREISMATCH, buf);
393 		}
394 	}
395 
396 	for (; (name = *query) != NULL; query++) {
397 		/* strip leading path name component */
398 		if ((cp = strrchr(name, '/')) != NULL)
399 			name = cp + 1;
400 		/* strip SCCS or RCS suffix/prefix */
401 		if (strlen(name) > 2 && strncmp(name, "s.", 2) == 0)
402 			name += 2;
403 		if ((s = strlen(name)) > 2 && strcmp(name + s - 2, ",v") == 0)
404 			name[s - 2] = '\0';
405 		/* compression suffix */
406 		s = strlen(name);
407 		if (s > 2 &&
408 		    (strcmp(name + s - 2, ".z") == 0 ||
409 		     strcmp(name + s - 2, ".Z") == 0))
410 			name[s - 2] = '\0';
411 		else if (s > 3 &&
412 			 strcmp(name + s - 3, ".gz") == 0)
413 			name[s - 3] = '\0';
414 		else if (s > 4 &&
415 			 strcmp(name + s - 4, ".bz2") == 0)
416 			name[s - 4] = '\0';
417 
418 		unusual = 0;
419 		bin = man = src = NULL;
420 		s = strlen(name);
421 
422 		if (opt_b) {
423 			/*
424 			 * Binaries have to match exactly, and must be regular
425 			 * executable files.
426 			 */
427 			unusual = unusual | NO_BIN_FOUND;
428 			for (dp = bindirs; *dp != NULL; dp++) {
429 				cp = malloc(strlen(*dp) + 1 + s + 1);
430 				if (cp == NULL)
431 					abort();
432 				strcpy(cp, *dp);
433 				strcat(cp, "/");
434 				strcat(cp, name);
435 				if (stat(cp, &sb) == 0 &&
436 				    (sb.st_mode & S_IFMT) == S_IFREG &&
437 				    (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
438 				    != 0) {
439 					unusual = unusual & ~NO_BIN_FOUND;
440 					if (bin == NULL) {
441 						bin = strdup(cp);
442 					} else {
443 						olen = strlen(bin);
444 						nlen = strlen(cp);
445 						bin = realloc(bin,
446 							      olen + nlen + 2);
447 						if (bin == 0)
448 							abort();
449 						strcat(bin, " ");
450 						strcat(bin, cp);
451 					}
452 					if (!opt_a) {
453 						free(cp);
454 						break;
455 					}
456 				}
457 				free(cp);
458 			}
459 		}
460 
461 		if (opt_m) {
462 			/*
463 			 * Ask the man command to perform the search for us.
464 			 */
465 			unusual = unusual | NO_MAN_FOUND;
466 			if (opt_a)
467 				cp = malloc(sizeof MANWHEREISALLCMD - 2 + s);
468 			else
469 				cp = malloc(sizeof MANWHEREISCMD - 2 + s);
470 
471 			if (cp == NULL)
472 				abort();
473 
474 			if (opt_a)
475 				sprintf(cp, MANWHEREISALLCMD, name);
476 			else
477 				sprintf(cp, MANWHEREISCMD, name);
478 
479 			if ((p = popen(cp, "r")) != NULL) {
480 
481 				while (fgets(buf, BUFSIZ - 1, p) != NULL) {
482 					unusual = unusual & ~NO_MAN_FOUND;
483 
484 					if ((cp2 = strchr(buf, '\n')) != NULL)
485 						*cp2 = '\0';
486 					if (regexec(&re, buf, 2,
487 						    matches, 0) == 0 &&
488 					    (rlen = matches[1].rm_eo -
489 					     matches[1].rm_so) > 0) {
490 						/*
491 						 * man -w found formated
492 						 * page, need to pick up
493 						 * source page name.
494 						 */
495 						cp2 = malloc(rlen + 1);
496 						if (cp2 == NULL)
497 							abort();
498 						memcpy(cp2,
499 						       buf + matches[1].rm_so,
500 						       rlen);
501 						cp2[rlen] = '\0';
502 					} else {
503 						/*
504 						 * man -w found plain source
505 						 * page, use it.
506 						 */
507 						s = strlen(buf);
508 						cp2 = malloc(s + 1);
509 						if (cp2 == NULL)
510 							abort();
511 						strcpy(cp2, buf);
512 					}
513 
514 					if (man == NULL) {
515 						man = strdup(cp2);
516 					} else {
517 						olen = strlen(man);
518 						nlen = strlen(cp2);
519 						man = realloc(man,
520 							      olen + nlen + 2);
521 						if (man == 0)
522 							abort();
523 						strcat(man, " ");
524 						strcat(man, cp2);
525 					}
526 
527 					free(cp2);
528 
529 					if (!opt_a)
530 						break;
531 				}
532 				pclose(p);
533 				free(cp);
534 			}
535 		}
536 
537 		if (opt_s) {
538 			/*
539 			 * Sources match if a subdir with the exact
540 			 * name is found.
541 			 */
542 			unusual = unusual | NO_SRC_FOUND;
543 			for (dp = sourcedirs; *dp != NULL; dp++) {
544 				cp = malloc(strlen(*dp) + 1 + s + 1);
545 				if (cp == NULL)
546 					abort();
547 				strcpy(cp, *dp);
548 				strcat(cp, "/");
549 				strcat(cp, name);
550 				if (stat(cp, &sb) == 0 &&
551 				    (sb.st_mode & S_IFMT) == S_IFDIR) {
552 					unusual = unusual & ~NO_SRC_FOUND;
553 					if (src == NULL) {
554 						src = strdup(cp);
555 					} else {
556 						olen = strlen(src);
557 						nlen = strlen(cp);
558 						src = realloc(src,
559 							      olen + nlen + 2);
560 						if (src == 0)
561 							abort();
562 						strcat(src, " ");
563 						strcat(src, cp);
564 					}
565 					if (!opt_a) {
566 						free(cp);
567 						break;
568 					}
569 				}
570 				free(cp);
571 			}
572 			/*
573 			 * If still not found, ask locate to search it
574 			 * for us.  This will find sources for things
575 			 * like lpr that are well hidden in the
576 			 * /usr/src tree, but takes a lot longer.
577 			 * Thus, option -x (`expensive') prevents this
578 			 * search.
579 			 *
580 			 * Do only match locate output that starts
581 			 * with one of our source directories, and at
582 			 * least one further level of subdirectories.
583 			 */
584 			if (opt_x || (src && !opt_a))
585 				goto done_sources;
586 
587 			cp = malloc(sizeof LOCATECMD - 2 + s);
588 			if (cp == NULL)
589 				abort();
590 			sprintf(cp, LOCATECMD, name);
591 			if ((p = popen(cp, "r")) == NULL)
592 				goto done_sources;
593 			while ((src == NULL || opt_a) &&
594 			       (fgets(buf, BUFSIZ - 1, p)) != NULL) {
595 				if ((cp2 = strchr(buf, '\n')) != NULL)
596 					*cp2 = '\0';
597 				for (dp = sourcedirs;
598 				     (src == NULL || opt_a) && *dp != NULL;
599 				     dp++) {
600 					cp2 = malloc(strlen(*dp) + 9);
601 					if (cp2 == NULL)
602 						abort();
603 					strcpy(cp2, "^");
604 					strcat(cp2, *dp);
605 					strcat(cp2, "/[^/]+/");
606 					if ((i = regcomp(&re2, cp2,
607 							 REG_EXTENDED|REG_NOSUB))
608 					    != 0) {
609 						regerror(i, &re, buf,
610 							 BUFSIZ - 1);
611 						errx(EX_UNAVAILABLE,
612 						     "regcomp(%s) failed: %s",
613 						     cp2, buf);
614 					}
615 					free(cp2);
616 					if (regexec(&re2, buf, 0,
617 						    (regmatch_t *)NULL, 0)
618 					    == 0) {
619 						unusual = unusual &
620 						          ~NO_SRC_FOUND;
621 						if (src == NULL) {
622 							src = strdup(buf);
623 						} else {
624 							olen = strlen(src);
625 							nlen = strlen(buf);
626 							src = realloc(src,
627 								      olen +
628 								      nlen + 2);
629 							if (src == 0)
630 								abort();
631 							strcat(src, " ");
632 							strcat(src, buf);
633 						}
634 					}
635 					regfree(&re2);
636 				}
637 			}
638 			pclose(p);
639 			free(cp);
640 		}
641 	  done_sources:
642 
643 		if (opt_u && !unusual)
644 			continue;
645 
646 		printed = 0;
647 		if (!opt_q) {
648 			printf("%s:", name);
649 			printed++;
650 		}
651 		if (bin) {
652 			if (printed++)
653 				putchar(' ');
654 			fputs(bin, stdout);
655 		}
656 		if (man) {
657 			if (printed++)
658 				putchar(' ');
659 			fputs(man, stdout);
660 		}
661 		if (src) {
662 			if (printed++)
663 				putchar(' ');
664 			fputs(src, stdout);
665 		}
666 		if (printed)
667 			putchar('\n');
668 	}
669 
670 	if (opt_m)
671 		regfree(&re);
672 
673 	return (0);
674 }
675