1 /* 2 * Copyright (c) 1980, 1992, 1993 3 * The Regents of the University of California. 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 static const char copyright[] = 36 "@(#) Copyright (c) 1980, 1992, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$Id: script.c,v 1.6 1997/12/29 13:31:46 peter Exp $"; 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/wait.h> 50 #include <sys/stat.h> 51 #include <sys/ioctl.h> 52 #include <sys/time.h> 53 54 #include <err.h> 55 #include <fcntl.h> 56 #include <libutil.h> 57 #include <paths.h> 58 #include <signal.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <termios.h> 63 #include <unistd.h> 64 65 FILE *fscript; 66 int master, slave; 67 int child, subchild; 68 int outcc; 69 char *fname; 70 int qflg; 71 72 struct termios tt; 73 74 void done __P((void)) __dead2; 75 void dooutput __P((void)); 76 void doshell __P((char **)); 77 void fail __P((void)); 78 void finish __P((int)); 79 void scriptflush __P((int)); 80 static void usage __P((void)); 81 82 int 83 main(argc, argv) 84 int argc; 85 char *argv[]; 86 { 87 register int cc; 88 struct termios rtt; 89 struct winsize win; 90 int aflg, ch; 91 char ibuf[BUFSIZ]; 92 93 aflg = 0; 94 while ((ch = getopt(argc, argv, "aq")) != -1) 95 switch(ch) { 96 case 'a': 97 aflg = 1; 98 break; 99 case 'q': 100 qflg = 1; 101 break; 102 case '?': 103 default: 104 usage(); 105 } 106 argc -= optind; 107 argv += optind; 108 109 if (argc > 0) { 110 fname = argv[0]; 111 argv++; 112 argc--; 113 } else 114 fname = "typescript"; 115 116 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) 117 err(1, "%s", fname); 118 119 (void)tcgetattr(STDIN_FILENO, &tt); 120 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 121 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 122 err(1, "openpty"); 123 124 if (!qflg) 125 (void)printf("Script started, output file is %s\n", fname); 126 rtt = tt; 127 cfmakeraw(&rtt); 128 rtt.c_lflag &= ~ECHO; 129 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 130 131 (void)signal(SIGCHLD, finish); 132 child = fork(); 133 if (child < 0) { 134 warn("fork"); 135 fail(); 136 } 137 if (child == 0) { 138 subchild = child = fork(); 139 if (child < 0) { 140 warn("fork"); 141 fail(); 142 } 143 if (child) 144 dooutput(); 145 else 146 doshell(argv); 147 } 148 149 (void)fclose(fscript); 150 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) 151 (void)write(master, ibuf, cc); 152 done(); 153 } 154 155 static void 156 usage() 157 { 158 (void)fprintf(stderr, "usage: script [-a] [-q] [file] [command]\n"); 159 exit(1); 160 } 161 162 void 163 finish(signo) 164 int signo; 165 { 166 register int die, pid; 167 union wait status; 168 169 die = 0; 170 while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0) 171 if (pid == child) 172 die = 1; 173 174 if (die) 175 done(); 176 } 177 178 void 179 dooutput() 180 { 181 struct itimerval value; 182 register int cc; 183 time_t tvec; 184 char obuf[BUFSIZ]; 185 186 (void)close(STDIN_FILENO); 187 tvec = time(NULL); 188 if (!qflg) 189 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 190 191 (void)signal(SIGALRM, scriptflush); 192 value.it_interval.tv_sec = 60 / 2; 193 value.it_interval.tv_usec = 0; 194 value.it_value = value.it_interval; 195 (void)setitimer(ITIMER_REAL, &value, NULL); 196 for (;;) { 197 cc = read(master, obuf, sizeof (obuf)); 198 if (cc <= 0) 199 break; 200 (void)write(1, obuf, cc); 201 (void)fwrite(obuf, 1, cc, fscript); 202 outcc += cc; 203 } 204 done(); 205 } 206 207 void 208 scriptflush(signo) 209 int signo; 210 { 211 if (outcc) { 212 (void)fflush(fscript); 213 outcc = 0; 214 } 215 } 216 217 void 218 doshell(av) 219 char **av; 220 { 221 char *shell; 222 223 shell = getenv("SHELL"); 224 if (shell == NULL) 225 shell = _PATH_BSHELL; 226 227 (void)close(master); 228 (void)fclose(fscript); 229 login_tty(slave); 230 if (av[0]) 231 execvp(av[0], av); 232 else 233 execl(shell, "sh", "-i", NULL); 234 warn(shell); 235 fail(); 236 } 237 238 void 239 fail() 240 { 241 242 (void)kill(0, SIGTERM); 243 done(); 244 } 245 246 void 247 done() 248 { 249 time_t tvec; 250 251 if (subchild) { 252 tvec = time(NULL); 253 if (!qflg) 254 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 255 (void)fclose(fscript); 256 (void)close(master); 257 } else { 258 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 259 if (!qflg) 260 (void)printf("Script done, output file is %s\n", fname); 261 } 262 exit(0); 263 } 264