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$"; 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 71 struct termios tt; 72 73 void done __P((void)) __dead2; 74 void dooutput __P((void)); 75 void doshell __P((void)); 76 void fail __P((void)); 77 void finish __P((int)); 78 void scriptflush __P((int)); 79 static void usage __P((void)); 80 81 int 82 main(argc, argv) 83 int argc; 84 char *argv[]; 85 { 86 register int cc; 87 struct termios rtt; 88 struct winsize win; 89 int aflg, ch; 90 char ibuf[BUFSIZ]; 91 92 aflg = 0; 93 while ((ch = getopt(argc, argv, "a")) != -1) 94 switch(ch) { 95 case 'a': 96 aflg = 1; 97 break; 98 case '?': 99 default: 100 usage(); 101 } 102 argc -= optind; 103 argv += optind; 104 105 if (argc > 0) 106 fname = argv[0]; 107 else 108 fname = "typescript"; 109 110 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) 111 err(1, "%s", fname); 112 113 (void)tcgetattr(STDIN_FILENO, &tt); 114 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 115 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 116 err(1, "openpty"); 117 118 (void)printf("Script started, output file is %s\n", fname); 119 rtt = tt; 120 cfmakeraw(&rtt); 121 rtt.c_lflag &= ~ECHO; 122 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 123 124 (void)signal(SIGCHLD, finish); 125 child = fork(); 126 if (child < 0) { 127 warn("fork"); 128 fail(); 129 } 130 if (child == 0) { 131 subchild = child = fork(); 132 if (child < 0) { 133 warn("fork"); 134 fail(); 135 } 136 if (child) 137 dooutput(); 138 else 139 doshell(); 140 } 141 142 (void)fclose(fscript); 143 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) 144 (void)write(master, ibuf, cc); 145 done(); 146 } 147 148 static void 149 usage() 150 { 151 (void)fprintf(stderr, "usage: script [-a] [file]\n"); 152 exit(1); 153 } 154 155 void 156 finish(signo) 157 int signo; 158 { 159 register int die, pid; 160 union wait status; 161 162 die = 0; 163 while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0) 164 if (pid == child) 165 die = 1; 166 167 if (die) 168 done(); 169 } 170 171 void 172 dooutput() 173 { 174 struct itimerval value; 175 register int cc; 176 time_t tvec; 177 char obuf[BUFSIZ]; 178 179 (void)close(STDIN_FILENO); 180 tvec = time(NULL); 181 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 182 183 (void)signal(SIGALRM, scriptflush); 184 value.it_interval.tv_sec = 60 / 2; 185 value.it_interval.tv_usec = 0; 186 value.it_value = value.it_interval; 187 (void)setitimer(ITIMER_REAL, &value, NULL); 188 for (;;) { 189 cc = read(master, obuf, sizeof (obuf)); 190 if (cc <= 0) 191 break; 192 (void)write(1, obuf, cc); 193 (void)fwrite(obuf, 1, cc, fscript); 194 outcc += cc; 195 } 196 done(); 197 } 198 199 void 200 scriptflush(signo) 201 int signo; 202 { 203 if (outcc) { 204 (void)fflush(fscript); 205 outcc = 0; 206 } 207 } 208 209 void 210 doshell() 211 { 212 char *shell; 213 214 shell = getenv("SHELL"); 215 if (shell == NULL) 216 shell = _PATH_BSHELL; 217 218 (void)close(master); 219 (void)fclose(fscript); 220 login_tty(slave); 221 execl(shell, "sh", "-i", NULL); 222 warn(shell); 223 fail(); 224 } 225 226 void 227 fail() 228 { 229 230 (void)kill(0, SIGTERM); 231 done(); 232 } 233 234 void 235 done() 236 { 237 time_t tvec; 238 239 if (subchild) { 240 tvec = time(NULL); 241 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 242 (void)fclose(fscript); 243 (void)close(master); 244 } else { 245 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 246 (void)printf("Script done, output file is %s\n", fname); 247 } 248 exit(0); 249 } 250