mail -f now correctly updates status fields
[unix-history] / usr / src / usr.bin / mail / fio.c
index faca996..96f2734 100644 (file)
@@ -10,7 +10,7 @@
  * File I/O.
  */
 
  * File I/O.
  */
 
-static char *SccsId = "@(#)fio.c       1.5 %G%";
+static char *SccsId = "@(#)fio.c       1.8 %G%";
 
 /*
  * Set up the input pointers while copying the mail file into
 
 /*
  * Set up the input pointers while copying the mail file into
@@ -20,8 +20,9 @@ static char *SccsId = "@(#)fio.c      1.5 %G%";
 setptr(ibuf)
        FILE *ibuf;
 {
 setptr(ibuf)
        FILE *ibuf;
 {
-       register int count, s, l;
+       register int c;
        register char *cp, *cp2;
        register char *cp, *cp2;
+       register int count, s, l;
        off_t offset;
        char linebuf[LINESIZE];
        char wbuf[LINESIZE];
        off_t offset;
        char linebuf[LINESIZE];
        char wbuf[LINESIZE];
@@ -38,7 +39,19 @@ setptr(ibuf)
        maybe = 1;
        flag = MUSED|MNEW;
        for (;;) {
        maybe = 1;
        flag = MUSED|MNEW;
        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;
                        flag = MUSED|MNEW;
                        this.m_offset = offsetof(offset);
                        this.m_flag = flag;
                        flag = MUSED|MNEW;
                        this.m_offset = offsetof(offset);
@@ -54,7 +67,11 @@ 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);
                }
@@ -120,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
@@ -248,11 +297,16 @@ edstop()
 
        if (readonly)
                return;
 
        if (readonly)
                return;
-       for (mp = &message[0], gotcha = 0; mp < &message[msgCount]; mp++)
-               if (mp->m_flag & (MODIFY|MDELETED)) {
+       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)
                return;
        printf("\"%s\" ", editfile);
        if (!gotcha)
                return;
        printf("\"%s\" ", editfile);