home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / misc-utils / script.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  5.9 KB  |  295 lines

  1. /*
  2.  * Copyright (c) 1980 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. char copyright[] =
  36. "@(#) Copyright (c) 1980 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)script.c    5.13 (Berkeley) 3/5/91";
  42. #endif /* not lint */
  43.  
  44. /*
  45.  * script
  46.  */
  47. #include <sys/types.h>
  48. #include <sys/stat.h>
  49. #include <termios.h>
  50. #include <sys/ioctl.h>
  51. #include <sys/time.h>
  52. #include <sys/file.h>
  53. #include <sys/signal.h>
  54. #include <stdio.h>
  55. #include <paths.h>
  56.  
  57. #ifdef linux
  58. #include <unistd.h>
  59. #include <string.h>
  60. #endif
  61.  
  62. char    *shell;
  63. FILE    *fscript;
  64. int    master;
  65. int    slave;
  66. int    child;
  67. int    subchild;
  68. char    *fname;
  69.  
  70. struct    termios tt;
  71. struct    winsize win;
  72. int    lb;
  73. int    l;
  74. char    line[] = "/dev/ptyXX";
  75. int    aflg;
  76.  
  77. main(argc, argv)
  78.     int argc;
  79.     char *argv[];
  80. {
  81.     extern char *optarg;
  82.     extern int optind;
  83.     int ch;
  84.     void finish();
  85.     char *getenv();
  86.  
  87.     while ((ch = getopt(argc, argv, "a")) != EOF)
  88.         switch((char)ch) {
  89.         case 'a':
  90.             aflg++;
  91.             break;
  92.         case '?':
  93.         default:
  94.             fprintf(stderr, "usage: script [-a] [file]\n");
  95.             exit(1);
  96.         }
  97.     argc -= optind;
  98.     argv += optind;
  99.  
  100.     if (argc > 0)
  101.         fname = argv[0];
  102.     else
  103.         fname = "typescript";
  104.     if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) {
  105.         perror(fname);
  106.         fail();
  107.     }
  108.  
  109.     shell = getenv("SHELL");
  110.     if (shell == NULL)
  111.         shell = _PATH_BSHELL;
  112.  
  113.     getmaster();
  114.     printf("Script started, file is %s\n", fname);
  115.     fixtty();
  116.  
  117.     (void) signal(SIGCHLD, finish);
  118.     child = fork();
  119.     if (child < 0) {
  120.         perror("fork");
  121.         fail();
  122.     }
  123.     if (child == 0) {
  124.         subchild = child = fork();
  125.         if (child < 0) {
  126.             perror("fork");
  127.             fail();
  128.         }
  129.         if (child)
  130.             dooutput();
  131.         else
  132.             doshell();
  133.     }
  134.     doinput();
  135. }
  136.  
  137. doinput()
  138. {
  139.     register int cc;
  140.     char ibuf[BUFSIZ];
  141.  
  142.     (void) fclose(fscript);
  143.     while ((cc = read(0, ibuf, BUFSIZ)) > 0)
  144.         (void) write(master, ibuf, cc);
  145.     done();
  146. }
  147.  
  148. #include <sys/wait.h>
  149.  
  150. void
  151. finish()
  152. {
  153.     union wait status;
  154.     register int pid;
  155.     register int die = 0;
  156.  
  157.     while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
  158.         if (pid == child)
  159.             die = 1;
  160.  
  161.     if (die)
  162.         done();
  163. }
  164.  
  165. dooutput()
  166. {
  167.     register int cc;
  168.     time_t tvec, time();
  169.     char obuf[BUFSIZ], *ctime();
  170.  
  171.     (void) close(0);
  172.     tvec = time((time_t *)NULL);
  173.     fprintf(fscript, "Script started on %s", ctime(&tvec));
  174.     for (;;) {
  175.         cc = read(master, obuf, sizeof (obuf));
  176.         if (cc <= 0)
  177.             break;
  178.         (void) write(1, obuf, cc);
  179.         (void) fwrite(obuf, 1, cc, fscript);
  180.     }
  181.     done();
  182. }
  183.  
  184. doshell()
  185. {
  186.     int t;
  187.  
  188.     /***
  189.     t = open(_PATH_TTY, O_RDWR);
  190.     if (t >= 0) {
  191.         (void) ioctl(t, TIOCNOTTY, (char *)0);
  192.         (void) close(t);
  193.     }
  194.     ***/
  195.     getslave();
  196.     (void) close(master);
  197.     (void) fclose(fscript);
  198.     (void) dup2(slave, 0);
  199.     (void) dup2(slave, 1);
  200.     (void) dup2(slave, 2);
  201.     (void) close(slave);
  202. #ifdef linux
  203.     execl(shell, strrchr(shell, '/') + 1, "-i", 0);
  204. #else
  205.     execl(shell, "sh", "-i", 0);
  206. #endif
  207.     perror(shell);
  208.     fail();
  209. }
  210.  
  211. fixtty()
  212. {
  213.     struct termios rtt;
  214.  
  215.     rtt = tt;
  216.     cfmakeraw(&rtt);
  217.     rtt.c_lflag &= ~ECHO;
  218.     (void) tcsetattr(0, TCSAFLUSH, &rtt);
  219. }
  220.  
  221. fail()
  222. {
  223.  
  224.     (void) kill(0, SIGTERM);
  225.     done();
  226. }
  227.  
  228. done()
  229. {
  230.     time_t tvec, time();
  231.     char *ctime();
  232.  
  233.     if (subchild) {
  234.         tvec = time((time_t *)NULL);
  235.         fprintf(fscript,"\nScript done on %s", ctime(&tvec));
  236.         (void) fclose(fscript);
  237.         (void) close(master);
  238.     } else {
  239.         (void) tcsetattr(0, TCSAFLUSH, &tt);
  240.         printf("Script done, file is %s\n", fname);
  241.     }
  242.     exit(0);
  243. }
  244.  
  245. getmaster()
  246. {
  247.     char *pty, *bank, *cp;
  248.     struct stat stb;
  249.  
  250.     pty = &line[strlen("/dev/ptyp")];
  251.     for (bank = "pqrs"; *bank; bank++) {
  252.         line[strlen("/dev/pty")] = *bank;
  253.         *pty = '0';
  254.         if (stat(line, &stb) < 0)
  255.             break;
  256.         for (cp = "0123456789abcdef"; *cp; cp++) {
  257.             *pty = *cp;
  258.             master = open(line, O_RDWR);
  259.             if (master >= 0) {
  260.                 char *tp = &line[strlen("/dev/")];
  261.                 int ok;
  262.  
  263.                 /* verify slave side is usable */
  264.                 *tp = 't';
  265.                 ok = access(line, R_OK|W_OK) == 0;
  266.                 *tp = 'p';
  267.                 if (ok) {
  268.                     (void) tcgetattr(0, &tt);
  269.                         (void) ioctl(0, TIOCGWINSZ, 
  270.                         (char *)&win);
  271.                     return;
  272.                 }
  273.                 (void) close(master);
  274.             }
  275.         }
  276.     }
  277.     fprintf(stderr, "Out of pty's\n");
  278.     fail();
  279. }
  280.  
  281. getslave()
  282. {
  283.  
  284.     line[strlen("/dev/")] = 't';
  285.     slave = open(line, O_RDWR);
  286.     if (slave < 0) {
  287.         perror(line);
  288.         fail();
  289.     }
  290.     (void) tcsetattr(slave, TCSAFLUSH, &tt);
  291.     (void) ioctl(slave, TIOCSWINSZ, (char *)&win);
  292.     (void) setsid();
  293.     (void) ioctl(slave, TIOCSCTTY, 0);
  294. }
  295.