add .Vx so tmac.andoc will call tmac.mdoc-old
[unix-history] / usr / src / usr.bin / mail / fio.c
index c213cea..7f0ca8a 100644 (file)
@@ -1,17 +1,19 @@
 /*
  * Copyright (c) 1980 Regents of the University of California.
 /*
  * Copyright (c) 1980 Regents of the University of California.
- * All rights reserved.  The Berkeley software License Agreement
- * specifies the terms and conditions for redistribution.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char *sccsid = "@(#)fio.c       5.5 (Berkeley) %G%";
-#endif not lint
+static char sccsid[] = "@(#)fio.c      5.24 (Berkeley) %G%";
+#endif /* not lint */
 
 #include "rcv.h"
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/wait.h>
 
 #include "rcv.h"
 #include <sys/stat.h>
 #include <sys/file.h>
 #include <sys/wait.h>
+#include <paths.h>
 #include <errno.h>
 
 /*
 #include <errno.h>
 
 /*
@@ -21,26 +23,28 @@ static char *sccsid = "@(#)fio.c    5.5 (Berkeley) %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.
  */
 setptr(ibuf)
        register FILE *ibuf;
 {
  */
 setptr(ibuf)
        register FILE *ibuf;
 {
-       register c;
+       register int c, count;
        register char *cp, *cp2;
        register char *cp, *cp2;
-       register count;
-       char linebuf[LINESIZE];
-       int maybe, inhead;
+       struct message this;
        FILE *mestmp;
        off_t offset;
        FILE *mestmp;
        off_t offset;
-       struct message this;
-       extern char tempSet[];
+       int maybe, inhead;
+       char linebuf[LINESIZE];
 
 
-       if ((c = opentemp(tempSet)) < 0)
+       /* Get temporary file. */
+       (void)sprintf(linebuf, "%s/mail.XXXXXX", _PATH_TMP);
+       if ((c = mkstemp(linebuf)) == -1 ||
+           (mestmp = Fdopen(c, "r+")) == NULL) {
+               (void)fprintf(stderr, "mail: can't open %s\n", linebuf);
                exit(1);
                exit(1);
-       if ((mestmp = fdopen(c, "r+")) == NULL)
-               panic("Can't open temporary");
+       }
+       (void)unlink(linebuf);
+
        msgCount = 0;
        maybe = 1;
        inhead = 0;
        msgCount = 0;
        maybe = 1;
        inhead = 0;
@@ -53,15 +57,14 @@ setptr(ibuf)
        for (;;) {
                if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
                        if (append(&this, mestmp)) {
        for (;;) {
                if (fgets(linebuf, LINESIZE, ibuf) == NULL) {
                        if (append(&this, mestmp)) {
-                               perror(tempSet);
+                               perror("temporary file");
                                exit(1);
                        }
                                exit(1);
                        }
-                       fclose(ibuf);
                        makemessage(mestmp);
                        return;
                }
                count = strlen(linebuf);
                        makemessage(mestmp);
                        return;
                }
                count = strlen(linebuf);
-               fwrite(linebuf, sizeof *linebuf, count, otf);
+               (void) fwrite(linebuf, sizeof *linebuf, count, otf);
                if (ferror(otf)) {
                        perror("/tmp");
                        exit(1);
                if (ferror(otf)) {
                        perror("/tmp");
                        exit(1);
@@ -70,7 +73,7 @@ setptr(ibuf)
                if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
                        msgCount++;
                        if (append(&this, mestmp)) {
                if (maybe && linebuf[0] == 'F' && ishead(linebuf)) {
                        msgCount++;
                        if (append(&this, mestmp)) {
-                               perror(tempSet);
+                               perror("temporary file");
                                exit(1);
                        }
                        this.m_flag = MUSED|MNEW;
                                exit(1);
                        }
                        this.m_flag = MUSED|MNEW;
@@ -119,8 +122,8 @@ putline(obuf, linebuf)
        register int c;
 
        c = strlen(linebuf);
        register int c;
 
        c = strlen(linebuf);
-       fwrite(linebuf, sizeof *linebuf, c, obuf);
-       putc('\n', obuf);
+       (void) fwrite(linebuf, sizeof *linebuf, c, obuf);
+       (void) putc('\n', obuf);
        if (ferror(obuf))
                return (-1);
        return (c + 1);
        if (ferror(obuf))
                return (-1);
        return (c + 1);
@@ -131,14 +134,14 @@ putline(obuf, linebuf)
  * buffer.  Return the number of characters read.  Do not
  * include the newline at the end.
  */
  * buffer.  Return the number of characters read.  Do not
  * include the newline at the end.
  */
-readline(ibuf, linebuf)
+readline(ibuf, linebuf, linesize)
        FILE *ibuf;
        char *linebuf;
 {
        register int n;
 
        clearerr(ibuf);
        FILE *ibuf;
        char *linebuf;
 {
        register int n;
 
        clearerr(ibuf);
-       if (fgets(linebuf, LINESIZE, ibuf) == NULL)
+       if (fgets(linebuf, linesize, ibuf) == NULL)
                return -1;
        n = strlen(linebuf);
        if (n > 0 && linebuf[n - 1] == '\n')
                return -1;
        n = strlen(linebuf);
        if (n > 0 && linebuf[n - 1] == '\n')
@@ -180,12 +183,12 @@ makemessage(f)
        dot = message;
        size -= sizeof (struct message);
        fflush(f);
        dot = message;
        size -= sizeof (struct message);
        fflush(f);
-       lseek(fileno(f), (long) sizeof *message, 0);
+       (void) lseek(fileno(f), (long) sizeof *message, 0);
        if (read(fileno(f), (char *) message, size) != size)
                panic("Message temporary file corrupted");
        message[msgCount].m_size = 0;
        message[msgCount].m_lines = 0;
        if (read(fileno(f), (char *) message, size) != size)
                panic("Message temporary file corrupted");
        message[msgCount].m_size = 0;
        message[msgCount].m_lines = 0;
-       fclose(f);
+       Fclose(f);
 }
 
 /*
 }
 
 /*
@@ -202,132 +205,22 @@ append(mp, f)
 /*
  * Delete a file, but only if the file is a plain file.
  */
 /*
  * Delete a file, but only if the file is a plain file.
  */
-remove(name)
-       char name[];
+rm(name)
+       char *name;
 {
 {
-       struct stat statb;
-       extern int errno;
+       struct stat sb;
 
 
-       if (stat(name, &statb) < 0)
+       if (stat(name, &sb) < 0)
                return(-1);
                return(-1);
-       if ((statb.st_mode & S_IFMT) != S_IFREG) {
+       if (!S_ISREG(sb.st_mode)) {
                errno = EISDIR;
                return(-1);
        }
                errno = EISDIR;
                return(-1);
        }
-       return unlink(name);
+       return(unlink(name));
 }
 
 }
 
-/*
- * Terminate an editing session by attempting to write out the user's
- * file from the temporary.  Save any new stuff appended to the file.
- */
-edstop()
-{
-       register int gotcha, c;
-       register struct message *mp;
-       FILE *obuf, *ibuf, *readstat;
-       struct stat statb;
-       char tempname[30], *id;
-       char *mktemp();
-
-       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++;
-               if (Tflag != NOSTR && (mp->m_flag & (MREAD|MDELETED)) != 0) {
-                       if ((id = hfield("article-id", mp)) != NOSTR)
-                               fprintf(readstat, "%s\n", id);
-               }
-       }
-       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);
-       fflush(stdout);
-       if ((obuf = fopen(editfile, "r+")) == NULL) {
-               perror(editfile);
-               relsesigs();
-               reset(0);
-       }
-       trunc(obuf);
-       c = 0;
-       for (mp = &message[0]; mp < &message[msgCount]; mp++) {
-               if ((mp->m_flag & MDELETED) != 0)
-                       continue;
-               c++;
-               if (send(mp, obuf, 0) < 0) {
-                       perror(editfile);
-                       relsesigs();
-                       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);
-               relsesigs();
-               reset(0);
-       }
-       fclose(obuf);
-       if (gotcha) {
-               remove(editfile);
-               printf("removed\n");
-       }
-       else
-               printf("complete\n");
-       fflush(stdout);
-
-done:
-       relsesigs();
-}
-
-static int sigdepth = 0;               /* depth of holdsigs() */
-static int omask = 0;
+static int sigdepth;           /* depth of holdsigs() */
+static int omask;
 /*
  * Hold signals SIGHUP, SIGINT, and SIGQUIT.
  */
 /*
  * Hold signals SIGHUP, SIGINT, and SIGQUIT.
  */
@@ -348,21 +241,6 @@ relsesigs()
                sigsetmask(omask);
 }
 
                sigsetmask(omask);
 }
 
-/*
- * Open a temp file by creating and unlinking.
- * Return the open file descriptor.
- */
-opentemp(file)
-       char file[];
-{
-       int f;
-
-       if ((f = open(file, O_CREAT|O_EXCL|O_RDWR, 0600)) < 0)
-               perror(file);
-       remove(file);
-       return (f);
-}
-
 /*
  * Determine the size of the file possessed by
  * the passed buffer.
 /*
  * Determine the size of the file possessed by
  * the passed buffer.
@@ -379,84 +257,103 @@ fsize(iob)
 }
 
 /*
 }
 
 /*
- * Take a file name, possibly with shell meta characters
- * in it and expand it by using "sh -c echo filename"
+ * Evaluate the string given as a new mailbox name.
+ * Supported meta characters:
+ *     %       for my system mail box
+ *     %user   for user's system mail box
+ *     #       for previous file
+ *     &       invoker's mbox file
+ *     +file   file in folder directory
+ *     any shell meta character
  * Return the file name as a dynamic string.
  */
 char *
 expand(name)
  * Return the file name as a dynamic string.
  */
 char *
 expand(name)
-       char name[];
+       register char *name;
 {
 {
-       char xname[BUFSIZ];
-       char cmdbuf[BUFSIZ];
+       char xname[PATHSIZE];
+       char cmdbuf[PATHSIZE];          /* also used for file names */
        register int pid, l;
        register int pid, l;
-       register char *cp, *Shell;
+       register char *cp, *shell;
        int pivec[2];
        struct stat sbuf;
        int pivec[2];
        struct stat sbuf;
-       union wait s;
-
+       extern union wait wait_status;
+
+       /*
+        * The order of evaluation is "%" and "#" expand into constants.
+        * "&" can expand into "+".  "+" can expand into shell meta characters.
+        * Shell meta characters expand into constants.
+        * This way, we make no recursive expansion.
+        */
+       switch (*name) {
+       case '%':
+               findmail(name[1] ? name + 1 : myname, xname);
+               return savestr(xname);
+       case '#':
+               if (name[1] != 0)
+                       break;
+               if (prevfile[0] == 0) {
+                       printf("No previous file\n");
+                       return NOSTR;
+               }
+               return savestr(prevfile);
+       case '&':
+               if (name[1] == 0 && (name = value("MBOX")) == NOSTR)
+                       name = "~/mbox";
+               /* fall through */
+       }
        if (name[0] == '+' && getfold(cmdbuf) >= 0) {
                sprintf(xname, "%s/%s", cmdbuf, name + 1);
        if (name[0] == '+' && getfold(cmdbuf) >= 0) {
                sprintf(xname, "%s/%s", cmdbuf, name + 1);
-               return(expand(savestr(xname)));
+               name = savestr(xname);
+       }
+       /* catch the most common shell meta character */
+       if (name[0] == '~' && (name[1] == '/' || name[1] == '\0')) {
+               sprintf(xname, "%s%s", homedir, name + 1);
+               name = savestr(xname);
        }
        if (!anyof(name, "~{[*?$`'\"\\"))
        }
        if (!anyof(name, "~{[*?$`'\"\\"))
-               return(name);
+               return name;
        if (pipe(pivec) < 0) {
                perror("pipe");
        if (pipe(pivec) < 0) {
                perror("pipe");
-               return(name);
+               return name;
        }
        sprintf(cmdbuf, "echo %s", name);
        }
        sprintf(cmdbuf, "echo %s", name);
-       if ((pid = vfork()) == 0) {
-               Shell = value("SHELL");
-               if (Shell == NOSTR)
-                       Shell = SHELL;
-               close(pivec[0]);
-               close(1);
-               dup(pivec[1]);
-               close(pivec[1]);
-               close(2);
-               execl(Shell, Shell, "-c", cmdbuf, 0);
-               _exit(1);
-       }
-       if (pid == -1) {
-               perror("fork");
+       if ((shell = value("SHELL")) == NOSTR)
+               shell = _PATH_CSHELL;
+       pid = start_command(shell, 0, -1, pivec[1], "-c", cmdbuf, NOSTR);
+       if (pid < 0) {
                close(pivec[0]);
                close(pivec[1]);
                close(pivec[0]);
                close(pivec[1]);
-               return(NOSTR);
+               return NOSTR;
        }
        close(pivec[1]);
        l = read(pivec[0], xname, BUFSIZ);
        close(pivec[0]);
        }
        close(pivec[1]);
        l = read(pivec[0], xname, BUFSIZ);
        close(pivec[0]);
-       while (wait(&s) != pid);
-               ;
-       if (s.w_status != 0 && s.w_termsig != SIGPIPE) {
-               fprintf(stderr, "\"Echo\" failed\n");
-               goto err;
+       if (wait_child(pid) < 0 && wait_status.w_termsig != SIGPIPE) {
+               fprintf(stderr, "\"%s\": Expansion failed.\n", name);
+               return NOSTR;
        }
        if (l < 0) {
                perror("read");
        }
        if (l < 0) {
                perror("read");
-               goto err;
+               return NOSTR;
        }
        if (l == 0) {
        }
        if (l == 0) {
-               fprintf(stderr, "\"%s\": No match\n", name);
-               goto err;
+               fprintf(stderr, "\"%s\": No match.\n", name);
+               return NOSTR;
        }
        if (l == BUFSIZ) {
        }
        if (l == BUFSIZ) {
-               fprintf(stderr, "Buffer overflow expanding \"%s\"\n", name);
-               goto err;
+               fprintf(stderr, "\"%s\": Expansion buffer overflow.\n", name);
+               return NOSTR;
        }
        xname[l] = 0;
        for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
                ;
        }
        xname[l] = 0;
        for (cp = &xname[l-1]; *cp == '\n' && cp > xname; cp--)
                ;
-       *++cp = '\0';
-       if (any(' ', xname) && stat(xname, &sbuf) < 0) {
-               fprintf(stderr, "\"%s\": Ambiguous\n", name);
-               goto err;
+       cp[1] = '\0';
+       if (index(xname, ' ') && stat(xname, &sbuf) < 0) {
+               fprintf(stderr, "\"%s\": Ambiguous.\n", name);
+               return NOSTR;
        }
        }
-       return(savestr(xname));
-
-err:
-       return(NOSTR);
+       return savestr(xname);
 }
 
 /*
 }
 
 /*
@@ -477,18 +374,20 @@ getfold(name)
 }
 
 /*
 }
 
 /*
- * A nicer version of Fdopen, which allows us to fclose
- * without losing the open file.
+ * Return the name of the dead.letter file.
  */
  */
-FILE *
-Fdopen(fildes, mode)
-       char *mode;
+char *
+getdeadletter()
 {
 {
-       int f;
+       register char *cp;
+
+       if ((cp = value("DEAD")) == NOSTR || (cp = expand(cp)) == NOSTR)
+               cp = expand("~/dead.letter");
+       else if (*cp != '/') {
+               char buf[PATHSIZE];
 
 
-       if ((f = dup(fildes)) < 0) {
-               perror("dup");
-               return (NULL);
+               (void) sprintf(buf, "~/%s", cp);
+               cp = expand(buf);
        }
        }
-       return fdopen(f, mode);
+       return cp;
 }
 }