4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / usr.bin / mail / quit.c
index a4ad70e..0d5f2af 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[] = "@(#)quit.c     5.13 (Berkeley) %G%";
+static char sccsid[] = "@(#)quit.c     8.1 (Berkeley) %G%";
 #endif /* not lint */
 
 #include "rcv.h"
 #endif /* not lint */
 
 #include "rcv.h"
-#include <sys/stat.h>
-#include <sys/file.h>
+#include <fcntl.h>
+#include "extern.h"
 
 /*
  * Rcv -- receive mail rationally.
 
 /*
  * Rcv -- receive mail rationally.
@@ -32,6 +22,7 @@ static char sccsid[] = "@(#)quit.c    5.13 (Berkeley) %G%";
 /*
  * The "quit" command.
  */
 /*
  * The "quit" command.
  */
+int
 quitcmd()
 {
        /*
 quitcmd()
 {
        /*
@@ -48,7 +39,7 @@ quitcmd()
  * Save all untouched messages back in the system mailbox.
  * Remove the system mailbox, if none saved there.
  */
  * Save all untouched messages back in the system mailbox.
  * Remove the system mailbox, if none saved there.
  */
-
+void
 quit()
 {
        int mcount, p, modify, autohold, anystat, holdbit, nohold;
 quit()
 {
        int mcount, p, modify, autohold, anystat, holdbit, nohold;
@@ -84,18 +75,18 @@ quit()
         * a message.
         */
 
         * a message.
         */
 
-       fbuf = fopen(mailname, "r");
+       fbuf = Fopen(mailname, "r");
        if (fbuf == NULL)
                goto newmail;
        flock(fileno(fbuf), LOCK_EX);
        rbuf = NULL;
        if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
                printf("New mail has arrived.\n");
        if (fbuf == NULL)
                goto newmail;
        flock(fileno(fbuf), LOCK_EX);
        rbuf = NULL;
        if (fstat(fileno(fbuf), &minfo) >= 0 && minfo.st_size > mailsize) {
                printf("New mail has arrived.\n");
-               rbuf = fopen(tempResid, "w");
+               rbuf = Fopen(tempResid, "w");
                if (rbuf == NULL || fbuf == NULL)
                        goto newmail;
 #ifdef APPEND
                if (rbuf == NULL || fbuf == NULL)
                        goto newmail;
 #ifdef APPEND
-               fseek(fbuf, mailsize, 0);
+               fseek(fbuf, (long)mailsize, 0);
                while ((c = getc(fbuf)) != EOF)
                        (void) putc(c, rbuf);
 #else
                while ((c = getc(fbuf)) != EOF)
                        (void) putc(c, rbuf);
 #else
@@ -107,10 +98,10 @@ quit()
                        (void) putc(c, rbuf);
                }
 #endif
                        (void) putc(c, rbuf);
                }
 #endif
-               fclose(rbuf);
-               if ((rbuf = fopen(tempResid, "r")) == NULL)
+               Fclose(rbuf);
+               if ((rbuf = Fopen(tempResid, "r")) == NULL)
                        goto newmail;
                        goto newmail;
-               remove(tempResid);
+               rm(tempResid);
        }
 
        /*
        }
 
        /*
@@ -137,7 +128,7 @@ quit()
        }
        modify = 0;
        if (Tflag != NOSTR) {
        }
        modify = 0;
        if (Tflag != NOSTR) {
-               if ((readstat = fopen(Tflag, "w")) == NULL)
+               if ((readstat = Fopen(Tflag, "w")) == NULL)
                        Tflag = NOSTR;
        }
        for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
                        Tflag = NOSTR;
        }
        for (c = 0, p = 0, mp = &message[0]; mp < &message[msgCount]; mp++) {
@@ -155,17 +146,17 @@ quit()
                }
        }
        if (Tflag != NOSTR)
                }
        }
        if (Tflag != NOSTR)
-               fclose(readstat);
+               Fclose(readstat);
        if (p == msgCount && !modify && !anystat) {
                printf("Held %d message%s in %s\n",
                        p, p == 1 ? "" : "s", mailname);
        if (p == msgCount && !modify && !anystat) {
                printf("Held %d message%s in %s\n",
                        p, p == 1 ? "" : "s", mailname);
-               fclose(fbuf);
+               Fclose(fbuf);
                return;
        }
        if (c == 0) {
                if (p != 0) {
                        writeback(rbuf);
                return;
        }
        if (c == 0) {
                if (p != 0) {
                        writeback(rbuf);
-                       fclose(fbuf);
+                       Fclose(fbuf);
                        return;
                }
                goto cream;
                        return;
                }
                goto cream;
@@ -181,44 +172,44 @@ quit()
        mbox = expand("&");
        mcount = c;
        if (value("append") == NOSTR) {
        mbox = expand("&");
        mcount = c;
        if (value("append") == NOSTR) {
-               if ((obuf = fopen(tempQuit, "w")) == NULL) {
+               if ((obuf = Fopen(tempQuit, "w")) == NULL) {
                        perror(tempQuit);
                        perror(tempQuit);
-                       fclose(fbuf);
+                       Fclose(fbuf);
                        return;
                }
                        return;
                }
-               if ((ibuf = fopen(tempQuit, "r")) == NULL) {
+               if ((ibuf = Fopen(tempQuit, "r")) == NULL) {
                        perror(tempQuit);
                        perror(tempQuit);
-                       remove(tempQuit);
-                       fclose(obuf);
-                       fclose(fbuf);
+                       rm(tempQuit);
+                       Fclose(obuf);
+                       Fclose(fbuf);
                        return;
                }
                        return;
                }
-               remove(tempQuit);
-               if ((abuf = fopen(mbox, "r")) != NULL) {
+               rm(tempQuit);
+               if ((abuf = Fopen(mbox, "r")) != NULL) {
                        while ((c = getc(abuf)) != EOF)
                                (void) putc(c, obuf);
                        while ((c = getc(abuf)) != EOF)
                                (void) putc(c, obuf);
-                       fclose(abuf);
+                       Fclose(abuf);
                }
                if (ferror(obuf)) {
                        perror(tempQuit);
                }
                if (ferror(obuf)) {
                        perror(tempQuit);
-                       fclose(ibuf);
-                       fclose(obuf);
-                       fclose(fbuf);
+                       Fclose(ibuf);
+                       Fclose(obuf);
+                       Fclose(fbuf);
                        return;
                }
                        return;
                }
-               fclose(obuf);
+               Fclose(obuf);
                close(creat(mbox, 0600));
                close(creat(mbox, 0600));
-               if ((obuf = fopen(mbox, "r+")) == NULL) {
+               if ((obuf = Fopen(mbox, "r+")) == NULL) {
                        perror(mbox);
                        perror(mbox);
-                       fclose(ibuf);
-                       fclose(fbuf);
+                       Fclose(ibuf);
+                       Fclose(fbuf);
                        return;
                }
        }
        if (value("append") != NOSTR) {
                        return;
                }
        }
        if (value("append") != NOSTR) {
-               if ((obuf = fopen(mbox, "a")) == NULL) {
+               if ((obuf = Fopen(mbox, "a")) == NULL) {
                        perror(mbox);
                        perror(mbox);
-                       fclose(fbuf);
+                       Fclose(fbuf);
                        return;
                }
                fchmod(fileno(obuf), 0600);
                        return;
                }
                fchmod(fileno(obuf), 0600);
@@ -227,9 +218,9 @@ quit()
                if (mp->m_flag & MBOX)
                        if (send(mp, obuf, saveignore, NOSTR) < 0) {
                                perror(mbox);
                if (mp->m_flag & MBOX)
                        if (send(mp, obuf, saveignore, NOSTR) < 0) {
                                perror(mbox);
-                               fclose(ibuf);
-                               fclose(obuf);
-                               fclose(fbuf);
+                               Fclose(ibuf);
+                               Fclose(obuf);
+                               Fclose(fbuf);
                                return;
                        }
 
                                return;
                        }
 
@@ -248,17 +239,17 @@ quit()
                                break;
                        c = getc(ibuf);
                }
                                break;
                        c = getc(ibuf);
                }
-               fclose(ibuf);
+               Fclose(ibuf);
                fflush(obuf);
        }
        trunc(obuf);
        if (ferror(obuf)) {
                perror(mbox);
                fflush(obuf);
        }
        trunc(obuf);
        if (ferror(obuf)) {
                perror(mbox);
-               fclose(obuf);
-               fclose(fbuf);
+               Fclose(obuf);
+               Fclose(fbuf);
                return;
        }
                return;
        }
-       fclose(obuf);
+       Fclose(obuf);
        if (mcount == 1)
                printf("Saved 1 message in mbox\n");
        else
        if (mcount == 1)
                printf("Saved 1 message in mbox\n");
        else
@@ -271,7 +262,7 @@ quit()
 
        if (p != 0) {
                writeback(rbuf);
 
        if (p != 0) {
                writeback(rbuf);
-               fclose(fbuf);
+               Fclose(fbuf);
                return;
        }
 
                return;
        }
 
@@ -282,26 +273,26 @@ quit()
 
 cream:
        if (rbuf != NULL) {
 
 cream:
        if (rbuf != NULL) {
-               abuf = fopen(mailname, "r+");
+               abuf = Fopen(mailname, "r+");
                if (abuf == NULL)
                        goto newmail;
                while ((c = getc(rbuf)) != EOF)
                        (void) putc(c, abuf);
                if (abuf == NULL)
                        goto newmail;
                while ((c = getc(rbuf)) != EOF)
                        (void) putc(c, abuf);
-               fclose(rbuf);
+               Fclose(rbuf);
                trunc(abuf);
                trunc(abuf);
-               fclose(abuf);
+               Fclose(abuf);
                alter(mailname);
                alter(mailname);
-               fclose(fbuf);
+               Fclose(fbuf);
                return;
        }
        demail();
                return;
        }
        demail();
-       fclose(fbuf);
+       Fclose(fbuf);
        return;
 
 newmail:
        printf("Thou hast new mail.\n");
        if (fbuf != NULL)
        return;
 
 newmail:
        printf("Thou hast new mail.\n");
        if (fbuf != NULL)
-               fclose(fbuf);
+               Fclose(fbuf);
 }
 
 /*
 }
 
 /*
@@ -310,6 +301,7 @@ 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.
  */
+int
 writeback(res)
        register FILE *res;
 {
 writeback(res)
        register FILE *res;
 {
@@ -318,7 +310,7 @@ writeback(res)
        FILE *obuf;
 
        p = 0;
        FILE *obuf;
 
        p = 0;
-       if ((obuf = fopen(mailname, "r+")) == NULL) {
+       if ((obuf = Fopen(mailname, "r+")) == NULL) {
                perror(mailname);
                return(-1);
        }
                perror(mailname);
                return(-1);
        }
@@ -332,7 +324,7 @@ writeback(res)
                        p++;
                        if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
                                perror(mailname);
                        p++;
                        if (send(mp, obuf, (struct ignoretab *)0, NOSTR) < 0) {
                                perror(mailname);
-                               fclose(obuf);
+                               Fclose(obuf);
                                return(-1);
                        }
                }
                                return(-1);
                        }
                }
