exit on end-of-file if file shorter than a screen, regardless
[unix-history] / usr / src / usr.bin / script / script.c
index a6ebf46..2ab9673 100644 (file)
@@ -1,28 +1,48 @@
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms are permitted
+ * provided that the above copyright notice and this paragraph are
+ * duplicated in all such forms and that any documentation,
+ * advertising materials, and other materials related to such
+ * distribution and use acknowledge that the software was developed
+ * by the University of California, Berkeley.  The name of the
+ * University may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+ * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)script.c    4.7 (Berkeley) 85/02/21";
-#endif
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif /* not lint */
+
+#ifndef lint
+static char sccsid[] = "@(#)script.c   5.6 (Berkeley) %G%";
+#endif /* not lint */
 
 /*
  * script
  */
 
 /*
  * script
  */
-#include <stdio.h>
-#include <signal.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
-#include <sgtty.h>
 #include <sys/time.h>
 #include <sys/file.h>
 #include <sys/time.h>
 #include <sys/file.h>
+#include <stdio.h>
+#include <signal.h>
 
 
-char   *getenv();
-char   *ctime();
 char   *shell;
 FILE   *fscript;
 int    master;
 int    slave;
 int    child;
 char   *shell;
 FILE   *fscript;
 int    master;
 int    slave;
 int    child;
-char   *fname = "typescript";
-int    finish();
+int    subchild;
+char   *fname;
 
 struct sgttyb b;
 struct tchars tc;
 
 struct sgttyb b;
 struct tchars tc;
@@ -37,32 +57,38 @@ main(argc, argv)
        int argc;
        char *argv[];
 {
        int argc;
        char *argv[];
 {
-       int f;
-
-       shell = getenv("SHELL");
-       if (shell == 0)
-               shell = "/bin/sh";
-       argc--, argv++;
-       while (argc > 0 && argv[0][0] == '-') {
-               switch (argv[0][1]) {
+       extern char *optarg;
+       extern int optind;
+       int ch;
+       int finish();
+       char *getenv();
 
 
+       while ((ch = getopt(argc, argv, "a")) != EOF)
+               switch((char)ch) {
                case 'a':
                        aflg++;
                        break;
                case 'a':
                        aflg++;
                        break;
-
+               case '?':
                default:
                default:
-                       fprintf(stderr,
-                           "usage: script [ -a ] [ typescript ]\n");
+                       fprintf(stderr, "usage: script [-a] [file]\n");
                        exit(1);
                }
                        exit(1);
                }
-               argc--, argv++;
-       }
+       argc -= optind;
+       argv += optind;
+
        if (argc > 0)
                fname = argv[0];
        if (argc > 0)
                fname = argv[0];
+       else
+               fname = "typescript";
        if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
                perror(fname);
                fail();
        }
        if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
                perror(fname);
                fail();
        }
+
+       shell = getenv("SHELL");
+       if (shell == NULL)
+               shell = "/bin/sh";
+
        getmaster();
        printf("Script started, file is %s\n", fname);
        fixtty();
        getmaster();
        printf("Script started, file is %s\n", fname);
        fixtty();
