document distributed with 4.3BSD
[unix-history] / usr / src / usr.bin / mail / fio.c
index 7a4e7df..c195eab 100644 (file)
@@ -1,4 +1,12 @@
-#
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+static char *sccsid = "@(#)fio.c       5.3 (Berkeley) %G%";
+#endif not lint
 
 #include "rcv.h"
 #include <sys/stat.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
@@ -10,8 +18,6 @@
  * File I/O.
  */
 
  * File I/O.
  */
 
-static char *SccsId = "@(#)fio.c       1.1 %G%";
-
 /*
  * Set up the input pointers while copying the mail file into
  * /tmp.
 /*
  * Set up the input pointers while copying the mail file into
  * /tmp.
@@ -20,10 +26,14 @@ static char *SccsId = "@(#)fio.c    1.1 %G%";
 setptr(ibuf)
        FILE *ibuf;
 {
 setptr(ibuf)
        FILE *ibuf;
 {
-       register int count, s, l;
+       register int c;
+       register char *cp, *cp2;
+       register int count, l;
+       long s;
        off_t offset;
        char linebuf[LINESIZE];
        off_t offset;
        char linebuf[LINESIZE];
-       int maybe, mestmp, flag;
+       char wbuf[LINESIZE];
+       int maybe, mestmp, flag, inhead;
        struct message this;
        extern char tempSet[];
 
        struct message this;
        extern char tempSet[];
 
@@ -31,15 +41,14 @@ setptr(ibuf)
                exit(1);
        msgCount = 0;
        offset = 0;
                exit(1);
        msgCount = 0;
        offset = 0;
-       s = 0;
+       s = 0L;
        l = 0;
        maybe = 1;
        l = 0;
        maybe = 1;
-       flag = MUSED;
-       if (value("hold") != NOSTR)
-               flag = MPRESERVE|MUSED;
+       flag = MUSED|MNEW;
        for (;;) {
        for (;;) {
-               if ((count = readline(ibuf, linebuf)) == 0) {
+               if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
                        this.m_flag = flag;
                        this.m_flag = flag;
+                       flag = MUSED|MNEW;
                        this.m_offset = offsetof(offset);
                        this.m_block = blockof(offset);
                        this.m_size = s;
                        this.m_offset = offsetof(offset);
                        this.m_block = blockof(offset);
                        this.m_size = s;
@@ -53,26 +62,46 @@ setptr(ibuf)
                        close(mestmp);
                        return;
                }
                        close(mestmp);
                        return;
                }
-               if (putline(otf, linebuf) < 0) {
+               count = strlen(linebuf);
+               fputs(linebuf, otf);
+               cp = linebuf + (count - 1);
+               if (*cp == '\n')
+                       *cp = 0;
+               if (ferror(otf)) {
                        perror("/tmp");
                        exit(1);
                }
                        perror("/tmp");
                        exit(1);
                }
-               if (maybe && ishead(linebuf)) {
+               if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
                        msgCount++;
                        this.m_flag = flag;
                        msgCount++;
                        this.m_flag = flag;
+                       flag = MUSED|MNEW;
+                       inhead = 1;
                        this.m_block = blockof(offset);
                        this.m_offset = offsetof(offset);
                        this.m_size = s;
                        this.m_lines = l;
                        this.m_block = blockof(offset);
                        this.m_offset = offsetof(offset);
                        this.m_size = s;
                        this.m_lines = l;
-                       s = 0;
+                       s = 0L;
                        l = 0;
                        if (append(&this, mestmp)) {
                                perror(tempSet);
                                exit(1);
                        }
                }
                        l = 0;
                        if (append(&this, mestmp)) {
                                perror(tempSet);
                                exit(1);
                        }
                }
+               if (linebuf[0] == 0)
+                       inhead = 0;
+               if (inhead && (cp = index(linebuf, ':'))) {
+                       *cp = 0;
+                       if (icequal(linebuf, "status")) {
+                               ++cp;
+                               if (index(cp, 'R'))
+                                       flag |= MREAD;
+                               if (index(cp, 'O'))
+                                       flag &= ~MNEW;
+                               inhead = 0;
+                       }
+               }
                offset += count;
                offset += count;
-               s += count;
+               s += (long) count;
                l++;
                maybe = 0;
                if (linebuf[0] == 0)
                l++;
                maybe = 0;
                if (linebuf[0] == 0)
@@ -110,23 +139,15 @@ readline(ibuf, linebuf)
        FILE *ibuf;
        char *linebuf;
 {
        FILE *ibuf;
        char *linebuf;
 {
-       register char *cp;
-       register int c;
+       register int n;
 
 
-       do {
-               clearerr(ibuf);
-               c = getc(ibuf);
-               for (cp = linebuf; c != '\n' && c != EOF; c = getc(ibuf)) {
-                       if (c == 0)
-                               continue;
-                       if (cp - linebuf < LINESIZE-2)
-                               *cp++ = c;
-               }
-       } while (ferror(ibuf) && ibuf == stdin);
-       *cp = 0;
-       if (c == EOF && cp == linebuf)
+       clearerr(ibuf);
+       if (fgets(linebuf, LINESIZE, ibuf) == NULL)
                return(0);
                return(0);
-       return(cp - linebuf + 1);
+       n = strlen(linebuf);
+       if (n >= 1 && linebuf[n-1] == '\n')
+               linebuf[n-1] = '\0';
+       return(n);
 }
 
 /*
 }
 
 /*
@@ -167,6 +188,8 @@ makemessage(f)
                printf("Insufficient memory for %d messages\n", msgCount);
                exit(1);
        }
                printf("Insufficient memory for %d messages\n", msgCount);
                exit(1);
        }
+       if (message != (struct message *) 0)
+               cfree((char *) message);
        message = (struct message *) mp;
        dot = message;
        lseek(f, 0L, 0);
        message = (struct message *) mp;
        dot = message;
        lseek(f, 0L, 0);
@@ -175,8 +198,9 @@ makemessage(f)
        for (m = &message[0]; m < &message[msgCount]; m++) {
                m->m_size = (m+1)->m_size;
                m->m_lines = (m+1)->m_lines;
        for (m = &message[0]; m < &message[msgCount]; m++) {
                m->m_size = (m+1)->m_size;
                m->m_lines = (m+1)->m_lines;
+               m->m_flag = (m+1)->m_flag;
        }
        }
-       message[msgCount].m_size = 0;
+       message[msgCount].m_size = 0L;
        message[msgCount].m_lines = 0;
 }
 
        message[msgCount].m_lines = 0;
 }
 
@@ -214,63 +238,135 @@ remove(name)
 
 /*
  * Terminate an editing session by attempting to write out the user's
 
 /*
  * Terminate an editing session by attempting to write out the user's
- * file from the temporary.
+ * file from the temporary.  Save any new stuff appended to the file.
  */
  */