@@ -345,12 +337,12 @@ writeback(res)
        trunc(obuf);
        if (ferror(obuf)) {
                perror(mailname);
        trunc(obuf);
        if (ferror(obuf)) {
                perror(mailname);
-               fclose(obuf);
+               Fclose(obuf);
                return(-1);
        }
        if (res != NULL)
                return(-1);
        }
        if (res != NULL)
-               fclose(res);
-       fclose(obuf);
+               Fclose(res);
+       Fclose(obuf);
        alter(mailname);
        if (p == 1)
                printf("Held 1 message in %s\n", mailname);
        alter(mailname);
        if (p == 1)
                printf("Held 1 message in %s\n", mailname);
@@ -363,8 +355,10 @@ writeback(res)
  * Terminate an editing session by attempting to write out the user's
  * file from the temporary.  Save any new stuff appended to the file.
  */
  * Terminate an editing session by attempting to write out the user's
  * file from the temporary.  Save any new stuff appended to the file.
  */
+void
 edstop()
 {
 edstop()
 {
+       extern char *tmpdir;
        register int gotcha, c;
        register struct message *mp;
        FILE *obuf, *ibuf, *readstat;
        register int gotcha, c;
        register struct message *mp;
        FILE *obuf, *ibuf, *readstat;
@@ -376,7 +370,7 @@ edstop()
                return;
        holdsigs();
        if (Tflag != NOSTR) {
                return;
        holdsigs();
        if (Tflag != NOSTR) {
-               if ((readstat = fopen(Tflag, "w")) == NULL)
+               if ((readstat = Fopen(Tflag, "w")) == NULL)
                        Tflag = NOSTR;
        }
        for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
                        Tflag = NOSTR;
        }
        for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++) {
@@ -394,42 +388,42 @@ edstop()
                }
        }
        if (Tflag != NOSTR)
                }
        }
        if (Tflag != NOSTR)