@@ -74,12 +100,12 @@ main(argc, argv)
                fail();
        }
        if (child == 0) {
                fail();
        }
        if (child == 0) {
-               f = fork();
-               if (f < 0) {
+               subchild = child = fork();
+               if (child < 0) {
                        perror("fork");
                        fail();
                }
                        perror("fork");
                        fail();
                }
-               if (f)
+               if (child)
                        dooutput();
                else
                        doshell();
                        dooutput();
                else
                        doshell();
@@ -89,8 +115,8 @@ main(argc, argv)
 
 doinput()
 {
 
 doinput()
 {
+       register int cc;
        char ibuf[BUFSIZ];
        char ibuf[BUFSIZ];
-       int cc;
 
        (void) fclose(fscript);
        while ((cc = read(0, ibuf, BUFSIZ)) > 0)
 
        (void) fclose(fscript);
        while ((cc = read(0, ibuf, BUFSIZ)) > 0)
@@ -103,20 +129,25 @@ doinput()
 finish()
 {
        union wait status;
 finish()
 {
        union wait status;
+       register int pid;
+       register int die = 0;
 
 
-       if (wait3(&status, WNOHANG, 0) != child)
-               return;
-       done();
+       while ((pid = wait3(&status, WNOHANG, 0)) > 0)
+               if (pid == child)
+                       die = 1;
+
+       if (die)
+               done();
 }
 
 dooutput()
 {
 }
 
 dooutput()
 {
-       time_t tvec;
-       char obuf[BUFSIZ];
-       int cc;
+       register int cc;
+       time_t tvec, time();
+       char obuf[BUFSIZ], *ctime();
 
        (void) close(0);
 
        (void) close(0);
-       tvec = time((time_t *)0);
+       tvec = time((time_t *)NULL);
        fprintf(fscript, "Script started on %s", ctime(&tvec));
        for (;;) {
                cc = read(master, obuf, sizeof (obuf));
        fprintf(fscript, "Script started on %s", ctime(&tvec));
        for (;;) {
                cc = read(master, obuf, sizeof (obuf));
@@ -125,11 +156,7 @@ dooutput()
                (void) write(1, obuf, cc);
                (void) fwrite(obuf, 1, cc, fscript);
        }
                (void) write(1, obuf, cc);
                (void) fwrite(obuf, 1, cc, fscript);
        }
-       tvec = time((time_t *)0);
-       fprintf(fscript,"\nscript done on %s", ctime(&tvec));
-       (void) fclose(fscript);
-       (void) close(master);
-       exit(0);
+       done();
 }
 
 doshell()
 }
 
 doshell()
@@ -138,15 +165,15 @@ doshell()
 
        t = open("/dev/tty", O_RDWR);
        if (t >= 0) {
 
        t = open("/dev/tty", O_RDWR);
        if (t >= 0) {
-               ioctl(t, TIOCNOTTY, (char *)0);
+               (void) ioctl(t, TIOCNOTTY, (char *)0);
                (void) close(t);
        }
        getslave();
        (void) close(master);
        (void) fclose(fscript);
                (void) close(t);
        }
        getslave();
        (void) close(master);
        (void) fclose(fscript);
-       dup2(slave, 0);
-       dup2(slave, 1);
-       dup2(slave, 2);
+       (void) dup2(slave, 0);
+       (void) dup2(slave, 1);
+       (void) dup2(slave, 2);
        (void) close(slave);
        execl(shell, "sh", "-i", 0);
        perror(shell);
        (void) close(slave);
        execl(shell, "sh", "-i", 0);
        perror(shell);
@@ -160,7 +187,7 @@ fixtty()
        sbuf = b;
        sbuf.sg_flags |= RAW;
        sbuf.sg_flags &= ~ECHO;
        sbuf = b;
        sbuf.sg_flags |= RAW;
        sbuf.sg_flags &= ~ECHO;
-       ioctl(0, TIOCSETP, (char *)&sbuf);
+       (void) ioctl(0, TIOCSETP, (char *)&sbuf);
 }
 
 fail()
 }
 
 fail()
@@ -172,9 +199,18 @@ fail()
 
 done()
 {
 
 done()
 {
+       time_t tvec, time();
+       char *ctime();
 
 
-       ioctl(0, TIOCSETP, (char *)&b);
-       printf("Script done, file is %s\n", fname);
+       if (subchild) {
+               tvec = time((time_t *)NULL);
+               fprintf(fscript,"\nscript done on %s", ctime(&tvec));
+               (void) fclose(fscript);
+               (void) close(master);
+       } else {
+               (void) ioctl(0, TIOCSETP, (char *)&b);
+               printf("Script done, file is %s\n", fname);
+       }
        exit(0);
 }
 
        exit(0);
 }
 
@@ -182,7 +218,6 @@ getmaster()
 {
        char *pty, *bank, *cp;
        struct stat stb;
 {
        char *pty, *bank, *cp;
        struct stat stb;
-       int i;
 
        pty = &line[strlen("/dev/ptyp")];
        for (bank = "pqrs"; *bank; bank++) {
 
        pty = &line[strlen("/dev/ptyp")];
        for (bank = "pqrs"; *bank; bank++) {
@@ -202,15 +237,15 @@ getmaster()
                                ok = access(line, R_OK|W_OK) == 0;
                                *tp = 'p';
                                if (ok) {
                                ok = access(line, R_OK|W_OK) == 0;
                                *tp = 'p';
                                if (ok) {
-                                       ioctl(0, TIOCGETP, (char *)&b);
-                                       ioctl(0, TIOCGETC, (char *)&tc);
-                                       ioctl(0, TIOCGETD, (char *)&l);
-                                       ioctl(0, TIOCGLTC, (char *)&lc);
-                                       ioctl(0, TIOCLGET, (char *)&lb);
-                                       ioctl(0, TIOCGWINSZ, (char *)&win);
+                                   (void) ioctl(0, TIOCGETP, (char *)&b);
+                                   (void) ioctl(0, TIOCGETC, (char *)&tc);
+                                   (void) ioctl(0, TIOCGETD, (char *)&l);
+                                   (void) ioctl(0, TIOCGLTC, (char *)&lc);
+                                   (void) ioctl(0, TIOCLGET, (char *)&lb);
+                                   (void) ioctl(0, TIOCGWINSZ, (char *)&win);
                                        return;
                                }
                                        return;
                                }
-                               close(master);
+                               (void) close(master);
                        }
                }
        }
                        }
                }
        }
@@ -227,10 +262,10 @@ getslave()
                perror(line);
                fail();
        }
                perror(line);
                fail();
        }
-       ioctl(slave, TIOCSETP, (char *)&b);
-       ioctl(slave, TIOCSETC, (char *)&tc);
-       ioctl(slave, TIOCSLTC, (char *)&lc);
-       ioctl(slave, TIOCLSET, (char *)&lb);
-       ioctl(slave, TIOCSETD, (char *)&l);
-       ioctl(slave, TIOCSWINSZ, (char *)&win);
+       (void) ioctl(slave, TIOCSETP, (char *)&b);
+       (void) ioctl(slave, TIOCSETC, (char *)&tc);
+       (void) ioctl(slave, TIOCSLTC, (char *)&lc);
+       (void) ioctl(slave, TIOCLSET, (char *)&lb);
+       (void) ioctl(slave, TIOCSETD, (char *)&l);
+       (void) ioctl(slave, TIOCSWINSZ, (char *)&win);
 }
 }