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. 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.
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
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1992, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
44 #include <sys/types.h>
47 #include <sys/ioctl.h>
69 __dead void done __P((void));
70 void dooutput __P((void));
71 void doshell __P((void));
72 void err __P((const char *, ...));
73 void fail __P((void));
74 void finish __P((int));
75 void scriptflush __P((int));
89 while ((ch = getopt(argc, argv, "a")) != EOF)
96 (void)fprintf(stderr, "usage: script [-a] [file]\n");
105 fname = "typescript";
107 if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
108 err("%s: %s", fname, strerror(errno));
110 (void)tcgetattr(STDIN_FILENO, &tt);
111 (void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
112 if (openpty(&master, &slave, NULL, &tt, &win) == -1)
113 err("openpty: %s", strerror(errno));
115 (void)printf("Script started, output file is %s\n", fname);
118 rtt.c_lflag &= ~ECHO;
119 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
121 (void)signal(SIGCHLD, finish);
128 subchild = child = fork();
139 (void)fclose(fscript);
140 while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0)
141 (void)write(master, ibuf, cc);
149 register int die, pid;
153 while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
164 struct itimerval value;
169 (void)close(STDIN_FILENO);
171 (void)fprintf(fscript, "Script started on %s", ctime(&tvec));
173 (void)signal(SIGALRM, scriptflush);
174 value.it_interval.tv_sec = SECSPERMIN / 2;
175 value.it_interval.tv_usec = 0;
176 value.it_value = value.it_interval;
177 (void)setitimer(ITIMER_REAL, &value, NULL);
179 cc = read(master, obuf, sizeof (obuf));
182 (void)write(1, obuf, cc);
183 (void)fwrite(obuf, 1, cc, fscript);
194 (void)fflush(fscript);
204 shell = getenv("SHELL");
206 shell = _PATH_BSHELL;
209 (void)fclose(fscript);
211 execl(shell, "sh", "-i", NULL);
220 (void)kill(0, SIGTERM);
231 (void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
232 (void)fclose(fscript);
235 (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
236 (void)printf("Script done, output file is %s\n", fname);
249 err(const char *fmt, ...)
262 (void)fprintf(stderr, "script: ");
263 (void)vfprintf(stderr, fmt, ap);
265 (void)fprintf(stderr, "\n");