2 * Copyright (c) 1980, 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#) Copyright (c) 1980, 1992, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)script.c 8.1 (Berkeley) 6/6/93
31 * $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 $
32 * $DragonFly: src/usr.bin/script/script.c,v 1.10 2005/03/16 06:26:49 cpressey Exp $
35 #include <sys/types.h>
38 #include <sys/ioctl.h>
54 static int master, slave;
55 static const char *fname;
56 static int qflg, ttyflg;
60 static void done(int) __dead2;
61 static void doshell(char **);
62 static int reap(pid_t);
63 static void usage(void);
66 main(int argc, char **argv)
69 struct termios rtt, stt;
71 int aflg, kflg, ch, n;
72 struct timeval tv, *tvp;
81 while ((ch = getopt(argc, argv, "aqkt:")) != -1) {
93 flushtime = atoi(optarg);
95 errx(1, "invalid flush time %d", flushtime);
110 fname = "typescript";
112 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
115 if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
116 if (tcgetattr(STDIN_FILENO, &tt) == -1)
118 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
120 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
123 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
129 printf("Script started, output file is %s\n", fname);
130 fprintf(fscript, "Script started on %s", ctime(&tvec));
137 rtt.c_lflag &= ~ECHO;
138 tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
158 FD_SET(master, &rfd);
159 FD_SET(STDIN_FILENO, &rfd);
161 tv.tv_sec = flushtime;
164 n = select(master + 1, &rfd, 0, 0, tvp);
165 if (n < 0 && errno != EINTR)
167 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
168 cc = read(STDIN_FILENO, ibuf, sizeof(ibuf));
172 write(master, ibuf, 0);
174 write(master, ibuf, cc);
175 if (kflg && tcgetattr(master, &stt) >= 0 &&
176 ((stt.c_lflag & ECHO) == 0)) {
177 fwrite(ibuf, 1, cc, fscript);
181 if (n > 0 && FD_ISSET(master, &rfd)) {
182 cc = read(master, obuf, sizeof(obuf));
185 write(STDOUT_FILENO, obuf, cc);
186 fwrite(obuf, 1, cc, fscript);
189 if (tvec - start >= flushtime) {
201 "usage: script [-a] [-q] [-k] [-t time] [file] [command]\n");
213 while ((pid = wait3((int *)&status, WNOHANG, NULL)) > 0) {
215 if (WIFEXITED(status))
216 e = WEXITSTATUS(status);
217 else if (WIFSIGNALED(status))
218 e = WTERMSIG(status);
219 else /* can't happen */
232 shell = getenv("SHELL");
234 shell = _PATH_BSHELL;
243 execl(shell, shell, "-i", NULL);
256 tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
259 fprintf(fscript,"\nScript done on %s", ctime(&tvec));
260 printf("\nScript done, output file is '%s'\n", fname);