386BSD 0.1 development
[unix-history] / usr / src / usr.bin / mail / cmd2.c
index cdb94fb..9efbe4f 100644 (file)
@@ -2,20 +2,40 @@
  * 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.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
  */
 
-#ifdef notdef
-static char sccsid[] = "@(#)cmd2.c     5.6 (Berkeley) %G%";
-#endif /* notdef */
+#ifndef lint
+static char sccsid[] = "@(#)cmd2.c     5.14 (Berkeley) 6/25/90";
+#endif /* not lint */
 
 #include "rcv.h"
 
 #include "rcv.h"
-#include <sys/stat.h>
 #include <sys/wait.h>
 
 /*
 #include <sys/wait.h>
 
 /*
@@ -112,7 +132,7 @@ save(str)
        char str[];
 {
 
        char str[];
 {
 
-       return(save1(str, 1));
+       return save1(str, 1, "save", saveignore);
 }
 
 /*
 }
 
 /*
@@ -122,25 +142,24 @@ copycmd(str)
        char 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)
+save1(str, mark, cmd, ignore)
        char str[];
        char str[];
+       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);
@@ -158,35 +177,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);
 }
 
@@ -198,65 +212,8 @@ save1(str, mark)
 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);
 }
 
 /*
 }
 
 /*
@@ -310,7 +267,8 @@ snarf(linebuf, flag)
 delete(msgvec)
        int msgvec[];
 {
 delete(msgvec)
        int msgvec[];
 {
-       return(delm(msgvec));
+       delm(msgvec);
+       return 0;
 }
 
 /*
 }
 
 /*
@@ -325,20 +283,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,17 +305,16 @@ delm(msgvec)
        int *msgvec;
 {
        register struct message *mp;
        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,17 +344,15 @@ undelete(msgvec)
        int *msgvec;
 {
        register struct message *mp;
        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;
 }
 
 /*
 }
 
 /*
@@ -410,25 +361,24 @@ undelete(msgvec)
 
 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;
 }
 
        return 0;
 }
 
@@ -445,6 +395,7 @@ clobber(argv)
        else
                times = (atoi(argv[0]) + 511) / 512;
        clob1(times);
        else
                times = (atoi(argv[0]) + 511) / 512;
        clob1(times);
+       return 0;
 }
 
 /*
 }
 
 /*
@@ -508,7 +459,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);