-
 edstop()
 {
        register int gotcha, c;
        register struct message *mp;
 edstop()
 {
        register int gotcha, c;
        register struct message *mp;
-       FILE *obuf;
+       FILE *obuf, *ibuf, *readstat;
+       struct stat statb;
+       char tempname[30], *id;
+       int (*sigs[3])();
 
 
-       for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++)
-               if (mp->m_flag & (MODIFY|MDELETED)) {
+       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++;
                        gotcha++;
-                       break;
+               if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
+                       if ((id = hfield("article-id", mp)) != NOSTR)
+                               fprintf(readstat, "%s\n", id);
                }
                }
-       if (!gotcha)
-               return;
+       }
+       if (Tflag != NOSTR)
+               fclose(readstat);
+       if (!gotcha || Tflag != NOSTR)
+               goto done;
+       ibuf = NULL;
+       if (stat(editfile, &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(editfile, "r")) == NULL) {
+                       perror(editfile);
+                       fclose(obuf);
+                       remove(tempname);
+                       relsesigs();
+                       reset(0);
+               }
+               fseek(ibuf, mailsize, 0);
+               while ((c = getc(ibuf)) != EOF)
+                       putc(c, obuf);
+               fclose(ibuf);
+               fclose(obuf);
+               if ((ibuf = fopen(tempname, "r")) == NULL) {
+                       perror(tempname);
+                       remove(tempname);
+                       relsesigs();
+                       reset(0);
+               }
+               remove(tempname);
+       }
        printf("\"%s\" ", editfile);
        printf("\"%s\" ", editfile);
-       flush();
-       if ((obuf = fopen(editfile, "w")) == NULL) {
+       fflush(stdout);
+       if ((obuf = fopen(editfile, "r+")) == NULL) {
                perror(editfile);
                perror(editfile);
+               relsesigs();
                reset(0);
        }
                reset(0);
        }
+       trunc(obuf);
        c = 0;
        for (mp = &message[0]; mp < &message[msgCount]; mp++) {
                if ((mp->m_flag & MDELETED) != 0)
                        continue;
                c++;
        c = 0;
        for (mp = &message[0]; mp < &message[msgCount]; mp++) {
                if ((mp->m_flag & MDELETED) != 0)
                        continue;
                c++;
-               if (send(mp, obuf) < 0) {
+               if (send(mp, obuf, 0) < 0) {
                        perror(editfile);
                        perror(editfile);
+                       relsesigs();
                        reset(0);
                }
        }
                        reset(0);
                }
        }
