4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / cmd2.c
index 987f17b..e3734a7 100644 (file)
@@ -1,15 +1,17 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * Copyright (c) 1980, 1993
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char *sccsid = "@(#)cmd2.c      5.2 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)cmd2.c     8.1 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 
 #include "rcv.h"
-#include <sys/stat.h>
+#include <sys/wait.h>
+#include "extern.h"
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -22,7 +24,7 @@ static char *sccsid = "@(#)cmd2.c     5.2 (Berkeley) %G%";
  * following dot, otherwise, go to the next applicable message.
  * If given as first command with no arguments, print first message.
  */
  * following dot, otherwise, go to the next applicable message.
  * If given as first command with no arguments, print first message.
  */
-
+int
 next(msgvec)
        int *msgvec;
 {
 next(msgvec)
        int *msgvec;
 {
@@ -101,39 +103,42 @@ hitit:
  * Save a message in a file.  Mark the message as saved
  * so we can discard when the user quits.
  */
  * Save a message in a file.  Mark the message as saved
  * so we can discard when the user quits.
  */
+int
 save(str)
        char str[];
 {
 
 save(str)
        char str[];
 {
 
-       return(save1(str, 1));
+       return save1(str, 1, "save", saveignore);
 }
 
 /*
  * Copy a message to a file without affected its saved-ness
  */
 }
 
 /*
  * Copy a message to a file without affected its saved-ness
  */
+int
 copycmd(str)
        char str[];
 {
 
 copycmd(str)
        char str[];
 {
 
-       return(save1(str, 0));
+       return save1(str, 0, "copy", saveignore);
 }
 
 /*
  * Save/copy the indicated messages at the end of the passed file name.
  * If mark is true, mark the message "saved."
  */
 }
 
 /*
  * Save/copy the indicated messages at the end of the passed file name.
  * If mark is true, mark the message "saved."
  */
-save1(str, mark)
+int
+save1(str, mark, cmd, ignore)
        char str[];
        char str[];
+       int mark;
+       char *cmd;
+       struct ignoretab *ignore;
 {
 {
-       register int *ip, mesg;
+       register int *ip;
        register struct message *mp;
        register struct message *mp;
-       char *file, *disp, *cmd;
-       int f, *msgvec, lc, t;
-       long cc;
+       char *file, *disp;
+       int f, *msgvec;
        FILE *obuf;
        FILE *obuf;
-       struct stat statb;
 
 
-       cmd = mark ? "save" : "copy";
        msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
        if ((file = snarf(str, &f)) == NOSTR)
                return(1);
        msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
        if ((file = snarf(str, &f)) == NOSTR)
                return(1);
@@ -151,35 +156,30 @@ save1(str, mark)
                return(1);
        printf("\"%s\" ", file);
        fflush(stdout);
                return(1);
        printf("\"%s\" ", file);
        fflush(stdout);
-       if (stat(file, &statb) >= 0)
+       if (access(file, 0) >= 0)
                disp = "[Appended]";
        else
                disp = "[New file]";
                disp = "[Appended]";
        else
                disp = "[New file]";
-       if ((obuf = fopen(file, "a")) == NULL) {
+       if ((obuf = Fopen(file, "a")) == NULL) {
                perror(NOSTR);
                return(1);
        }
                perror(NOSTR);
                return(1);
        }
-       cc = 0L;
-       lc = 0;
        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
        for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
-               if ((t = send(mp, obuf, 0)) < 0) {
+               mp = &message[*ip - 1];
+               touch(mp);
+               if (send(mp, obuf, ignore, NOSTR) < 0) {
                        perror(file);
                        perror(file);
-                       fclose(obuf);
+                       Fclose(obuf);
                        return(1);
                }
                        return(1);
                }
-               lc += t;
-               cc += mp->m_size;
                if (mark)
                        mp->m_flag |= MSAVED;
        }
        fflush(obuf);
        if (ferror(obuf))
                perror(file);
                if (mark)
                        mp->m_flag |= MSAVED;
        }
        fflush(obuf);
        if (ferror(obuf))
                perror(file);
-       fclose(obuf);
-       printf("%s %d/%ld\n", disp, lc, cc);
+       Fclose(obuf);
+       printf("%s\n", disp);
        return(0);
 }
 
        return(0);
 }
 
@@ -187,65 +187,12 @@ save1(str, mark)
  * Write the indicated messages at the end of the passed
  * file name, minus header and trailing blank line.
  */
  * Write the indicated messages at the end of the passed
  * file name, minus header and trailing blank line.
  */
