aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/whereis/whereis.c
blob: 84584b2244c512fb5558c33da0db1ad26cc6b870 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * Copyright © 2002, Jörg Wunsch
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * 4.3BSD UI-compatible whereis(1) utility.  Rewritten from scratch
 * since the original 4.3BSD version suffers legal problems that
 * prevent it from being redistributed, and since the 4.4BSD version
 * was pretty inferior in functionality.
 */

#include <sys/types.h>

__FBSDID("$FreeBSD$");

#include <sys/stat.h>
#include <sys/sysctl.h>

#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#include "pathnames.h"

typedef const char *ccharp;

int opt_b, opt_m, opt_q, opt_s, opt_u, opt_x;
ccharp *bindirs, *mandirs, *sourcedirs;
char **query;

const char *sourcepath = PATH_SOURCES;

char	*colonify(ccharp *);
int	 contains(ccharp *, const char *);
void	 decolonify(char *, ccharp **, int *);
void	 defaults(void);
void	 scanopts(int, char **);
void	 usage(void);

/*
 * Throughout this program, a number of strings are dynamically
 * allocated but never freed.  Their memory is written to when
 * splitting the strings into string lists which will later be
 * processed.  Since it's important that those string lists remain
 * valid even after the functions allocating the memory returned,
 * those functions cannot free them.  They could be freed only at end
 * of main(), which is pretty pointless anyway.
 *
 * The overall amount of memory to be allocated for processing the
 * strings is not expected to exceed a few kilobytes.  For that
 * reason, allocation can usually always be assumed to succeed (within
 * a virtual memory environment), thus we simply bail out using
 * abort(3) in case of an allocation failure.
 */

void
usage(void)
{
	errx(EX_USAGE,
	     "usage: whereis [-bmqsux] [-BMS dir... -f] name ...");
}

/*
 * Scan options passed to program.
 *
 * Note that the -B/-M/-S options expect a list of directory
 * names that must be terminated with -f.
 */
void
scanopts(int argc, char **argv)
{
	int c, i, opt_f;
	ccharp **dirlist;

	opt_f = 0;
	while ((c = getopt(argc, argv, "BMSbfmqsux")) != -1)
		switch (c) {
		case 'B':
			dirlist = &bindirs;
			goto dolist;

		case 'M':
			dirlist = &mandirs;
			goto dolist;

		case 'S':
			dirlist = &sourcedirs;
		  dolist:
			i = 0;
			while (optind < argc &&
			       strcmp(argv[optind], "-f") != 0 &&
			       strcmp(argv[optind], "-B") != 0 &&
			       strcmp(argv[optind], "-M") != 0 &&
			       strcmp(argv[optind], "-S") != 0) {
				*dirlist = realloc(*dirlist,
						   (i + 2) * sizeof(char *));
				if (*dirlist == NULL)
					abort();
				(*dirlist)[i] = argv[optind];
				i++;
				optind++;
			}
			(*dirlist)[i] = NULL;
			break;

		case 'b':
			opt_b = 1;
			break;

		case 'f':
			goto breakout;

		case 'm':
			opt_m = 1;
			break;

		case 'q':
			opt_q = 1;
			break;

		case 's':
			opt_s = 1;
			break;

		case 'u':
			opt_u = 1;
			break;

		case 'x':
			opt_x = 1;
			break;

		default:
			usage();
		}
  breakout:
	if (optind == argc)
		usage();
	query = argv + optind;
}

/*
 * Find out whether string `s' is contained in list `cpp'.
 */
int
contains(ccharp *cpp, const char *s)
{
	ccharp cp;

	if (cpp == NULL)
		return (0);

	while ((cp = *cpp) != NULL) {
		if (strcmp(cp, s) == 0)
			return (1);
		cpp++;
	}
	return (0);
}

/*
 * Split string `s' at colons, and pass it to the string list pointed
 * to by `cppp' (which has `*ip' elements).  Note that the original
 * string is modified by replacing the colon with a NUL byte.  The
 * partial string is only added if it has a length greater than 0, and
 * if it's not already contained in the string list.
 */