+       gotcha = (c == 0 && ibuf == NULL);
+       if (ibuf != NULL) {
+               while ((c = getc(ibuf)) != EOF)
+                       putc(c, obuf);
+               fclose(ibuf);
+       }
        fflush(obuf);
        if (ferror(obuf)) {
                perror(editfile);
        fflush(obuf);
        if (ferror(obuf)) {
                perror(editfile);
+               relsesigs();
                reset(0);
        }
                reset(0);
        }
-       if (c == 0) {
+       fclose(obuf);
+       if (gotcha) {
                remove(editfile);
                printf("removed\n");
        }
        else
                printf("complete\n");
                remove(editfile);
                printf("removed\n");
        }
        else
                printf("complete\n");
-       flush();
+       fflush(stdout);
+
+done:
+       relsesigs();
 }
 
 }
 
+static int sigdepth = 0;               /* depth of holdsigs() */
+static int omask = 0;
 /*
 /*
- * Empty the output buffer.
+ * Hold signals SIGHUP - SIGQUIT.
  */
  */
+holdsigs()
+{
+       register int i;
+
+       if (sigdepth++ == 0)
+               omask = sigblock(sigmask(SIGHUP)|sigmask(SIGINT)|sigmask(SIGQUIT));
+}
 
 
-clrbuf(buf)
-       register FILE *buf;
+/*
+ * Release signals SIGHUP - SIGQUIT
+ */
+relsesigs()
 {
 {
+       register int i;
 
 
-       buf = stdout;
-       buf->_ptr = buf->_base;
-       buf->_cnt = BUFSIZ;
+       if (--sigdepth == 0)
+               sigsetmask(omask);
 }
 
 /*
 }
 
 /*
@@ -297,16 +393,6 @@ opentemp(file)
        return(f);
 }
 
        return(f);
 }
 
-/*
- * Flush the standard output.
- */
-
-flush()
-{
-       fflush(stdout);
-       fflush(stderr);
-}
-
 /*
  * Determine the size of the file possessed by
  * the passed buffer.
 /*
  * Determine the size of the file possessed by
  * the passed buffer.
@@ -342,16 +428,19 @@ expand(name)
        int s, pivec[2], (*sigint)();
        struct stat sbuf;
 
        int s, pivec[2], (*sigint)();
        struct stat sbuf;
 
+       if (name[0] == '+' && getfold(cmdbuf) >= 0) {
+               sprintf(xname, "%s/%s", cmdbuf, name + 1);
+               return(expand(savestr(xname)));
+       }
        if (!anyof(name, "~{[*?$`'\"\\"))
                return(name);
        if (!anyof(name, "~{[*?$`'\"\\"))
                return(name);
-       /* sigint = signal(SIGINT, SIG_IGN); */
        if (pipe(pivec) < 0) {
                perror("pipe");
        if (pipe(pivec) < 0) {
                perror("pipe");
-               /* signal(SIGINT, sigint) */
                return(name);
        }
        sprintf(cmdbuf, "echo %s", name);
        if ((pid = vfork()) == 0) {
                return(name);
        }
        sprintf(cmdbuf, "echo %s", name);
        if ((pid = vfork()) == 0) {
+               sigchild();
                Shell = value("SHELL");
                if (Shell == NOSTR)
                        Shell = SHELL;
                Shell = value("SHELL");
                if (Shell == NOSTR)
                        Shell = SHELL;
@@ -399,14 +488,29 @@ expand(name)
                fprintf(stderr, "\"%s\": Ambiguous\n", name);
                goto err;
        }
                fprintf(stderr, "\"%s\": Ambiguous\n", name);
                goto err;
        }
-       /* signal(SIGINT, sigint) */
        return(savestr(xname));
 
 err:
        return(savestr(xname));
 
 err:
-       /* signal(SIGINT, sigint); */
        return(NOSTR);
 }
 
        return(NOSTR);
 }
 
+/*
+ * Determine the current folder directory name.
+ */
+getfold(name)
+       char *name;
+{
+       char *folder;
+
+       if ((folder = value("folder")) == NOSTR)
+               return(-1);
+       if (*folder == '/')
+               strcpy(name, folder);
+       else
+               sprintf(name, "%s/%s", homedir, folder);
+       return(0);
+}
+
 /*
  * A nicer version of Fdopen, which allows us to fclose
  * without losing the open file.
 /*
  * A nicer version of Fdopen, which allows us to fclose
  * without losing the open file.