|
|
1.1 root 1: /*
2: * apropos.c
3: *
4: * implement a simple version of the Berkeley command "apropos". This
5: * command searches the file /usr/man/man.index for a word that the
6: * user enters on the command line, and prints every index entry in which
7: * that token appears.
8: *
9: * Usage: apropos token
10: *
11: * first draft, fwb, 3/17/1993 (aye and begorah!)
12: */
13: #ifdef TEST
14: # define INDEXFILE "/v/fwb/doc/coherent/man.index"
15: #else
16: # define INDEXFILE "/usr/man/man.index"
17: #endif
18:
19: #define BUFFERSIZE 200
20:
21: #include <ctype.h>
22: #include <stdio.h>
23: #include <string.h>
24: #include <stdlib.h>
25:
26: main(argc, argv)
27: int argc; char **argv;
28: {
29: char inb[BUFFERSIZE], testb[BUFFERSIZE], *inbp;
30: int i, j, lowerflag;
31: FILE *fp;
32:
33: if (argc < 2) {
34: fprintf(stderr, "Usage: apropos word\n");
35: exit(EXIT_FAILURE);
36: }
37:
38: if ((fp = fopen(INDEXFILE, "r")) == NULL) {
39: fprintf(stderr, "apropos: Cannot open index file %s\n",
40: INDEXFILE);
41: exit(EXIT_FAILURE);
42: }
43:
44: /* if first character of user's word is upper-cased, then assume
45: * the user wants an exact match, and therefore do not
46: * shift the description into lower case.
47: */
48:
49: if (isupper(argv[1][0]))
50: lowerflag = 0;
51: else
52: lowerflag = 1;
53:
54: for (i = 1; fgets(inb, BUFFERSIZE-1, fp) != NULL; i++) {
55: if ((inbp = strchr(inb, '\t')) == NULL) {
56: /* blank lines are OK */
57: if (*inb != '\n' && *inb != '\0') {
58: fprintf(stderr,
59: "apropos: badly formed line %d in %s\n",
60: i, INDEXFILE);
61: exit(EXIT_FAILURE);
62: } else
63: continue;
64: }
65: inbp++;
66:
67: strcpy (testb, inbp);
68:
69: if (lowerflag)
70: for (j = 0; j < strlen (testb); j++)
71: if (isupper(testb[j]))
72: testb[j] = tolower(testb[j]);
73:
74: if (strstr(testb, argv[1]) != NULL)
75: printf("%s", inbp);
76: }
77: fclose(fp);
78: exit(EXIT_SUCCESS);
79: }
80:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.