use fgets, not gets so we don't overflow; split apropos and whatis
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Mon, 19 Sep 1988 09:16:18 +0000 (01:16 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Mon, 19 Sep 1988 09:16:18 +0000 (01:16 -0800)
SCCS-vsn: usr.bin/apropos/apropos.c 5.7
SCCS-vsn: usr.bin/whatis/whatis.c 5.1

usr/src/usr.bin/apropos/apropos.c
usr/src/usr.bin/whatis/whatis.c [new file with mode: 0644]

index 7c9365a..ee0b761 100644 (file)
@@ -22,7 +22,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)apropos.c  5.6 (Berkeley) %G%";
+static char sccsid[] = "@(#)apropos.c  5.7 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -34,41 +34,26 @@ static char sccsid[] = "@(#)apropos.c       5.6 (Berkeley) %G%";
 #define        MAXLINELEN      1000                    /* max line handled */
 #define        WHATIS          "whatis"                /* database name */
 
 #define        MAXLINELEN      1000                    /* max line handled */
 #define        WHATIS          "whatis"                /* database name */
 
-#define        NO      0                               /* no/false */
-#define        YES     1                               /* yes/true */
-
-static char *myname;
-
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern char *optarg;
        extern int optind;
 main(argc, argv)
        int argc;
        char **argv;
 {
        extern char *optarg;
        extern int optind;
-       register char *beg, *end, **C;
-       int ch, foundman = NO, *found, isapropos;
-       int a_match(), w_match(), (*match)();
-       char *manpath = NULL, buf[MAXLINELEN + 1], fname[MAXPATHLEN + 1];
+       register char *beg, *end, **p;
+       int ch, foundman = 0, *found;
+       char *manpath, buf[MAXLINELEN + 1], fname[MAXPATHLEN + 1];
        char wbuf[MAXLINELEN + 1], *getenv(), *malloc();
 
        char wbuf[MAXLINELEN + 1], *getenv(), *malloc();
 
-       myname = (beg = rindex(*argv, '/')) ? beg + 1 : *argv;
-       if (!strcmp(myname, "apropos")) {
-               isapropos = YES;
-               match = a_match;
-       }
-       else {
-               isapropos = NO;
-               match = w_match;
-       }
        while ((ch = getopt(argc, argv, "M:P:")) != EOF)
                switch((char)ch) {
        while ((ch = getopt(argc, argv, "M:P:")) != EOF)
                switch((char)ch) {
-                       case 'M':
-                       case 'P':               /* backward contemptible */
-                               manpath = optarg;
-                               break;
-                       case '?':
-                       default:
-                               usage();
+               case 'M':
+               case 'P':               /* backward compatible */
+                       manpath = optarg;
+                       break;
+               case '?':
+               default:
+                       usage();
                }
        argv += optind;
        argc -= optind;
                }
        argv += optind;
        argc -= optind;
@@ -80,18 +65,13 @@ main(argc, argv)
 
        /*NOSTRICT*/
        if (!(found = (int *)malloc((u_int)argc))) {
 
        /*NOSTRICT*/
        if (!(found = (int *)malloc((u_int)argc))) {
-               fprintf(stderr, "%s: out of space.\n", myname);
+               fprintf(stderr, "apropos: out of space.\n");
                exit(1);
        }
        bzero((char *)found, argc * sizeof(int));
 
                exit(1);
        }
        bzero((char *)found, argc * sizeof(int));
 
-       if (isapropos)
-               for (C = argv; *C; ++C)         /* convert to lower-case */
-                       lowstr(*C, *C);
-       else for (C = argv; *C; ++C)            /* trim full paths */
-               if (beg = rindex(*C, '/'))
-                       *C = beg + 1;
-
+       for (p = argv; *p; ++p)                 /* convert to lower-case */
+               lowstr(*p, *p);
        for (beg = manpath; beg; beg = end) {   /* through path list */
                end = index(beg, ':');
                if (!end)
        for (beg = manpath; beg; beg = end) {   /* through path list */
                end = index(beg, ':');
                if (!end)
@@ -103,115 +83,73 @@ main(argc, argv)
                if (!freopen(fname, "r", stdin))
                        continue;
 
                if (!freopen(fname, "r", stdin))
                        continue;
 
-                                               /* for each file found */
-               for (foundman = YES; gets(buf);) {
-                       if (isapropos)
-                               lowstr(buf, wbuf);
-                       else
-                               dashtrunc(buf, wbuf);
-                       for (C = argv; *C; ++C)
-                               if ((*match)(wbuf, *C)) {
-                                       puts(buf);
-                                       found[C - argv] = YES;
+               /* for each file found */
+               for (foundman = 1; fgets(buf, sizeof(buf), stdin);) {
+                       lowstr(buf, wbuf);
+                       for (p = argv; *p; ++p)
+                               if (match(wbuf, *p)) {
+                                       printf("%s", buf);
+                                       found[p - argv] = 1;
 
                                        /* only print line once */
 
                                        /* only print line once */
-                                       while (*++C)
-                                               if ((*match)(wbuf, *C))
-                                                       found[C - argv] = YES;
+                                       while (*++p)
+                                               if (match(wbuf, *p))
+                                                       found[p - argv] = 1;
                                        break;
                                }
                }
        }
        if (!foundman) {
                                        break;
                                }
                }
        }
        if (!foundman) {
-               fprintf(stderr, "%s: no %s file found in %s.\n", myname, WHATIS, manpath);
+               fprintf(stderr, "apropos: no %s file found in %s.\n",
+                   WHATIS, manpath);
                exit(1);
        }
                exit(1);
        }
-       for (C = argv; *C; ++C)
-               if (!found[C - argv])
-                       printf("%s: %s\n", *C, isapropos ? "nothing appropriate" : "not found");
+       for (p = argv; *p; ++p)
+               if (!found[p - argv])
+                       printf("%s: nothing appropriate\n", *p);
 }
 
 /*
 }
 
 /*
- * a_match --
- *     match for apropos; anywhere the string appears
+ * match --
+ *     match anywhere the string appears
  */
  */
-static
-a_match(bp, str)
+match(bp, str)
        register char *bp, *str;
 {
        register int len;
        register char test;
 
        if (!*bp)
        register char *bp, *str;
 {
        register int len;
        register char test;
 
        if (!*bp)
-               return(NO);
+               return(0);
        /* backward compatible: everything matches empty string */
        if (!*str)
        /* backward compatible: everything matches empty string */
        if (!*str)
-               return(YES);
+               return(1);
        for (test = *str++, len = strlen(str); *bp;)
                if (test == *bp++ && !strncmp(bp, str, len))
        for (test = *str++, len = strlen(str); *bp;)
                if (test == *bp++ && !strncmp(bp, str, len))
-                       return(YES);
-       return(NO);
-}
-
-/*
- * w_match --
- *     match for whatis; looks for full word match
- */
-static
-w_match(bp, str)
-       register char *bp, *str;
-{
-       register int len;
-       register char *start;
-
-       if (!*str || !*bp)
-               return(NO);
-       for (len = strlen(str);;) {
-               for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
-               if (!*bp)
-                       break;
-               for (start = bp++; *bp && (isdigit(*bp) || isalpha(*bp)); ++bp);
-               if (bp - start == len && !strncasecmp(start, str, len))
-                       return(YES);
-       }
-       return(NO);
-}
-
-/*
- * dashtrunc --
- *     truncate a string at " - "
- */
-static
-dashtrunc(from, to)
-       register char *from, *to;
-{
-       do {
-               if (from[0] == ' ' && from[1] == '-' && from[2] == ' ')
-                       break;
-       } while (*to++ = *from++);
-       *to = '\0';
+                       return(1);
+       return(0);
 }
 
 /*
  * lowstr --
  *     convert a string to lower case
  */
 }
 
 /*
  * lowstr --
  *     convert a string to lower case
  */
-static
 lowstr(from, to)
        register char *from, *to;
 {
 lowstr(from, to)
        register char *from, *to;
 {
-       do {
-               *to++ = isupper(*from) ? tolower(*from) : *from;
-       } while (*from++);
+       register char ch;
+
+       while ((ch = *from++) && ch != '\n')
+               *to++ = isupper(ch) ? tolower(ch) : ch;
+       *to = '\0';
 }
 
 /*
  * usage --
  *     print usage message and die
  */
 }
 
 /*
  * usage --
  *     print usage message and die
  */
-static
 usage()
 {
 usage()
 {
-       fprintf(stderr, "usage: %s [-M path] string ...\n", myname);
+       fprintf(stderr, "usage: apropos [-M path] string ...\n");
        exit(1);
 }
        exit(1);
 }
diff --git a/usr/src/usr.bin/whatis/whatis.c b/usr/src/usr.bin/whatis/whatis.c
new file mode 100644 (file)
index 0000000..afb2c73
--- /dev/null
@@ -0,0 +1,160 @@
+/*
+ * Copyright (c) 1987 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1987 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)whatis.c   5.1 (Berkeley) %G%";
+#endif /* not lint */
+
+#include <sys/param.h>
+#include <stdio.h>
+#include <ctype.h>
+#include <strings.h>
+
+#define        DEF_PATH        "/usr/man:/usr/new/man:/usr/local/man"
+#define        MAXLINELEN      1000                    /* max line handled */
+#define        WHATIS          "whatis"                /* database name */
+
+main(argc, argv)
+       int argc;
+       char **argv;
+{
+       extern char *optarg;
+       extern int optind;
+       register char *beg, *end, **p;
+       int ch, foundman = 0, *found;
+       char *manpath, buf[MAXLINELEN + 1], fname[MAXPATHLEN + 1];
+       char wbuf[MAXLINELEN + 1], *getenv(), *malloc();
+
+       while ((ch = getopt(argc, argv, "M:P:")) != EOF)
+               switch((char)ch) {
+               case 'M':
+               case 'P':               /* backward compatible */
+                       manpath = optarg;
+                       break;
+               case '?':
+               default:
+                       usage();
+               }
+       argv += optind;
+       argc -= optind;
+       if (argc < 1)
+               usage();
+
+       if (!(manpath = getenv("MANPATH")))
+               manpath = DEF_PATH;
+
+       /*NOSTRICT*/
+       if (!(found = (int *)malloc((u_int)argc))) {
+               fprintf(stderr, "whatis: out of space.\n");
+               exit(1);
+       }
+       bzero((char *)found, argc * sizeof(int));
+
+       for (p = argv; *p; ++p)                 /* trim full paths */
+               if (beg = rindex(*p, '/'))
+                       *p = beg + 1;
+
+       for (beg = manpath; beg; beg = end) {   /* through path list */
+               end = index(beg, ':');
+               if (!end)
+                       (void)sprintf(fname, "%s/%s", beg, WHATIS);
+               else {
+                       (void)sprintf(fname, "%.*s/%s", end - beg, beg, WHATIS);
+                       ++end;
+               }
+               if (!freopen(fname, "r", stdin))
+                       continue;
+
+               /* for each file found */
+               for (foundman = 1; fgets(buf, sizeof(buf), stdin);) {
+                       dashtrunc(buf, wbuf);
+                       for (p = argv; *p; ++p)
+                               if (match(wbuf, *p)) {
+                                       printf("%s", buf);
+                                       found[p - argv] = 1;
+
+                                       /* only print line once */
+                                       while (*++p)
+                                               if (match(wbuf, *p))
+                                                       found[p - argv] = 1;
+                                       break;
+                               }
+               }
+       }
+       if (!foundman) {
+               fprintf(stderr, "whatis: no %s file found in %s.\n",
+                   WHATIS, manpath);
+               exit(1);
+       }
+       for (p = argv; *p; ++p)
+               if (!found[p - argv])
+                       printf("%s: not found\n", *p);
+}
+
+/*
+ * match --
+ *     match a full word
+ */
+match(bp, str)
+       register char *bp, *str;
+{
+       register int len;
+       register char *start;
+
+       if (!*str || !*bp)
+               return(0);
+       for (len = strlen(str);;) {
+               for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
+               if (!*bp)
+                       break;
+               for (start = bp++; *bp && (isdigit(*bp) || isalpha(*bp)); ++bp);
+               if (bp - start == len && !strncasecmp(start, str, len))
+                       return(1);
+       }
+       return(0);
+}
+
+/*
+ * dashtrunc --
+ *     truncate a string at " - "
+ */
+dashtrunc(from, to)
+       register char *from, *to;
+{
+       register int ch;
+
+       for (; (ch = *from) && ch != '\n' &&
+           (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
+               *to++ = ch;
+       *to = '\0';
+}
+
+/*
+ * usage --
+ *     print usage message and die
+ */
+usage()
+{
+       fprintf(stderr, "usage: whatis [-M path] string ...\n");
+       exit(1);
+}