-
+int
 swrite(str)
        char str[];
 {
 swrite(str)
        char str[];
 {
-       register int *ip, mesg;
-       register struct message *mp;
-       register char *file, *disp;
-       char linebuf[BUFSIZ];
-       int f, *msgvec, lc, cc, t;
-       FILE *obuf, *mesf;
-       struct stat statb;
 
 
-       msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
-       if ((file = snarf(str, &f)) == NOSTR)
-               return(1);
-       if ((file = expand(file)) == NOSTR)
-               return(1);
-       if (!f) {
-               *msgvec = first(0, MMNORM);
-               if (*msgvec == NULL) {
-                       printf("No messages to write.\n");
-                       return(1);
-               }
-               msgvec[1] = NULL;
-       }
-       if (f && getmsglist(str, msgvec, 0) < 0)
-               return(1);
-       printf("\"%s\" ", file);
-       fflush(stdout);
-       if (stat(file, &statb) >= 0)
-               disp = "[Appended]";
-       else
-               disp = "[New file]";
-       if ((obuf = fopen(file, "a")) == NULL) {
-               perror(NOSTR);
-               return(1);
-       }
-       cc = lc = 0;
-       for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
-               mesf = setinput(mp);
-               t = mp->m_lines - 2;
-               readline(mesf, linebuf);
-               while (t-- > 0) {
-                       fgets(linebuf, BUFSIZ, mesf);
-                       fputs(linebuf, obuf);
-                       cc += strlen(linebuf);
-               }
-               lc += mp->m_lines - 2;
-               mp->m_flag |= MSAVED;
-       }
-       fflush(obuf);
-       if (ferror(obuf))
-               perror(file);
-       fclose(obuf);
-       printf("%s %d/%d\n", disp, lc, cc);
-       return(0);
+       return save1(str, 1, "write", ignoreall);
 }
 
 /*
 }
 
 /*
@@ -271,7 +218,7 @@ snarf(linebuf, flag)
         * Strip away trailing blanks.
         */
 
         * Strip away trailing blanks.
         */
 
-       while (*cp == ' ' && cp > linebuf)
+       while (cp > linebuf && isspace(*cp))
                cp--;
        *++cp = 0;
 
                cp--;
        *++cp = 0;
 
@@ -279,13 +226,13 @@ snarf(linebuf, flag)
         * Now search for the beginning of the file name.
         */
 
         * Now search for the beginning of the file name.
         */
 
-       while (cp > linebuf && !any(*cp, "\t "))
+       while (cp > linebuf && !isspace(*cp))
                cp--;
        if (*cp == '\0') {
                printf("No file specified.\n");
                return(NOSTR);
        }
                cp--;
        if (*cp == '\0') {
                printf("No file specified.\n");
                return(NOSTR);
        }
-       if (any(*cp, " \t"))
+       if (isspace(*cp))
                *cp++ = 0;
        else
                *flag = 0;
                *cp++ = 0;
        else
                *flag = 0;
@@ -295,17 +242,18 @@ snarf(linebuf, flag)
 /*
  * Delete messages.
  */
 /*
  * Delete messages.
  */
-
+int
 delete(msgvec)
        int msgvec[];
 {
 delete(msgvec)
        int msgvec[];
 {
-       return(delm(msgvec));
+       delm(msgvec);
+       return 0;
 }
 
 /*
  * Delete messages, then type the new dot.
  */
 }
 
 /*
  * Delete messages, then type the new dot.
  */
