xref: /dragonfly/usr.bin/whereis/whereis.c (revision 9f7604d7)
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  * $FreeBSD: src/usr.bin/whereis/whereis.c,v 1.12 2002/08/22 01:50:51 johan Exp $
25  */
26 
27 /*
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.
32  */
33 
34 #include <sys/types.h>
35 
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 	errx(EX_USAGE,
92 	     "usage: whereis [-abmqsux] [-BMS dir... -f] name ...");
93 }
94 
95 /*
96  * Scan options passed to program.
97  *
98  * Note that the -B/-M/-S options expect a list of directory
99  * names that must be terminated with -f.
100  */
101 void
102 scanopts(int argc, char **argv)
103 {
104 	int c, i;
105 	ccharp **dirlist;
106 
107 	while ((c = getopt(argc, argv, "BMSabfmqsux")) != -1)
108 		switch (c) {
109 		case 'B':
110 			dirlist = &bindirs;
111 			goto dolist;
112 
113 		case 'M':
114 			dirlist = &mandirs;
115 			goto dolist;
116 
117 		case 'S':
118 			dirlist = &sourcedirs;
119 		  dolist:
120 			i = 0;
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);
129 				optind++;
130 			}
131 			break;
132 
133 		case 'a':
134 			opt_a = 1;
135 			break;
136 
137 		case 'b':
138 			opt_b = 1;
139 			break;
140 
141 		case 'f':
142 			goto breakout;
143 
144 		case 'm':
145 			opt_m = 1;
146 			break;
147 
148 		case 'q':
149 			opt_q = 1;
150 			break;
151 
152 		case 's':
153 			opt_s = 1;
154 			break;
155 
156 		case 'u':
157 			opt_u = 1;
158 			break;
159 
160 		case 'x':
161 			opt_x = 1;
162 			break;
163 
164 		default:
165 			usage();
166 		}
167 breakout:
168 	if (optind == argc)
169 		usage();
170 	query = argv + optind;
171 }
172 
173 /*
174  * Find out whether string `s' is contained in list `cpp'.
175  */
176 int
177 contains(ccharp *cpp, const char *s)
178 {
179 	ccharp cp;
180 
181 	if (cpp == NULL)
182 		return (0);
183 
184 	while ((cp = *cpp) != NULL) {
185 		if (strcmp(cp, s) == 0)
186 			return (1);
187 		cpp++;
188 	}
189 	return (0);
190 }
191 
192 /*
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.
198  */
199 void
200 decolonify(char *s, ccharp **cppp, int *ip)
201 {
202 	char *cp;
203 
204 	while ((cp = strchr(s, ':')), *s != '\0') {
205 		if (cp)
206 			*cp = '\0';
207 		if (strlen(s) && !contains(*cppp, s)) {
208 			*cppp = realloc(*cppp, (*ip + 2) * sizeof(char *));
209 			if (cppp == NULL)
210 				abort();
211 			(*cppp)[*ip] = s;
212 			(*cppp)[*ip + 1] = NULL;
213 			(*ip)++;
214 		}
215 		if (cp)
216 			s = cp + 1;
217 		else
218 			break;
219 	}
220 }
221 
222 /*
223  * Join string list `cpp' into a colon-separated string.
224  */
225 char *
226 colonify(ccharp *cpp)
227 {
228 	size_t s;
229 	char *cp;
230 	int i;
231 
232 	if (cpp == NULL)
233 		return (0);
234 
235 	for (s = 0, i = 0; cpp[i] != NULL; i++)
236 		s += strlen(cpp[i]) + 1;
237 	if ((cp = malloc(s + 1)) == NULL)
238 		abort();
239 	for (i = 0, *cp = '\0'; cpp[i] != NULL; i++) {
240 		strcat(cp, cpp[i]);
241 		strcat(cp, ":");
242 	}
243 	cp[s - 1] = '\0';		/* eliminate last colon */
244 
245 	return (cp);
246 }
247 
248 /*
249  * Provide defaults for all options and directory lists.
250  */
251 void
252 defaults(void)
253 {
254 	size_t s;
255 	char *b, buf[BUFSIZ], *cp;
256 	int nele;
257 	FILE *p;
258 	DIR *dir;
259 	struct stat sb;
260 	struct dirent *dirp;
261 
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;
265 
266 	/* -b defaults to default path + /usr/libexec +
267 	 * /usr/games + user's path */
268 	if (!bindirs) {
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)
272 			abort();
273 		if (sysctlbyname("user.cs_path", b, &s, NULL, 0) == -1)
274 			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
275 		nele = 0;
276 		decolonify(b, &bindirs, &nele);
277 		bindirs = realloc(bindirs, (nele + 3) * sizeof(char *));
278 		if (bindirs == NULL)
279 			abort();
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)
286 				abort();
287 			strcpy(b, cp);
288 			decolonify(b, &bindirs, &nele);
289 		}
290 	}
291 
292 	/* -m defaults to $(manpath) */
293 	if (!mandirs) {
294 		if ((p = popen(MANPATHCMD, "r")) == NULL)
295 			err(EX_OSERR, "cannot execute manpath command");
296 		if (fgets(buf, BUFSIZ - 1, p) == NULL ||
297 		    pclose(p))
298 			err(EX_OSERR, "error processing manpath results");
299 		if ((b = strchr(buf, '\n')) != NULL)
300 			*b = '\0';
301 		if ((b = malloc(strlen(buf) + 1)) == NULL)
302 			abort();
303 		strcpy(b, buf);
304 		nele = 0;
305 		decolonify(b, &mandirs, &nele);
306 	}
307 
308 	/*
309 	 * -s defaults to precompiled list, plus subdirs of /usr/dports and
310 	 * /usr/pkgsrc
311 	 */
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_DPORTS, &sb) == -1) {
320 			if (errno != ENOENT)
321 				err(EX_OSERR, "stat(" PATH_DPORTS ")");
322 		} else {
323 			if ((sb.st_mode & S_IFMT) != S_IFDIR)
324 				/* /usr/dports is not a directory, ignore */
325 				return;
326 			if (access(PATH_DPORTS, R_OK | X_OK) != 0)
327 				return;
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 */
333 					continue;
334 				b = malloc(sizeof PATH_DPORTS + 1 +
335 				    dirp->d_namlen);
336 				if (b == NULL)
337 					abort();
338 				strcpy(b, PATH_DPORTS);
339 				strcat(b, "/");
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) {
344 					free(b);
345 					continue;
346 				}
347 				sourcedirs = realloc(sourcedirs,
348 				    (nele + 2) * sizeof(char *));
349 				if (sourcedirs == NULL)
350 					abort();
351 				sourcedirs[nele++] = b;
352 				sourcedirs[nele] = NULL;
353 			}
354 			closedir(dir);
355 		}
356 		if (stat(PATH_PKGSRC, &sb) == -1) {
357 			if (errno == ENOENT)
358 				/* no /usr/pkgsrc, we are done */
359 				return;
360 			err(EX_OSERR, "stat(" PATH_PKGSRC ")");
361 		}
362 		if ((sb.st_mode & S_IFMT) != S_IFDIR)
363 			/* /usr/pkgsrc is not a directory, ignore */
364 			return;
365 		if (access(PATH_PKGSRC, R_OK | X_OK) != 0)
366 			return;
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 */
373 				continue;
374 			if ((b = malloc(sizeof PATH_PKGSRC + 1 + dirp->d_namlen))
375 			    == NULL)
376 				abort();
377 			strcpy(b, PATH_PKGSRC);
378 			strcat(b, "/");
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) {
383 				free(b);
384 				continue;
385 			}
386 			sourcedirs = realloc(sourcedirs,
387 					     (nele + 2) * sizeof(char *));
388 			if (sourcedirs == NULL)
389 				abort();
390 			sourcedirs[nele++] = b;
391 			sourcedirs[nele] = NULL;
392 		}
393 		closedir(dir);
394 	}
395 }
396 
397 int
398 main(int argc, char **argv)
399 {
400 	int unusual, i, printed;
401 	char *bin, buf[BUFSIZ], *cp, *cp2, *man, *name, *src;
402 	ccharp *dp;
403 	size_t nlen, olen, s;
404 	struct stat sb;
405 	regex_t re, re2;
406 	regmatch_t matches[2];
407 	regoff_t rlen;
408 	FILE *p;
409 
410 	setlocale(LC_ALL, "");
411 	scanopts(argc, argv);
412 	defaults();
413 
414 	if (mandirs == NULL)
415 		opt_m = 0;
416 	if (bindirs == NULL)
417 		opt_b = 0;
418 	if (sourcedirs == NULL)
419 		opt_s = 0;
420 	if (opt_m + opt_b + opt_s == 0)
421 		errx(EX_DATAERR, "no directories to search");
422 
423 	if (opt_m) {
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);
430 		}
431 	}
432 
433 	for (; (name = *query) != NULL; query++) {
434 		/* strip leading path name component */
435 		if ((cp = strrchr(name, '/')) != NULL)
436 			name = cp + 1;
437 		/* strip SCCS or RCS suffix/prefix */
438 		if (strlen(name) > 2 && strncmp(name, "s.", 2) == 0)
439 			name += 2;
440 		if ((s = strlen(name)) > 2 && strcmp(name + s - 2, ",v") == 0)
441 			name[s - 2] = '\0';
442 		/* compression suffix */
443 		s = strlen(name);
444 		if (s > 2 &&
445 		    (strcmp(name + s - 2, ".z") == 0 ||
446 		     strcmp(name + s - 2, ".Z") == 0))
447 			name[s - 2] = '\0';
448 		else if (s > 3 &&
449 			 strcmp(name + s - 3, ".gz") == 0)
450 			name[s - 3] = '\0';
451 		else if (s > 4 &&
452 			 strcmp(name + s - 4, ".bz2") == 0)
453 			name[s - 4] = '\0';
454 
455 		unusual = 0;
456 		bin = man = src = NULL;
457 		s = strlen(name);
458 
459 		if (opt_b) {
460 			/*
461 			 * Binaries have to match exactly, and must be regular
462 			 * executable files.
463 			 */
464 			unusual = unusual | NO_BIN_FOUND;
465 			for (dp = bindirs; *dp != NULL; dp++) {
466 				cp = malloc(strlen(*dp) + 1 + s + 1);
467 				if (cp == NULL)
468 					abort();
469 				strcpy(cp, *dp);
470 				strcat(cp, "/");
471 				strcat(cp, name);
472 				if (stat(cp, &sb) == 0 &&
473 				    (sb.st_mode & S_IFMT) == S_IFREG &&
474 				    (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
475 				    != 0) {
476 					unusual = unusual & ~NO_BIN_FOUND;
477 					if (bin == NULL) {
478 						bin = strdup(cp);
479 					} else {
480 						olen = strlen(bin);
481 						nlen = strlen(cp);
482 						bin = realloc(bin,
483 							      olen + nlen + 2);
484 						if (bin == NULL)
485 							abort();
486 						strcat(bin, " ");
487 						strcat(bin, cp);
488 					}
489 					if (!opt_a) {
490 						free(cp);
491 						break;
492 					}
493 				}
494 				free(cp);
495 			}
496 		}
497 
498 		if (opt_m) {
499 			/*
500 			 * Ask the man command to perform the search for us.
501 			 */
502 			unusual = unusual | NO_MAN_FOUND;
503 			if (opt_a)
504 				cp = malloc(sizeof MANWHEREISALLCMD - 2 + s);
505 			else
506 				cp = malloc(sizeof MANWHEREISCMD - 2 + s);
507 
508 			if (cp == NULL)
509 				abort();
510 
511 			if (opt_a)
512 				sprintf(cp, MANWHEREISALLCMD, name);
513 			else
514 				sprintf(cp, MANWHEREISCMD, name);
515 
516 			if ((p = popen(cp, "r")) != NULL) {
517 
518 				while (fgets(buf, BUFSIZ - 1, p) != NULL) {
519 					unusual = unusual & ~NO_MAN_FOUND;
520 
521 					if ((cp2 = strchr(buf, '\n')) != NULL)
522 						*cp2 = '\0';
523 					if (regexec(&re, buf, 2,
524 						    matches, 0) == 0 &&
525 					    (rlen = matches[1].rm_eo -
526 					     matches[1].rm_so) > 0) {
527 						/*
528 						 * man -w found formated
529 						 * page, need to pick up
530 						 * source page name.
531 						 */
532 						cp2 = malloc(rlen + 1);
533 						if (cp2 == NULL)
534 							abort();
535 						memcpy(cp2,
536 						       buf + matches[1].rm_so,
537 						       rlen);
538 						cp2[rlen] = '\0';
539 					} else {
540 						/*
541 						 * man -w found plain source
542 						 * page, use it.
543 						 */
544 						s = strlen(buf);
545 						cp2 = malloc(s + 1);
546 						if (cp2 == NULL)
547 							abort();
548 						strcpy(cp2, buf);
549 					}
550 
551 					if (man == NULL) {
552 						man = strdup(cp2);
553 					} else {
554 						olen = strlen(man);
555 						nlen = strlen(cp2);
556 						man = realloc(man,
557 							      olen + nlen + 2);
558 						if (man == NULL)
559 							abort();
560 						strcat(man, " ");
561 						strcat(man, cp2);
562 					}
563 
564 					free(cp2);
565 
566 					if (!opt_a)
567 						break;
568 				}
569 				pclose(p);
570 				free(cp);
571 			}
572 		}
573 
574 		if (opt_s) {
575 			/*
576 			 * Sources match if a subdir with the exact
577 			 * name is found.
578 			 */
579 			unusual = unusual | NO_SRC_FOUND;
580 			for (dp = sourcedirs; *dp != NULL; dp++) {
581 				cp = malloc(strlen(*dp) + 1 + s + 1);
582 				if (cp == NULL)
583 					abort();
584 				strcpy(cp, *dp);
585 				strcat(cp, "/");
586 				strcat(cp, name);
587 				if (stat(cp, &sb) == 0 &&
588 				    (sb.st_mode & S_IFMT) == S_IFDIR) {
589 					unusual = unusual & ~NO_SRC_FOUND;
590 					if (src == NULL) {
591 						src = strdup(cp);
592 					} else {
593 						olen = strlen(src);
594 						nlen = strlen(cp);
595 						src = realloc(src,
596 							      olen + nlen + 2);
597 						if (src == NULL)
598 							abort();
599 						strcat(src, " ");
600 						strcat(src, cp);
601 					}
602 					if (!opt_a) {
603 						free(cp);
604 						break;
605 					}
606 				}
607 				free(cp);
608 			}
609 			/*
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
615 			 * search.
616 			 *
617 			 * Do only match locate output that starts
618 			 * with one of our source directories, and at
619 			 * least one further level of subdirectories.
620 			 */
621 			if (opt_x || (src && !opt_a))
622 				goto done_sources;
623 
624 			cp = malloc(sizeof LOCATECMD - 2 + s);
625 			if (cp == NULL)
626 				abort();
627 			sprintf(cp, LOCATECMD, name);
628 			if ((p = popen(cp, "r")) == NULL)
629 				goto done_sources;
630 			while ((src == NULL || opt_a) &&
631 			       (fgets(buf, BUFSIZ - 1, p)) != NULL) {
632 				if ((cp2 = strchr(buf, '\n')) != NULL)
633 					*cp2 = '\0';
634 				for (dp = sourcedirs;
635 				     (src == NULL || opt_a) && *dp != NULL;
636 				     dp++) {
637 					cp2 = malloc(strlen(*dp) + 9);
638 					if (cp2 == NULL)
639 						abort();
640 					strcpy(cp2, "^");
641 					strcat(cp2, *dp);
642 					strcat(cp2, "/[^/]+/");
643 					if ((i = regcomp(&re2, cp2,
644 							 REG_EXTENDED|REG_NOSUB))
645 					    != 0) {
646 						regerror(i, &re, buf,
647 							 BUFSIZ - 1);
648 						errx(EX_UNAVAILABLE,
649 						     "regcomp(%s) failed: %s",
650 						     cp2, buf);
651 					}
652 					free(cp2);
653 					if (regexec(&re2, buf, 0, NULL, 0)
654 					    == 0) {
655 						unusual = unusual &
656 						          ~NO_SRC_FOUND;
657 						if (src == NULL) {
658 							src = strdup(buf);
659 						} else {
660 							olen = strlen(src);
661 							nlen = strlen(buf);
662 							src = realloc(src,
663 								      olen +
664 								      nlen + 2);
665 							if (src == NULL)
666 								abort();
667 							strcat(src, " ");
668 							strcat(src, buf);
669 						}
670 					}
671 					regfree(&re2);
672 				}
673 			}
674 			pclose(p);
675 			free(cp);
676 		}
677 done_sources:
678 
679 		if (opt_u && !unusual)
680 			continue;
681 
682 		printed = 0;
683 		if (!opt_q) {
684 			printf("%s:", name);
685 			printed++;
686 		}
687 		if (bin) {
688 			if (printed++)
689 				putchar(' ');
690 			fputs(bin, stdout);
691 		}
692 		if (man) {
693 			if (printed++)
694 				putchar(' ');
695 			fputs(man, stdout);
696 		}
697 		if (src) {
698 			if (printed++)
699 				putchar(' ');
700 			fputs(src, stdout);
701 		}
702 		if (printed)
703 			putchar('\n');
704 	}
705 
706 	if (opt_m)
707 		regfree(&re);
708 
709 	return (0);
710 }
711