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 * 4. 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
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char copyright[] =
36 "@(#) Copyright (c) 1980, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
47 #include <sys/ioctl.h>
63 static int master, slave;
65 static const char *fname;
66 static int qflg, ttyflg;
68 static struct termios tt;
70 static void done(int) __dead2;
71 static void doshell(char **);
72 static void fail(void);
73 static void finish(void);
74 static void usage(void);
77 main(int argc, char *argv[])
80 struct termios rtt, stt;
82 int aflg, kflg, ch, n;
83 struct timeval tv, *tvp;
92 while ((ch = getopt(argc, argv, "aqkt:")) != -1)
104 flushtime = atoi(optarg);
106 err(1, "invalid flush time %d", flushtime);
120 fname = "typescript";
122 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
125 if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
126 if (tcgetattr(STDIN_FILENO, &tt) == -1)
128 if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
130 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
133 if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
139 (void)printf("Script started, output file is %s\n", fname);
140 (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
146 rtt.c_lflag &= ~ECHO;
147 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
159 start = tvec = time(0);
163 FD_SET(master, &rfd);
165 FD_SET(STDIN_FILENO, &rfd);
166 if (!readstdin && ttyflg) {
171 } else if (flushtime > 0) {
172 tv.tv_sec = flushtime - (tvec - start);
178 n = select(master + 1, &rfd, 0, 0, tvp);
179 if (n < 0 && errno != EINTR)
181 if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
182 cc = read(STDIN_FILENO, ibuf, BUFSIZ);
186 if (tcgetattr(master, &stt) == 0 &&
187 (stt.c_lflag & ICANON) != 0) {
188 (void)write(master, &stt.c_cc[VEOF], 1);
193 (void)write(master, ibuf, cc);
194 if (kflg && tcgetattr(master, &stt) >= 0 &&
195 ((stt.c_lflag & ECHO) == 0)) {
196 (void)fwrite(ibuf, 1, cc, fscript);
200 if (n > 0 && FD_ISSET(master, &rfd)) {
201 cc = read(master, obuf, sizeof (obuf));
204 (void)write(STDOUT_FILENO, obuf, cc);
205 (void)fwrite(obuf, 1, cc, fscript);
208 if (tvec - start >= flushtime) {
220 (void)fprintf(stderr,
221 "usage: script [-akq] [-t time] [file [command ...]]\n");
230 if (waitpid(child, &status, 0) == child) {
231 if (WIFEXITED(status))
232 e = WEXITSTATUS(status);
233 else if (WIFSIGNALED(status))
234 e = WTERMSIG(status);
235 else /* can't happen */
247 shell = getenv("SHELL");
249 shell = _PATH_BSHELL;
252 for (k = 0 ; av[k] ; ++k)
253 fprintf(fscript, "%s%s", k ? " " : "", av[k]);
254 fprintf(fscript, "\r\n");
257 (void)fclose(fscript);
259 setenv("SCRIPT", fname, 1);
264 execl(shell, shell, "-i", (char *)NULL);
273 (void)kill(0, SIGTERM);
283 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
286 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
287 (void)printf("\nScript done, output file is %s\n", fname);
289 (void)fclose(fscript);