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 * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California. All rights reserved. 34 * @(#)script.c 8.1 (Berkeley) 6/6/93 35 * $FreeBSD: /usr/local/www/cvsroot/FreeBSD/src/usr.bin/script/script.c,v 1.11.2.2 2004/03/13 09:21:00 cperciva Exp $ 36 * $DragonFly: src/usr.bin/script/script.c,v 1.6 2005/01/03 17:35:57 liamfoy Exp $ 37 */ 38 39 #include <sys/types.h> 40 #include <sys/wait.h> 41 #include <sys/stat.h> 42 #include <sys/ioctl.h> 43 #include <sys/time.h> 44 45 #include <err.h> 46 #include <errno.h> 47 #include <fcntl.h> 48 #include <libutil.h> 49 #include <paths.h> 50 #include <signal.h> 51 #include <stdio.h> 52 #include <stdlib.h> 53 #include <string.h> 54 #include <termios.h> 55 #include <unistd.h> 56 57 static FILE *fscript; 58 static int master, slave; 59 static pid_t child; 60 static const char *fname; 61 static int qflg, ttyflg; 62 63 struct termios tt; 64 65 static void done(int) __dead2; 66 static void doshell(char **); 67 static void fail(void); 68 static void finish(void); 69 static void usage(void); 70 71 int 72 main(int argc, char **argv) 73 { 74 int cc; 75 struct termios rtt, stt; 76 struct winsize win; 77 int aflg, kflg, ch, n; 78 struct timeval tv, *tvp; 79 time_t tvec, start; 80 char obuf[BUFSIZ]; 81 char ibuf[BUFSIZ]; 82 fd_set rfd; 83 int flushtime = 30; 84 85 aflg = kflg = 0; 86 while ((ch = getopt(argc, argv, "aqkt:")) != -1) 87 switch(ch) { 88 case 'a': 89 aflg = 1; 90 break; 91 case 'q': 92 qflg = 1; 93 break; 94 case 'k': 95 kflg = 1; 96 break; 97 case 't': 98 flushtime = atoi(optarg); 99 if (flushtime < 0) 100 err(1, "invalid flush time %d", flushtime); 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 if ((ttyflg = isatty(STDIN_FILENO)) != 0) { 120 if (tcgetattr(STDIN_FILENO, &tt) == -1) 121 err(1, "tcgetattr"); 122 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) 123 err(1, "ioctl"); 124 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 125 err(1, "openpty"); 126 } else { 127 if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 128 err(1, "openpty"); 129 } 130 131 if (!qflg) { 132 tvec = time(NULL); 133 printf("Script started, output file is %s\n", fname); 134 fprintf(fscript, "Script started on %s", ctime(&tvec)); 135 fflush(fscript); 136 } 137 138 if (ttyflg) { 139 rtt = tt; 140 cfmakeraw(&rtt); 141 rtt.c_lflag &= ~ECHO; 142 tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 143 } 144 145 child = fork(); 146 if (child < 0) { 147 warn("fork"); 148 done(1); 149 } 150 if (child == 0) 151 doshell(argv); 152 153 if (flushtime > 0) 154 tvp = &tv; 155 else 156 tvp = NULL; 157 158 start = time(0); 159 FD_ZERO(&rfd); 160 for (;;) { 161 FD_SET(master, &rfd); 162 FD_SET(STDIN_FILENO, &rfd); 163 if (flushtime > 0) { 164 tv.tv_sec = flushtime; 165 tv.tv_usec = 0; 166 } 167 n = select(master + 1, &rfd, 0, 0, tvp); 168 if (n < 0 && errno != EINTR) 169 break; 170 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 171 cc = read(STDIN_FILENO, ibuf, sizeof(ibuf)); 172 if (cc < 0) 173 break; 174 if (cc == 0) 175 write(master, ibuf, 0); 176 if (cc > 0) { 177 write(master, ibuf, cc); 178 if (kflg && tcgetattr(master, &stt) >= 0 && 179 ((stt.c_lflag & ECHO) == 0)) { 180 fwrite(ibuf, 1, cc, fscript); 181 } 182 } 183 } 184 if (n > 0 && FD_ISSET(master, &rfd)) { 185 cc = read(master, obuf, sizeof(obuf)); 186 if (cc <= 0) 187 break; 188 write(STDOUT_FILENO, obuf, cc); 189 fwrite(obuf, 1, cc, fscript); 190 } 191 tvec = time(0); 192 if (tvec - start >= flushtime) { 193 fflush(fscript); 194 start = tvec; 195 } 196 } 197 finish(); 198 done(0); 199 } 200 201 static void 202 usage(void) 203 { 204 fprintf(stderr, 205 "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n"); 206 exit(1); 207 } 208 209 static void 210 finish(void) 211 { 212 int die, e; 213 pid_t pid; 214 union wait status; 215 216 die = e = 0; 217 while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0) 218 if (pid == child) { 219 die = 1; 220 if (WIFEXITED(status)) 221 e = WEXITSTATUS(status); 222 else if (WIFSIGNALED(status)) 223 e = WTERMSIG(status); 224 else /* can't happen */ 225 e = 1; 226 } 227 228 if (die) 229 done(e); 230 } 231 232 static void 233 doshell(char **av) 234 { 235 const char *shell; 236 237 shell = getenv("SHELL"); 238 if (shell == NULL) 239 shell = _PATH_BSHELL; 240 241 close(master); 242 fclose(fscript); 243 login_tty(slave); 244 if (av[0]) { 245 execvp(av[0], av); 246 warn("%s", av[0]); 247 } else { 248 execl(shell, shell, "-i", NULL); 249 warn("%s", shell); 250 } 251 fail(); 252 } 253 254 static void 255 fail(void) 256 { 257 kill(0, SIGTERM); 258 done(1); 259 } 260 261 static void 262 done(int eno) 263 { 264 time_t tvec; 265 266 if (ttyflg) 267 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 268 tvec = time(NULL); 269 if (!qflg) { 270 fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 271 printf("\nScript done, output file is '%s'\n", fname); 272 } 273 fclose(fscript); 274 close(master); 275 exit(eno); 276 } 277