|
|
1.1 ! root 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: (1) source distributions retain this entire copyright ! 7: * notice and comment, and (2) distributions including binaries display ! 8: * the following acknowledgement: ``This product includes software ! 9: * developed by the University of California, Berkeley and its contributors'' ! 10: * in the documentation or other materials provided with the distribution ! 11: * and in all advertising materials mentioning features or use of this ! 12: * software. Neither the name of the University nor the names of its ! 13: * contributors may be used to endorse or promote products derived ! 14: * from this software without specific prior written permission. ! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR ! 16: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED ! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ! 18: */ ! 19: ! 20: #ifndef lint ! 21: char copyright[] = ! 22: "@(#) Copyright (c) 1987 Regents of the University of California.\n\ ! 23: All rights reserved.\n"; ! 24: #endif /* not lint */ ! 25: ! 26: #ifndef lint ! 27: static char sccsid[] = "@(#)whatis.c 5.6 (Berkeley) 6/1/90"; ! 28: #endif /* not lint */ ! 29: ! 30: #include <sys/param.h> ! 31: #include <stdio.h> ! 32: #include <ctype.h> ! 33: #include <string.h> ! 34: #include <stdlib.h> ! 35: #include "../man/pathnames.h" ! 36: ! 37: #define MAXLINELEN 256 /* max line handled */ ! 38: ! 39: char *progname; ! 40: ! 41: static int *found, foundman; ! 42: ! 43: main(argc, argv) ! 44: int argc; ! 45: char **argv; ! 46: { ! 47: extern char *optarg; ! 48: extern int optind; ! 49: register char *beg, **p; ! 50: int ch; ! 51: char *p_augment, *p_path, **getdb(); ! 52: ! 53: progname = "whatis"; ! 54: while ((ch = getopt(argc, argv, "M:m:P:")) != EOF) ! 55: switch((char)ch) { ! 56: case 'M': ! 57: case 'P': /* backward compatible */ ! 58: p_path = optarg; ! 59: break; ! 60: case 'm': ! 61: p_augment = optarg; ! 62: break; ! 63: case '?': ! 64: default: ! 65: usage(); ! 66: } ! 67: argv += optind; ! 68: argc -= optind; ! 69: ! 70: if (argc < 1) ! 71: usage(); ! 72: ! 73: /*NOSTRICT*/ ! 74: if (!(found = (int *)malloc((u_int)argc))) ! 75: enomem(); ! 76: bzero((char *)found, argc * sizeof(int)); ! 77: ! 78: for (p = argv; *p; ++p) /* trim full paths */ ! 79: if (beg = rindex(*p, '/')) ! 80: *p = beg + 1; ! 81: ! 82: if (p_augment) ! 83: whatis(argv, p_augment, 1); ! 84: if (p_path || (p_path = getenv("MANPATH"))) ! 85: whatis(argv, p_path, 1); ! 86: else ! 87: for (p = getdb(); *p; ++p) ! 88: whatis(argv, *p, 0); ! 89: ! 90: if (!foundman) { ! 91: fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS); ! 92: exit(1); ! 93: } ! 94: for (p = argv; *p; ++p) ! 95: if (!found[p - argv]) ! 96: printf("%s: not found\n", *p); ! 97: } ! 98: ! 99: whatis(argv, path, buildpath) ! 100: char **argv, *path; ! 101: int buildpath; ! 102: { ! 103: register char *end, *name, **p; ! 104: char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1]; ! 105: ! 106: for (name = path; name; name = end) { /* through name list */ ! 107: if (end = index(name, ':')) ! 108: *end++ = '\0'; ! 109: ! 110: if (buildpath) { ! 111: char hold[MAXPATHLEN + 1]; ! 112: ! 113: (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS); ! 114: name = hold; ! 115: } ! 116: ! 117: if (!freopen(name, "r", stdin)) ! 118: continue; ! 119: ! 120: foundman = 1; ! 121: ! 122: /* for each file found */ ! 123: while (fgets(buf, sizeof(buf), stdin)) { ! 124: dashtrunc(buf, wbuf); ! 125: for (p = argv; *p; ++p) ! 126: if (match(wbuf, *p)) { ! 127: printf("%s", buf); ! 128: found[p - argv] = 1; ! 129: ! 130: /* only print line once */ ! 131: while (*++p) ! 132: if (match(wbuf, *p)) ! 133: found[p - argv] = 1; ! 134: break; ! 135: } ! 136: } ! 137: } ! 138: } ! 139: ! 140: /* ! 141: * match -- ! 142: * match a full word ! 143: */ ! 144: match(bp, str) ! 145: register char *bp, *str; ! 146: { ! 147: register int len; ! 148: register char *start; ! 149: ! 150: if (!*str || !*bp) ! 151: return(0); ! 152: for (len = strlen(str);;) { ! 153: for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp); ! 154: if (!*bp) ! 155: break; ! 156: for (start = bp++; ! 157: *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp); ! 158: if (bp - start == len && !strncasecmp(start, str, len)) ! 159: return(1); ! 160: } ! 161: return(0); ! 162: } ! 163: ! 164: /* ! 165: * dashtrunc -- ! 166: * truncate a string at " - " ! 167: */ ! 168: dashtrunc(from, to) ! 169: register char *from, *to; ! 170: { ! 171: register int ch; ! 172: ! 173: for (; (ch = *from) && ch != '\n' && ! 174: (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from) ! 175: *to++ = ch; ! 176: *to = '\0'; ! 177: } ! 178: ! 179: /* ! 180: * usage -- ! 181: * print usage message and die ! 182: */ ! 183: usage() ! 184: { ! 185: (void)fprintf(stderr, ! 186: "usage: whatis [-M path] [-m path] command ...\n"); ! 187: exit(1); ! 188: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.