install with -s
[unix-history] / usr / src / usr.bin / mail / lex.c
index 3741d25..904f68f 100644 (file)
@@ -1,6 +1,9 @@
-#
+#ifndef lint
+static char *sccsid = "@(#)lex.c       2.17 (Berkeley) %G%";
+#endif
 
 #include "rcv.h"
 
 #include "rcv.h"
+#include <sys/stat.h>
 
 /*
  * Mail -- a mail program
 
 /*
  * Mail -- a mail program
@@ -8,8 +11,6 @@
  * Lexical processing of commands.
  */
 
  * Lexical processing of commands.
  */
 
-static char *SccsId = "@(#)lex.c       2.12 %G%";
-
 char   *prompt = "& ";
 
 /*
 char   *prompt = "& ";
 
 /*
@@ -24,12 +25,21 @@ setfile(name, isedit)
 {
        FILE *ibuf;
        int i;
 {
        FILE *ibuf;
        int i;
+       struct stat stb;
        static int shudclob;
        static char efile[128];
        extern char tempMesg[];
 
        if ((ibuf = fopen(name, "r")) == NULL)
                return(-1);
        static int shudclob;
        static char efile[128];
        extern char tempMesg[];
 
        if ((ibuf = fopen(name, "r")) == NULL)
                return(-1);
+       if (!edit) {
+               if (fstat(fileno(ibuf), &stb) < 0) {
+                       perror(name);
+                       exit(1);
+               }
+               if (stb.st_size == 0)
+                       return(-1);
+       }
 
        /*
         * Looks like all will be well.  We must now relinquish our
 
        /*
         * Looks like all will be well.  We must now relinquish our
@@ -121,12 +131,13 @@ commands()
                eofloop = 0;
 top:
                if (shudprompt) {
                eofloop = 0;
 top:
                if (shudprompt) {
+                       printf(prompt);
+                       fflush(stdout);
 # ifdef VMUNIX
                        sigset(SIGCONT, contin);
 # endif VMUNIX
 # ifdef VMUNIX
                        sigset(SIGCONT, contin);
 # endif VMUNIX
-                       printf(prompt);
-               }
-               flush();
+               } else
+                       fflush(stdout);
                sreset();
 
                /*
                sreset();
 
                /*
@@ -488,6 +499,34 @@ isprefix(as1, as2)
 
 int    inithdr;                        /* am printing startup headers */
 
 
 int    inithdr;                        /* am printing startup headers */
 
+#ifdef _NFILE
+static
+_fwalk(function)
+       register int (*function)();
+{
+       register FILE *iop;
+
+       for (iop = _iob; iop < _iob + _NFILE; iop++)
+               (*function)(iop);
+}
+#endif
+
+static
+xclose(iop)
+       register FILE *iop;
+{
+       if (iop == stdin || iop == stdout ||
+           iop == stderr || iop == itf || iop == otf)
+               return;
+
+       if (iop != pipef)
+               fclose(iop);
+       else {
+               pclose(pipef);
+               pipef = NULL;
+       }
+}
+
 stop(s)
 {
        register FILE *fp;
 stop(s)
 {
        register FILE *fp;
@@ -502,26 +541,17 @@ stop(s)
        while (sourcing)
                unstack();
        getuserid((char *) -1);
        while (sourcing)
                unstack();
        getuserid((char *) -1);
-       for (fp = &_iob[0]; fp < &_iob[_NFILE]; fp++) {
-               if (fp == stdin || fp == stdout)
-                       continue;
-               if (fp == itf || fp == otf)
-                       continue;
-               if (fp == stderr)
-                       continue;
-               if (fp == pipef) {
-                       pclose(pipef);
-                       pipef = NULL;
-                       continue;
-               }
-               fclose(fp);
-       }
+
+       /*
+        * Walk through all the open FILEs, applying xclose() to them
+        */
+       _fwalk(xclose);
+
        if (image >= 0) {
                close(image);
                image = -1;
        }
        if (image >= 0) {
                close(image);
                image = -1;
        }
-       clrbuf(stdout);
-       printf("Interrupt\n");
+       fprintf(stderr, "Interrupt\n");
 # ifndef VMUNIX
        signal(s, stop);
 # endif
 # ifndef VMUNIX
        signal(s, stop);
 # endif