Commit | Line | Data |
---|---|---|
43f23b36 KB |
1 | /* |
2 | * Copyright (c) 1987 Regents of the University of California. | |
ea9afd82 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b36fc510 KB |
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. | |
43f23b36 KB |
16 | */ |
17 | ||
18 | #ifndef lint | |
19 | char copyright[] = | |
20 | "@(#) Copyright (c) 1987 Regents of the University of California.\n\ | |
21 | All rights reserved.\n"; | |
ea9afd82 | 22 | #endif /* not lint */ |
43f23b36 KB |
23 | |
24 | #ifndef lint | |
9273b7be | 25 | static char sccsid[] = "@(#)apropos.c 5.7 (Berkeley) %G%"; |
ea9afd82 | 26 | #endif /* not lint */ |
43f23b36 KB |
27 | |
28 | #include <sys/param.h> | |
29 | #include <stdio.h> | |
30 | #include <ctype.h> | |
76f1f97b | 31 | #include <strings.h> |
43f23b36 | 32 | |
beb0c7d1 | 33 | #define DEF_PATH "/usr/man:/usr/new/man:/usr/local/man" |
e39a3843 | 34 | #define MAXLINELEN 1000 /* max line handled */ |
43f23b36 | 35 | #define WHATIS "whatis" /* database name */ |
43f23b36 | 36 | |
43f23b36 | 37 | main(argc, argv) |
76f1f97b KB |
38 | int argc; |
39 | char **argv; | |
43f23b36 | 40 | { |
76f1f97b KB |
41 | extern char *optarg; |
42 | extern int optind; | |
9273b7be KB |
43 | register char *beg, *end, **p; |
44 | int ch, foundman = 0, *found; | |
45 | char *manpath, buf[MAXLINELEN + 1], fname[MAXPATHLEN + 1]; | |
76f1f97b | 46 | char wbuf[MAXLINELEN + 1], *getenv(), *malloc(); |
43f23b36 | 47 | |
43f23b36 KB |
48 | while ((ch = getopt(argc, argv, "M:P:")) != EOF) |
49 | switch((char)ch) { | |
9273b7be KB |
50 | case 'M': |
51 | case 'P': /* backward compatible */ | |
52 | manpath = optarg; | |
53 | break; | |
54 | case '?': | |
55 | default: | |
56 | usage(); | |
43f23b36 KB |
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))) { | |
9273b7be | 68 | fprintf(stderr, "apropos: out of space.\n"); |
43f23b36 KB |
69 | exit(1); |
70 | } | |
76f1f97b | 71 | bzero((char *)found, argc * sizeof(int)); |
43f23b36 | 72 | |
9273b7be KB |
73 | for (p = argv; *p; ++p) /* convert to lower-case */ |
74 | lowstr(*p, *p); | |
43f23b36 KB |
75 | for (beg = manpath; beg; beg = end) { /* through path list */ |
76 | end = index(beg, ':'); | |
77 | if (!end) | |
78 | (void)sprintf(fname, "%s/%s", beg, WHATIS); | |
79 | else { | |
80 | (void)sprintf(fname, "%.*s/%s", end - beg, beg, WHATIS); | |
81 | ++end; | |
82 | } | |
76f1f97b KB |
83 | if (!freopen(fname, "r", stdin)) |
84 | continue; | |
85 | ||
9273b7be KB |
86 | /* for each file found */ |
87 | for (foundman = 1; fgets(buf, sizeof(buf), stdin);) { | |
88 | lowstr(buf, wbuf); | |
89 | for (p = argv; *p; ++p) | |
90 | if (match(wbuf, *p)) { | |
91 | printf("%s", buf); | |
92 | found[p - argv] = 1; | |
76f1f97b KB |
93 | |
94 | /* only print line once */ | |
9273b7be KB |
95 | while (*++p) |
96 | if (match(wbuf, *p)) | |
97 | found[p - argv] = 1; | |
76f1f97b KB |
98 | break; |
99 | } | |
43f23b36 KB |
100 | } |
101 | } | |
102 | if (!foundman) { | |
9273b7be KB |
103 | fprintf(stderr, "apropos: no %s file found in %s.\n", |
104 | WHATIS, manpath); | |
43f23b36 KB |
105 | exit(1); |
106 | } | |
9273b7be KB |
107 | for (p = argv; *p; ++p) |
108 | if (!found[p - argv]) | |
109 | printf("%s: nothing appropriate\n", *p); | |
43f23b36 KB |
110 | } |
111 | ||
76f1f97b | 112 | /* |
9273b7be KB |
113 | * match -- |
114 | * match anywhere the string appears | |
76f1f97b | 115 | */ |
9273b7be | 116 | match(bp, str) |
76f1f97b | 117 | register char *bp, *str; |
43f23b36 | 118 | { |
76f1f97b KB |
119 | register int len; |
120 | register char test; | |
43f23b36 KB |
121 | |
122 | if (!*bp) | |
9273b7be | 123 | return(0); |
76f1f97b | 124 | /* backward compatible: everything matches empty string */ |
43f23b36 | 125 | if (!*str) |
9273b7be | 126 | return(1); |
76f1f97b KB |
127 | for (test = *str++, len = strlen(str); *bp;) |
128 | if (test == *bp++ && !strncmp(bp, str, len)) | |
9273b7be KB |
129 | return(1); |
130 | return(0); | |
76f1f97b KB |
131 | } |
132 | ||
133 | /* | |
134 | * lowstr -- | |
135 | * convert a string to lower case | |
136 | */ | |
43f23b36 | 137 | lowstr(from, to) |
76f1f97b | 138 | register char *from, *to; |
43f23b36 | 139 | { |
9273b7be KB |
140 | register char ch; |
141 | ||
142 | while ((ch = *from++) && ch != '\n') | |
143 | *to++ = isupper(ch) ? tolower(ch) : ch; | |
144 | *to = '\0'; | |
43f23b36 KB |
145 | } |
146 | ||
76f1f97b KB |
147 | /* |
148 | * usage -- | |
149 | * print usage message and die | |
150 | */ | |
43f23b36 KB |
151 | usage() |
152 | { | |
9273b7be | 153 | fprintf(stderr, "usage: apropos [-M path] string ...\n"); |
43f23b36 KB |
154 | exit(1); |
155 | } |