changed alloca to salloc since no alloca on pdp-11's, generally
[unix-history] / usr / src / usr.bin / mail / fio.c
index 96f2734..8fb39d2 100644 (file)
@@ -10,7 +10,7 @@
  * File I/O.
  */
 
  * File I/O.
  */
 
-static char *SccsId = "@(#)fio.c       1.8 %G%";
+static char *SccsId = "@(#)fio.c       2.10 %G%";
 
 /*
  * Set up the input pointers while copying the mail file into
 
 /*
  * Set up the input pointers while copying the mail file into
@@ -42,7 +42,7 @@ setptr(ibuf)
                cp = linebuf;
                c = getc(ibuf);
                while (c != EOF && c != '\n') {
                cp = linebuf;
                c = getc(ibuf);
                while (c != EOF && c != '\n') {
-                       if (cp - linebuf >= BUFSIZ - 1) {
+                       if (cp - linebuf >= LINESIZE - 1) {
                                ungetc(c, ibuf);
                                *cp = 0;
                                break;
                                ungetc(c, ibuf);
                                *cp = 0;
                                break;
@@ -286,33 +286,74 @@ 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])();
 
        if (readonly)
                return;
 
        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;
                }
        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)) {
+               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);
        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;
@@ -320,23 +361,57 @@ edstop()
                if ((mp->m_flag & MDELETED) != 0)
                        continue;
                c++;
                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");
        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);
 }
 
 /*
 }
 
 /*
@@ -421,16 +496,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;
@@ -478,14 +556,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.