4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / cmd2.c
index 97c4e93..e3734a7 100644 (file)
@@ -1,27 +1,17 @@
 /*
 /*
- * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1980, 1993
+ *     The 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.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)cmd2.c     5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)cmd2.c     8.1 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "rcv.h"
 #endif /* not lint */
 
 #include "rcv.h"
-#include <sys/stat.h>
 #include <sys/wait.h>
 #include <sys/wait.h>
+#include "extern.h"
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -34,7 +24,7 @@ static char sccsid[] = "@(#)cmd2.c    5.7 (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;
 {
@@ -113,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);
@@ -163,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, saveignore)) < 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);
 }
 
@@ -199,69 +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 - 1;
-               while (t-- > 0) {
-                       readline(mesf, linebuf);
-                       if (blankline(linebuf))
-                               break;
-               }
-               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);
 }
 
 /*
 }
 
 /*
@@ -311,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[];
 {
@@ -330,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);
 }
 
 /*
 }
 
 /*
@@ -351,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];
@@ -391,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;
-       union wait 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) {
+       case 0:
                abort();
                _exit(1);
        }
        printf("Okie dokie");
        fflush(stdout);
                abort();
                _exit(1);
        }
        printf("Okie dokie");
        fflush(stdout);
-       while (wait(&status) != pid)
-               ;
-       if (status.w_coredump)
-               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.
  */
        return 0;
 }
 
 /*
  * Clobber as many bytes of stack as the user requests.
  */
+int
 clobber(argv)
        char **argv;
 {
 clobber(argv)
        char **argv;
 {
@@ -450,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;
@@ -471,6 +399,7 @@ 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[];
 {
@@ -482,6 +411,7 @@ retfield(list)
  * Add the given header fields to the ignored list.
  * If no arguments, print the current list of ignored fields.
  */
  * Add the given header fields to the ignored list.
  * If no arguments, print the current list of ignored fields.
  */
+int
 igfield(list)
        char *list[];
 {
 igfield(list)
        char *list[];
 {
@@ -489,6 +419,7 @@ igfield(list)
        return ignore1(list, ignore, "ignored");
 }
 
        return ignore1(list, ignore, "ignored");
 }
 
+int
 saveretfield(list)
        char *list[];
 {
 saveretfield(list)
        char *list[];
 {
@@ -496,6 +427,7 @@ saveretfield(list)
        return ignore1(list, saveignore + 1, "retained");
 }
 
        return ignore1(list, saveignore + 1, "retained");
 }
 
+int
 saveigfield(list)
        char *list[];
 {
 saveigfield(list)
        char *list[];
 {
@@ -503,6 +435,7 @@ saveigfield(list)
        return ignore1(list, saveignore, "ignored");
 }
 
        return ignore1(list, saveignore, "ignored");
 }
 
+int
 ignore1(list, tab, which)
        char *list[];
        struct ignoretab *tab;
 ignore1(list, tab, which)
        char *list[];
        struct ignoretab *tab;
@@ -513,7 +446,7 @@ ignore1(list, tab, which)
        register struct ignore *igp;
        char **ap;
 
        register struct ignore *igp;
        char **ap;
 
-       if (argcount(list) == 0)
+       if (*list == NOSTR)
                return igshow(tab, which);
        for (ap = list; *ap != 0; ap++) {
                istrcpy(field, *ap);
                return igshow(tab, which);
        for (ap = list; *ap != 0; ap++) {
                istrcpy(field, *ap);
@@ -534,6 +467,7 @@ ignore1(list, tab, which)
 /*
  * Print out all currently retained fields.
  */
 /*
  * Print out all currently retained fields.
  */
+int
 igshow(tab, which)
        struct ignoretab *tab;
        char *which;
 igshow(tab, which)
        struct ignoretab *tab;
        char *which;
@@ -553,7 +487,7 @@ igshow(tab, which)
                for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
                        *ap++ = igp->i_field;
        *ap = 0;
                for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
                        *ap++ = igp->i_field;
        *ap = 0;
-       qsort((char *) ring, tab->i_count, sizeof (char *), igcomp);
+       qsort(ring, tab->i_count, sizeof (char *), igcomp);
        for (ap = ring; *ap != 0; ap++)
                printf("%s\n", *ap);
        return 0;
        for (ap = ring; *ap != 0; ap++)
                printf("%s\n", *ap);
        return 0;
@@ -562,9 +496,9 @@ igshow(tab, which)
 /*
  * 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));
 }
 }