]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/whereis.c
Merge branch 'help' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / misc-utils / whereis.c
1 /*-
2 * Copyright (c) 1980 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * 1999-02-22 Arkadiusz Miƛkiewicz <misiek@pld.ORG.PL>
34 * - added Native Language Support
35 * 2011-08-12 Davidlohr Bueso <dave@gnu.org>
36 * - added $PATH lookup
37 *
38 * Copyright (C) 2013 Karel Zak <kzak@redhat.com>
39 * 2013 Sami Kerola <kerolasa@iki.fi>
40 */
41
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <dirent.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <ctype.h>
50 #include <assert.h>
51
52 #include "xalloc.h"
53 #include "nls.h"
54 #include "c.h"
55 #include "closestream.h"
56 #include "canonicalize.h"
57
58 #include "debug.h"
59
60 static UL_DEBUG_DEFINE_MASK(whereis);
61 UL_DEBUG_DEFINE_MASKNAMES(whereis) = UL_DEBUG_EMPTY_MASKNAMES;
62
63 #define WHEREIS_DEBUG_INIT (1 << 1)
64 #define WHEREIS_DEBUG_PATH (1 << 2)
65 #define WHEREIS_DEBUG_ENV (1 << 3)
66 #define WHEREIS_DEBUG_ARGV (1 << 4)
67 #define WHEREIS_DEBUG_SEARCH (1 << 5)
68 #define WHEREIS_DEBUG_STATIC (1 << 6)
69 #define WHEREIS_DEBUG_LIST (1 << 7)
70 #define WHEREIS_DEBUG_ALL 0xFFFF
71
72 #define DBG(m, x) __UL_DBG(whereis, WHEREIS_DEBUG_, m, x)
73 #define ON_DBG(m, x) __UL_DBG_CALL(whereis, WHEREIS_DEBUG_, m, x)
74
75 static char uflag = 0;
76
77 /* supported types */
78 enum {
79 BIN_DIR = (1 << 1),
80 MAN_DIR = (1 << 2),
81 SRC_DIR = (1 << 3),
82
83 ALL_DIRS = BIN_DIR | MAN_DIR | SRC_DIR
84 };
85
86 /* directories */
87 struct wh_dirlist {
88 int type;
89 dev_t st_dev;
90 ino_t st_ino;
91 char *path;
92
93 struct wh_dirlist *next;
94 };
95
96 static const char *bindirs[] = {
97 "/usr/bin",
98 "/usr/sbin",
99 "/usr/lib",
100 "/usr/lib64",
101 "/bin",
102 "/sbin",
103 "/etc",
104 "/usr/etc",
105 "/lib",
106 "/lib64",
107 "/usr/games",
108 "/usr/games/bin",
109 "/usr/games/lib",
110 "/usr/emacs/etc",
111 "/usr/lib/emacs/*/etc",
112 "/usr/TeX/bin",
113 "/usr/tex/bin",
114 "/usr/interviews/bin/LINUX",
115
116 "/usr/X11R6/bin",
117 "/usr/X386/bin",
118 "/usr/bin/X11",
119 "/usr/X11/bin",
120 "/usr/X11R5/bin",
121
122 "/usr/local/bin",
123 "/usr/local/sbin",
124 "/usr/local/etc",
125 "/usr/local/lib",
126 "/usr/local/games",
127 "/usr/local/games/bin",
128 "/usr/local/emacs/etc",
129 "/usr/local/TeX/bin",
130 "/usr/local/tex/bin",
131 "/usr/local/bin/X11",
132
133 "/usr/contrib",
134 "/usr/hosts",
135 "/usr/include",
136
137 "/usr/g++-include",
138
139 "/usr/ucb",
140 "/usr/old",
141 "/usr/new",
142 "/usr/local",
143 "/usr/libexec",
144 "/usr/share",
145
146 "/opt/*/bin",
147 NULL
148 };
149
150 static const char *mandirs[] = {
151 "/usr/man/*",
152 "/usr/share/man/*",
153 "/usr/X386/man/*",
154 "/usr/X11/man/*",
155 "/usr/TeX/man/*",
156 "/usr/interviews/man/mann",
157 "/usr/share/info",
158 NULL
159 };
160
161 static const char *srcdirs[] = {
162 "/usr/src/*",
163 "/usr/src/lib/libc/*",
164 "/usr/src/lib/libc/net/*",
165 "/usr/src/ucb/pascal",
166 "/usr/src/ucb/pascal/utilities",
167 "/usr/src/undoc",
168 NULL
169 };
170
171 static void whereis_init_debug(void)
172 {
173 __UL_INIT_DEBUG(whereis, WHEREIS_DEBUG_, 0, WHEREIS_DEBUG);
174 }
175
176 static const char *whereis_type_to_name(int type)
177 {
178 switch (type) {
179 case BIN_DIR: return "bin";
180 case MAN_DIR: return "man";
181 case SRC_DIR: return "src";
182 default: return "???";
183 }
184 }
185
186 static void __attribute__((__noreturn__)) usage(void)
187 {
188 FILE *out = stdout;
189
190 fputs(USAGE_HEADER, out);
191 fprintf(out, _(" %s [options] [-BMS <dir>... -f] <name>\n"), program_invocation_short_name);
192
193 fputs(USAGE_SEPARATOR, out);
194 fputs(_("Locate the binary, source, and manual-page files for a command.\n"), out);
195
196 fputs(USAGE_OPTIONS, out);
197 fputs(_(" -b search only for binaries\n"), out);
198 fputs(_(" -B <dirs> define binaries lookup path\n"), out);
199 fputs(_(" -m search only for manuals and infos\n"), out);
200 fputs(_(" -M <dirs> define man and info lookup path\n"), out);
201 fputs(_(" -s search only for sources\n"), out);
202 fputs(_(" -S <dirs> define sources lookup path\n"), out);
203 fputs(_(" -f terminate <dirs> argument list\n"), out);
204 fputs(_(" -u search for unusual entries\n"), out);
205 fputs(_(" -l output effective lookup paths\n"), out);
206
207 fputs(USAGE_SEPARATOR, out);
208 printf(USAGE_HELP_OPTIONS(16));
209 printf(USAGE_MAN_TAIL("whereis(1)"));
210 exit(EXIT_SUCCESS);
211 }
212
213 static void dirlist_add_dir(struct wh_dirlist **ls0, int type, const char *dir)
214 {
215 struct stat st;
216 struct wh_dirlist *prev = NULL, *ls = *ls0;
217
218 if (access(dir, R_OK) != 0)
219 return;
220 if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
221 return;
222
223 while (ls) {
224 if (ls->st_ino == st.st_ino &&
225 ls->st_dev == st.st_dev &&
226 ls->type == type) {
227 DBG(LIST, ul_debugobj(*ls0, " ignore (already in list): %s", dir));
228 return;
229 }
230 prev = ls;
231 ls = ls->next;
232 }
233
234
235 ls = xcalloc(1, sizeof(*ls));
236 ls->st_ino = st.st_ino;
237 ls->st_dev = st.st_dev;
238 ls->type = type;
239 ls->path = canonicalize_path(dir);
240
241 if (!*ls0)
242 *ls0 = ls; /* first in the list */
243 else {
244 assert(prev);
245 prev->next = ls; /* add to the end of the list */
246 }
247
248 DBG(LIST, ul_debugobj(*ls0, " add dir: %s", ls->path));
249 return;
250 }
251
252 /* special case for '*' in the paths */
253 static void dirlist_add_subdir(struct wh_dirlist **ls, int type, const char *dir)
254 {
255 char buf[PATH_MAX], *d;
256 DIR *dirp;
257 struct dirent *dp;
258
259 strncpy(buf, dir, PATH_MAX);
260 buf[PATH_MAX - 1] = '\0';
261
262 d = strchr(buf, '*');
263 if (!d)
264 return;
265 *d = 0;
266
267 dirp = opendir(buf);
268 if (!dirp)
269 return;
270
271 DBG(LIST, ul_debugobj(*ls, " scanning subdir: %s", dir));
272
273 while ((dp = readdir(dirp)) != NULL) {
274 if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
275 continue;
276 snprintf(d, PATH_MAX - (d - buf), "%s", dp->d_name);
277 /* a dir definition can have a star in middle of path */
278 strcat(buf, strchr(dir, '*') + 1);
279 dirlist_add_dir(ls, type, buf);
280 }
281 closedir(dirp);
282 return;
283 }
284
285 static void construct_dirlist_from_env(const char *env,
286 struct wh_dirlist **ls,
287 int type)
288 {
289 char *key = NULL, *tok = NULL, *pathcp, *path = getenv(env);
290
291 if (!path)
292 return;
293 pathcp = xstrdup(path);
294
295 DBG(ENV, ul_debugobj(*ls, "construct %s dirlist from: %s",
296 whereis_type_to_name(type), path));
297
298 for (tok = strtok_r(pathcp, ":", &key); tok;
299 tok = strtok_r(NULL, ":", &key))
300 dirlist_add_dir(ls, type, tok);
301
302 free(pathcp);
303 return;
304 }
305
306 static void construct_dirlist_from_argv(struct wh_dirlist **ls,
307 int *idx,
308 int argc,
309 char *argv[],
310 int type)
311 {
312 int i;
313
314 DBG(ARGV, ul_debugobj(*ls, "construct %s dirlist from argv[%d..]",
315 whereis_type_to_name(type), *idx));
316
317 for (i = *idx; i < argc; i++) {
318 if (*argv[i] == '-') /* end of the list */
319 break;
320
321 DBG(ARGV, ul_debugobj(*ls, " using argv[%d]: %s", *idx, argv[*idx]));
322 dirlist_add_dir(ls, type, argv[i]);
323 *idx = i;
324 }
325
326 return;
327 }
328
329 static void construct_dirlist(struct wh_dirlist **ls,
330 int type,
331 const char **paths)
332 {
333 size_t i;
334
335 DBG(STATIC, ul_debugobj(*ls, "construct %s dirlist from static array",
336 whereis_type_to_name(type)));
337
338 for (i = 0; paths[i]; i++) {
339 if (!strchr(paths[i], '*'))
340 dirlist_add_dir(ls, type, paths[i]);
341 else
342 dirlist_add_subdir(ls, type, paths[i]);
343 }
344 return;
345 }
346
347 static void free_dirlist(struct wh_dirlist **ls0, int type)
348 {
349 struct wh_dirlist *prev = NULL, *next, *ls = *ls0;
350
351 *ls0 = NULL;
352
353 DBG(LIST, ul_debugobj(*ls0, "free dirlist"));
354
355 while (ls) {
356 if (ls->type & type) {
357 next = ls->next;
358 DBG(LIST, ul_debugobj(*ls0, " free: %s", ls->path));
359 free(ls->path);
360 free(ls);
361 ls = next;
362 if (prev)
363 prev->next = ls;
364 } else {
365 if (!prev)
366 *ls0 = ls; /* first unremoved */
367 prev = ls;
368 ls = ls->next;
369 }
370 }
371
372 return;
373 }
374
375
376 static int filename_equal(const char *cp, const char *dp)
377 {
378 int i = strlen(dp);
379
380 DBG(SEARCH, ul_debug("compare '%s' and '%s'", cp, dp));
381
382 if (dp[0] == 's' && dp[1] == '.' && filename_equal(cp, dp + 2))
383 return 1;
384 if (!strcmp(dp + i - 2, ".Z"))
385 i -= 2;
386 else if (!strcmp(dp + i - 3, ".gz"))
387 i -= 3;
388 else if (!strcmp(dp + i - 3, ".xz"))
389 i -= 3;
390 else if (!strcmp(dp + i - 4, ".bz2"))
391 i -= 4;
392 while (*cp && *dp && *cp == *dp)
393 cp++, dp++, i--;
394 if (*cp == 0 && *dp == 0)
395 return 1;
396 while (isdigit(*dp))
397 dp++;
398 if (*cp == 0 && *dp++ == '.') {
399 --i;
400 while (i > 0 && *dp)
401 if (--i, *dp++ == '.')
402 return (*dp++ == 'C' && *dp++ == 0);
403 return 1;
404 }
405 return 0;
406 }
407
408 static void findin(const char *dir, const char *pattern, int *count, char **wait)
409 {
410 DIR *dirp;
411 struct dirent *dp;
412
413 dirp = opendir(dir);
414 if (dirp == NULL)
415 return;
416
417 DBG(SEARCH, ul_debug("find '%s' in '%s'", pattern, dir));
418
419 while ((dp = readdir(dirp)) != NULL) {
420 if (!filename_equal(pattern, dp->d_name))
421 continue;
422
423 if (uflag && *count == 0)
424 xasprintf(wait, "%s/%s", dir, dp->d_name);
425
426 else if (uflag && *count == 1 && *wait) {
427 printf("%s: %s %s/%s", pattern, *wait, dir, dp->d_name);
428 free(*wait);
429 *wait = NULL;
430 } else
431 printf(" %s/%s", dir, dp->d_name);
432 ++(*count);
433 }
434 closedir(dirp);
435 return;
436 }
437
438 static void lookup(const char *pattern, struct wh_dirlist *ls, int want)
439 {
440 char patbuf[PATH_MAX];
441 int count = 0;
442 char *wait = NULL, *p;
443
444 /* canonicalize pattern -- remove path suffix etc. */
445 p = strrchr(pattern, '/');
446 p = p ? p + 1 : (char *) pattern;
447 strncpy(patbuf, p, PATH_MAX);
448 patbuf[PATH_MAX - 1] = '\0';
449
450 DBG(SEARCH, ul_debug("lookup dirs for '%s' (%s), want: %s %s %s",
451 patbuf, pattern,
452 want & BIN_DIR ? "bin" : "",
453 want & MAN_DIR ? "min" : "",
454 want & SRC_DIR ? "src" : ""));
455 p = strrchr(patbuf, '.');
456 if (p)
457 *p = '\0';
458
459 if (!uflag)
460 /* if -u not specified then we always print the pattern */
461 printf("%s:", patbuf);
462
463 for (; ls; ls = ls->next) {
464 if ((ls->type & want) && ls->path)
465 findin(ls->path, patbuf, &count, &wait);
466 }
467
468 free(wait);
469
470 if (!uflag || count > 1)
471 putchar('\n');
472 return;
473 }
474
475 static void list_dirlist(struct wh_dirlist *ls)
476 {
477 while (ls) {
478 if (ls->path) {
479 switch (ls->type) {
480 case BIN_DIR:
481 printf("bin: ");
482 break;
483 case MAN_DIR:
484 printf("man: ");
485 break;
486 case SRC_DIR:
487 printf("src: ");
488 break;
489 default:
490 abort();
491 }
492 printf("%s\n", ls->path);
493 }
494 ls = ls->next;
495 }
496 }
497
498 int main(int argc, char **argv)
499 {
500 struct wh_dirlist *ls = NULL;
501 int want = ALL_DIRS;
502 int i, want_resetable = 0, opt_f_missing = 0;
503
504 setlocale(LC_ALL, "");
505 bindtextdomain(PACKAGE, LOCALEDIR);
506 textdomain(PACKAGE);
507 atexit(close_stdout);
508
509 if (argc <= 1) {
510 warnx(_("not enough arguments"));
511 errtryhelp(EXIT_FAILURE);
512 } else {
513 /* first arg may be one of our standard longopts */
514 if (!strcmp(argv[1], "--help"))
515 usage();
516 if (!strcmp(argv[1], "--version")) {
517 printf(UTIL_LINUX_VERSION);
518 exit(EXIT_SUCCESS);
519 }
520 }
521
522 whereis_init_debug();
523
524 construct_dirlist(&ls, BIN_DIR, bindirs);
525 construct_dirlist_from_env("PATH", &ls, BIN_DIR);
526
527 construct_dirlist(&ls, MAN_DIR, mandirs);
528 construct_dirlist_from_env("MANPATH", &ls, MAN_DIR);
529
530 construct_dirlist(&ls, SRC_DIR, srcdirs);
531
532 for (i = 1; i < argc; i++) {
533 const char *arg = argv[i];
534 int arg_i = i;
535
536 DBG(ARGV, ul_debug("argv[%d]: %s", i, arg));
537
538 if (*arg != '-') {
539 lookup(arg, ls, want);
540 /*
541 * The lookup mask ("want") is cumulative and it's
542 * resetable only when it has been already used.
543 *
544 * whereis -b -m foo :'foo' mask=BIN|MAN
545 * whereis -b foo bar :'foo' and 'bar' mask=BIN|MAN
546 * whereis -b foo -m bar :'foo' mask=BIN; 'bar' mask=MAN
547 */
548 want_resetable = 1;
549 continue;
550 }
551
552 for (++arg; arg && *arg; arg++) {
553 DBG(ARGV, ul_debug(" arg: %s", arg));
554
555 switch (*arg) {
556 case 'f':
557 opt_f_missing = 0;
558 break;
559 case 'u':
560 uflag = 1;
561 opt_f_missing = 0;
562 break;
563 case 'B':
564 if (*(arg + 1)) {
565 warnx(_("bad usage"));
566 errtryhelp(EXIT_FAILURE);
567 }
568 i++;
569 free_dirlist(&ls, BIN_DIR);
570 construct_dirlist_from_argv(
571 &ls, &i, argc, argv, BIN_DIR);
572 opt_f_missing = 1;
573 break;
574 case 'M':
575 if (*(arg + 1)) {
576 warnx(_("bad usage"));
577 errtryhelp(EXIT_FAILURE);
578 }
579 i++;
580 free_dirlist(&ls, MAN_DIR);
581 construct_dirlist_from_argv(
582 &ls, &i, argc, argv, MAN_DIR);
583 opt_f_missing = 1;
584 break;
585 case 'S':
586 if (*(arg + 1)) {
587 warnx(_("bad usage"));
588 errtryhelp(EXIT_FAILURE);
589 }
590 i++;
591 free_dirlist(&ls, SRC_DIR);
592 construct_dirlist_from_argv(
593 &ls, &i, argc, argv, SRC_DIR);
594 opt_f_missing = 1;
595 break;
596 case 'b':
597 if (want_resetable) {
598 want = ALL_DIRS;
599 want_resetable = 0;
600 }
601 want = want == ALL_DIRS ? BIN_DIR : want | BIN_DIR;
602 opt_f_missing = 0;
603 break;
604 case 'm':
605 if (want_resetable) {
606 want = ALL_DIRS;
607 want_resetable = 0;
608 }
609 want = want == ALL_DIRS ? MAN_DIR : want | MAN_DIR;
610 opt_f_missing = 0;
611 break;
612 case 's':
613 if (want_resetable) {
614 want = ALL_DIRS;
615 want_resetable = 0;
616 }
617 want = want == ALL_DIRS ? SRC_DIR : want | SRC_DIR;
618 opt_f_missing = 0;
619 break;
620 case 'l':
621 list_dirlist(ls);
622 break;
623 case 'V':
624 printf(UTIL_LINUX_VERSION);
625 return EXIT_SUCCESS;
626 case 'h':
627 usage();
628 default:
629 warnx(_("bad usage"));
630 errtryhelp(EXIT_FAILURE);
631 }
632
633 if (arg_i < i) /* moved to the next argv[] item */
634 break;
635 }
636 }
637
638 free_dirlist(&ls, ALL_DIRS);
639 if (opt_f_missing)
640 errx(EXIT_FAILURE, _("option -f is missing"));
641 return EXIT_SUCCESS;
642 }