1 /* $NetBSD: script.c,v 1.6 1998/04/02 11:08:34 kleink Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1992, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 45 #endif 46 __RCSID("$NetBSD: script.c,v 1.6 1998/04/02 11:08:34 kleink Exp $"); 47 #endif /* not lint */ 48 49 #include <sys/types.h> 50 #include <sys/wait.h> 51 #include <sys/stat.h> 52 #include <sys/ioctl.h> 53 #include <sys/time.h> 54 55 #include <err.h> 56 #include <errno.h> 57 #include <fcntl.h> 58 #include <paths.h> 59 #include <signal.h> 60 #include <stdio.h> 61 #include <stdlib.h> 62 #include <string.h> 63 #include <termios.h> 64 #include <time.h> 65 #include <tzfile.h> 66 #include <unistd.h> 67 #include <util.h> 68 69 FILE *fscript; 70 int master, slave; 71 int child, subchild; 72 int outcc; 73 char *fname; 74 75 struct termios tt; 76 77 void done __P((void)); 78 void dooutput __P((void)); 79 void doshell __P((void)); 80 void fail __P((void)); 81 void finish __P((int)); 82 int main __P((int, char **)); 83 void scriptflush __P((int)); 84 85 int 86 main(argc, argv) 87 int argc; 88 char *argv[]; 89 { 90 int cc; 91 struct termios rtt; 92 struct winsize win; 93 int aflg, ch; 94 char ibuf[BUFSIZ]; 95 96 aflg = 0; 97 while ((ch = getopt(argc, argv, "a")) != -1) 98 switch(ch) { 99 case 'a': 100 aflg = 1; 101 break; 102 case '?': 103 default: 104 (void)fprintf(stderr, "usage: script [-a] [file]\n"); 105 exit(1); 106 } 107 argc -= optind; 108 argv += optind; 109 110 if (argc > 0) 111 fname = argv[0]; 112 else 113 fname = "typescript"; 114 115 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) 116 err(1, "fopen %s", fname); 117 118 (void)tcgetattr(STDIN_FILENO, &tt); 119 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 120 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 121 err(1, "openpty"); 122 123 (void)printf("Script started, output file is %s\n", fname); 124 rtt = tt; 125 cfmakeraw(&rtt); 126 rtt.c_lflag &= ~ECHO; 127 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 128 129 (void)signal(SIGCHLD, finish); 130 child = fork(); 131 if (child < 0) { 132 warn("fork"); 133 fail(); 134 } 135 if (child == 0) { 136 subchild = child = fork(); 137 if (child < 0) { 138 warn("fork"); 139 fail(); 140 } 141 if (child) 142 dooutput(); 143 else 144 doshell(); 145 } 146 147 (void)fclose(fscript); 148 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) 149 (void)write(master, ibuf, cc); 150 done(); 151 /* NOTREACHED */ 152 return (0); 153 } 154 155 void 156 finish(signo) 157 int signo; 158 { 159 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 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 = SECSPERMIN / 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, shell, "-i", NULL); 222 warn("execl %s", 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