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 #include <sys/cdefs.h> 35 36 __FBSDID("$FreeBSD$"); 37 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1980, 1992, 1993\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif 43 44 #ifndef lint 45 static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93"; 46 #endif 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 <errno.h> 56 #include <fcntl.h> 57 #include <libutil.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 <unistd.h> 65 66 FILE *fscript; 67 int master, slave; 68 int child; 69 const char *fname; 70 int qflg; 71 72 struct termios tt; 73 74 void done(int) __dead2; 75 void dooutput(void); 76 void doshell(char **); 77 void fail(void); 78 void finish(void); 79 static void usage(void); 80 81 int 82 main(int argc, char *argv[]) 83 { 84 int cc; 85 struct termios rtt, stt; 86 struct winsize win; 87 int aflg, kflg, ch, n; 88 struct timeval tv, *tvp; 89 time_t tvec, start; 90 char obuf[BUFSIZ]; 91 char ibuf[BUFSIZ]; 92 fd_set rfd; 93 int flushtime = 30; 94 95 aflg = kflg = 0; 96 while ((ch = getopt(argc, argv, "aqkt:")) != -1) 97 switch(ch) { 98 case 'a': 99 aflg = 1; 100 break; 101 case 'q': 102 qflg = 1; 103 break; 104 case 'k': 105 kflg = 1; 106 break; 107 case 't': 108 flushtime = atoi(optarg); 109 if (flushtime < 0) 110 err(1, "invalid flush time %d", flushtime); 111 break; 112 case '?': 113 default: 114 usage(); 115 } 116 argc -= optind; 117 argv += optind; 118 119 if (argc > 0) { 120 fname = argv[0]; 121 argv++; 122 argc--; 123 } else 124 fname = "typescript"; 125 126 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL) 127 err(1, "%s", fname); 128 129 (void)tcgetattr(STDIN_FILENO, &tt); 130 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win); 131 if (openpty(&master, &slave, NULL, &tt, &win) == -1) 132 err(1, "openpty"); 133 134 if (!qflg) { 135 tvec = time(NULL); 136 (void)printf("Script started, output file is %s\n", fname); 137 (void)fprintf(fscript, "Script started on %s", ctime(&tvec)); 138 fflush(fscript); 139 } 140 rtt = tt; 141 cfmakeraw(&rtt); 142 rtt.c_lflag &= ~ECHO; 143 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 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, BUFSIZ); 172 if (cc <= 0) 173 break; 174 if (cc > 0) { 175 (void)write(master, ibuf, cc); 176 if (kflg && tcgetattr(master, &stt) >= 0 && 177 ((stt.c_lflag & ECHO) == 0)) { 178 (void)fwrite(ibuf, 1, cc, fscript); 179 } 180 } 181 } 182 if (n > 0 && FD_ISSET(master, &rfd)) { 183 cc = read(master, obuf, sizeof (obuf)); 184 if (cc <= 0) 185 break; 186 (void)write(STDOUT_FILENO, obuf, cc); 187 (void)fwrite(obuf, 1, cc, fscript); 188 } 189 tvec = time(0); 190 if (tvec - start >= flushtime) { 191 fflush(fscript); 192 start = tvec; 193 } 194 } 195 finish(); 196 done(0); 197 } 198 199 static void 200 usage(void) 201 { 202 (void)fprintf(stderr, 203 "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n"); 204 exit(1); 205 } 206 207 void 208 finish(void) 209 { 210 pid_t pid; 211 int die, e, status; 212 213 die = e = 0; 214 while ((pid = wait3(&status, WNOHANG, 0)) > 0) 215 if (pid == child) { 216 die = 1; 217 if (WIFEXITED(status)) 218 e = WEXITSTATUS(status); 219 else if (WIFSIGNALED(status)) 220 e = WTERMSIG(status); 221 else /* can't happen */ 222 e = 1; 223 } 224 225 if (die) 226 done(e); 227 } 228 229 void 230 doshell(char **av) 231 { 232 const char *shell; 233 234 shell = getenv("SHELL"); 235 if (shell == NULL) 236 shell = _PATH_BSHELL; 237 238 (void)close(master); 239 (void)fclose(fscript); 240 login_tty(slave); 241 if (av[0]) { 242 execvp(av[0], av); 243 warn("%s", av[0]); 244 } else { 245 execl(shell, shell, "-i", (char *)NULL); 246 warn("%s", shell); 247 } 248 fail(); 249 } 250 251 void 252 fail(void) 253 { 254 (void)kill(0, SIGTERM); 255 done(1); 256 } 257 258 void 259 done(int eno) 260 { 261 time_t tvec; 262 263 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 264 tvec = time(NULL); 265 if (!qflg) { 266 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec)); 267 (void)printf("\nScript done, output file is %s\n", fname); 268 } 269 (void)fclose(fscript); 270 (void)close(master); 271 exit(eno); 272 } 273