-               fclose(readstat);
+               Fclose(readstat);
        if (!gotcha || Tflag != NOSTR)
                goto done;
        ibuf = NULL;
        if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
        if (!gotcha || Tflag != NOSTR)
                goto done;
        ibuf = NULL;
        if (stat(mailname, &statb) >= 0 && statb.st_size > mailsize) {
-               strcpy(tempname, _PATH_TMP);
+               strcpy(tempname, tmpdir);
                strcat(tempname, "mboxXXXXXX");
                mktemp(tempname);
                strcat(tempname, "mboxXXXXXX");
                mktemp(tempname);
-               if ((obuf = fopen(tempname, "w")) == NULL) {
+               if ((obuf = Fopen(tempname, "w")) == NULL) {
                        perror(tempname);
                        relsesigs();
                        reset(0);
                }
                        perror(tempname);
                        relsesigs();
                        reset(0);
                }
-               if ((ibuf = fopen(mailname, "r")) == NULL) {
+               if ((ibuf = Fopen(mailname, "r")) == NULL) {
                        perror(mailname);
                        perror(mailname);
-                       fclose(obuf);
-                       remove(tempname);
+                       Fclose(obuf);
+                       rm(tempname);
                        relsesigs();
                        reset(0);
                }
                        relsesigs();
                        reset(0);
                }
-               fseek(ibuf, mailsize, 0);
+               fseek(ibuf, (long)mailsize, 0);
                while ((c = getc(ibuf)) != EOF)
                        (void) putc(c, obuf);
                while ((c = getc(ibuf)) != EOF)
                        (void) putc(c, obuf);
-               fclose(ibuf);
-               fclose(obuf);
-               if ((ibuf = fopen(tempname, "r")) == NULL) {
+               Fclose(ibuf);
+               Fclose(obuf);
+               if ((ibuf = Fopen(tempname, "r")) == NULL) {
                        perror(tempname);
                        perror(tempname);
-                       remove(tempname);
+                       rm(tempname);
                        relsesigs();
                        reset(0);
                }
                        relsesigs();
                        reset(0);
                }
-               remove(tempname);
+               rm(tempname);
        }
        printf("\"%s\" ", mailname);
        fflush(stdout);
        }
        printf("\"%s\" ", mailname);
        fflush(stdout);
-       if ((obuf = fopen(mailname, "r+")) == NULL) {
+       if ((obuf = Fopen(mailname, "r+")) == NULL) {
                perror(mailname);
                relsesigs();
                reset(0);
                perror(mailname);
                relsesigs();
                reset(0);
@@ -450,7 +444,7 @@ edstop()
        if (ibuf != NULL) {
                while ((c = getc(ibuf)) != EOF)
                        (void) putc(c, obuf);
        if (ibuf != NULL) {
                while ((c = getc(ibuf)) != EOF)
                        (void) putc(c, obuf);
-               fclose(ibuf);
+               Fclose(ibuf);
        }
        fflush(obuf);
        if (ferror(obuf)) {
        }
        fflush(obuf);
        if (ferror(obuf)) {
@@ -458,9 +452,9 @@ edstop()
                relsesigs();
                reset(0);
        }
                relsesigs();
                reset(0);
        }
-       fclose(obuf);
+       Fclose(obuf);
        if (gotcha) {
        if (gotcha) {
-               remove(mailname);
+               rm(mailname);
                printf("removed\n");
        } else
                printf("complete\n");
                printf("removed\n");
        } else
                printf("complete\n");