-
+int
 deltype(msgvec)
        int msgvec[];
 {
 deltype(msgvec)
        int msgvec[];
 {
@@ -314,20 +262,16 @@ deltype(msgvec)
 
        lastdot = dot - &message[0] + 1;
        if (delm(msgvec) >= 0) {
 
        lastdot = dot - &message[0] + 1;
        if (delm(msgvec) >= 0) {
-               list[0] = dot - &message[0];
-               list[0]++;
+               list[0] = dot - &message[0] + 1;
                if (list[0] > lastdot) {
                if (list[0] > lastdot) {
-                       touch(list[0]);
+                       touch(dot);
                        list[1] = NULL;
                        return(type(list));
                }
                printf("At EOF\n");
                        list[1] = NULL;
                        return(type(list));
                }
                printf("At EOF\n");
-               return(0);
-       }
-       else {
+       } else
                printf("No more messages\n");
                printf("No more messages\n");
-               return(0);
-       }
+       return(0);
 }
 
 /*
 }
 
 /*
@@ -335,22 +279,21 @@ deltype(msgvec)
  * Set dot to some nice place afterwards.
  * Internal interface.
  */
  * Set dot to some nice place afterwards.
  * Internal interface.
  */
-
+int
 delm(msgvec)
        int *msgvec;
 {
        register struct message *mp;
 delm(msgvec)
        int *msgvec;
 {
        register struct message *mp;
-       register *ip, mesg;
+       register *ip;
        int last;
 
        last = NULL;
        for (ip = msgvec; *ip != NULL; ip++) {
        int last;
 
        last = NULL;
        for (ip = msgvec; *ip != NULL; ip++) {
-               mesg = *ip;
-               touch(mesg);
-               mp = &message[mesg-1];
+               mp = &message[*ip - 1];
+               touch(mp);
                mp->m_flag |= MDELETED|MTOUCH;
                mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
                mp->m_flag |= MDELETED|MTOUCH;
                mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
-               last = mesg;
+               last = *ip;
        }
        if (last != NULL) {
                dot = &message[last-1];
        }
        if (last != NULL) {
                dot = &message[last-1];
@@ -375,55 +318,53 @@ delm(msgvec)
 /*
  * Undelete the indicated messages.
  */
 /*
  * Undelete the indicated messages.
  */
-
+int
 undelete(msgvec)
        int *msgvec;
 {
        register struct message *mp;
 undelete(msgvec)
        int *msgvec;
 {
        register struct message *mp;
-       register *ip, mesg;
-
-       for (ip = msgvec; ip-msgvec < msgCount; ip++) {
-               mesg = *ip;
-               if (mesg == 0)
-                       return;
-               touch(mesg);
-               mp = &message[mesg-1];
+       register *ip;
+
+       for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
+               mp = &message[*ip - 1];
+               touch(mp);
                dot = mp;
                mp->m_flag &= ~MDELETED;
        }
                dot = mp;
                mp->m_flag &= ~MDELETED;
        }
+       return 0;
 }
 
 /*
  * Interactively dump core on "core"
  */
 }
 
 /*
  * Interactively dump core on "core"
  */
-
+int
 core()
 {
 core()
 {
-       register int pid;
-       int status;
+       int pid;
+       extern union wait wait_status;
 
 
-       if ((pid = vfork()) == -1) {
+       switch (pid = vfork()) {
+       case -1:
                perror("fork");
                return(1);
                perror("fork");
                return(1);
-       }
-       if (pid == 0) {
-               sigchild();
+       case 0:
                abort();
                _exit(1);
        }
        printf("Okie dokie");
        fflush(stdout);
                abort();
                _exit(1);
        }
        printf("Okie dokie");
        fflush(stdout);
-       while (wait(&status) != pid)
-               ;
-       if (status & 0200)
-               printf(" -- Core dumped\n");
+       wait_child(pid);
+       if (wait_status.w_coredump)
+               printf(" -- Core dumped.\n");
        else
        else
-               printf("\n");
+               printf(" -- Can't dump core.\n");
+       return 0;
 }
 
 /*
  * Clobber as many bytes of stack as the user requests.
  */
 }
 
 /*
  * Clobber as many bytes of stack as the user requests.
  */
+int
 clobber(argv)
        char **argv;
 {
 clobber(argv)
        char **argv;
 {
@@ -434,12 +375,15 @@ clobber(argv)
        else
                times = (atoi(argv[0]) + 511) / 512;
        clob1(times);
        else
                times = (atoi(argv[0]) + 511) / 512;
        clob1(times);
+       return 0;
 }
 
 /*
  * Clobber the stack.
  */
 }
 
 /*
  * Clobber the stack.
  */
+void
 clob1(n)
 clob1(n)
+       int n;
 {
        char buf[512];
        register char *cp;
 {
        char buf[512];
        register char *cp;
@@ -455,127 +399,106 @@ clob1(n)
  * Add the given header fields to the retained list.
  * If no arguments, print the current list of retained fields.
  */
  * Add the given header fields to the retained list.
  * If no arguments, print the current list of retained fields.
  */
+int
 retfield(list)
        char *list[];
 {
 retfield(list)
        char *list[];
 {
-       char field[BUFSIZ];
-       register int h;
-       register struct ignore *igp;
-       char **ap;
 
 
-       if (argcount(list) == 0)
-               return(retshow());
-       for (ap = list; *ap != 0; ap++) {
-               istrcpy(field, *ap);
-
-               if (member(field, retain))
-                       continue;
-
-               h = hash(field);
-               igp = (struct ignore *) calloc(1, sizeof (struct ignore));
-               igp->i_field = calloc(strlen(field) + 1, sizeof (char));
-               strcpy(igp->i_field, field);
-               igp->i_link = retain[h];
-               retain[h] = igp;
-               nretained++;
-       }
-       return(0);
+       return ignore1(list, ignore + 1, "retained");
 }
 
 /*
 }
 
 /*
- * Print out all currently retained fields.
+ * Add the given header fields to the ignored list.
+ * If no arguments, print the current list of ignored fields.
  */
  */
-retshow()
+int
+igfield(list)
+       char *list[];
 {
 {
-       register int h, count;
-       struct ignore *igp;
-       char **ap, **ring;
-       int igcomp();
 
 
-       count = 0;
-       for (h = 0; h < HSHSIZE; h++)
-               for (igp = retain[h]; igp != 0; igp = igp->i_link)
-                       count++;
-       if (count == 0) {
-               printf("No fields currently being retained.\n");
-               return(0);
-       }
-       ring = (char **) salloc((count + 1) * sizeof (char *));
-       ap = ring;
-       for (h = 0; h < HSHSIZE; h++)
-               for (igp = retain[h]; igp != 0; igp = igp->i_link)
-                       *ap++ = igp->i_field;
-       *ap = 0;
-       qsort(ring, count, sizeof (char *), igcomp);
-       for (ap = ring; *ap != 0; ap++)
-               printf("%s\n", *ap);
-       return(0);
+       return ignore1(list, ignore, "ignored");
 }
 
 }
 
-/*
- * Add the given header fields to the ignored list.
- * If no arguments, print the current list of ignored fields.
- */
-igfield(list)
+int
+saveretfield(list)
        char *list[];
 {
        char *list[];
 {
+
+       return ignore1(list, saveignore + 1, "retained");
+}
+
+int
+saveigfield(list)
+       char *list[];
+{
+
+       return ignore1(list, saveignore, "ignored");
+}
+
+int
+ignore1(list, tab, which)
+       char *list[];
+       struct ignoretab *tab;
+       char *which;
+{
        char field[BUFSIZ];
        register int h;
        register struct ignore *igp;
        char **ap;
 
        char field[BUFSIZ];
        register int h;
        register struct ignore *igp;
        char **ap;
 
-       if (argcount(list) == 0)
-               return(igshow());
+       if (*list == NOSTR)
+               return igshow(tab, which);
        for (ap = list; *ap != 0; ap++) {
        for (ap = list; *ap != 0; ap++) {
-               if (isign(*ap))
-                       continue;
                istrcpy(field, *ap);
                istrcpy(field, *ap);
+               if (member(field, tab))
+                       continue;
                h = hash(field);
                igp = (struct ignore *) calloc(1, sizeof (struct ignore));
                h = hash(field);
                igp = (struct ignore *) calloc(1, sizeof (struct ignore));
-               igp->i_field = calloc(strlen(field) + 1, sizeof (char));
+               igp->i_field = calloc((unsigned) strlen(field) + 1,
+                       sizeof (char));
                strcpy(igp->i_field, field);
                strcpy(igp->i_field, field);
-               igp->i_link = ignore[h];
-               ignore[h] = igp;
+               igp->i_link = tab->i_head[h];
+               tab->i_head[h] = igp;
+               tab->i_count++;
        }
        }
-       return(0);
+       return 0;
 }
 
 /*
 }
 
 /*
- * Print out all currently ignored fields.
+ * Print out all currently retained fields.
  */
  */
-igshow()
+int
+igshow(tab, which)
+       struct ignoretab *tab;
+       char *which;
 {
 {
-       register int h, count;
+       register int h;
        struct ignore *igp;
        char **ap, **ring;
        int igcomp();
 
        struct ignore *igp;
        char **ap, **ring;
        int igcomp();
 
-       count = 0;
-       for (h = 0; h < HSHSIZE; h++)
-               for (igp = ignore[h]; igp != 0; igp = igp->i_link)
-                       count++;
-       if (count == 0) {
-               printf("No fields currently being ignored.\n");
-               return(0);
+       if (tab->i_count == 0) {
+               printf("No fields currently being %s.\n", which);
+               return 0;
        }
        }
-       ring = (char **) salloc((count + 1) * sizeof (char *));
+       ring = (char **) salloc((tab->i_count + 1) * sizeof (char *));
        ap = ring;
        for (h = 0; h < HSHSIZE; h++)
        ap = ring;
        for (h = 0; h < HSHSIZE; h++)
-               for (igp = ignore[h]; igp != 0; igp = igp->i_link)
+               for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
                        *ap++ = igp->i_field;
        *ap = 0;
                        *ap++ = igp->i_field;
        *ap = 0;
-       qsort(ring, count, sizeof (char *), igcomp);
+       qsort(ring, tab->i_count, sizeof (char *), igcomp);
        for (ap = ring; *ap != 0; ap++)
                printf("%s\n", *ap);
        for (ap = ring; *ap != 0; ap++)
                printf("%s\n", *ap);
-       return(0);
+       return 0;
 }
 
 /*
  * Compare two names for sorting ignored field list.
  */
 }
 
 /*
  * Compare two names for sorting ignored field list.
  */
+int
 igcomp(l, r)
 igcomp(l, r)
-       char **l, **r;
+       const void *l, *r;
 {
 {
-
-       return(strcmp(*l, *r));
+       return (strcmp(*(char **)l, *(char **)r));
 }
 }