19b50d902SRodney W. Grimes /*
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
46cff4e07SDavid E. O'Brien * Copyright (c) 2010, 2012 David E. O'Brien
59b50d902SRodney W. Grimes * Copyright (c) 1980, 1992, 1993
69b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
79b50d902SRodney W. Grimes *
89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes * are met:
119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
16fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes * without specific prior written permission.
199b50d902SRodney W. Grimes *
209b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes * SUCH DAMAGE.
319b50d902SRodney W. Grimes */
329b50d902SRodney W. Grimes
3313f02f28SDavid E. O'Brien #include <sys/param.h>
34d20f95e1SMark Murray __FBSDID("$FreeBSD$");
359b50d902SRodney W. Grimes #ifndef lint
36236d2f55SPhilippe Charnier static const char copyright[] =
379b50d902SRodney W. Grimes "@(#) Copyright (c) 1980, 1992, 1993\n\
389b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n";
39d20f95e1SMark Murray #endif
409b50d902SRodney W. Grimes #ifndef lint
41d20f95e1SMark Murray static const char sccsid[] = "@(#)script.c 8.1 (Berkeley) 6/6/93";
42236d2f55SPhilippe Charnier #endif
439b50d902SRodney W. Grimes
449b50d902SRodney W. Grimes #include <sys/wait.h>
459b50d902SRodney W. Grimes #include <sys/stat.h>
469b50d902SRodney W. Grimes #include <sys/ioctl.h>
479b50d902SRodney W. Grimes #include <sys/time.h>
48df53360cSBrian Somers #include <sys/uio.h>
49df53360cSBrian Somers #include <sys/endian.h>
506cff4e07SDavid E. O'Brien #include <dev/filemon/filemon.h>
519b50d902SRodney W. Grimes
52236d2f55SPhilippe Charnier #include <err.h>
53e8eb82a8SPeter Wemm #include <errno.h>
549b50d902SRodney W. Grimes #include <fcntl.h>
55236d2f55SPhilippe Charnier #include <libutil.h>
569b50d902SRodney W. Grimes #include <paths.h>
579b50d902SRodney W. Grimes #include <signal.h>
589b50d902SRodney W. Grimes #include <stdio.h>
599b50d902SRodney W. Grimes #include <stdlib.h>
609b50d902SRodney W. Grimes #include <string.h>
619b50d902SRodney W. Grimes #include <termios.h>
629b50d902SRodney W. Grimes #include <unistd.h>
639b50d902SRodney W. Grimes
64df53360cSBrian Somers #define DEF_BUF 65536
65df53360cSBrian Somers
66df53360cSBrian Somers struct stamp {
67df53360cSBrian Somers uint64_t scr_len; /* amount of data */
68df53360cSBrian Somers uint64_t scr_sec; /* time it arrived in seconds... */
69df53360cSBrian Somers uint32_t scr_usec; /* ...and microseconds */
70df53360cSBrian Somers uint32_t scr_direction; /* 'i', 'o', etc (also indicates endianness) */
71df53360cSBrian Somers };
72df53360cSBrian Somers
73e8efeec6SEd Schouten static FILE *fscript;
74e8efeec6SEd Schouten static int master, slave;
75e8efeec6SEd Schouten static int child;
76e8efeec6SEd Schouten static const char *fname;
776cff4e07SDavid E. O'Brien static char *fmfname;
786cff4e07SDavid E. O'Brien static int fflg, qflg, ttyflg;
7966d93438SBryan Drewery static int usesleep, rawout, showexit;
809b50d902SRodney W. Grimes
81e8efeec6SEd Schouten static struct termios tt;
829b50d902SRodney W. Grimes
83e8efeec6SEd Schouten static void done(int) __dead2;
84e8efeec6SEd Schouten static void doshell(char **);
85e8efeec6SEd Schouten static void finish(void);
86df53360cSBrian Somers static void record(FILE *, char *, size_t, int);
87df53360cSBrian Somers static void consume(FILE *, off_t, char *, int);
88df53360cSBrian Somers static void playback(FILE *) __dead2;
893f330d7dSWarner Losh static void usage(void);
909b50d902SRodney W. Grimes
919b50d902SRodney W. Grimes int
main(int argc,char * argv[])92f4ac32deSDavid Malone main(int argc, char *argv[])
939b50d902SRodney W. Grimes {
94d20f95e1SMark Murray int cc;
95e8eb82a8SPeter Wemm struct termios rtt, stt;
969b50d902SRodney W. Grimes struct winsize win;
97e8eb82a8SPeter Wemm struct timeval tv, *tvp;
98e8eb82a8SPeter Wemm time_t tvec, start;
99e8eb82a8SPeter Wemm char obuf[BUFSIZ];
1009b50d902SRodney W. Grimes char ibuf[BUFSIZ];
101e8eb82a8SPeter Wemm fd_set rfd;
1028d105abcSTom Rhodes int aflg, Fflg, kflg, pflg, ch, k, n;
1036cff4e07SDavid E. O'Brien int flushtime, readstdin;
1046cff4e07SDavid E. O'Brien int fm_fd, fm_log;
1059b50d902SRodney W. Grimes
1068d105abcSTom Rhodes aflg = Fflg = kflg = pflg = 0;
107df53360cSBrian Somers usesleep = 1;
108df53360cSBrian Somers rawout = 0;
1096cff4e07SDavid E. O'Brien flushtime = 30;
1106cff4e07SDavid E. O'Brien fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC
1116cff4e07SDavid E. O'Brien warning. (not needed w/clang) */
11266d93438SBryan Drewery showexit = 0;
113df53360cSBrian Somers
1148d105abcSTom Rhodes while ((ch = getopt(argc, argv, "adFfkpqrt:")) != -1)
1159b50d902SRodney W. Grimes switch(ch) {
1169b50d902SRodney W. Grimes case 'a':
1179b50d902SRodney W. Grimes aflg = 1;
1189b50d902SRodney W. Grimes break;
119df53360cSBrian Somers case 'd':
120df53360cSBrian Somers usesleep = 0;
12151afb8dfSPeter Wemm break;
1228d105abcSTom Rhodes case 'F':
1238d105abcSTom Rhodes Fflg = 1;
1248d105abcSTom Rhodes break;
1256cff4e07SDavid E. O'Brien case 'f':
1266cff4e07SDavid E. O'Brien fflg = 1;
1276cff4e07SDavid E. O'Brien break;
128e8eb82a8SPeter Wemm case 'k':
129e8eb82a8SPeter Wemm kflg = 1;
130e8eb82a8SPeter Wemm break;
131df53360cSBrian Somers case 'p':
132df53360cSBrian Somers pflg = 1;
133df53360cSBrian Somers break;
134df53360cSBrian Somers case 'q':
135df53360cSBrian Somers qflg = 1;
136df53360cSBrian Somers break;
137df53360cSBrian Somers case 'r':
138df53360cSBrian Somers rawout = 1;
139df53360cSBrian Somers break;
140e8eb82a8SPeter Wemm case 't':
141e8eb82a8SPeter Wemm flushtime = atoi(optarg);
142e8eb82a8SPeter Wemm if (flushtime < 0)
143e8eb82a8SPeter Wemm err(1, "invalid flush time %d", flushtime);
144e8eb82a8SPeter Wemm break;
1459b50d902SRodney W. Grimes case '?':
1469b50d902SRodney W. Grimes default:
147236d2f55SPhilippe Charnier usage();
1489b50d902SRodney W. Grimes }
1499b50d902SRodney W. Grimes argc -= optind;
1509b50d902SRodney W. Grimes argv += optind;
1519b50d902SRodney W. Grimes
15251afb8dfSPeter Wemm if (argc > 0) {
1539b50d902SRodney W. Grimes fname = argv[0];
15451afb8dfSPeter Wemm argv++;
15551afb8dfSPeter Wemm argc--;
15651afb8dfSPeter Wemm } else
1579b50d902SRodney W. Grimes fname = "typescript";
1589b50d902SRodney W. Grimes
159df53360cSBrian Somers if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
160236d2f55SPhilippe Charnier err(1, "%s", fname);
1619b50d902SRodney W. Grimes
1626cff4e07SDavid E. O'Brien if (fflg) {
1636cff4e07SDavid E. O'Brien asprintf(&fmfname, "%s.filemon", fname);
1646cff4e07SDavid E. O'Brien if (!fmfname)
1656cff4e07SDavid E. O'Brien err(1, "%s.filemon", fname);
16626e736b2SBaptiste Daroussin if ((fm_fd = open("/dev/filemon", O_RDWR | O_CLOEXEC)) == -1)
1676cff4e07SDavid E. O'Brien err(1, "open(\"/dev/filemon\", O_RDWR)");
16826e736b2SBaptiste Daroussin if ((fm_log = open(fmfname,
16926e736b2SBaptiste Daroussin O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
1706cff4e07SDavid E. O'Brien S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
1716cff4e07SDavid E. O'Brien err(1, "open(%s)", fmfname);
1726cff4e07SDavid E. O'Brien if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0)
1736cff4e07SDavid E. O'Brien err(1, "Cannot set filemon log file descriptor");
1746cff4e07SDavid E. O'Brien }
1756cff4e07SDavid E. O'Brien
176df53360cSBrian Somers if (pflg)
177df53360cSBrian Somers playback(fscript);
178df53360cSBrian Somers
179d93708c3SEd Schouten if ((ttyflg = isatty(STDIN_FILENO)) != 0) {
180e292a0e3SColin Percival if (tcgetattr(STDIN_FILENO, &tt) == -1)
181e292a0e3SColin Percival err(1, "tcgetattr");
182e292a0e3SColin Percival if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1)
183e292a0e3SColin Percival err(1, "ioctl");
1849b50d902SRodney W. Grimes if (openpty(&master, &slave, NULL, &tt, &win) == -1)
185236d2f55SPhilippe Charnier err(1, "openpty");
186e292a0e3SColin Percival } else {
187e292a0e3SColin Percival if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
188e292a0e3SColin Percival err(1, "openpty");
189e292a0e3SColin Percival }
1909b50d902SRodney W. Grimes
191df53360cSBrian Somers if (rawout)
192df53360cSBrian Somers record(fscript, NULL, 0, 's');
193df53360cSBrian Somers
194e8eb82a8SPeter Wemm if (!qflg) {
195e8eb82a8SPeter Wemm tvec = time(NULL);
1969b50d902SRodney W. Grimes (void)printf("Script started, output file is %s\n", fname);
197df53360cSBrian Somers if (!rawout) {
198df53360cSBrian Somers (void)fprintf(fscript, "Script started on %s",
199df53360cSBrian Somers ctime(&tvec));
200df53360cSBrian Somers if (argv[0]) {
20166d93438SBryan Drewery showexit = 1;
20266d93438SBryan Drewery 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 }
2277b245cb8SBryan Drewery if (child == 0) {
2287b245cb8SBryan Drewery if (fflg) {
2297b245cb8SBryan Drewery int pid;
2309b50d902SRodney W. Grimes
2317b245cb8SBryan Drewery pid = getpid();
2327b245cb8SBryan Drewery if (ioctl(fm_fd, FILEMON_SET_PID, &pid) < 0)
2336cff4e07SDavid E. O'Brien err(1, "Cannot set filemon PID");
2347b245cb8SBryan Drewery }
2357b245cb8SBryan Drewery
2367b245cb8SBryan Drewery doshell(argv);
2377b245cb8SBryan Drewery }
2387b245cb8SBryan Drewery close(slave);
2396cff4e07SDavid E. O'Brien
24029da7547SMikolaj Golub start = tvec = time(0);
24129da7547SMikolaj Golub readstdin = 1;
242e8eb82a8SPeter Wemm for (;;) {
24329da7547SMikolaj Golub FD_ZERO(&rfd);
244e8eb82a8SPeter Wemm FD_SET(master, &rfd);
24529da7547SMikolaj Golub if (readstdin)
246e8eb82a8SPeter Wemm FD_SET(STDIN_FILENO, &rfd);
24729f4384aSMikolaj Golub if (!readstdin && ttyflg) {
24829f4384aSMikolaj Golub tv.tv_sec = 1;
249e8eb82a8SPeter Wemm tv.tv_usec = 0;
25029da7547SMikolaj Golub tvp = &tv;
25129da7547SMikolaj Golub readstdin = 1;
25229f4384aSMikolaj Golub } else if (flushtime > 0) {
25329f4384aSMikolaj Golub tv.tv_sec = flushtime - (tvec - start);
25429f4384aSMikolaj Golub tv.tv_usec = 0;
25529f4384aSMikolaj Golub tvp = &tv;
25629da7547SMikolaj Golub } else {
25729da7547SMikolaj Golub tvp = NULL;
258e8eb82a8SPeter Wemm }
259e8eb82a8SPeter Wemm n = select(master + 1, &rfd, 0, 0, tvp);
260e8eb82a8SPeter Wemm if (n < 0 && errno != EINTR)
261e8eb82a8SPeter Wemm break;
262e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) {
263e8eb82a8SPeter Wemm cc = read(STDIN_FILENO, ibuf, BUFSIZ);
264d6a68195SColin Percival if (cc < 0)
265e8eb82a8SPeter Wemm break;
26629da7547SMikolaj Golub if (cc == 0) {
26729da7547SMikolaj Golub if (tcgetattr(master, &stt) == 0 &&
26829da7547SMikolaj Golub (stt.c_lflag & ICANON) != 0) {
26929da7547SMikolaj Golub (void)write(master, &stt.c_cc[VEOF], 1);
27029da7547SMikolaj Golub }
27129da7547SMikolaj Golub readstdin = 0;
27229da7547SMikolaj Golub }
273e8eb82a8SPeter Wemm if (cc > 0) {
274df53360cSBrian Somers if (rawout)
275df53360cSBrian Somers record(fscript, ibuf, cc, 'i');
2769b50d902SRodney W. Grimes (void)write(master, ibuf, cc);
277e8eb82a8SPeter Wemm if (kflg && tcgetattr(master, &stt) >= 0 &&
278e8eb82a8SPeter Wemm ((stt.c_lflag & ECHO) == 0)) {
279e8eb82a8SPeter Wemm (void)fwrite(ibuf, 1, cc, fscript);
280e8eb82a8SPeter Wemm }
281e8eb82a8SPeter Wemm }
282e8eb82a8SPeter Wemm }
283e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(master, &rfd)) {
284e8eb82a8SPeter Wemm cc = read(master, obuf, sizeof (obuf));
285e8eb82a8SPeter Wemm if (cc <= 0)
286e8eb82a8SPeter Wemm break;
287e1b4d8d0SSheldon Hearn (void)write(STDOUT_FILENO, obuf, cc);
288df53360cSBrian Somers if (rawout)
289df53360cSBrian Somers record(fscript, obuf, cc, 'o');
290df53360cSBrian Somers else
291e8eb82a8SPeter Wemm (void)fwrite(obuf, 1, cc, fscript);
292e8eb82a8SPeter Wemm }
293e8eb82a8SPeter Wemm tvec = time(0);
294e8eb82a8SPeter Wemm if (tvec - start >= flushtime) {
295e8eb82a8SPeter Wemm fflush(fscript);
296e8eb82a8SPeter Wemm start = tvec;
297e8eb82a8SPeter Wemm }
2988d105abcSTom Rhodes if (Fflg)
2998d105abcSTom Rhodes fflush(fscript);
300e8eb82a8SPeter Wemm }
301b1c66970SDag-Erling Smørgrav finish();
302b1c66970SDag-Erling Smørgrav done(0);
3039b50d902SRodney W. Grimes }
3049b50d902SRodney W. Grimes
305236d2f55SPhilippe Charnier static void
usage(void)306f4ac32deSDavid Malone usage(void)
307236d2f55SPhilippe Charnier {
308e8eb82a8SPeter Wemm (void)fprintf(stderr,
3096cff4e07SDavid E. O'Brien "usage: script [-adfkpqr] [-t time] [file [command ...]]\n");
310236d2f55SPhilippe Charnier exit(1);
311236d2f55SPhilippe Charnier }
312236d2f55SPhilippe Charnier
313e8efeec6SEd Schouten static void
finish(void)314f4ac32deSDavid Malone finish(void)
3159b50d902SRodney W. Grimes {
316e1e9ba33SEd Schouten int e, status;
3179b50d902SRodney W. Grimes
318e1e9ba33SEd Schouten if (waitpid(child, &status, 0) == child) {
319b1c66970SDag-Erling Smørgrav if (WIFEXITED(status))
320b1c66970SDag-Erling Smørgrav e = WEXITSTATUS(status);
321b1c66970SDag-Erling Smørgrav else if (WIFSIGNALED(status))
322b1c66970SDag-Erling Smørgrav e = WTERMSIG(status);
323b1c66970SDag-Erling Smørgrav else /* can't happen */
324b1c66970SDag-Erling Smørgrav e = 1;
325b1c66970SDag-Erling Smørgrav done(e);
3269b50d902SRodney W. Grimes }
327e1e9ba33SEd Schouten }
3289b50d902SRodney W. Grimes
329e8efeec6SEd Schouten static void
doshell(char ** av)330f4ac32deSDavid Malone doshell(char **av)
3319b50d902SRodney W. Grimes {
332b139689cSDavid Malone const char *shell;
3339b50d902SRodney W. Grimes
3349b50d902SRodney W. Grimes shell = getenv("SHELL");
3359b50d902SRodney W. Grimes if (shell == NULL)
3369b50d902SRodney W. Grimes shell = _PATH_BSHELL;
3379b50d902SRodney W. Grimes
3389b50d902SRodney W. Grimes (void)close(master);
3399b50d902SRodney W. Grimes (void)fclose(fscript);
3406cff4e07SDavid E. O'Brien free(fmfname);
3419b50d902SRodney W. Grimes login_tty(slave);
3429b91846cSDavid E. O'Brien setenv("SCRIPT", fname, 1);
343b1c66970SDag-Erling Smørgrav if (av[0]) {
3440e604e98SPeter Wemm execvp(av[0], av);
345b7ffba17SKris Kennaway warn("%s", av[0]);
346b1c66970SDag-Erling Smørgrav } else {
3477bc6d015SBrian Somers execl(shell, shell, "-i", (char *)NULL);
348b7ffba17SKris Kennaway warn("%s", shell);
349b1c66970SDag-Erling Smørgrav }
35095d13d1bSBryan Drewery exit(1);
3519b50d902SRodney W. Grimes }
3529b50d902SRodney W. Grimes
353e8efeec6SEd Schouten static void
done(int eno)354f4ac32deSDavid Malone done(int eno)
3559b50d902SRodney W. Grimes {
3569b50d902SRodney W. Grimes time_t tvec;
3579b50d902SRodney W. Grimes
358e292a0e3SColin Percival if (ttyflg)
359e8eb82a8SPeter Wemm (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
3609b50d902SRodney W. Grimes tvec = time(NULL);
361df53360cSBrian Somers if (rawout)
362df53360cSBrian Somers record(fscript, NULL, 0, 'e');
363e8eb82a8SPeter Wemm if (!qflg) {
36466d93438SBryan Drewery if (!rawout) {
36566d93438SBryan Drewery if (showexit)
36666d93438SBryan Drewery (void)fprintf(fscript, "\nCommand exit status:"
36766d93438SBryan Drewery " %d", eno);
368df53360cSBrian Somers (void)fprintf(fscript,"\nScript done on %s",
369df53360cSBrian Somers ctime(&tvec));
37066d93438SBryan Drewery }
371e8eb82a8SPeter Wemm (void)printf("\nScript done, output file is %s\n", fname);
3726cff4e07SDavid E. O'Brien if (fflg) {
3736cff4e07SDavid E. O'Brien (void)printf("Filemon done, output file is %s\n",
3746cff4e07SDavid E. O'Brien fmfname);
3756cff4e07SDavid E. O'Brien }
376e8eb82a8SPeter Wemm }
3779b50d902SRodney W. Grimes (void)fclose(fscript);
3789b50d902SRodney W. Grimes (void)close(master);
379b1c66970SDag-Erling Smørgrav exit(eno);
3809b50d902SRodney W. Grimes }
381df53360cSBrian Somers
382df53360cSBrian Somers static void
record(FILE * fp,char * buf,size_t cc,int direction)383df53360cSBrian Somers record(FILE *fp, char *buf, size_t cc, int direction)
384df53360cSBrian Somers {
385df53360cSBrian Somers struct iovec iov[2];
386df53360cSBrian Somers struct stamp stamp;
387df53360cSBrian Somers struct timeval tv;
388df53360cSBrian Somers
389df53360cSBrian Somers (void)gettimeofday(&tv, NULL);
390df53360cSBrian Somers stamp.scr_len = cc;
391df53360cSBrian Somers stamp.scr_sec = tv.tv_sec;
392df53360cSBrian Somers stamp.scr_usec = tv.tv_usec;
393df53360cSBrian Somers stamp.scr_direction = direction;
394df53360cSBrian Somers iov[0].iov_len = sizeof(stamp);
395df53360cSBrian Somers iov[0].iov_base = &stamp;
396df53360cSBrian Somers iov[1].iov_len = cc;
397df53360cSBrian Somers iov[1].iov_base = buf;
398df53360cSBrian Somers if (writev(fileno(fp), &iov[0], 2) == -1)
399df53360cSBrian Somers err(1, "writev");
400df53360cSBrian Somers }
401df53360cSBrian Somers
402df53360cSBrian Somers static void
consume(FILE * fp,off_t len,char * buf,int reg)403df53360cSBrian Somers consume(FILE *fp, off_t len, char *buf, int reg)
404df53360cSBrian Somers {
405df53360cSBrian Somers size_t l;
406df53360cSBrian Somers
407df53360cSBrian Somers if (reg) {
408df53360cSBrian Somers if (fseeko(fp, len, SEEK_CUR) == -1)
409df53360cSBrian Somers err(1, NULL);
410df53360cSBrian Somers }
411df53360cSBrian Somers else {
412df53360cSBrian Somers while (len > 0) {
413df53360cSBrian Somers l = MIN(DEF_BUF, len);
414df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l)
415df53360cSBrian Somers err(1, "cannot read buffer");
416df53360cSBrian Somers len -= l;
417df53360cSBrian Somers }
418df53360cSBrian Somers }
419df53360cSBrian Somers }
420df53360cSBrian Somers
421df53360cSBrian Somers #define swapstamp(stamp) do { \
422df53360cSBrian Somers if (stamp.scr_direction > 0xff) { \
423df53360cSBrian Somers stamp.scr_len = bswap64(stamp.scr_len); \
424df53360cSBrian Somers stamp.scr_sec = bswap64(stamp.scr_sec); \
425df53360cSBrian Somers stamp.scr_usec = bswap32(stamp.scr_usec); \
426df53360cSBrian Somers stamp.scr_direction = bswap32(stamp.scr_direction); \
427df53360cSBrian Somers } \
428df53360cSBrian Somers } while (0/*CONSTCOND*/)
429df53360cSBrian Somers
430df53360cSBrian Somers static void
playback(FILE * fp)431df53360cSBrian Somers playback(FILE *fp)
432df53360cSBrian Somers {
433df53360cSBrian Somers struct timespec tsi, tso;
434df53360cSBrian Somers struct stamp stamp;
435df53360cSBrian Somers struct stat pst;
436df53360cSBrian Somers char buf[DEF_BUF];
437df53360cSBrian Somers off_t nread, save_len;
438df53360cSBrian Somers size_t l;
439df53360cSBrian Somers time_t tclock;
440df53360cSBrian Somers int reg;
441df53360cSBrian Somers
442df53360cSBrian Somers if (fstat(fileno(fp), &pst) == -1)
443df53360cSBrian Somers err(1, "fstat failed");
444df53360cSBrian Somers
445df53360cSBrian Somers reg = S_ISREG(pst.st_mode);
446df53360cSBrian Somers
447df53360cSBrian Somers for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
448df53360cSBrian Somers if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
449df53360cSBrian Somers if (reg)
450df53360cSBrian Somers err(1, "reading playback header");
451df53360cSBrian Somers else
452df53360cSBrian Somers break;
453df53360cSBrian Somers }
454df53360cSBrian Somers swapstamp(stamp);
455df53360cSBrian Somers save_len = sizeof(stamp);
456df53360cSBrian Somers
457df53360cSBrian Somers if (reg && stamp.scr_len >
458df53360cSBrian Somers (uint64_t)(pst.st_size - save_len) - nread)
459df53360cSBrian Somers errx(1, "invalid stamp");
460df53360cSBrian Somers
461df53360cSBrian Somers save_len += stamp.scr_len;
462df53360cSBrian Somers tclock = stamp.scr_sec;
463df53360cSBrian Somers tso.tv_sec = stamp.scr_sec;
464df53360cSBrian Somers tso.tv_nsec = stamp.scr_usec * 1000;
465df53360cSBrian Somers
466df53360cSBrian Somers switch (stamp.scr_direction) {
467df53360cSBrian Somers case 's':
468df53360cSBrian Somers if (!qflg)
469df53360cSBrian Somers (void)printf("Script started on %s",
470df53360cSBrian Somers ctime(&tclock));
471df53360cSBrian Somers tsi = tso;
472df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg);
473df53360cSBrian Somers break;
474df53360cSBrian Somers case 'e':
475df53360cSBrian Somers if (!qflg)
476df53360cSBrian Somers (void)printf("\nScript done on %s",
477df53360cSBrian Somers ctime(&tclock));
478df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg);
479df53360cSBrian Somers break;
480df53360cSBrian Somers case 'i':
481df53360cSBrian Somers /* throw input away */
482df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg);
483df53360cSBrian Somers break;
484df53360cSBrian Somers case 'o':
485df53360cSBrian Somers tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
486df53360cSBrian Somers tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
487df53360cSBrian Somers if (tsi.tv_nsec < 0) {
488df53360cSBrian Somers tsi.tv_sec -= 1;
489df53360cSBrian Somers tsi.tv_nsec += 1000000000;
490df53360cSBrian Somers }
491df53360cSBrian Somers if (usesleep)
492df53360cSBrian Somers (void)nanosleep(&tsi, NULL);
493df53360cSBrian Somers tsi = tso;
494df53360cSBrian Somers while (stamp.scr_len > 0) {
495df53360cSBrian Somers l = MIN(DEF_BUF, stamp.scr_len);
496df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l)
497df53360cSBrian Somers err(1, "cannot read buffer");
498df53360cSBrian Somers
499df53360cSBrian Somers (void)write(STDOUT_FILENO, buf, l);
500df53360cSBrian Somers stamp.scr_len -= l;
501df53360cSBrian Somers }
502df53360cSBrian Somers break;
503df53360cSBrian Somers default:
504df53360cSBrian Somers errx(1, "invalid direction");
505df53360cSBrian Somers }
506df53360cSBrian Somers }
507df53360cSBrian Somers (void)fclose(fp);
508df53360cSBrian Somers exit(0);
509df53360cSBrian Somers }
510