bcmp collided with C library routine
[unix-history] / usr / src / usr.bin / mail / head.c
index bb6978a..1e53da6 100644 (file)
@@ -1,4 +1,23 @@
-#
+/*
+ * Copyright (c) 1980 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
+static char sccsid[] = "@(#)head.c     5.6 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 
 
 #include "rcv.h"
 
  * Routines for processing and detecting headlines.
  */
 
  * Routines for processing and detecting headlines.
  */
 
-static char *SccsId = "@(#)head.c      1.2 %G%";
-
 /*
  * See if the passed line buffer is a mail header.
  * Return true if yes.  Note the extreme pains to
  * accomodate all funny formats.
  */
 /*
  * See if the passed line buffer is a mail header.
  * Return true if yes.  Note the extreme pains to
  * accomodate all funny formats.
  */
-
 ishead(linebuf)
        char linebuf[];
 {
 ishead(linebuf)
        char linebuf[];
 {
@@ -24,32 +40,34 @@ ishead(linebuf)
        char parbuf[BUFSIZ];
 
        cp = linebuf;
        char parbuf[BUFSIZ];
 
        cp = linebuf;
-       if (!isname("From ", cp, 5))
-               return(0);
-       parse(cp, &hl, parbuf);
+       if (*cp++ != 'F' || *cp++ != 'r' || *cp++ != 'o' || *cp++ != 'm' ||
+           *cp++ != ' ')
+               return (0);
+       parse(linebuf, &hl, parbuf);
        if (hl.l_from == NOSTR || hl.l_date == NOSTR) {
                fail(linebuf, "No from or date field");
        if (hl.l_from == NOSTR || hl.l_date == NOSTR) {
                fail(linebuf, "No from or date field");
-               return(0);
+               return (0);
        }
        if (!isdate(hl.l_date)) {
                fail(linebuf, "Date field not legal date");
        }
        if (!isdate(hl.l_date)) {
                fail(linebuf, "Date field not legal date");
-               return(0);
+               return (0);
        }
        }
-       
        /*
         * I guess we got it!
         */
        /*
         * I guess we got it!
         */
-
-       return(1);
+       return (1);
 }
 
 }
 
