BSD 4 release
[unix-history] / usr / src / cmd / script.c
index 21f5aa4..ed2a3f0 100644 (file)
@@ -1,9 +1,9 @@
+static char *sccsid = "@(#)script.c    4.1 (Berkeley) 10/1/80";
  /*
  /*
-  *
   * script - makes copy of terminal conversation. usage:
   * script - makes copy of terminal conversation. usage:
+  *
   * script [ -n ] [ -s ] [ -q ] [ -a ] [ -S shell ] [ file ]
   * conversation saved in file. default is DFNAME
   * script [ -n ] [ -s ] [ -q ] [ -a ] [ -S shell ] [ file ]
   * conversation saved in file. default is DFNAME
-  *
   */
 
 #define DFNAME "typescript"
   */
 
 #define DFNAME "typescript"
@@ -37,6 +37,7 @@ char *shell = NEWSHELL;
 #endif
 
 #ifdef V7ENV
 #endif
 
 #ifdef V7ENV
+#include <signal.h>
 /* used for version 7 with environments - gets your environment shell */
 #define STDSHELL "/bin/sh"
 #define NEWSHELL "/bin/csh"
 /* used for version 7 with environments - gets your environment shell */
 #define STDSHELL "/bin/sh"
 #define NEWSHELL "/bin/csh"
@@ -165,33 +166,48 @@ main(argc,argv) int argc; char **argv; {
        pipe(outpipe);
 
        forkval = fork();
        pipe(outpipe);
 
        forkval = fork();
-       if (forkval < 0) {
-               printf("Fork failed - try again.\n");
-               fail();
+       if (forkval < 0)
+               goto ffail;
+       if (forkval == 0) {
+               forkval = fork();
+               if (forkval < 0)
+                       goto ffail;
+               if (forkval == 0)
+                       dooutput();
+               forkval = fork();
+               if (forkval < 0)
+                       goto ffail;
+               if (forkval == 0)
+                       doinput();
+               doshell();
        }
        }
-       if (forkval == 0) doshell();
+       close(inpipe[0]); close(inpipe[1]);
+       close(outpipe[0]); close(outpipe[1]);
+       signal(SIGINT, SIG_IGN);
+       signal(SIGQUIT, done);
+       wait(&status);
+       done();
+       /*NOTREACHED*/
 
 
-       forkval = fork();
-       if (forkval < 0) {
-               printf("Fork failed. Try again.\n");
-               fail();
-       }
-       if (forkval == 0) dooutput();
-               else      doinput();
+ffail:
+       printf("Fork failed. Try again.\n");
+       fail();
 }
 
 }
 
-doinput() {
-       int done();
-       /* input process - copy tty to pipe and file */
-       signal(2,1);    /* ignore interrupts from delete */
-       signal(3,done); /* fix files when users quits. */
+/* input process - copy tty to pipe and file */
+doinput()
+{
+
+       signal(SIGINT, SIG_IGN);
+       signal(SIGQUIT, SIG_IGN);
+       signal(SIGTSTP, SIG_IGN);
 
        close(inpipe[0]);
        close(outpipe[0]);
        close(outpipe[1]);
 
        /* main input loop - copy until end of file (ctrl D) */
 
        close(inpipe[0]);
        close(outpipe[0]);
        close(outpipe[1]);
 
        /* main input loop - copy until end of file (ctrl D) */
-       while (n=read(0,buffer,256)) {
+       while ((n=read(0,buffer,256)) > 0) {
                check(write(fd,buffer,n));
                write(inpipe[1],buffer,n);
        }
                check(write(fd,buffer,n));
                write(inpipe[1],buffer,n);
        }
@@ -199,16 +215,16 @@ doinput() {
        /* end of script - close files and exit */
        close(inpipe[1]);
        close(fd);
        /* end of script - close files and exit */
        close(inpipe[1]);
        close(fd);
-       wait(&status);  /* wait for shell to terminate */
-       wait(&status);  /* wait for output to terminate */
        done();
 }
 
        done();
 }
 
-dooutput() {
-       /* do output process - copy to tty & file */
-       signal(2,flsh); /* trap to flsh on interrupts */
-       signal(3,1);    /* ignore quits */
+/* do output process - copy to tty & file */
+dooutput()
+{
 
 
+       signal(SIGINT, flsh);
+       signal(SIGQUIT, SIG_IGN);
+       signal(SIGTSTP, SIG_IGN);
        close(0);
        close(inpipe[0]);
        close(inpipe[1]);
        close(0);
        close(inpipe[0]);
        close(inpipe[1]);
@@ -230,11 +246,13 @@ dooutput() {
                check(write(fd,ctime(&tvec),25));
        }
        close(fd);
                check(write(fd,ctime(&tvec),25));
        }
        close(fd);
-       exit();
+       exit(0);
 }
 
 }
 
-doshell() {
-       /* exec shell, after divirting std input & output */
+/* exec shell, after diverting std input & output */
+doshell()
+{
+
        close(0);
        dup(inpipe[0]);
        close(1);
        close(0);
        dup(inpipe[0]);
        close(1);
@@ -247,10 +265,9 @@ doshell() {
        close(inpipe[1]);
        close(outpipe[0]);
        close(outpipe[1]);
        close(inpipe[1]);
        close(outpipe[0]);
        close(outpipe[1]);
-/*     signal(2,1);    /* shell should ignore interrupts */
-       execl(shell,"sh","-i",0);
-       execl(STDSHELL,"sh","-i",0);
-       execl(NEWSHELL,"sh","-i",0);
+       execl(shell, "sh", "-i", 0);
+       execl(STDSHELL, "sh", "-i", 0);
+       execl(NEWSHELL, "sh", "-i", 0);
        printf("Can't execute shell\n");
        fail();
 }
        printf("Can't execute shell\n");
        fail();
 }
@@ -263,21 +280,27 @@ fixtty()
        chmod(tty, 0600);
 }
 
        chmod(tty, 0600);
 }
 
-flsh() {
-       /* come here on rubout to flush output - this doesn't work */
-       signal(2,flsh);
+/* come here on rubout to flush output - this doesn't work */
+flsh()
+{
+
+       signal(SIGINT, flsh);
        /* lseek(outpipe[0],0l,2);      /* seeks on pipes don't work !"$"$!! */
 }
 
        /* lseek(outpipe[0],0l,2);      /* seeks on pipes don't work !"$"$!! */
 }
 
-fail() {
+fail()
+{
+
        unlink(fname);
        unlink(fname);
-       kill(0,15);     /* shut off other script processes */
+       kill(0, 15);    /* shut off other script processes */
        done();
 }
 
        done();
 }
 
-done() {
-       chmod(tty,mode);
-       chmod(fname,0664);
+done()
+{
+
+       chmod(tty, mode);
+       chmod(fname, 0664);
        exit();
 }
 
        exit();
 }
 
@@ -293,7 +316,9 @@ char *ttyname(i) int i; {
 #endif
 #endif
 
 #endif
 #endif
 
-check(n) int n; {
+check(n)
+int n;
+{
        /* checks the result of a write call, if neg
           assume ran out of disk space & die */
        if (n < 0) {
        /* checks the result of a write call, if neg
           assume ran out of disk space & die */
        if (n < 0) {