date and time created 83/10/02 21:41:04 by sam
[unix-history] / usr / src / usr.bin / mail / quit.c
index 33abe0a..f2b2e8d 100644 (file)
@@ -1,4 +1,6 @@
-#
+#ifndef lint
+static char sccsid[] = "@(#)quit.c     2.7 (Berkeley) %G%";
+#endif
 
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
@@ -9,8 +11,6 @@
  * Termination processing.
  */
 
  * Termination processing.
  */
 
-static char *SccsId = "@(#)quit.c      1.2 %G%";
-
 /*
  * Save all of the undetermined messages at the top of "mbox"
  * Save all untouched messages back in the system mailbox.
 /*
  * Save all of the undetermined messages at the top of "mbox"
  * Save all untouched messages back in the system mailbox.
@@ -19,12 +19,13 @@ static char *SccsId = "@(#)quit.c   1.2 %G%";
 
 quit()
 {
 
 quit()
 {
-       int mcount, p, modify;
-       FILE *ibuf, *obuf, *fbuf, *rbuf;
+       int mcount, p, modify, autohold, anystat, holdbit, nohold;
+       FILE *ibuf, *obuf, *fbuf, *rbuf, *readstat;
        register struct message *mp;
        register int c;
        extern char tempQuit[], tempResid[];
        struct stat minfo;
        register struct message *mp;
        register int c;
        extern char tempQuit[], tempResid[];
        struct stat minfo;
+       char *id;
 
        /*
         * If we are read only, we can't do anything,
 
        /*
         * If we are read only, we can't do anything,
@@ -80,22 +81,50 @@ quit()
                        goto newmail;
                remove(tempResid);
        }
                        goto newmail;
                remove(tempResid);
        }
+
+       /*
+        * Adjust the message flags in each message.
+        */
+
+       anystat = 0;
+       autohold = value("hold") != NOSTR;
+       holdbit = autohold ? MPRESERVE : MBOX;
+       nohold = MBOX|MSAVED|MDELETED|MPRESERVE;
+       if (value("keepsave") != NOSTR)
+               nohold &= ~MSAVED;
        for (mp = &message[0]; mp < &message[msgCount]; mp++) {
        for (mp = &message[0]; mp < &message[msgCount]; mp++) {
-               if (mp->m_flag & MDELETED)
-                       mp->m_flag = MDELETED|MTOUCH;
+               if (mp->m_flag & MNEW) {
+                       mp->m_flag &= ~MNEW;
+                       mp->m_flag |= MSTATUS;
+               }
+               if (mp->m_flag & MSTATUS)
+                       anystat++;
                if ((mp->m_flag & MTOUCH) == 0)
                if ((mp->m_flag & MTOUCH) == 0)
-                       mp->m_flag |= MDELETED;
+                       mp->m_flag |= MPRESERVE;
+               if ((mp->m_flag & nohold) == 0)
+                       mp->m_flag |= holdbit;
        }
        modify = 0;
        }
        modify = 0;
+       if (Tflag != NOSTR) {
+               if ((readstat = fopen(Tflag, "w")) == NULL)
+                       Tflag = NOSTR;
+       }
        for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
        for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
-               if ((mp->m_flag & (MSAVED|MDELETED|MPRESERVE)) == 0)
+               if (mp->m_flag & MBOX)
                        c++;
                        c++;
-               if ((mp->m_flag & MPRESERVE) || (mp->m_flag & MTOUCH) == 0)
+               if (mp->m_flag & MPRESERVE)
                        p++;
                if (mp->m_flag & MODIFY)
                        modify++;
                        p++;
                if (mp->m_flag & MODIFY)
                        modify++;
+               if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
+                       id = hfield("article-id", mp);
+                       if (id != NOSTR)
+                               fprintf(readstat, "%s\n", id);
+               }
        }
        }
