bcmp collided with C library routine
[unix-history] / usr / src / usr.bin / mail / quit.c
index 959de52..a4b1ee2 100644 (file)
@@ -3,16 +3,21 @@
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms are permitted
  * 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.
+ * 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.
  */
 
  */
 
-#ifdef notdef
-static char sccsid[] = "@(#)quit.c     5.5 (Berkeley) %G%";
-#endif /* notdef */
+#ifndef lint
+static char sccsid[] = "@(#)quit.c     5.11 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
@@ -24,6 +29,20 @@ static char sccsid[] = "@(#)quit.c   5.5 (Berkeley) %G%";
  * Termination processing.
  */
 
  * Termination processing.
  */
 
+/*
+ * The "quit" command.
+ */
+quitcmd()
+{
+       /*
+        * If we are sourcing, then return 1 so execute() can handle it.
+        * Otherwise, return -1 to abort command loop.
+        */
+       if (sourcing)
+               return 1;
+       return -1;
+}
+
 /*
  * 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.
@@ -38,15 +57,23 @@ quit()
        register int c;
        extern char tempQuit[], tempResid[];
        struct stat minfo;
        register int c;
        extern char tempQuit[], tempResid[];
        struct stat minfo;
-       char *id;
+       char *mbox;
 
        /*
         * If we are read only, we can't do anything,
         * so just return quickly.
         */
 
        /*
         * If we are read only, we can't do anything,
         * so just return quickly.
         */
-
        if (readonly)
                return;
        if (readonly)
                return;
+       /*
+        * If editing (not reading system mail box), then do the work
+        * in edstop()
+        */
+       if (edit) {
+               edstop();
+               return;
+       }
+
        /*
         * See if there any messages to save in mbox.  If no, we
         * can save copying mbox to /tmp and back.
        /*
         * See if there any messages to save in mbox.  If no, we
         * can save copying mbox to /tmp and back.
@@ -55,22 +82,12 @@ quit()
         * Delete all untouched messages to keep them out of mbox.
         * If all the messages are to be preserved, just exit with
         * a message.
         * Delete all untouched messages to keep them out of mbox.
         * If all the messages are to be preserved, just exit with
         * a message.
-        *
-        * If the luser has sent mail to himself, refuse to do
-        * anything with the mailbox, unless mail locking works.
         */
 
        fbuf = fopen(mailname, "r");
        if (fbuf == NULL)
                goto newmail;
        flock(fileno(fbuf), LOCK_EX);
         */
 
        fbuf = fopen(mailname, "r");
        if (fbuf == NULL)
                goto newmail;
        flock(fileno(fbuf), LOCK_EX);
-#ifndef CANLOCK
-       if (selfsent) {
-               printf("You have new mail.\n");
-               fclose(fbuf);
-               return;
-       }
-#endif
        rbuf = NULL;
        if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
                printf("New mail has arrived.\n");
        rbuf = NULL;
        if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
                printf("New mail has arrived.\n");
@@ -80,14 +97,14 @@ quit()
 #ifdef APPEND
                fseek(fbuf, mailsize, 0);
                while ((c = getc(fbuf)) != EOF)
 #ifdef APPEND
                fseek(fbuf, mailsize, 0);
                while ((c = getc(fbuf)) != EOF)
-                       putc(c, rbuf);
+                       (void) putc(c, rbuf);
 #else
                p = minfo.st_size - mailsize;
                while (p-- > 0) {
                        c = getc(fbuf);
                        if (c == EOF)
                                goto newmail;
 #else
                p = minfo.st_size - mailsize;
                while (p-- > 0) {
                        c = getc(fbuf);
                        if (c == EOF)
                                goto newmail;
-                       putc(c, rbuf);
+                       (void) putc(c, rbuf);
                }
 #endif
                fclose(rbuf);
                }
 #endif
                fclose(rbuf);
@@ -131,18 +148,17 @@ quit()
                if (mp->m_flag & MODIFY)
                        modify++;
                if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
                if (mp->m_flag & MODIFY)
                        modify++;
                if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
