135535Sbostic /* 235535Sbostic * Copyright (c) 1987 Regents of the University of California. 335535Sbostic * All rights reserved. 435535Sbostic * 535535Sbostic * Redistribution and use in source and binary forms are permitted 635535Sbostic * provided that the above copyright notice and this paragraph are 735535Sbostic * duplicated in all such forms and that any documentation, 835535Sbostic * advertising materials, and other materials related to such 935535Sbostic * distribution and use acknowledge that the software was developed 1035535Sbostic * by the University of California, Berkeley. The name of the 1135535Sbostic * University may not be used to endorse or promote products derived 1235535Sbostic * from this software without specific prior written permission. 1335535Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1435535Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1535535Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1635535Sbostic */ 1735535Sbostic 1835535Sbostic #ifndef lint 1935535Sbostic char copyright[] = 2035535Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\ 2135535Sbostic All rights reserved.\n"; 2235535Sbostic #endif /* not lint */ 2335535Sbostic 2435535Sbostic #ifndef lint 25*40387Sbostic static char sccsid[] = "@(#)whatis.c 5.4 (Berkeley) 03/11/90"; 2635535Sbostic #endif /* not lint */ 2735535Sbostic 2835535Sbostic #include <sys/param.h> 2935535Sbostic #include <stdio.h> 3035535Sbostic #include <ctype.h> 31*40387Sbostic #include <string.h> 32*40387Sbostic #include "../man/pathnames.h" 3335535Sbostic 34*40387Sbostic #define MAXLINELEN 256 /* max line handled */ 3535535Sbostic 36*40387Sbostic int *found, foundman; 37*40387Sbostic char *progname; 38*40387Sbostic 3935535Sbostic main(argc, argv) 4035535Sbostic int argc; 4135535Sbostic char **argv; 4235535Sbostic { 4335535Sbostic extern char *optarg; 4435535Sbostic extern int optind; 45*40387Sbostic register char *beg, **p; 46*40387Sbostic int ch; 47*40387Sbostic char *p_augment, *p_path, *config(), *getenv(), *malloc(); 4835535Sbostic 49*40387Sbostic progname = "whatis"; 50*40387Sbostic while ((ch = getopt(argc, argv, "M:m:P:")) != EOF) 5135535Sbostic switch((char)ch) { 5235535Sbostic case 'M': 5335535Sbostic case 'P': /* backward compatible */ 54*40387Sbostic p_path = optarg; 5535535Sbostic break; 56*40387Sbostic case 'm': 57*40387Sbostic p_augment = optarg; 58*40387Sbostic break; 5935535Sbostic case '?': 6035535Sbostic default: 6135535Sbostic usage(); 6235535Sbostic } 6335535Sbostic argv += optind; 6435535Sbostic argc -= optind; 65*40387Sbostic 6635535Sbostic if (argc < 1) 6735535Sbostic usage(); 6835535Sbostic 69*40387Sbostic if (!p_path && !(p_path = getenv("MANPATH"))) 70*40387Sbostic p_path = config(); 7135535Sbostic 7235535Sbostic /*NOSTRICT*/ 73*40387Sbostic if (!(found = (int *)malloc((u_int)argc))) 74*40387Sbostic enomem(); 7535535Sbostic bzero((char *)found, argc * sizeof(int)); 7635535Sbostic 7735535Sbostic for (p = argv; *p; ++p) /* trim full paths */ 7835535Sbostic if (beg = rindex(*p, '/')) 7935535Sbostic *p = beg + 1; 8035535Sbostic 81*40387Sbostic if (p_augment) 82*40387Sbostic whatis(argv, p_augment); 83*40387Sbostic if (p_path) 84*40387Sbostic whatis(argv, p_path); 85*40387Sbostic 86*40387Sbostic if (!foundman) { 87*40387Sbostic fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS); 88*40387Sbostic exit(1); 89*40387Sbostic } 90*40387Sbostic for (p = argv; *p; ++p) 91*40387Sbostic if (!found[p - argv]) 92*40387Sbostic printf("%s: not found\n", *p); 93*40387Sbostic } 94*40387Sbostic 95*40387Sbostic whatis(argv, path) 96*40387Sbostic char **argv, *path; 97*40387Sbostic { 98*40387Sbostic register char *beg, *end, **p; 99*40387Sbostic char fname[MAXPATHLEN + 1]; 100*40387Sbostic char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1]; 101*40387Sbostic 102*40387Sbostic for (beg = path; beg; beg = end) { /* through path list */ 10335535Sbostic end = index(beg, ':'); 10435535Sbostic if (!end) 105*40387Sbostic (void)sprintf(fname, "%s/%s", beg, _PATH_WHATIS); 10635535Sbostic else { 107*40387Sbostic (void)sprintf(fname, "%.*s/%s", end - beg, beg, 108*40387Sbostic _PATH_WHATIS); 10935535Sbostic ++end; 11035535Sbostic } 11135535Sbostic if (!freopen(fname, "r", stdin)) 11235535Sbostic continue; 11335535Sbostic 11435535Sbostic /* for each file found */ 11535535Sbostic for (foundman = 1; fgets(buf, sizeof(buf), stdin);) { 11635535Sbostic dashtrunc(buf, wbuf); 11735535Sbostic for (p = argv; *p; ++p) 11835535Sbostic if (match(wbuf, *p)) { 11935535Sbostic printf("%s", buf); 12035535Sbostic found[p - argv] = 1; 12135535Sbostic 12235535Sbostic /* only print line once */ 12335535Sbostic while (*++p) 12435535Sbostic if (match(wbuf, *p)) 12535535Sbostic found[p - argv] = 1; 12635535Sbostic break; 12735535Sbostic } 12835535Sbostic } 12935535Sbostic } 13035535Sbostic } 13135535Sbostic 13235535Sbostic /* 13335535Sbostic * match -- 13435535Sbostic * match a full word 13535535Sbostic */ 13635535Sbostic match(bp, str) 13735535Sbostic register char *bp, *str; 13835535Sbostic { 13935535Sbostic register int len; 14035535Sbostic register char *start; 14135535Sbostic 14235535Sbostic if (!*str || !*bp) 14335535Sbostic return(0); 14435535Sbostic for (len = strlen(str);;) { 14535535Sbostic for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp); 14635535Sbostic if (!*bp) 14735535Sbostic break; 14835536Sbostic for (start = bp++; 14935536Sbostic *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp); 15035535Sbostic if (bp - start == len && !strncasecmp(start, str, len)) 15135535Sbostic return(1); 15235535Sbostic } 15335535Sbostic return(0); 15435535Sbostic } 15535535Sbostic 15635535Sbostic /* 15735535Sbostic * dashtrunc -- 15835535Sbostic * truncate a string at " - " 15935535Sbostic */ 16035535Sbostic dashtrunc(from, to) 16135535Sbostic register char *from, *to; 16235535Sbostic { 16335535Sbostic register int ch; 16435535Sbostic 16535535Sbostic for (; (ch = *from) && ch != '\n' && 16635535Sbostic (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from) 16735535Sbostic *to++ = ch; 16835535Sbostic *to = '\0'; 16935535Sbostic } 17035535Sbostic 17135535Sbostic /* 17235535Sbostic * usage -- 17335535Sbostic * print usage message and die 17435535Sbostic */ 17535535Sbostic usage() 17635535Sbostic { 177*40387Sbostic (void)fprintf(stderr, 178*40387Sbostic "usage: whatis [-M path] [-m path] command ...\n"); 17935535Sbostic exit(1); 18035535Sbostic } 181