-       if (p == msgCount && !modify) {
+       if (Tflag != NOSTR)
+               fclose(readstat);
+       if (p == msgCount && !modify && !anystat) {
                if (p == 1)
                        printf("Held 1 message in %s\n", mailname);
                else
                if (p == 1)
                        printf("Held 1 message in %s\n", mailname);
                else
@@ -148,7 +177,7 @@ quit()
                }
                fclose(obuf);
                close(creat(mbox, 0600));
                }
                fclose(obuf);
                close(creat(mbox, 0600));
-               if ((obuf = fopen(mbox, "w")) == NULL) {
+               if ((obuf = fopen(mbox, "r+")) == NULL) {
                        perror(mbox);
                        fclose(ibuf);
                        unlock();
                        perror(mbox);
                        fclose(ibuf);
                        unlock();
@@ -162,8 +191,8 @@ quit()
                        return;
                }
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                        return;
                }
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
-               if ((mp->m_flag & (MDELETED|MSAVED|MPRESERVE)) == 0)
-                       if (send(mp, obuf) < 0) {
+               if (mp->m_flag & MBOX)
+                       if (send(mp, obuf, 0) < 0) {
                                perror(mbox);
                                fclose(ibuf);
                                fclose(obuf);
                                perror(mbox);
                                fclose(ibuf);
                                fclose(obuf);
@@ -189,6 +218,7 @@ quit()
                fclose(ibuf);
                fflush(obuf);
        }
                fclose(ibuf);
                fflush(obuf);
        }
+       trunc(obuf);
        if (ferror(obuf)) {
                perror(mbox);
                fclose(obuf);
        if (ferror(obuf)) {
                perror(mbox);
                fclose(obuf);
@@ -219,12 +249,13 @@ quit()
 
 cream:
        if (rbuf != NULL) {
 
 cream:
        if (rbuf != NULL) {
-               fbuf = fopen(mailname, "w");
+               fbuf = fopen(mailname, "r+");
                if (fbuf == NULL)
                        goto newmail;
                while ((c = getc(rbuf)) != EOF)
                        putc(c, fbuf);
                fclose(rbuf);
                if (fbuf == NULL)
                        goto newmail;
                while ((c = getc(rbuf)) != EOF)
                        putc(c, fbuf);
                fclose(rbuf);
+               trunc(fbuf);
                fclose(fbuf);
                alter(mailname);
                unlock();
                fclose(fbuf);
                alter(mailname);
                unlock();
@@ -245,7 +276,6 @@ newmail:
  * saved.  On any error, just return -1.  Else return 0.
  * Incorporate the any new mail that we found.
  */
  * saved.  On any error, just return -1.  Else return 0.
  * Incorporate the any new mail that we found.
  */
-
 writeback(res)
        register FILE *res;
 {
 writeback(res)
        register FILE *res;
 {
@@ -254,7 +284,7 @@ writeback(res)
        FILE *obuf;
 
        p = 0;
        FILE *obuf;
 
        p = 0;
-       if ((obuf = fopen(mailname, "w")) == NULL) {
+       if ((obuf = fopen(mailname, "r+")) == NULL) {
                perror(mailname);
                return(-1);
        }
                perror(mailname);
                return(-1);
        }
@@ -266,7 +296,7 @@ writeback(res)
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
                        p++;
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
                        p++;
-                       if (send(mp, obuf) < 0) {
+                       if (send(mp, obuf, 0) < 0) {
                                perror(mailname);
                                fclose(obuf);
                                return(-1);
                                perror(mailname);
                                fclose(obuf);
                                return(-1);
@@ -278,6 +308,7 @@ writeback(res)
                        putc(c, obuf);
 #endif
        fflush(obuf);
                        putc(c, obuf);
 #endif
        fflush(obuf);
+       trunc(obuf);
        if (ferror(obuf)) {
                perror(mailname);
                fclose(obuf);
        if (ferror(obuf)) {
                perror(mailname);
                fclose(obuf);