-                       id = hfield("article-id", mp);
-                       if (id != NOSTR)
+                       char *id;
+
+                       if ((id = hfield("article-id", mp)) != NOSTR)
                                fprintf(readstat, "%s\n", id);
                }
        }
        if (Tflag != NOSTR)
                fclose(readstat);
        if (p == msgCount && !modify && !anystat) {
                                fprintf(readstat, "%s\n", id);
                }
        }
        if (Tflag != NOSTR)
                fclose(readstat);
        if (p == msgCount && !modify && !anystat) {
-               if (p == 1)
-                       printf("Held 1 message in %s\n", mailname);
-               else
-                       printf("Held %2d messages in %s\n", p, mailname);
+               printf("Held %d message%s in %s\n",
+                       p, p == 1 ? "" : "s", mailname);
                fclose(fbuf);
                return;
        }
                fclose(fbuf);
                return;
        }
@@ -162,6 +178,7 @@ quit()
         * just copy saveable entries at the end.
         */
 
         * just copy saveable entries at the end.
         */
 
+       mbox = expand("&");
        mcount = c;
        if (value("append") == NOSTR) {
                if ((obuf = fopen(tempQuit, "w")) == NULL) {
        mcount = c;
        if (value("append") == NOSTR) {
                if ((obuf = fopen(tempQuit, "w")) == NULL) {
@@ -179,7 +196,7 @@ quit()
                remove(tempQuit);
                if ((abuf = fopen(mbox, "r")) != NULL) {
                        while ((c = getc(abuf)) != EOF)
                remove(tempQuit);
                if ((abuf = fopen(mbox, "r")) != NULL) {
                        while ((c = getc(abuf)) != EOF)
-                               putc(c, obuf);
+                               (void) putc(c, obuf);
                        fclose(abuf);
                }
                if (ferror(obuf)) {
                        fclose(abuf);
                }
                if (ferror(obuf)) {
@@ -208,7 +225,7 @@ quit()
        }
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if (mp->m_flag & MBOX)
        }
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if (mp->m_flag & MBOX)
-                       if (send(mp, obuf, saveignore) < 0) {
+                       if (send(mp, obuf, saveignore, NOSTR) < 0) {
                                perror(mbox);
                                fclose(ibuf);
                                fclose(obuf);
                                perror(mbox);
                                fclose(ibuf);
                                fclose(obuf);
@@ -226,7 +243,7 @@ quit()
                rewind(ibuf);
                c = getc(ibuf);
                while (c != EOF) {
                rewind(ibuf);
                c = getc(ibuf);
                while (c != EOF) {
-                       putc(c, obuf);
+                       (void) putc(c, obuf);
                        if (ferror(obuf))
                                break;
                        c = getc(ibuf);
                        if (ferror(obuf))
                                break;
                        c = getc(ibuf);
@@ -269,7 +286,7 @@ cream:
                if (abuf == NULL)
                        goto newmail;
                while ((c = getc(rbuf)) != EOF)
                if (abuf == NULL)
                        goto newmail;
                while ((c = getc(rbuf)) != EOF)
-                       putc(c, abuf);
+                       (void) putc(c, abuf);
                fclose(rbuf);
                trunc(abuf);
                fclose(abuf);
                fclose(rbuf);
                trunc(abuf);
                fclose(abuf);
@@ -308,12 +325,12 @@ writeback(res)
 #ifndef APPEND
        if (res != NULL)
                while ((c = getc(res)) != EOF)
 #ifndef APPEND
        if (res != NULL)
                while ((c = getc(res)) != EOF)
-                       putc(c, obuf);
+                       (void) putc(c, obuf);
 #endif
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
                        p++;
 #endif
        for (mp = &message[0]; mp < &message[msgCount]; mp++)
                if ((mp->m_flag&MPRESERVE)||(mp->m_flag&MTOUCH)==0) {
                        p++;
-                       if (send(mp, obuf, 0) < 0) {
+                       if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
                                perror(mailname);
                                fclose(obuf);
                                return(-1);
                                perror(mailname);
                                fclose(obuf);
                                return(-1);
@@ -322,7 +339,7 @@ writeback(res)
 #ifdef APPEND
        if (res != NULL)
                while ((c = getc(res)) != EOF)
 #ifdef APPEND
        if (res != NULL)
                while ((c = getc(res)) != EOF)
-                       putc(c, obuf);
+                       (void) putc(c, obuf);
 #endif
        fflush(obuf);
        trunc(obuf);
 #endif
        fflush(obuf);
        trunc(obuf);
@@ -341,3 +358,113 @@ writeback(res)
                printf("Held %d messages in %s\n", p, mailname);
        return(0);
 }
                printf("Held %d messages in %s\n", p, mailname);
        return(0);
 }
+
+/*
+ * Terminate an editing session by attempting to write out the user's
+ * file from the temporary.  Save any new stuff appended to the file.
+ */
+edstop()
+{
+       register int gotcha, c;
+       register struct message *mp;
+       FILE *obuf, *ibuf, *readstat;
+       struct stat statb;
+       char tempname[30];
+       char *mktemp();
+
+       if (readonly)
+               return;
+       holdsigs();
+       if (Tflag != NOSTR) {
+               if ((readstat = fopen(Tflag, "w")) == NULL)
+                       Tflag = NOSTR;
+       }
+       for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
+               if (mp->m_flag & MNEW) {
+                       mp->m_flag &= ~MNEW;
+                       mp->m_flag |= MSTATUS;
+               }
+               if (mp->m_flag & (MODIFY|MDELETED|MSTATUS))
+                       gotcha++;
+               if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
+                       char *id;
+
+                       if ((id = hfield("article-id", mp)) != NOSTR)
+                               fprintf(readstat, "%s\n", id);
+               }
+       }
+       if (Tflag != NOSTR)
+               fclose(readstat);
+       if (!gotcha || Tflag != NOSTR)
+               goto done;
+       ibuf = NULL;
+       if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
+               strcpy(tempname, "/tmp/mboxXXXXXX");
+               mktemp(tempname);
+               if ((obuf = fopen(tempname, "w")) == NULL) {
+                       perror(tempname);
+                       relsesigs();
+                       reset(0);
+               }
+               if ((ibuf = fopen(mailname, "r")) == NULL) {
+                       perror(mailname);
+                       fclose(obuf);
+                       remove(tempname);
+                       relsesigs();
+                       reset(0);
+               }
+               fseek(ibuf, mailsize, 0);
+               while ((c = getc(ibuf)) != EOF)
+                       (void) putc(c, obuf);
+               fclose(ibuf);
+               fclose(obuf);
+               if ((ibuf = fopen(tempname, "r")) == NULL) {
+                       perror(tempname);
+                       remove(tempname);
+                       relsesigs();
+                       reset(0);
+               }
+               remove(tempname);
+       }
+       printf("\"%s\" ", mailname);
+       fflush(stdout);
+       if ((obuf = fopen(mailname, "r+")) == NULL) {
+               perror(mailname);
+               relsesigs();
+               reset(0);
+       }
+       trunc(obuf);
+       c = 0;
+       for (mp = &message[0]; mp < &message[msgCount]; mp++) {
+               if ((mp->m_flag & MDELETED) != 0)
+                       continue;
+               c++;
+               if (send(mp, obuf, (struct ignoretab *) NULL, NOSTR) < 0) {
+                       perror(mailname);
+                       relsesigs();
+                       reset(0);
+               }
+       }
+       gotcha = (c == 0 && ibuf == NULL);
+       if (ibuf != NULL) {
+               while ((c = getc(ibuf)) != EOF)
+                       (void) putc(c, obuf);
+               fclose(ibuf);
+       }
+       fflush(obuf);
+       if (ferror(obuf)) {
+               perror(mailname);
+               relsesigs();
+               reset(0);
+       }
+       fclose(obuf);
+       if (gotcha) {
+               remove(mailname);
+               printf("removed\n");
+       } else
+               printf("complete\n");
+       fflush(stdout);
+
+done:
+       relsesigs();
+}