xref: /openbsd/usr.bin/script/script.c (revision 07ea8d15)
1 /*	$OpenBSD: script.c,v 1.2 1996/06/26 05:39:02 deraadt Exp $	*/
2 /*	$NetBSD: script.c,v 1.3 1994/12/21 08:55:43 jtc Exp $	*/
3 
4 /*
5  * Copyright (c) 1980, 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #ifndef lint
38 static char copyright[] =
39 "@(#) Copyright (c) 1980, 1992, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
46 #endif
47 static char rcsid[] = "$OpenBSD: script.c,v 1.2 1996/06/26 05:39:02 deraadt Exp $";
48 #endif /* not lint */
49 
50 #include <sys/types.h>
51 #include <sys/wait.h>
52 #include <sys/stat.h>
53 #include <sys/ioctl.h>
54 #include <sys/time.h>
55 
56 #include <errno.h>
57 #include <fcntl.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 <tzfile.h>
65 #include <unistd.h>
66 
67 FILE	*fscript;
68 int	master, slave;
69 int	child, subchild;
70 int	outcc;
71 char	*fname;
72 
73 struct	termios tt;
74 
75 __dead	void done __P((void));
76 	void dooutput __P((void));
77 	void doshell __P((void));
78 	void err __P((const char *, ...));
79 	void fail __P((void));
80 	void finish __P((int));
81 	void scriptflush __P((int));
82 
83 int
84 main(argc, argv)
85 	int argc;
86 	char *argv[];
87 {
88 	register int cc;
89 	struct termios rtt;
90 	struct winsize win;
91 	int aflg, ch;
92 	char ibuf[BUFSIZ];
93 
94 	aflg = 0;
95 	while ((ch = getopt(argc, argv, "a")) != EOF)
96 		switch(ch) {
97 		case 'a':
98 			aflg = 1;
99 			break;
100 		case '?':
101 		default:
102 			(void)fprintf(stderr, "usage: script [-a] [file]\n");
103 			exit(1);
104 		}
105 	argc -= optind;
106 	argv += optind;
107 
108 	if (argc > 0)
109 		fname = argv[0];
110 	else
111 		fname = "typescript";
112 
113 	if ((fscript = fopen(fname, aflg ? "a" : "w")) == NULL)
114 		err("%s: %s", fname, strerror(errno));
115 
116 	(void)tcgetattr(STDIN_FILENO, &tt);
117 	(void)ioctl(STDIN_FILENO, TIOCGWINSZ, &win);
118 	if (openpty(&master, &slave, NULL, &tt, &win) == -1)
119 		err("openpty: %s", strerror(errno));
120 
121 	(void)printf("Script started, output file is %s\n", fname);
122 	rtt = tt;
123 	cfmakeraw(&rtt);
124 	rtt.c_lflag &= ~ECHO;
125 	(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
126 
127 	(void)signal(SIGCHLD, finish);
128 	child = fork();
129 	if (child < 0) {
130 		perror("fork");
131 		fail();
132 	}
133 	if (child == 0) {
134 		subchild = child = fork();
135 		if (child < 0) {
136 			perror("fork");
137 			fail();
138 		}
139 		if (child)
140 			dooutput();
141 		else
142 			doshell();
143 	}
144 
145 	(void)fclose(fscript);
146 	while ((cc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0)
147 		(void)write(master, ibuf, cc);
148 	done();
149 }
150 
151 void
152 finish(signo)
153 	int signo;
154 {
155 	register int die, pid;
156 	union wait status;
157 
158 	die = 0;
159 	while ((pid = wait3((int *)&status, WNOHANG, 0)) > 0)
160 		if (pid == child)
161 			die = 1;
162 
163 	if (die)
164 		done();
165 }
166 
167 void
168 dooutput()
169 {
170 	struct itimerval value;
171 	register int cc;
172 	time_t tvec;
173 	char obuf[BUFSIZ];
174 
175 	(void)close(STDIN_FILENO);
176 	tvec = time(NULL);
177 	(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
178 
179 	(void)signal(SIGALRM, scriptflush);
180 	value.it_interval.tv_sec = SECSPERMIN / 2;
181 	value.it_interval.tv_usec = 0;
182 	value.it_value = value.it_interval;
183 	(void)setitimer(ITIMER_REAL, &value, NULL);
184 	for (;;) {
185 		cc = read(master, obuf, sizeof (obuf));
186 		if (cc <= 0)
187 			break;
188 		(void)write(1, obuf, cc);
189 		(void)fwrite(obuf, 1, cc, fscript);
190 		outcc += cc;
191 	}
192 	done();
193 }
194 
195 void
196 scriptflush(signo)
197 	int signo;
198 {
199 	if (outcc) {
200 		(void)fflush(fscript);
201 		outcc = 0;
202 	}
203 }
204 
205 void
206 doshell()
207 {
208 	char *shell;
209 
210 	shell = getenv("SHELL");
211 	if (shell == NULL)
212 		shell = _PATH_BSHELL;
213 
214 	(void)close(master);
215 	(void)fclose(fscript);
216 	login_tty(slave);
217 	execl(shell, "sh", "-i", NULL);
218 	perror(shell);
219 	fail();
220 }
221 
222 void
223 fail()
224 {
225 
226 	(void)kill(0, SIGTERM);
227 	done();
228 }
229 
230 void
231 done()
232 {
233 	time_t tvec;
234 
235 	if (subchild) {
236 		tvec = time(NULL);
237 		(void)fprintf(fscript,"\nScript done on %s", ctime(&tvec));
238 		(void)fclose(fscript);
239 		(void)close(master);
240 	} else {
241 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
242 		(void)printf("Script done, output file is %s\n", fname);
243 	}
244 	exit(0);
245 }
246 
247 #if __STDC__
248 #include <stdarg.h>
249 #else
250 #include <varargs.h>
251 #endif
252 
253 void
254 #if __STDC__
255 err(const char *fmt, ...)
256 #else
257 err(fmt, va_alist)
258 	char *fmt;
259 	va_dcl
260 #endif
261 {
262 	va_list ap;
263 #if __STDC__
264 	va_start(ap, fmt);
265 #else
266 	va_start(ap);
267 #endif
268 	(void)fprintf(stderr, "script: ");
269 	(void)vfprintf(stderr, fmt, ap);
270 	va_end(ap);
271 	(void)fprintf(stderr, "\n");
272 	exit(1);
273 	/* NOTREACHED */
274 }
275