void
decolonify(char *s, ccharp **cppp, int *ip)
{
	char *cp;

	while ((cp = strchr(s, ':')), *s != '\0') {
		if (cp)
			*cp = '\0';
		if (strlen(s) && !contains(*cppp, s)) {
			*cppp = realloc(*cppp, (*ip + 2) * sizeof(char *));
			if (cppp == NULL)
				abort();
			(*cppp)[*ip] = s;
			(*cppp)[*ip + 1] = NULL;
			(*ip)++;
		}
		if (cp)
			s = cp + 1;
		else
			break;
	}
}

/*
 * Join string list `cpp' into a colon-separated string.
 */
char *
colonify(ccharp *cpp)
{
	size_t s;
	char *cp;
	int i;

	if (cpp == NULL)
		return (0);

	for (s = 0, i = 0; cpp[i] != NULL; i++)
		s += strlen(cpp[i]) + 1;
	if ((cp = malloc(s + 1)) == NULL)
		abort();
	for (i = 0, *cp = '\0'; cpp[i] != NULL; i++) {
		strcat(cp, cpp[i]);
		strcat(cp, ":");
	}
	cp[s - 1] = '\0';		/* eliminate last colon */

	return (cp);
}

/*
 * Provide defaults for all options and directory lists.
 */
void
defaults(void)
{
	size_t s;
	char *b, buf[BUFSIZ], *cp;
	int nele;
	FILE *p;
	DIR *dir;
	struct stat sb;
	struct dirent *dirp;

	/* default to -bms if none has been specified */
	if (!opt_b && !opt_m && !opt_s)
		opt_b = opt_m = opt_s = 1;

	/* -b defaults to default path + /usr/libexec + user's path */
	if (!bindirs) {
		if (sysctlbyname("user.cs_path", (void *)NULL, &s,
				 (void *)NULL, 0) == -1)
			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
		if ((b = malloc(s + 1)) == NULL)
			abort();
		if (sysctlbyname("user.cs_path", b, &s, (void *)NULL, 0) == -1)
			err(EX_OSERR, "sysctlbyname(\"user.cs_path\")");
		nele = 0;
		decolonify(b, &bindirs, &nele);
		bindirs = realloc(bindirs, (nele + 2) * sizeof(char *));
		if (bindirs == NULL)
			abort();
		bindirs[nele++] = "/usr/libexec";
		bindirs[nele] = NULL;
		if ((cp = getenv("PATH")) != NULL) {
			/* don't destroy the original environment... */
			if ((b = malloc(strlen(cp) + 1)) == NULL)
				abort();
			strcpy(b, cp);
			decolonify(b, &bindirs, &nele);
		}
	}

	/* -m defaults to $(manpath) */
	if (!mandirs) {
		if ((p = popen(MANPATHCMD, "r")) == NULL)
			err(EX_OSERR, "cannot execute manpath command");
		if (fgets(buf, BUFSIZ - 1, p) == NULL ||
		    pclose(p))
			err(EX_OSERR, "error processing manpath results");
		if ((b = strchr(buf, '\n')) != NULL)
			*b = '\0';
		if ((b = malloc(strlen(buf) + 1)) == NULL)
			abort();
		strcpy(b, buf);
		nele = 0;
		decolonify(b, &mandirs, &nele);
	}

	/* -s defaults to precompiled list, plus subdirs of /usr/ports */
	if (!sourcedirs) {
		if ((b = malloc(strlen(sourcepath) + 1)) == NULL)
			abort();
		strcpy(b, sourcepath);
		nele = 0;
		decolonify(b, &sourcedirs, &nele);

		if (stat(PATH_PORTS, &sb) == -1) {
			if (errno == ENOENT)
				/* no /usr/ports, we are done */
				return;
			err(EX_OSERR, "stat(" PATH_PORTS ")");
		}
		if ((sb.st_mode & S_IFMT) != S_IFDIR)
			/* /usr/ports is not a directory, ignore */
			return;
		if (access(PATH_PORTS, R_OK | X_OK) != 0)
			return;
		if ((dir = opendir(PATH_PORTS)) == NULL)
			err(EX_OSERR, "opendir" PATH_PORTS ")");
		while ((dirp = readdir(dir)) != NULL) {
			if (dirp->d_name[0] == '.' ||
			    strcmp(dirp->d_name, "CVS") == 0)
				/* ignore dot entries and CVS subdir */
				continue;
			if ((b = malloc(sizeof PATH_PORTS + 1 + dirp->d_namlen))
			    == NULL)
				abort();
			strcpy(b, PATH_PORTS);
			strcat(b, "/");
			strcat(b, dirp->d_name);
			if (stat(b, &sb) == -1 ||
			    (sb.st_mode & S_IFMT) != S_IFDIR ||
			    access(b, R_OK | X_OK) != 0) {
				free(b);
				continue;
			}
			sourcedirs = realloc(sourcedirs,
					     (nele + 2) * sizeof(char *));
			if (sourcedirs == NULL)
				abort();
			sourcedirs[nele++] = b;
			sourcedirs[nele] = NULL;
		}
		closedir(dir);
	}
}

