remove call of _fwalk
[unix-history] / usr / src / usr.bin / mail / aux.c
index af26edd..b488b16 100644 (file)
@@ -2,20 +2,16 @@
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at 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'' without express or implied warranty.
+ * %sccs.include.redist.c%
  */
 
  */
 
-#ifdef notdef
-static char sccsid[] = "@(#)aux.c      5.7 (Berkeley) %G%";
-#endif /* notdef */
+#ifndef lint
+static char sccsid[] = "@(#)aux.c      5.20 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
+#include <sys/time.h>
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -26,22 +22,16 @@ static char sccsid[] = "@(#)aux.c   5.7 (Berkeley) %G%";
 /*
  * Return a pointer to a dynamic copy of the argument.
  */
 /*
  * Return a pointer to a dynamic copy of the argument.
  */
-
 char *
 savestr(str)
        char *str;
 {
 char *
 savestr(str)
        char *str;
 {
-       register char *cp, *cp2, *top;
+       char *new;
+       int size = strlen(str) + 1;
 
 
-       for (cp = str; *cp; cp++)
-               ;
-       top = salloc(cp-str + 1);
-       if (top == NOSTR)
-               return(NOSTR);
-       for (cp = str, cp2 = top; *cp; cp++)
-               *cp2++ = *cp;
-       *cp2 = 0;
-       return(top);
+       if ((new = salloc(size)) != NOSTR)
+               bcopy(str, new, size);
+       return new;
 }
 
 /*
 }
 
 /*
@@ -55,7 +45,8 @@ panic(fmt, a, b)
        fprintf(stderr, "panic: ");
        fprintf(stderr, fmt, a, b);
        putc('\n', stderr);
        fprintf(stderr, "panic: ");
        fprintf(stderr, fmt, a, b);
        putc('\n', stderr);
-       exit(1);
+       fflush(stdout);
+       abort();
 }
 
 /*
 }
 
 /*
@@ -63,14 +54,10 @@ panic(fmt, a, b)
  * Touched messages have the effect of not being sent
  * back to the system mailbox on exit.
  */
  * Touched messages have the effect of not being sent
  * back to the system mailbox on exit.
  */
-
-touch(mesg)
-{
+touch(mp)
        register struct message *mp;
        register struct message *mp;
+{
 
 
-       if (mesg < 1 || mesg > msgCount)
-               return;
-       mp = &message[mesg-1];
        mp->m_flag |= MTOUCH;
        if ((mp->m_flag & MREAD) == 0)
                mp->m_flag |= MREAD|MSTATUS;
        mp->m_flag |= MTOUCH;
        if ((mp->m_flag & MREAD) == 0)
                mp->m_flag |= MREAD|MSTATUS;
@@ -80,7 +67,6 @@ touch(mesg)
  * Test to see if the passed file name is a directory.
  * Return true if it is.
  */
  * Test to see if the passed file name is a directory.
  * Return true if it is.
  */
-
 isdir(name)
        char name[];
 {
 isdir(name)
        char name[];
 {
@@ -94,7 +80,6 @@ isdir(name)
 /*
  * Count the number of arguments in the given string raw list.
  */
 /*
  * Count the number of arguments in the given string raw list.
  */
-
 argcount(argv)
        char **argv;
 {
 argcount(argv)
        char **argv;
 {
@@ -109,7 +94,6 @@ argcount(argv)
  * Return the desired header line from the passed message
  * pointer (or NOSTR if the desired header field is not available).
  */
  * Return the desired header line from the passed message
  * pointer (or NOSTR if the desired header field is not available).
  */
-
 char *
 hfield(field, mp)
        char field[];
 char *
 hfield(field, mp)
        char field[];
@@ -124,7 +108,7 @@ hfield(field, mp)
        ibuf = setinput(mp);
        if ((lc = mp->m_lines - 1) < 0)
                return NOSTR;
        ibuf = setinput(mp);
        if ((lc = mp->m_lines - 1) < 0)
                return NOSTR;
-       if (readline(ibuf, linebuf) < 0)
+       if (readline(ibuf, linebuf, LINESIZE) < 0)
                return NOSTR;
        while (lc > 0) {
                if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
                return NOSTR;
        while (lc > 0) {
                if ((lc = gethfield(ibuf, linebuf, lc, &colon)) < 0)
@@ -141,7 +125,6 @@ hfield(field, mp)
  * "colon" is set to point to the colon in the header.
  * Must deal with \ continuations & other such fraud.
  */
  * "colon" is set to point to the colon in the header.
  * Must deal with \ continuations & other such fraud.
  */
-
 gethfield(f, linebuf, rem, colon)
        register FILE *f;
        char linebuf[];
 gethfield(f, linebuf, rem, colon)
        register FILE *f;
        char linebuf[];
@@ -155,7 +138,7 @@ gethfield(f, linebuf, rem, colon)
        for (;;) {
                if (--rem < 0)
                        return -1;
        for (;;) {
                if (--rem < 0)
                        return -1;
-               if ((c = readline(f, linebuf)) <= 0)
+               if ((c = readline(f, linebuf, LINESIZE)) <= 0)
                        return -1;
                for (cp = linebuf; isprint(*cp) && *cp != ' ' && *cp != ':';
                     cp++)
                        return -1;
                for (cp = linebuf; isprint(*cp) && *cp != ' ' && *cp != ':';
                     cp++)
@@ -177,7 +160,7 @@ gethfield(f, linebuf, rem, colon)
                        ungetc(c = getc(f), f);
                        if (c != ' ' && c != '\t')
                                break;
                        ungetc(c = getc(f), f);
                        if (c != ' ' && c != '\t')
                                break;
-                       if ((c = readline(f, line2)) < 0)
+                       if ((c = readline(f, line2, LINESIZE)) < 0)
                                break;
                        rem--;
                        for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
                                break;
                        rem--;
                        for (cp2 = line2; *cp2 == ' ' || *cp2 == '\t'; cp2++)
@@ -208,7 +191,7 @@ ishfield(linebuf, colon, field)
        register char *cp = colon;
 
        *cp = 0;
        register char *cp = colon;
 
        *cp = 0;
-       if (!icequal(linebuf, field)) {
+       if (strcasecmp(linebuf, field) != 0) {
                *cp = ':';
                return 0;
        }
                *cp = ':';
                return 0;
        }
@@ -218,29 +201,6 @@ ishfield(linebuf, colon, field)
        return cp;
 }
 
        return cp;
 }
 
-/*
- * Compare two strings, ignoring case.
- */
-
-icequal(s1, s2)
-       register char *s1, *s2;
-{
-       register c1, c2;
-
-       for (;;) {
-               if ((c1 = (unsigned char)*s1++) !=
-                   (c2 = (unsigned char)*s2++)) {
-                       if (isupper(c1))
-                               c1 = tolower(c1);
-                       if (c1 != c2)
-                               return 0;
-               }
-               if (c1 == 0)
-                       return 1;
-       }
-       /*NOTREACHED*/
-}
-
 /*
  * Copy a string, lowercasing it as we go.
  */
 /*
  * Copy a string, lowercasing it as we go.
  */
@@ -262,7 +222,7 @@ istrcpy(dest, src)
  * the stack.
  */
 
  * the stack.
  */
 
-static int     ssp = -1;               /* Top of file stack */
+static int     ssp;                    /* Top of file stack */
 struct sstack {
        FILE    *s_file;                /* File we were in. */
        int     s_cond;                 /* Saved state of conditionals */
 struct sstack {
        FILE    *s_file;                /* File we were in. */
        int     s_cond;                 /* Saved state of conditionals */
@@ -274,27 +234,27 @@ struct sstack {
  * Set the global flag "sourcing" so that others will realize
  * that they are no longer reading from a tty (in all probability).
  */
  * Set the global flag "sourcing" so that others will realize
  * that they are no longer reading from a tty (in all probability).
  */
-
-source(name)
-       char name[];
+source(arglist)
+       char **arglist;
 {
 {
-       register FILE *fi;
-       register char *cp;
+       FILE *fi;
+       char *cp;
 
 
-       if ((cp = expand(name)) == NOSTR)
+       if ((cp = expand(*arglist)) == NOSTR)
                return(1);
                return(1);
-       if ((fi = fopen(cp, "r")) == NULL) {
+       if ((fi = Fopen(cp, "r")) == NULL) {
                perror(cp);
                return(1);
        }
                perror(cp);
                return(1);
        }
-       if (ssp >= NOFILE - 2) {
+       if (ssp >= NOFILE - 1) {
                printf("Too much \"sourcing\" going on.\n");
                printf("Too much \"sourcing\" going on.\n");
-               fclose(fi);
+               Fclose(fi);
                return(1);
        }
                return(1);
        }
-       sstack[++ssp].s_file = input;
+       sstack[ssp].s_file = input;
        sstack[ssp].s_cond = cond;
        sstack[ssp].s_loading = loading;
        sstack[ssp].s_cond = cond;
        sstack[ssp].s_loading = loading;
+       ssp++;
        loading = 0;
        cond = CANY;
        input = fi;
        loading = 0;
        cond = CANY;
        input = fi;
@@ -306,21 +266,21 @@ source(name)
  * Pop the current input back to the previous level.
  * Update the "sourcing" flag as appropriate.
  */
  * Pop the current input back to the previous level.
  * Update the "sourcing" flag as appropriate.
  */
-
 unstack()
 {
 unstack()
 {
-       if (ssp < 0) {
+       if (ssp <= 0) {
                printf("\"Source\" stack over-pop.\n");
                sourcing = 0;
                return(1);
        }
                printf("\"Source\" stack over-pop.\n");
                sourcing = 0;
                return(1);
        }
-       fclose(input);
+       Fclose(input);
        if (cond != CANY)
                printf("Unmatched \"if\"\n");
        if (cond != CANY)
                printf("Unmatched \"if\"\n");
+       ssp--;
        cond = sstack[ssp].s_cond;
        loading = sstack[ssp].s_loading;
        cond = sstack[ssp].s_cond;
        loading = sstack[ssp].s_loading;
-       input = sstack[ssp--].s_file;
-       if (ssp < 0)
+       input = sstack[ssp].s_file;
+       if (ssp == 0)
                sourcing = loading;
        return(0);
 }
                sourcing = loading;
        return(0);
 }
@@ -328,43 +288,26 @@ unstack()
 /*
  * Touch the indicated file.
  * This is nifty for the shell.
 /*
  * Touch the indicated file.
  * This is nifty for the shell.
- * If we have the utime() system call, this is better served
- * by using that, since it will work for empty files.
- * On non-utime systems, we must sleep a second, then read.
  */
  */
-
 alter(name)
 alter(name)
-       char name[];
+       char *name;
 {
 {
-#ifdef UTIME
-       struct stat statb;
-       long time();
-       time_t time_p[2];
-#else
-       register int pid, f;
-       char w;
-#endif UTIME
-
-#ifdef UTIME
-       if (stat(name, &statb) < 0)
-               return;
-       time_p[0] = time((long *) 0) + 1;
-       time_p[1] = statb.st_mtime;
-       utime(name, time_p);
-#else
-       sleep(1);
-       if ((f = open(name, 0)) < 0)
+       struct stat sb;
+       struct timeval tv[2];
+       time_t time();
+
+       if (stat(name, &sb))
                return;
                return;
-       read(f, &w, 1);
-       exit(0);
-#endif
+       tv[0].tv_sec = time((time_t *)0) + 1;
+       tv[1].tv_sec = sb.st_mtime;
+       tv[0].tv_usec = tv[1].tv_usec = 0;
+       (void)utimes(name, tv);
 }
 
 /*
  * Examine the passed line buffer and
  * return true if it is all blanks and tabs.
  */
 }
 
 /*
  * Examine the passed line buffer and
  * return true if it is all blanks and tabs.
  */
-
 blankline(linebuf)
        char linebuf[];
 {
 blankline(linebuf)
        char linebuf[];
 {
@@ -399,6 +342,33 @@ nameof(mp, reptype)
        return(cp);
 }
 
        return(cp);
 }
 
+/*
+ * Start of a "comment".
+ * Ignore it.
+ */
+char *
+skip_comment(cp)
+       register char *cp;
+{
+       register nesting = 1;
+
+       for (; nesting > 0 && *cp; cp++) {
+               switch (*cp) {
+               case '\\':
+                       if (cp[1])
+                               cp++;
+                       break;
+               case '(':
+                       nesting++;
+                       break;
+               case ')':
+                       nesting--;
+                       break;
+               }
+       }
+       return cp;
+}
+
 /*
  * Skin an arpa net address according to the RFC 822 interpretation
  * of "host-phrase."
 /*
  * Skin an arpa net address according to the RFC 822 interpretation
  * of "host-phrase."
@@ -412,7 +382,6 @@ skin(name)
        char *bufend;
        int gotlt, lastsp;
        char nbuf[BUFSIZ];
        char *bufend;
        int gotlt, lastsp;
        char nbuf[BUFSIZ];
-       int nesting;
 
        if (name == NOSTR)
                return(NOSTR);
 
        if (name == NOSTR)
                return(NOSTR);
@@ -425,32 +394,7 @@ skin(name)
        for (cp = name, cp2 = bufend; c = *cp++; ) {
                switch (c) {
                case '(':
        for (cp = name, cp2 = bufend; c = *cp++; ) {
                switch (c) {
                case '(':
-                       /*
-                        * Start of a "comment".
-                        * Ignore it.
-                        */
-                       nesting = 1;
-                       while ((c = *cp) != 0) {
-                               cp++;
-                               switch (c) {
-                               case '\\':
-                                       if (*cp == 0)
-                                               goto outcm;
-                                       cp++;
-                                       break;
-                               case '(':
-                                       nesting++;
-                                       break;
-
-                               case ')':
-                                       --nesting;
-                                       break;
-                               }
-
-                               if (nesting <= 0)
-                                       break;
-                       }
-               outcm:
+                       cp = skip_comment(cp);
                        lastsp = 0;
                        break;
 
                        lastsp = 0;
                        break;
 
@@ -459,20 +403,17 @@ skin(name)
                         * Start of a "quoted-string".
                         * Copy it in its entirety.
                         */
                         * Start of a "quoted-string".
                         * Copy it in its entirety.
                         */
-                       while ((c = *cp) != 0) {
+                       while (c = *cp) {
                                cp++;
                                cp++;
-                               switch (c) {
-                               case '\\':
-                                       if ((c = *cp) == 0)
-                                               goto outqs;
-                                       cp++;
+                               if (c == '"')
                                        break;
                                        break;
-                               case '"':
-                                       goto outqs;
+                               if (c != '\\')
+                                       *cp2++ = c;
+                               else if (c = *cp) {
+                                       *cp2++ = c;
+                                       cp++;
                                }
                                }
-                               *cp2++ = c;
                        }
                        }
-               outqs:
                        lastsp = 0;
                        break;
 
                        lastsp = 0;
                        break;
 
@@ -495,16 +436,22 @@ skin(name)
                case '>':
                        if (gotlt) {
                                gotlt = 0;
                case '>':
                        if (gotlt) {
                                gotlt = 0;
-                               while (*cp != ',' && *cp != 0)
+                               while ((c = *cp) && c != ',') {
                                        cp++;
                                        cp++;
-                               if (*cp == 0 )
-                                       goto done;
-                               *cp2++ = ',';
-                               *cp2++ = ' ';
-                               bufend = cp2;
+                                       if (c == '(')
+                                               cp = skip_comment(cp);
+                                       else if (c == '"')
+                                               while (c = *cp) {
+                                                       cp++;
+                                                       if (c == '"')
+                                                               break;
+                                                       if (c == '\\' && *cp)
+                                                               cp++;
+                                               }
+                               }
+                               lastsp = 0;
                                break;
                        }
                                break;
                        }
-
                        /* Fall into . . . */
 
                default:
                        /* Fall into . . . */
 
                default:
@@ -513,10 +460,15 @@ skin(name)
                                *cp2++ = ' ';
                        }
                        *cp2++ = c;
                                *cp2++ = ' ';
                        }
                        *cp2++ = c;
-                       break;
+                       if (c == ',' && !gotlt) {
+                               *cp2++ = ' ';
+                               for (; *cp == ' '; cp++)
+                                       ;
+                               lastsp = 0;
+                               bufend = cp2;
+                       }
                }
        }
                }
        }
-done:
        *cp2 = 0;
 
        return(savestr(nbuf));
        *cp2 = 0;
 
        return(savestr(nbuf));
@@ -529,7 +481,6 @@ done:
  *     1 -- get sender's name for reply
  *     2 -- get sender's name for Reply
  */
  *     1 -- get sender's name for reply
  *     2 -- get sender's name for Reply
  */
-
 char *
 name1(mp, reptype)
        register struct message *mp;
 char *
 name1(mp, reptype)
        register struct message *mp;
@@ -546,7 +497,7 @@ name1(mp, reptype)
                return cp;
        ibuf = setinput(mp);
        namebuf[0] = 0;
                return cp;
        ibuf = setinput(mp);
        namebuf[0] = 0;
-       if (readline(ibuf, linebuf) < 0)
+       if (readline(ibuf, linebuf, LINESIZE) < 0)
                return(savestr(namebuf));
 newname:
        for (cp = linebuf; *cp && *cp != ' '; cp++)
                return(savestr(namebuf));
 newname:
        for (cp = linebuf; *cp && *cp != ' '; cp++)
@@ -557,7 +508,7 @@ newname:
             *cp && *cp != ' ' && *cp != '\t' && cp2 < namebuf + LINESIZE - 1;)
                *cp2++ = *cp++;
        *cp2 = '\0';
             *cp && *cp != ' ' && *cp != '\t' && cp2 < namebuf + LINESIZE - 1;)
                *cp2++ = *cp++;
        *cp2 = '\0';
-       if (readline(ibuf, linebuf) < 0)
+       if (readline(ibuf, linebuf, LINESIZE) < 0)
                return(savestr(namebuf));
        if ((cp = index(linebuf, 'F')) == NULL)
                return(savestr(namebuf));
                return(savestr(namebuf));
        if ((cp = index(linebuf, 'F')) == NULL)
                return(savestr(namebuf));
@@ -603,7 +554,6 @@ charcount(str, c)
 /*
  * Are any of the characters in the two strings the same?
  */
 /*
  * Are any of the characters in the two strings the same?
  */
-
 anyof(s1, s2)
        register char *s1, *s2;
 {
 anyof(s1, s2)
        register char *s1, *s2;
 {
@@ -617,7 +567,6 @@ anyof(s1, s2)
 /*
  * Convert c to upper case
  */
 /*
  * Convert c to upper case
  */
-
 raise(c)
        register c;
 {
 raise(c)
        register c;
 {
@@ -630,7 +579,6 @@ raise(c)
 /*
  * Copy s1 to s2, return pointer to null in s2.
  */
 /*
  * Copy s1 to s2, return pointer to null in s2.
  */
-
 char *
 copy(s1, s2)
        register char *s1, *s2;
 char *
 copy(s1, s2)
        register char *s1, *s2;
@@ -641,46 +589,35 @@ copy(s1, s2)
        return s2 - 1;
 }
 
        return s2 - 1;
 }
 
-/*
- * Add a single character onto a string.
- */
-
-stradd(str, c)
-       register char *str;
-{
-
-       while (*str++)
-               ;
-       str[-1] = c;
-       *str = 0;
-}
-
 /*
  * See if the given header field is supposed to be ignored.
  */
 /*
  * See if the given header field is supposed to be ignored.
  */
-isign(field)
+isign(field, ignore)
        char *field;
        char *field;
+       struct ignoretab ignore[2];
 {
        char realfld[BUFSIZ];
 
 {
        char realfld[BUFSIZ];
 
+       if (ignore == ignoreall)
+               return 1;
        /*
         * Lower-case the string, so that "Status" and "status"
         * will hash to the same place.
         */
        istrcpy(realfld, field);
        /*
         * Lower-case the string, so that "Status" and "status"
         * will hash to the same place.
         */
        istrcpy(realfld, field);
-       if (nretained > 0)
-               return (!member(realfld, retain));
+       if (ignore[1].i_count > 0)
+               return (!member(realfld, ignore + 1));
        else
                return (member(realfld, ignore));
 }
 
 member(realfield, table)
        register char *realfield;
        else
                return (member(realfld, ignore));
 }
 
 member(realfield, table)
        register char *realfield;
-       struct ignore **table;
+       struct ignoretab *table;
 {
        register struct ignore *igp;
 
 {
        register struct ignore *igp;
 
-       for (igp = table[hash(realfield)]; igp != 0; igp = igp->i_link)
+       for (igp = table->i_head[hash(realfield)]; igp != 0; igp = igp->i_link)
                if (*igp->i_field == *realfield &&
                    equal(igp->i_field, realfield))
                        return (1);
                if (*igp->i_field == *realfield &&
                    equal(igp->i_field, realfield))
                        return (1);