changed to version 2.1
[unix-history] / usr / src / usr.bin / mail / fio.c
index 72a669b..3a92c27 100644 (file)
@@ -10,7 +10,7 @@
  * File I/O.
  */
 
  * File I/O.
  */
 
-static char *SccsId = "@(#)fio.c       1.2 %G%";
+static char *SccsId = "@(#)fio.c       1.11 %G%";
 
 /*
  * Set up the input pointers while copying the mail file into
 
 /*
  * Set up the input pointers while copying the mail file into
@@ -20,10 +20,13 @@ static char *SccsId = "@(#)fio.c    1.2 %G%";
 setptr(ibuf)
        FILE *ibuf;
 {
 setptr(ibuf)
        FILE *ibuf;
 {
+       register int c;
+       register char *cp, *cp2;
        register int count, s, l;
        off_t offset;
        char linebuf[LINESIZE];
        register int count, s, l;
        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[];
 
@@ -34,12 +37,23 @@ setptr(ibuf)
        s = 0;
        l = 0;
        maybe = 1;
        s = 0;
        l = 0;
        maybe = 1;
-       flag = MUSED;
-       if (value("hold") != NOSTR)
-               flag = MPRESERVE|MUSED;
+       flag = MUSED|MNEW;
        for (;;) {
        for (;;) {
-               if ((count = readline(ibuf, linebuf)) == 0) {
+               cp = linebuf;
+               c = getc(ibuf);
+               while (c != EOF && c != '\n') {
+                       if (cp - linebuf >= BUFSIZ - 1) {
+                               ungetc(c, ibuf);
+                               *cp = 0;
+                               break;
+                       }
+                       *cp++ = c;
+                       c = getc(ibuf);
+               }
+               *cp = 0;
+               if (cp == linebuf && c == EOF) {
                        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,13 +67,19 @@ setptr(ibuf)
                        close(mestmp);
                        return;
                }
                        close(mestmp);
                        return;
                }
-               if (putline(otf, linebuf) < 0) {
+               count = cp - linebuf + 1;
+               for (cp = linebuf; *cp;)
+                       putc(*cp++, otf);
+               putc('\n', otf);
+               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_block = blockof(offset);
                        this.m_offset = offsetof(offset);
                        this.m_size = s;
@@ -71,6 +91,23 @@ setptr(ibuf)
                                exit(1);
                        }
                }
                                exit(1);
                        }
                }
+               if (linebuf[0] == 0)
+                       inhead = 0;
+               if (inhead && index(linebuf, ':')) {
+                       cp = linebuf;
+                       cp2 = wbuf;
+                       while (isalpha(*cp))
+                               *cp2++ = *cp++;
+                       *cp2 = 0;
+                       if (icequal(wbuf, "status")) {
+                               cp = index(linebuf, ':');
+                               if (index(cp, 'R'))
+                                       flag |= MREAD;
+                               if (index(cp, 'O'))
+                                       flag &= ~MNEW;
+                               inhead = 0;
+                       }
+               }
                offset += count;
                s += count;
                l++;
                offset += count;
                s += count;
                l++;
@@ -100,6 +137,38 @@ putline(obuf, linebuf)
        return(c+1);
 }
 
        return(c+1);
 }
 
+/*
+ * Quickly read a line from the specified input into the line
+ * buffer; return characters read.
+ */
+
+freadline(ibuf, linebuf)
+       register FILE *ibuf;
+       register char *linebuf;
+{
+       register int c;
+       register char *cp;
+
+       c = getc(ibuf);
+       cp = linebuf;
+       while (c != '\n' && c != EOF) {
+               if (c == 0) {
+                       c = getc(ibuf);
+                       continue;
+               }
+               if (cp - linebuf >= BUFSIZ-1) {
+                       *cp = 0;
+                       return(cp - linebuf + 1);
+               }
+               *cp++ = c;
+               c = getc(ibuf);
+       }
+       if (c == EOF && cp == linebuf)
+               return(0);
+       *cp = 0;
+       return(cp - linebuf + 1);
+}
+
 /*
  * Read up a line from the specified input into the line
  * buffer.  Return the number of characters read.  Do not
 /*
  * Read up a line from the specified input into the line
  * buffer.  Return the number of characters read.  Do not
@@ -177,6 +246,7 @@ 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_lines = 0;
        }
        message[msgCount].m_size = 0;
        message[msgCount].m_lines = 0;
@@ -216,28 +286,65 @@ 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;
+       struct stat statb;
+       char tempname[30];
+       int (*sigs[3])();
 
        if (readonly)
                return;
 
        if (readonly)
                return;
-       for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++)
-               if (mp->m_flag & (MODIFY|MDELETED)) {
+       holdsigs();
+       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++;
                        break;
                }
                        gotcha++;
                        break;
                }
+       }
        if (!gotcha)
        if (!gotcha)
-               return;
+               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);
+               }
+               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);
        flush();
        if ((obuf = fopen(editfile, "w")) == NULL) {
                perror(editfile);
        printf("\"%s\" ", editfile);
        flush();
        if ((obuf = fopen(editfile, "w")) == NULL) {
                perror(editfile);
+               relsesigs();
                reset(0);
        }
        c = 0;
                reset(0);
        }
        c = 0;
@@ -247,21 +354,55 @@ edstop()
                c++;
                if (send(mp, obuf) < 0) {
                        perror(editfile);
                c++;
                if (send(mp, obuf) < 0) {
                        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");
        flush();
                remove(editfile);
                printf("removed\n");
        }
        else
                printf("complete\n");
        flush();
+
+done:
+       relsesigs();
+}
+
+/*
+ * Hold signals SIGHUP - SIGQUIT.
+ */
+holdsigs()
+{
+       register int i;
+
+       for (i = SIGHUP; i <= SIGQUIT; i++)
+               sighold(i);
+}
+
+/*
+ * Release signals SIGHUP - SIGQUIT
+ */
+relsesigs()
+{
+       register int i;
+
+       for (i = SIGHUP; i <= SIGQUIT; i++)
+               sigrelse(i);
 }
 
 /*
 }
 
 /*
@@ -348,10 +489,8 @@ expand(name)
 
        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);
                return(name);
        }
        sprintf(cmdbuf, "echo %s", name);
@@ -403,11 +542,9 @@ 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);
 }