int
main(int argc, char **argv)
{
	int unusual, i, printed;
	char *bin, buf[BUFSIZ], *cp, *cp2, *man, *name, *src;
	ccharp *dp;
	size_t s;
	struct stat sb;
	regex_t re, re2;
	regmatch_t matches[2];
	regoff_t rlen;
	FILE *p;

	scanopts(argc, argv);
	defaults();

	if (mandirs == NULL)
		opt_m = 0;
	if (bindirs == NULL)
		opt_b = 0;
	if (sourcedirs == NULL)
		opt_s = 0;
	if (opt_m + opt_b + opt_s == 0)
		errx(EX_DATAERR, "no directories to search");

	if (opt_m) {
		setenv("MANPATH", colonify(mandirs), 1);
		if ((i = regcomp(&re, MANWHEREISMATCH, REG_EXTENDED)) != 0) {
			regerror(i, &re, buf, BUFSIZ - 1);
			errx(EX_UNAVAILABLE, "regcomp(%s) failed: %s",
			     MANWHEREISMATCH, buf);
		}
	}

	for (; (name = *query) != NULL; query++) {
		/* strip leading path name component */
		if ((cp = strrchr(name, '/')) != NULL)
			name = cp + 1;
		/* strip SCCS or RCS suffix/prefix */
		if (strlen(name) > 2 && strncmp(name, "s.", 2) == 0)
			name += 2;
		if ((s = strlen(name)) > 2 && strcmp(name + s - 2, ",v") == 0)
			name[s - 2] = '\0';
		/* compression suffix */
		s = strlen(name);
		if (s > 2 &&
		    (strcmp(name + s - 2, ".z") == 0 ||
		     strcmp(name + s - 2, ".Z") == 0))
			name[s - 2] = '\0';
		else if (s > 3 &&
			 strcmp(name + s - 3, ".gz") == 0)
			name[s - 3] = '\0';
		else if (s > 4 &&
			 strcmp(name + s - 4, ".bz2") == 0)
			name[s - 4] = '\0';

		unusual = 0;
		bin = man = src = NULL;
		s = strlen(name);

		if (opt_b) {
			/*
			 * Binaries have to match exactly, and must be regular
			 * executable files.
			 */
			unusual++;
			for (dp = bindirs; *dp != NULL; dp++) {
				cp = malloc(strlen(*dp) + 1 + s + 1);
				if (cp == NULL)
					abort();
				strcpy(cp, *dp);
				strcat(cp, "/");
				strcat(cp, name);
				if (stat(cp, &sb) == 0 &&
				    (sb.st_mode & S_IFMT) == S_IFREG &&
				    (sb.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
				    != 0) {
					unusual--;
					bin = cp;
					break;
				}
				free(cp);
			}
		}

		if (opt_m) {
			/*
			 * Ask the man command to perform the search for us.
			 */
			unusual++;
			cp = malloc(sizeof MANWHEREISCMD - 2 + s);
			if (cp == NULL)
				abort();
			sprintf(cp, MANWHEREISCMD, name);
			if ((p = popen(cp, "r")) != NULL &&
			    fgets(buf, BUFSIZ - 1, p) != NULL &&
			    pclose(p) == 0) {
				unusual--;
				if ((cp2 = strchr(buf, '\n')) != NULL)
					*cp2 = '\0';
				if (regexec(&re, buf, 2, matches, 0) == 0 &&
				    (rlen = matches[1].rm_eo - matches[1].rm_so)
				    > 0) {
					/*
					 * man -w found compressed
					 * page, need to pick up
					 * source page name.
					 */
					cp2 = malloc(rlen + 1);
					if (cp2 == NULL)
						abort();
					memcpy(cp2, buf + matches[1].rm_so,
					       rlen);
					cp2[rlen] = '\0';
					man = cp2;
				} else {
					/*
					 * man -w found plain source
					 * page, use it.
					 */
					s = strlen(buf);
					cp2 = malloc(s + 1);
					if (cp2 == NULL)
						abort();
					strcpy(cp2, buf);
					man = cp2;
				}
			}
			free(cp);
		}

		if (opt_s) {
			/*
			 * Sources match if a subdir with the exact
			 * name is found.
			 */
			unusual++;
			for (dp = sourcedirs; *dp != NULL; dp++) {
				cp = malloc(strlen(*dp) + 1 + s + 1);
				if (cp == NULL)
					abort();
				strcpy(cp, *dp);
				strcat(cp, "/");
				strcat(cp, name);
				if (stat(cp, &sb) == 0 &&
				    (sb.st_mode & S_IFMT) == S_IFDIR) {
					unusual--;
					src = cp;
					break;
				}
				free(cp);
			}
			/*
			 * If still not found, ask locate to search it
			 * for us.  This will find sources for things
			 * like lpr that are well hidden in the
			 * /usr/src tree, but takes a lot longer.
			 * Thus, option -x (`expensive') prevents this
			 * search.
			 *
			 * Do only match locate output that starts
			 * with one of our source directories, and at
			 * least one further level of subdirectories.
			 */
			if (opt_x || src)
				goto done_sources;

			cp = malloc(sizeof LOCATECMD - 2 + s);
			if (cp == NULL)
				abort();
			sprintf(cp, LOCATECMD, name);
			if ((p = popen(cp, "r")) == NULL)
				goto done_sources;
			while (src == NULL &&
			       (fgets(buf, BUFSIZ - 1, p)) != NULL) {
				if ((cp2 = strchr(buf, '\n')) != NULL)
					*cp2 = '\0';
				for (dp = sourcedirs;
				     src == NULL && *dp != NULL;
				     dp++) {
					cp2 = malloc(strlen(*dp) + 9);
					if (cp2 == NULL)
						abort();
					strcpy(cp2, "^");
					strcat(cp2, *dp);
					strcat(cp2, "/[^/]+/");
					if ((i = regcomp(&re2, cp2,
							 REG_EXTENDED|REG_NOSUB))
					    != 0) {
						regerror(i, &re, buf,
							 BUFSIZ - 1);
						errx(EX_UNAVAILABLE,
						     "regcomp(%s) failed: %s",
						     cp2, buf);
					}
					free(cp2);
					if (regexec(&re2, buf, 0,
						    (regmatch_t *)NULL, 0)
					    == 0) {
						unusual--;
						src = buf;
					}
					regfree(&re2);
				}
			}
			pclose(p);
			free(cp);
		}
	  done_sources:

		if (opt_u && !unusual)
			continue;

		printed = 0;
		if (!opt_q) {
			printf("%s:", name);
			printed++;
		}
		if (bin) {
			if (printed++)
				putchar(' ');
			fputs(bin, stdout);
		}
		if (man) {
			if (printed++)
				putchar(' ');
			fputs(man, stdout);
		}
		if (src) {
			if (printed++)
				putchar(' ');
			fputs(src, stdout);
		}
		if (printed)
			putchar('\n');
	}

	if (opt_m)
		regfree(&re);

	return (0);
}