less verbose...
[unix-history] / usr / src / usr.bin / whatis / whatis.c
CommitLineData
9273b7be
KB
1/*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17
18#ifndef lint
19char copyright[] =
20"@(#) Copyright (c) 1987 Regents of the University of California.\n\
21 All rights reserved.\n";
22#endif /* not lint */
23
24#ifndef lint
4ec3c3c0 25static char sccsid[] = "@(#)whatis.c 5.2 (Berkeley) %G%";
9273b7be
KB
26#endif /* not lint */
27
28#include <sys/param.h>
29#include <stdio.h>
30#include <ctype.h>
31#include <strings.h>
32
33#define DEF_PATH "/usr/man:/usr/new/man:/usr/local/man"
34#define MAXLINELEN 1000 /* max line handled */
35#define WHATIS "whatis" /* database name */
36
37main(argc, argv)
38 int argc;
39 char **argv;
40{
41 extern char *optarg;
42 extern int optind;
43 register char *beg, *end, **p;
44 int ch, foundman = 0, *found;
45 char *manpath, buf[MAXLINELEN + 1], fname[MAXPATHLEN + 1];
46 char wbuf[MAXLINELEN + 1], *getenv(), *malloc();
47
48 while ((ch = getopt(argc, argv, "M:P:")) != EOF)
49 switch((char)ch) {
50 case 'M':
51 case 'P': /* backward compatible */
52 manpath = optarg;
53 break;
54 case '?':
55 default:
56 usage();
57 }
58 argv += optind;
59 argc -= optind;
60 if (argc < 1)
61 usage();
62
63 if (!(manpath = getenv("MANPATH")))
64 manpath = DEF_PATH;
65
66 /*NOSTRICT*/
67 if (!(found = (int *)malloc((u_int)argc))) {
68 fprintf(stderr, "whatis: out of space.\n");
69 exit(1);
70 }
71 bzero((char *)found, argc * sizeof(int));
72
73 for (p = argv; *p; ++p) /* trim full paths */
74 if (beg = rindex(*p, '/'))
75 *p = beg + 1;
76
77 for (beg = manpath; beg; beg = end) { /* through path list */
78 end = index(beg, ':');
79 if (!end)
80 (void)sprintf(fname, "%s/%s", beg, WHATIS);
81 else {
82 (void)sprintf(fname, "%.*s/%s", end - beg, beg, WHATIS);
83 ++end;
84 }
85 if (!freopen(fname, "r", stdin))
86 continue;
87
88 /* for each file found */
89 for (foundman = 1; fgets(buf, sizeof(buf), stdin);) {
90 dashtrunc(buf, wbuf);
91 for (p = argv; *p; ++p)
92 if (match(wbuf, *p)) {
93 printf("%s", buf);
94 found[p - argv] = 1;
95
96 /* only print line once */
97 while (*++p)
98 if (match(wbuf, *p))
99 found[p - argv] = 1;
100 break;
101 }
102 }
103 }
104 if (!foundman) {
105 fprintf(stderr, "whatis: no %s file found in %s.\n",
106 WHATIS, manpath);
107 exit(1);
108 }
109 for (p = argv; *p; ++p)
110 if (!found[p - argv])
111 printf("%s: not found\n", *p);
112}
113
114/*
115 * match --
116 * match a full word
117 */
118match(bp, str)
119 register char *bp, *str;
120{
121 register int len;
122 register char *start;
123
124 if (!*str || !*bp)
125 return(0);
126 for (len = strlen(str);;) {
127 for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
128 if (!*bp)
129 break;
4ec3c3c0
KB
130 for (start = bp++;
131 *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
9273b7be
KB
132 if (bp - start == len && !strncasecmp(start, str, len))
133 return(1);
134 }
135 return(0);
136}
137
138/*
139 * dashtrunc --
140 * truncate a string at " - "
141 */
142dashtrunc(from, to)
143 register char *from, *to;
144{
145 register int ch;
146
147 for (; (ch = *from) && ch != '\n' &&
148 (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
149 *to++ = ch;
150 *to = '\0';
151}
152
153/*
154 * usage --
155 * print usage message and die
156 */
157usage()
158{
159 fprintf(stderr, "usage: whatis [-M path] string ...\n");
160 exit(1);
161}