+/*ARGSUSED*/
 fail(linebuf, reason)
        char linebuf[], reason[];
 {
 
 fail(linebuf, reason)
        char linebuf[], reason[];
 {
 
-       if (1 /*value("debug") == NOSTR*/)
+       /*
+       if (value("debug") == NOSTR)
                return;
        fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
                return;
        fprintf(stderr, "\"%s\"\nnot a header because %s\n", linebuf, reason);
+       */
 }
 
 /*
 }
 
 /*
@@ -58,12 +76,11 @@ fail(linebuf, reason)
  * pointers into the copied line in the passed headline
  * structure.  Actually, it scans.
  */
  * pointers into the copied line in the passed headline
  * structure.  Actually, it scans.
  */
-
 parse(line, hl, pbuf)
        char line[], pbuf[];
 parse(line, hl, pbuf)
        char line[], pbuf[];
-       struct headline *hl;
+       register struct headline *hl;
 {
 {
-       register char *cp, *dp;
+       register char *cp;
        char *sp;
        char word[LINESIZE];
 
        char *sp;
        char word[LINESIZE];
 
@@ -72,25 +89,19 @@ parse(line, hl, pbuf)
        hl->l_date = NOSTR;
        cp = line;
        sp = pbuf;
        hl->l_date = NOSTR;
        cp = line;
        sp = pbuf;
-
        /*
        /*
-        * Skip the first "word" of the line, which should be "From"
-        * anyway.
+        * Skip over "From" first.
         */
         */
-
        cp = nextword(cp, word);
        cp = nextword(cp, word);
-       dp = nextword(cp, word);
-       if (!equal(word, ""))
+       cp = nextword(cp, word);
+       if (*word)
                hl->l_from = copyin(word, &sp);
                hl->l_from = copyin(word, &sp);
-       if (isname(dp, "tty", 3)) {
-               cp = nextword(dp, word);
+       if (cp != NOSTR && cp[0] == 't' && cp[1] == 't' && cp[2] == 'y') {
+               cp = nextword(cp, word);
                hl->l_tty = copyin(word, &sp);
                hl->l_tty = copyin(word, &sp);
-               if (cp != NOSTR)
-                       hl->l_date = copyin(cp, &sp);
        }
        }
-       else
-               if (dp != NOSTR)
-                       hl->l_date = copyin(dp, &sp);
+       if (cp != NOSTR)
+               hl->l_date = copyin(cp, &sp);
 }
 
 /*
 }
 
 /*
@@ -99,66 +110,19 @@ parse(line, hl, pbuf)
  * Thus, dynamically allocate space in the right string, copying
  * the left string into it.
  */
  * Thus, dynamically allocate space in the right string, copying
  * the left string into it.
  */
-
 char *
 copyin(src, space)
 char *
 copyin(src, space)
-       char src[];
+       register char *src;
        char **space;
 {
        char **space;
 {
-       register char *cp, *top;
-       register int s;
+       register char *cp;
+       char *top;
 
 
-       s = strlen(src);
-       cp = *space;
-       top = cp;
-       strcpy(cp, src);
-       cp += s + 1;
+       top = cp = *space;
+       while (*cp++ = *src++)
+               ;
        *space = cp;
        *space = cp;
-       return(top);
-}
-
-/*
- * See if the two passed strings agree in the first n characters.
- * Return true if they do, gnu.
- */
-
-isname(as1, as2, acount)
-       char *as1, *as2;
-{
-       register char *s1, *s2;
-       register count;
-
-       s1 = as1;
-       s2 = as2;
-       count = acount;
-       if (count > 0)
-               do
-                       if (*s1++ != *s2++)
-                               return(0);
-               while (--count);
-       return(1);
-}
-
-/*
- * See if the two passed strings agree in the first n characters.
- * Return true if they do, ignoring case.
- */
-
-icisname(as1, as2, acount)
-       char *as1, *as2;
-{
-       register char *s1, *s2;
-       register count;
-
-       s1 = as1;
-       s2 = as2;
-       count = acount;
-       if (count > 0)
-               do
-                       if (raise(*s1++) != raise(*s2++))
-                               return(0);
-               while (--count);
-       return(1);
+       return (top);
 }
 
 /*
 }
 
 /*
@@ -166,86 +130,71 @@ icisname(as1, as2, acount)
  * date string as documented in the manual.  The template
  * below is used as the criterion of correctness.
  * Also, we check for a possible trailing time zone using
  * date string as documented in the manual.  The template
  * below is used as the criterion of correctness.
  * Also, we check for a possible trailing time zone using
- * the auxtype template.
+ * the tmztype template.
  */
 
  */
 
-#define        L       1               /* A lower case char */
-#define        S       2               /* A space */
-#define        D       3               /* A digit */
-#define        O       4               /* An optional digit or space */
-#define        C       5               /* A colon */
-#define        N       6               /* A new line */
-#define U      7               /* An upper case char */
-
-char ctypes[] = {U,L,L,S,U,L,L,S,O,D,S,D,D,C,D,D,C,D,D,S,D,D,D,D,0};
-char tmztypes[] = {U,L,L,S,U,L,L,S,O,D,S,D,D,C,D,D,C,D,D,S,U,U,U,S,D,D,D,D,0};
+/*
+ * 'A' An upper case char
+ * 'a' A lower case char
+ * ' ' A space
+ * '0' A digit
+ * 'O' An optional digit or space
+ * ':' A colon
+ * 'N' A new line
+ */
+char ctype[] = "Aaa Aaa O0 00:00:00 0000";
+char tmztype[] = "Aaa Aaa O0 00:00:00 AAA 0000";
 
 isdate(date)
        char date[];
 {
 
 isdate(date)
        char date[];
 {
-       register char *cp;
 
 
-       cp = date;
-       if (cmatch(cp, ctypes))
-               return(1);
-       return(cmatch(cp, tmztypes));
+       return cmatch(date, ctype) || cmatch(date, tmztype);
 }
 
 /*
 }
 
 /*
- * Match the given string against the given template.
+ * Match the given string (cp) against the given template (tp).
  * Return 1 if they match, 0 if they don't
  */
  * Return 1 if they match, 0 if they don't
  */
-
-cmatch(str, temp)
-       char str[], temp[];
-{
+cmatch(cp, tp)
        register char *cp, *tp;
        register char *cp, *tp;
-       register int c;
+{
 
 
-       cp = str;
-       tp = temp;
-       while (*cp != '\0' && *tp != 0) {
-               c = *cp++;
+       while (*cp && *tp)
                switch (*tp++) {
                switch (*tp++) {
-               case L:
-                       if (c < 'a' || c > 'z')
-                               return(0);
+               case 'a':
+                       if (!islower(*cp++))
+                               return 0;
                        break;
                        break;
-
-               case U:
-                       if (c < 'A' || c > 'Z')
-                               return(0);
+               case 'A':
+                       if (!isupper(*cp++))
+                               return 0;
                        break;
                        break;
-
-               case S:
-                       if (c != ' ')
-                               return(0);
+               case ' ':
+                       if (*cp++ != ' ')
+                               return 0;
                        break;
                        break;
-
-               case D:
-                       if (!isdigit(c))
-                               return(0);
+               case '0':
+                       if (!isdigit(*cp++))
+                               return 0;
                        break;
                        break;
-
-               case O:
-                       if (c != ' ' && !isdigit(c))
-                               return(0);
+               case 'O':
+                       if (*cp != ' ' && !isdigit(*cp))
+                               return 0;
+                       cp++;
                        break;
                        break;
-
-               case C:
-                       if (c != ':')
-                               return(0);
+               case ':':
+                       if (*cp++ != ':')
+                               return 0;
                        break;
                        break;
-
-               case N:
-                       if (c != '\n')
-                               return(0);
+               case 'N':
+                       if (*cp++ != '\n')
+                               return 0;
                        break;
                }
                        break;
                }
-       }
-       if (*cp != '\0' || *tp != 0)
-               return(0);
-       return(1);
+       if (*cp || *tp)
+               return 0;
+       return (1);
 }
 
 /*
 }
 
 /*
@@ -253,93 +202,31 @@ cmatch(str, temp)
  * passed.  Also, return a pointer to the next word following that,
  * or NOSTR if none follow.
  */
  * passed.  Also, return a pointer to the next word following that,
  * or NOSTR if none follow.
  */
-
 char *
 nextword(wp, wbuf)
 char *
 nextword(wp, wbuf)
-       char wp[], wbuf[];
-{
-       register char *cp, *cp2;
-
-       if ((cp = wp) == NOSTR) {
-               copy("", wbuf);
-               return(NOSTR);
-       }
-       cp2 = wbuf;
-       while (!any(*cp, " \t") && *cp != '\0')
-               *cp2++ = *cp++;
-       *cp2 = '\0';
-       while (any(*cp, " \t"))
-               cp++;
-       if (*cp == '\0')
-               return(NOSTR);
-       return(cp);
-}
-
-/*
- * Test to see if the character is an ascii alphabetic.
- */
-
-isalpha(c)
-{
-       register int ch;
-
-       ch = raise(c);
-       return(ch >= 'A' && ch <= 'Z');
-}
-
-/*
- * Test to see if the character is an ascii digit.
- */
-
-isdigit(c)
-{
-       return(c >= '0' && c <= '9');
-}
-
-/*
- * Copy str1 to str2, return pointer to null in str2.
- */
-
-char *
-copy(str1, str2)
-       char *str1, *str2;
-{
-       register char *s1, *s2;
-
-       s1 = str1;
-       s2 = str2;
-       while (*s1)
-               *s2++ = *s1++;
-       *s2 = 0;
-       return(s2);
-}
-
-/*
- * Is ch any of the characters in str?
- */
-
-any(ch, str)
-       char *str;
+       register char *wp, *wbuf;
 {
 {
-       register char *f;
        register c;
 
        register c;
 
-       f = str;
-       c = ch;
-       while (*f)
-               if (c == *f++)
-                       return(1);
-       return(0);
-}
-
-/*
- * Convert lower case letters to upper case.
- */
-
-raise(c)
-       register int c;
-{
-       if (c >= 'a' && c <= 'z')
-               c += 'A' - 'a';
-       return(c);
+       if (wp == NOSTR) {
+               *wbuf = 0;
+               return (NOSTR);
+       }
+       while ((c = *wp++) && c != ' ' && c != '\t') {
+               *wbuf++ = c;
+               if (c == '"') {
+                       while ((c = *wp++) && c != '"')
+                               *wbuf++ = c;
+                       if (c == '"')
+                               *wbuf++ = c;
+                       else
+                               wp--;
+               }
+       }
+       *wbuf = '\0';
+       for (; c == ' ' || c == '\t'; c = *wp++)
+               ;
+       if (c == 0)
+               return (NOSTR);
+       return (wp - 1);
 }
 }