use pty klibrary
[unix-history] / usr / src / usr.bin / script / script.c
index 96c66f7..5bfd0ce 100644 (file)
@@ -2,17 +2,7 @@
  * Copyright (c) 1980 Regents of the University of California.
  * All rights reserved.
  *
  * 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.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -22,7 +12,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)script.c   5.7 (Berkeley) %G%";
+static char sccsid[] = "@(#)script.c   5.14 (Berkeley) %G%";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -34,8 +24,9 @@ static char sccsid[] = "@(#)script.c  5.7 (Berkeley) %G%";
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <sys/file.h>
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <sys/file.h>
+#include <sys/signal.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <signal.h>
+#include <paths.h>
 
 char   *shell;
 FILE   *fscript;
 
 char   *shell;
 FILE   *fscript;
@@ -49,7 +40,7 @@ struct        termios tt;
 struct winsize win;
 int    lb;
 int    l;
 struct winsize win;
 int    lb;
 int    l;
-char   *line = "/dev/ptyXX";
+char   line[] = "/dev/ptyXX";
 int    aflg;
 
 main(argc, argv)
 int    aflg;
 
 main(argc, argv)
@@ -59,7 +50,7 @@ main(argc, argv)
        extern char *optarg;
        extern int optind;
        int ch;
        extern char *optarg;
        extern int optind;
        int ch;
-       int finish();
+       void finish();
        char *getenv();
 
        while ((ch = getopt(argc, argv, "a")) != EOF)
        char *getenv();
 
        while ((ch = getopt(argc, argv, "a")) != EOF)
@@ -86,11 +77,17 @@ main(argc, argv)
 
        shell = getenv("SHELL");
        if (shell == NULL)
 
        shell = getenv("SHELL");
        if (shell == NULL)
-               shell = "/bin/sh";
+               shell = _PATH_BSHELL;
+
+       (void) tcgetattr(0, &tt);
+       (void) ioctl(0, TIOCGWINSZ, (char *)&win);
+       if (openpty(&master, &slave, NULL, &tt, &win) == -1) {
+               perror("openpty");
+               exit(1);
+       }
 
 
-       getmaster();
        printf("Script started, file is %s\n", fname);
        printf("Script started, file is %s\n", fname);
-       fixtty();
+       fixtty();       /* go raw */
 
        (void) signal(SIGCHLD, finish);
        child = fork();
 
        (void) signal(SIGCHLD, finish);
        child = fork();
@@ -125,13 +122,14 @@ doinput()
 
 #include <sys/wait.h>
 
 
 #include <sys/wait.h>
 
+void
 finish()
 {
        union wait status;
        register int pid;
        register int die = 0;
 
 finish()
 {
        union wait status;
        register int pid;
        register int die = 0;
 
-       while ((pid = wait3(&status, WNOHANG, 0)) > 0)
+       while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
                if (pid == child)
                        die = 1;
 
                if (pid == child)
                        die = 1;
 
@@ -160,22 +158,10 @@ dooutput()
 
 doshell()
 {
 
 doshell()
 {
-       int t;
 
 
-       /***
-       t = open("/dev/tty", O_RDWR);
-       if (t >= 0) {
-               (void) ioctl(t, TIOCNOTTY, (char *)0);
-               (void) close(t);
-       }
-       ***/
-       getslave();
-       (void) close(master);
+       close(master);
        (void) fclose(fscript);
        (void) fclose(fscript);
-       (void) dup2(slave, 0);
-       (void) dup2(slave, 1);
-       (void) dup2(slave, 2);
-       (void) close(slave);
+       login_tty(slave);
        execl(shell, "sh", "-i", 0);
        perror(shell);
        fail();
        execl(shell, "sh", "-i", 0);
        perror(shell);
        fail();
@@ -188,7 +174,7 @@ fixtty()
        rtt = tt;
        cfmakeraw(&rtt);
        rtt.c_lflag &= ~ECHO;
        rtt = tt;
        cfmakeraw(&rtt);
        rtt.c_lflag &= ~ECHO;
-       (void) tcsetattr(0, TCSADFLUSH, &rtt);
+       (void) tcsetattr(0, TCSAFLUSH, &rtt);
 }
 
 fail()
 }
 
 fail()
@@ -205,63 +191,12 @@ done()
 
        if (subchild) {
                tvec = time((time_t *)NULL);
 
        if (subchild) {
                tvec = time((time_t *)NULL);
-               fprintf(fscript,"\nscript done on %s", ctime(&tvec));
+               fprintf(fscript,"\nScript done on %s", ctime(&tvec));
                (void) fclose(fscript);
                (void) close(master);
        } else {
                (void) fclose(fscript);
                (void) close(master);
        } else {
-               (void) tcsetattr(0, TCSADFLUSH, &tt);
+               (void) tcsetattr(0, TCSAFLUSH, &tt);
                printf("Script done, file is %s\n", fname);
        }
        exit(0);
 }
                printf("Script done, file is %s\n", fname);
        }
        exit(0);
 }
-
-getmaster()
-{
-       char *pty, *bank, *cp;
-       struct stat stb;
-
-       pty = &line[strlen("/dev/ptyp")];
-       for (bank = "pqrs"; *bank; bank++) {
-               line[strlen("/dev/pty")] = *bank;
-               *pty = '0';
-               if (stat(line, &stb) < 0)
-                       break;
-               for (cp = "0123456789abcdef"; *cp; cp++) {
-                       *pty = *cp;
-                       master = open(line, O_RDWR);
-                       if (master >= 0) {
-                               char *tp = &line[strlen("/dev/")];
-                               int ok;
-
-                               /* verify slave side is usable */
-                               *tp = 't';
-                               ok = access(line, R_OK|W_OK) == 0;
-                               *tp = 'p';
-                               if (ok) {
-                                       (void) tcgetattr(0, &tt);
-                                       (void) ioctl(0, TIOCGWINSZ, 
-                                               (char *)&win);
-                                       return;
-                               }
-                               (void) close(master);
-                       }
-               }
-       }
-       fprintf(stderr, "Out of pty's\n");
-       fail();
-}
-
-getslave()
-{
-
-       line[strlen("/dev/")] = 't';
-       slave = open(line, O_RDWR);
-       if (slave < 0) {
-               perror(line);
-               fail();
-       }
-       (void) tcsetattr(slave, TCSADFLUSH, &tt);
-       (void) ioctl(slave, TIOCSWINSZ, (char *)&win);
-       (void) setsid();
-       (void) ioctl(slave, TIOCSCTTY, 0);
-}