xref: /freebsd/usr.bin/script/script.c (revision 8d105abc4fb48bbd546056f83bbff951e8a77865)
19b50d902SRodney W. Grimes /*
26cff4e07SDavid E. O'Brien  * Copyright (c) 2010, 2012  David E. O'Brien
39b50d902SRodney W. Grimes  * Copyright (c) 1980, 1992, 1993
49b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
59b50d902SRodney W. Grimes  *
69b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
79b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
89b50d902SRodney W. Grimes  * are met:
99b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
109b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
119b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
139b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
149b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
159b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
169b50d902SRodney W. Grimes  *    without specific prior written permission.
179b50d902SRodney W. Grimes  *
189b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
209b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
219b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
229b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
239b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
249b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
259b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
269b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
279b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
289b50d902SRodney W. Grimes  * SUCH DAMAGE.
299b50d902SRodney W. Grimes  */
309b50d902SRodney W. Grimes 
3113f02f28SDavid E. O'Brien #include <sys/param.h>
32d20f95e1SMark Murray __FBSDID("$FreeBSD$");
339b50d902SRodney W. Grimes #ifndef lint
34236d2f55SPhilippe Charnier static const char copyright[] =
359b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1992, 1993\n\
369b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
37d20f95e1SMark Murray #endif
389b50d902SRodney W. Grimes #ifndef lint
39d20f95e1SMark Murray static const char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
40236d2f55SPhilippe Charnier #endif
419b50d902SRodney W. Grimes 
429b50d902SRodney W. Grimes #include <sys/wait.h>
439b50d902SRodney W. Grimes #include <sys/stat.h>
449b50d902SRodney W. Grimes #include <sys/ioctl.h>
459b50d902SRodney W. Grimes #include <sys/time.h>
46df53360cSBrian Somers #include <sys/uio.h>
47df53360cSBrian Somers #include <sys/endian.h>
486cff4e07SDavid E. O'Brien #include <dev/filemon/filemon.h>
499b50d902SRodney W. Grimes 
50236d2f55SPhilippe Charnier #include <err.h>
51e8eb82a8SPeter Wemm #include <errno.h>
529b50d902SRodney W. Grimes #include <fcntl.h>
53236d2f55SPhilippe Charnier #include <libutil.h>
549b50d902SRodney W. Grimes #include <paths.h>
559b50d902SRodney W. Grimes #include <signal.h>
569b50d902SRodney W. Grimes #include <stdio.h>
579b50d902SRodney W. Grimes #include <stdlib.h>
589b50d902SRodney W. Grimes #include <string.h>
599b50d902SRodney W. Grimes #include <termios.h>
609b50d902SRodney W. Grimes #include <unistd.h>
619b50d902SRodney W. Grimes 
62df53360cSBrian Somers #define DEF_BUF 65536
63df53360cSBrian Somers 
64df53360cSBrian Somers struct stamp {
65df53360cSBrian Somers 	uint64_t scr_len;	/* amount of data */
66df53360cSBrian Somers 	uint64_t scr_sec;	/* time it arrived in seconds... */
67df53360cSBrian Somers 	uint32_t scr_usec;	/* ...and microseconds */
68df53360cSBrian Somers 	uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
69df53360cSBrian Somers };
70df53360cSBrian Somers 
71e8efeec6SEd Schouten static FILE *fscript;
72e8efeec6SEd Schouten static int master, slave;
73e8efeec6SEd Schouten static int child;
74e8efeec6SEd Schouten static const char *fname;
756cff4e07SDavid E. O'Brien static char *fmfname;
766cff4e07SDavid E. O'Brien static int fflg, qflg, ttyflg;
77df53360cSBrian Somers static int usesleep, rawout;
789b50d902SRodney W. Grimes 
79e8efeec6SEd Schouten static struct termios tt;
809b50d902SRodney W. Grimes 
81e8efeec6SEd Schouten static void done(int) __dead2;
82e8efeec6SEd Schouten static void doshell(char **);
83e8efeec6SEd Schouten static void fail(void);
84e8efeec6SEd Schouten static void finish(void);
85df53360cSBrian Somers static void record(FILE *, char *, size_t, int);
86df53360cSBrian Somers static void consume(FILE *, off_t, char *, int);
87df53360cSBrian Somers static void playback(FILE *) __dead2;
883f330d7dSWarner Losh static void usage(void);
899b50d902SRodney W. Grimes 
909b50d902SRodney W. Grimes int
91f4ac32deSDavid Malone main(int argc, char *argv[])
929b50d902SRodney W. Grimes {
93d20f95e1SMark Murray 	int cc;
94e8eb82a8SPeter Wemm 	struct termios rtt, stt;
959b50d902SRodney W. Grimes 	struct winsize win;
96e8eb82a8SPeter Wemm 	struct timeval tv, *tvp;
97e8eb82a8SPeter Wemm 	time_t tvec, start;
98e8eb82a8SPeter Wemm 	char obuf[BUFSIZ];
999b50d902SRodney W. Grimes 	char ibuf[BUFSIZ];
100e8eb82a8SPeter Wemm 	fd_set rfd;
101*8d105abcSTom Rhodes 	int aflg, Fflg, kflg, pflg, ch, k, n;
1026cff4e07SDavid E. O'Brien 	int flushtime, readstdin;
1036cff4e07SDavid E. O'Brien 	int fm_fd, fm_log;
1049b50d902SRodney W. Grimes 
105*8d105abcSTom Rhodes 	aflg = Fflg = kflg = pflg = 0;
106df53360cSBrian Somers 	usesleep = 1;
107df53360cSBrian Somers 	rawout = 0;
1086cff4e07SDavid E. O'Brien 	flushtime = 30;
1096cff4e07SDavid E. O'Brien 	fm_fd = -1;	/* Shut up stupid "may be used uninitialized" GCC
1106cff4e07SDavid E. O'Brien 			   warning. (not needed w/clang) */
111df53360cSBrian Somers 
112*8d105abcSTom Rhodes 	while ((ch = getopt(argc, argv, "adFfkpqrt:")) != -1)
1139b50d902SRodney W. Grimes 		switch(ch) {
1149b50d902SRodney W. Grimes 		case 'a':
1159b50d902SRodney W. Grimes 			aflg = 1;
1169b50d902SRodney W. Grimes 			break;
117df53360cSBrian Somers 		case 'd':
118df53360cSBrian Somers 			usesleep = 0;
11951afb8dfSPeter Wemm 			break;
120*8d105abcSTom Rhodes 		case 'F':
121*8d105abcSTom Rhodes 			Fflg = 1;
122*8d105abcSTom Rhodes 			break;
1236cff4e07SDavid E. O'Brien 		case 'f':
1246cff4e07SDavid E. O'Brien 			fflg = 1;
1256cff4e07SDavid E. O'Brien 			break;
126e8eb82a8SPeter Wemm 		case 'k':
127e8eb82a8SPeter Wemm 			kflg = 1;
128e8eb82a8SPeter Wemm 			break;
129df53360cSBrian Somers 		case 'p':
130df53360cSBrian Somers 			pflg = 1;
131df53360cSBrian Somers 			break;
132df53360cSBrian Somers 		case 'q':
133df53360cSBrian Somers 			qflg = 1;
134df53360cSBrian Somers 			break;
135df53360cSBrian Somers 		case 'r':
136df53360cSBrian Somers 			rawout = 1;
137df53360cSBrian Somers 			break;
138e8eb82a8SPeter Wemm 		case 't':
139e8eb82a8SPeter Wemm 			flushtime = atoi(optarg);
140e8eb82a8SPeter Wemm 			if (flushtime < 0)
141e8eb82a8SPeter Wemm 				err(1, "invalid flush time %d", flushtime);
142e8eb82a8SPeter Wemm 			break;
1439b50d902SRodney W. Grimes 		case '?':
1449b50d902SRodney W. Grimes 		default:
145236d2f55SPhilippe Charnier 			usage();
1469b50d902SRodney W. Grimes 		}
1479b50d902SRodney W. Grimes 	argc -= optind;
1489b50d902SRodney W. Grimes 	argv += optind;
1499b50d902SRodney W. Grimes 
15051afb8dfSPeter Wemm 	if (argc > 0) {
1519b50d902SRodney W. Grimes 		fname = argv[0];
15251afb8dfSPeter Wemm 		argv++;
15351afb8dfSPeter Wemm 		argc--;
15451afb8dfSPeter Wemm 	} else
1559b50d902SRodney W. Grimes 		fname = "typescript";
1569b50d902SRodney W. Grimes 
157df53360cSBrian Somers 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
158236d2f55SPhilippe Charnier 		err(1, "%s", fname);
1599b50d902SRodney W. Grimes 
1606cff4e07SDavid E. O'Brien 	if (fflg) {
1616cff4e07SDavid E. O'Brien 		asprintf(&fmfname, "%s.filemon", fname);
1626cff4e07SDavid E. O'Brien 		if (!fmfname)
1636cff4e07SDavid E. O'Brien 			err(1, "%s.filemon", fname);
1646cff4e07SDavid E. O'Brien 		if ((fm_fd = open("/dev/filemon", O_RDWR)) == -1)
1656cff4e07SDavid E. O'Brien 			err(1, "open(\"/dev/filemon\", O_RDWR)");
1666cff4e07SDavid E. O'Brien 		if ((fm_log = open(fmfname, O_WRONLY | O_CREAT | O_TRUNC,
1676cff4e07SDavid E. O'Brien 		    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
1686cff4e07SDavid E. O'Brien 			err(1, "open(%s)", fmfname);
1696cff4e07SDavid E. O'Brien 		if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
1706cff4e07SDavid E. O'Brien 			err(1, "Cannot set filemon log file descriptor");
1716cff4e07SDavid E. O'Brien 
1726cff4e07SDavid E. O'Brien 		/* Set up these two fd's to close on exec. */
1736cff4e07SDavid E. O'Brien 		(void)fcntl(fm_fd, F_SETFD, FD_CLOEXEC);
1746cff4e07SDavid E. O'Brien 		(void)fcntl(fm_log, F_SETFD, FD_CLOEXEC);
1756cff4e07SDavid E. O'Brien 	}
1766cff4e07SDavid E. O'Brien 
177df53360cSBrian Somers 	if (pflg)
178df53360cSBrian Somers 		playback(fscript);
179df53360cSBrian Somers 
180d93708c3SEd Schouten 	if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
181e292a0e3SColin Percival 		if (tcgetattr(STDIN_FILENO, &tt) == -1)
182e292a0e3SColin Percival 			err(1, "tcgetattr");
183e292a0e3SColin Percival 		if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
184e292a0e3SColin Percival 			err(1, "ioctl");
1859b50d902SRodney W. Grimes 		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
186236d2f55SPhilippe Charnier 			err(1, "openpty");
187e292a0e3SColin Percival 	} else {
188e292a0e3SColin Percival 		if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
189e292a0e3SColin Percival 			err(1, "openpty");
190e292a0e3SColin Percival 	}
1919b50d902SRodney W. Grimes 
192df53360cSBrian Somers 	if (rawout)
193df53360cSBrian Somers 		record(fscript, NULL, 0, 's');
194df53360cSBrian Somers 
195e8eb82a8SPeter Wemm 	if (!qflg) {
196e8eb82a8SPeter Wemm 		tvec = time(NULL);
1979b50d902SRodney W. Grimes 		(void)printf("Script started, output file is %s\n", fname);
198df53360cSBrian Somers 		if (!rawout) {
199df53360cSBrian Somers 			(void)fprintf(fscript, "Script started on %s",
200df53360cSBrian Somers 			    ctime(&tvec));
201df53360cSBrian Somers 			if (argv[0]) {
202df53360cSBrian Somers 				fprintf(fscript, "command: ");
203df53360cSBrian Somers 				for (k = 0 ; argv[k] ; ++k)
204df53360cSBrian Somers 					fprintf(fscript, "%s%s", k ? " " : "",
205df53360cSBrian Somers 						argv[k]);
206df53360cSBrian Somers 				fprintf(fscript, "\n");
207df53360cSBrian Somers 			}
208df53360cSBrian Somers 		}
209e8eb82a8SPeter Wemm 		fflush(fscript);
2106cff4e07SDavid E. O'Brien 		if (fflg) {
2116cff4e07SDavid E. O'Brien 			(void)printf("Filemon started, output file is %s\n",
2126cff4e07SDavid E. O'Brien 			    fmfname);
2136cff4e07SDavid E. O'Brien 		}
214e8eb82a8SPeter Wemm 	}
215e292a0e3SColin Percival 	if (ttyflg) {
2169b50d902SRodney W. Grimes 		rtt = tt;
2179b50d902SRodney W. Grimes 		cfmakeraw(&rtt);
2189b50d902SRodney W. Grimes 		rtt.c_lflag &= ~ECHO;
2199b50d902SRodney W. Grimes 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
220e292a0e3SColin Percival 	}
2219b50d902SRodney W. Grimes 
2229b50d902SRodney W. Grimes 	child = fork();
2239b50d902SRodney W. Grimes 	if (child < 0) {
224236d2f55SPhilippe Charnier 		warn("fork");
225b1c66970SDag-Erling Smørgrav 		done(1);
2269b50d902SRodney W. Grimes 	}
227e8eb82a8SPeter Wemm 	if (child == 0)
22851afb8dfSPeter Wemm 		doshell(argv);
2298d5e4a14SEd Schouten 	close(slave);
2309b50d902SRodney W. Grimes 
2316cff4e07SDavid E. O'Brien 	if (fflg && ioctl(fm_fd, FILEMON_SET_PID, &child) < 0)
2326cff4e07SDavid E. O'Brien 		err(1, "Cannot set filemon PID");
2336cff4e07SDavid E. O'Brien 
23429da7547SMikolaj Golub 	start = tvec = time(0);
23529da7547SMikolaj Golub 	readstdin = 1;
236e8eb82a8SPeter Wemm 	for (;;) {
23729da7547SMikolaj Golub 		FD_ZERO(&rfd);
238e8eb82a8SPeter Wemm 		FD_SET(master, &rfd);
23929da7547SMikolaj Golub 		if (readstdin)
240e8eb82a8SPeter Wemm 			FD_SET(STDIN_FILENO, &rfd);
241df53360cSBrian Somers 		if ((!readstdin && ttyflg) || flushtime > 0) {
242df53360cSBrian Somers 			tv.tv_sec = !readstdin && ttyflg ? 1 :
243df53360cSBrian Somers 			    flushtime - (tvec - start);
244e8eb82a8SPeter Wemm 			tv.tv_usec = 0;
24529da7547SMikolaj Golub 			tvp = &tv;
24629da7547SMikolaj Golub 			readstdin = 1;
24729da7547SMikolaj Golub 		} else {
24829da7547SMikolaj Golub 			tvp = NULL;
249e8eb82a8SPeter Wemm 		}
250e8eb82a8SPeter Wemm 		n = select(master + 1, &rfd, 0, 0, tvp);
251e8eb82a8SPeter Wemm 		if (n < 0 && errno != EINTR)
252e8eb82a8SPeter Wemm 			break;
253e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
254e8eb82a8SPeter Wemm 			cc = read(STDIN_FILENO, ibuf, BUFSIZ);
255d6a68195SColin Percival 			if (cc < 0)
256e8eb82a8SPeter Wemm 				break;
25729da7547SMikolaj Golub 			if (cc == 0) {
25829da7547SMikolaj Golub 				if (tcgetattr(master, &stt) == 0 &&
25929da7547SMikolaj Golub 				    (stt.c_lflag & ICANON) != 0) {
26029da7547SMikolaj Golub 					(void)write(master, &stt.c_cc[VEOF], 1);
26129da7547SMikolaj Golub 				}
26229da7547SMikolaj Golub 				readstdin = 0;
26329da7547SMikolaj Golub 			}
264e8eb82a8SPeter Wemm 			if (cc > 0) {
265df53360cSBrian Somers 				if (rawout)
266df53360cSBrian Somers 					record(fscript, ibuf, cc, 'i');
2679b50d902SRodney W. Grimes 				(void)write(master, ibuf, cc);
268e8eb82a8SPeter Wemm 				if (kflg && tcgetattr(master, &stt) >= 0 &&
269e8eb82a8SPeter Wemm 				    ((stt.c_lflag & ECHO) == 0)) {
270e8eb82a8SPeter Wemm 					(void)fwrite(ibuf, 1, cc, fscript);
271e8eb82a8SPeter Wemm 				}
272e8eb82a8SPeter Wemm 			}
273e8eb82a8SPeter Wemm 		}
274e8eb82a8SPeter Wemm 		if (n > 0 && FD_ISSET(master, &rfd)) {
275e8eb82a8SPeter Wemm 			cc = read(master, obuf, sizeof (obuf));
276e8eb82a8SPeter Wemm 			if (cc <= 0)
277e8eb82a8SPeter Wemm 				break;
278e1b4d8d0SSheldon Hearn 			(void)write(STDOUT_FILENO, obuf, cc);
279df53360cSBrian Somers 			if (rawout)
280df53360cSBrian Somers 				record(fscript, obuf, cc, 'o');
281df53360cSBrian Somers 			else
282e8eb82a8SPeter Wemm 				(void)fwrite(obuf, 1, cc, fscript);
283e8eb82a8SPeter Wemm 		}
284e8eb82a8SPeter Wemm 		tvec = time(0);
285e8eb82a8SPeter Wemm 		if (tvec - start >= flushtime) {
286e8eb82a8SPeter Wemm 			fflush(fscript);
287e8eb82a8SPeter Wemm 			start = tvec;
288e8eb82a8SPeter Wemm 		}
289*8d105abcSTom Rhodes 		if (Fflg)
290*8d105abcSTom Rhodes 			fflush(fscript);
291e8eb82a8SPeter Wemm 	}
292b1c66970SDag-Erling Smørgrav 	finish();
293b1c66970SDag-Erling Smørgrav 	done(0);
2949b50d902SRodney W. Grimes }
2959b50d902SRodney W. Grimes 
296236d2f55SPhilippe Charnier static void
297f4ac32deSDavid Malone usage(void)
298236d2f55SPhilippe Charnier {
299e8eb82a8SPeter Wemm 	(void)fprintf(stderr,
3006cff4e07SDavid E. O'Brien 	    "usage: script [-adfkpqr] [-t time] [file [command ...]]\n");
301236d2f55SPhilippe Charnier 	exit(1);
302236d2f55SPhilippe Charnier }
303236d2f55SPhilippe Charnier 
304e8efeec6SEd Schouten static void
305f4ac32deSDavid Malone finish(void)
3069b50d902SRodney W. Grimes {
307e1e9ba33SEd Schouten 	int e, status;
3089b50d902SRodney W. Grimes 
309e1e9ba33SEd Schouten 	if (waitpid(child, &status, 0) == child) {
310b1c66970SDag-Erling Smørgrav 		if (WIFEXITED(status))
311b1c66970SDag-Erling Smørgrav 			e = WEXITSTATUS(status);
312b1c66970SDag-Erling Smørgrav 		else if (WIFSIGNALED(status))
313b1c66970SDag-Erling Smørgrav 			e = WTERMSIG(status);
314b1c66970SDag-Erling Smørgrav 		else /* can't happen */
315b1c66970SDag-Erling Smørgrav 			e = 1;
316b1c66970SDag-Erling Smørgrav 		done(e);
3179b50d902SRodney W. Grimes 	}
318e1e9ba33SEd Schouten }
3199b50d902SRodney W. Grimes 
320e8efeec6SEd Schouten static void
321f4ac32deSDavid Malone doshell(char **av)
3229b50d902SRodney W. Grimes {
323b139689cSDavid Malone 	const char *shell;
3249b50d902SRodney W. Grimes 
3259b50d902SRodney W. Grimes 	shell = getenv("SHELL");
3269b50d902SRodney W. Grimes 	if (shell == NULL)
3279b50d902SRodney W. Grimes 		shell = _PATH_BSHELL;
3289b50d902SRodney W. Grimes 
3299b50d902SRodney W. Grimes 	(void)close(master);
3309b50d902SRodney W. Grimes 	(void)fclose(fscript);
3316cff4e07SDavid E. O'Brien 	free(fmfname);
3329b50d902SRodney W. Grimes 	login_tty(slave);
3339b91846cSDavid E. O'Brien 	setenv("SCRIPT", fname, 1);
334b1c66970SDag-Erling Smørgrav 	if (av[0]) {
3350e604e98SPeter Wemm 		execvp(av[0], av);
336b7ffba17SKris Kennaway 		warn("%s", av[0]);
337b1c66970SDag-Erling Smørgrav 	} else {
3387bc6d015SBrian Somers 		execl(shell, shell, "-i", (char *)NULL);
339b7ffba17SKris Kennaway 		warn("%s", shell);
340b1c66970SDag-Erling Smørgrav 	}
3419b50d902SRodney W. Grimes 	fail();
3429b50d902SRodney W. Grimes }
3439b50d902SRodney W. Grimes 
344e8efeec6SEd Schouten static void
345f4ac32deSDavid Malone fail(void)
3469b50d902SRodney W. Grimes {
3479b50d902SRodney W. Grimes 	(void)kill(0, SIGTERM);
348b1c66970SDag-Erling Smørgrav 	done(1);
3499b50d902SRodney W. Grimes }
3509b50d902SRodney W. Grimes 
351e8efeec6SEd Schouten static void
352f4ac32deSDavid Malone done(int eno)
3539b50d902SRodney W. Grimes {
3549b50d902SRodney W. Grimes 	time_t tvec;
3559b50d902SRodney W. Grimes 
356e292a0e3SColin Percival 	if (ttyflg)
357e8eb82a8SPeter Wemm 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
3589b50d902SRodney W. Grimes 	tvec = time(NULL);
359df53360cSBrian Somers 	if (rawout)
360df53360cSBrian Somers 		record(fscript, NULL, 0, 'e');
361e8eb82a8SPeter Wemm 	if (!qflg) {
362df53360cSBrian Somers 		if (!rawout)
363df53360cSBrian Somers 			(void)fprintf(fscript,"\nScript done on %s",
364df53360cSBrian Somers 			    ctime(&tvec));
365e8eb82a8SPeter Wemm 		(void)printf("\nScript done, output file is %s\n", fname);
3666cff4e07SDavid E. O'Brien 		if (fflg) {
3676cff4e07SDavid E. O'Brien 			(void)printf("Filemon done, output file is %s\n",
3686cff4e07SDavid E. O'Brien 			    fmfname);
3696cff4e07SDavid E. O'Brien 		}
370e8eb82a8SPeter Wemm 	}
3719b50d902SRodney W. Grimes 	(void)fclose(fscript);
3729b50d902SRodney W. Grimes 	(void)close(master);
373b1c66970SDag-Erling Smørgrav 	exit(eno);
3749b50d902SRodney W. Grimes }
375df53360cSBrian Somers 
376df53360cSBrian Somers static void
377df53360cSBrian Somers record(FILE *fp, char *buf, size_t cc, int direction)
378df53360cSBrian Somers {
379df53360cSBrian Somers 	struct iovec iov[2];
380df53360cSBrian Somers 	struct stamp stamp;
381df53360cSBrian Somers 	struct timeval tv;
382df53360cSBrian Somers 
383df53360cSBrian Somers 	(void)gettimeofday(&tv, NULL);
384df53360cSBrian Somers 	stamp.scr_len = cc;
385df53360cSBrian Somers 	stamp.scr_sec = tv.tv_sec;
386df53360cSBrian Somers 	stamp.scr_usec = tv.tv_usec;
387df53360cSBrian Somers 	stamp.scr_direction = direction;
388df53360cSBrian Somers 	iov[0].iov_len = sizeof(stamp);
389df53360cSBrian Somers 	iov[0].iov_base = &stamp;
390df53360cSBrian Somers 	iov[1].iov_len = cc;
391df53360cSBrian Somers 	iov[1].iov_base = buf;
392df53360cSBrian Somers 	if (writev(fileno(fp), &iov[0], 2) == -1)
393df53360cSBrian Somers 		err(1, "writev");
394df53360cSBrian Somers }
395df53360cSBrian Somers 
396df53360cSBrian Somers static void
397df53360cSBrian Somers consume(FILE *fp, off_t len, char *buf, int reg)
398df53360cSBrian Somers {
399df53360cSBrian Somers 	size_t l;
400df53360cSBrian Somers 
401df53360cSBrian Somers 	if (reg) {
402df53360cSBrian Somers 		if (fseeko(fp, len, SEEK_CUR) == -1)
403df53360cSBrian Somers 			err(1, NULL);
404df53360cSBrian Somers 	}
405df53360cSBrian Somers 	else {
406df53360cSBrian Somers 		while (len > 0) {
407df53360cSBrian Somers 			l = MIN(DEF_BUF, len);
408df53360cSBrian Somers 			if (fread(buf, sizeof(char), l, fp) != l)
409df53360cSBrian Somers 				err(1, "cannot read buffer");
410df53360cSBrian Somers 			len -= l;
411df53360cSBrian Somers 		}
412df53360cSBrian Somers 	}
413df53360cSBrian Somers }
414df53360cSBrian Somers 
415df53360cSBrian Somers #define swapstamp(stamp) do { \
416df53360cSBrian Somers 	if (stamp.scr_direction > 0xff) { \
417df53360cSBrian Somers 		stamp.scr_len = bswap64(stamp.scr_len); \
418df53360cSBrian Somers 		stamp.scr_sec = bswap64(stamp.scr_sec); \
419df53360cSBrian Somers 		stamp.scr_usec = bswap32(stamp.scr_usec); \
420df53360cSBrian Somers 		stamp.scr_direction = bswap32(stamp.scr_direction); \
421df53360cSBrian Somers 	} \
422df53360cSBrian Somers } while (0/*CONSTCOND*/)
423df53360cSBrian Somers 
424df53360cSBrian Somers static void
425df53360cSBrian Somers playback(FILE *fp)
426df53360cSBrian Somers {
427df53360cSBrian Somers 	struct timespec tsi, tso;
428df53360cSBrian Somers 	struct stamp stamp;
429df53360cSBrian Somers 	struct stat pst;
430df53360cSBrian Somers 	char buf[DEF_BUF];
431df53360cSBrian Somers 	off_t nread, save_len;
432df53360cSBrian Somers 	size_t l;
433df53360cSBrian Somers 	time_t tclock;
434df53360cSBrian Somers 	int reg;
435df53360cSBrian Somers 
436df53360cSBrian Somers 	if (fstat(fileno(fp), &pst) == -1)
437df53360cSBrian Somers 		err(1, "fstat failed");
438df53360cSBrian Somers 
439df53360cSBrian Somers 	reg = S_ISREG(pst.st_mode);
440df53360cSBrian Somers 
441df53360cSBrian Somers 	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
442df53360cSBrian Somers 		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
443df53360cSBrian Somers 			if (reg)
444df53360cSBrian Somers 				err(1, "reading playback header");
445df53360cSBrian Somers 			else
446df53360cSBrian Somers 				break;
447df53360cSBrian Somers 		}
448df53360cSBrian Somers 		swapstamp(stamp);
449df53360cSBrian Somers 		save_len = sizeof(stamp);
450df53360cSBrian Somers 
451df53360cSBrian Somers 		if (reg && stamp.scr_len >
452df53360cSBrian Somers 		    (uint64_t)(pst.st_size - save_len) - nread)
453df53360cSBrian Somers 			errx(1, "invalid stamp");
454df53360cSBrian Somers 
455df53360cSBrian Somers 		save_len += stamp.scr_len;
456df53360cSBrian Somers 		tclock = stamp.scr_sec;
457df53360cSBrian Somers 		tso.tv_sec = stamp.scr_sec;
458df53360cSBrian Somers 		tso.tv_nsec = stamp.scr_usec * 1000;
459df53360cSBrian Somers 
460df53360cSBrian Somers 		switch (stamp.scr_direction) {
461df53360cSBrian Somers 		case 's':
462df53360cSBrian Somers 			if (!qflg)
463df53360cSBrian Somers 			    (void)printf("Script started on %s",
464df53360cSBrian Somers 				ctime(&tclock));
465df53360cSBrian Somers 			tsi = tso;
466df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
467df53360cSBrian Somers 			break;
468df53360cSBrian Somers 		case 'e':
469df53360cSBrian Somers 			if (!qflg)
470df53360cSBrian Somers 				(void)printf("\nScript done on %s",
471df53360cSBrian Somers 				    ctime(&tclock));
472df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
473df53360cSBrian Somers 			break;
474df53360cSBrian Somers 		case 'i':
475df53360cSBrian Somers 			/* throw input away */
476df53360cSBrian Somers 			(void)consume(fp, stamp.scr_len, buf, reg);
477df53360cSBrian Somers 			break;
478df53360cSBrian Somers 		case 'o':
479df53360cSBrian Somers 			tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
480df53360cSBrian Somers 			tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
481df53360cSBrian Somers 			if (tsi.tv_nsec < 0) {
482df53360cSBrian Somers 				tsi.tv_sec -= 1;
483df53360cSBrian Somers 				tsi.tv_nsec += 1000000000;
484df53360cSBrian Somers 			}
485df53360cSBrian Somers 			if (usesleep)
486df53360cSBrian Somers 				(void)nanosleep(&tsi, NULL);
487df53360cSBrian Somers 			tsi = tso;
488df53360cSBrian Somers 			while (stamp.scr_len > 0) {
489df53360cSBrian Somers 				l = MIN(DEF_BUF, stamp.scr_len);
490df53360cSBrian Somers 				if (fread(buf, sizeof(char), l, fp) != l)
491df53360cSBrian Somers 					err(1, "cannot read buffer");
492df53360cSBrian Somers 
493df53360cSBrian Somers 				(void)write(STDOUT_FILENO, buf, l);
494df53360cSBrian Somers 				stamp.scr_len -= l;
495df53360cSBrian Somers 			}
496df53360cSBrian Somers 			break;
497df53360cSBrian Somers 		default:
498df53360cSBrian Somers 			errx(1, "invalid direction");
499df53360cSBrian Somers 		}
500df53360cSBrian Somers 	}
501df53360cSBrian Somers 	(void)fclose(fp);
502df53360cSBrian Somers 	exit(0);
503df53360cSBrian Somers }
504