19b50d902SRodney W. Grimes /* 2*6cff4e07SDavid 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> 48*6cff4e07SDavid 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; 75*6cff4e07SDavid E. O'Brien static char *fmfname; 76*6cff4e07SDavid 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*6cff4e07SDavid E. O'Brien int aflg, kflg, pflg, ch, k, n; 102*6cff4e07SDavid E. O'Brien int flushtime, readstdin; 103*6cff4e07SDavid E. O'Brien int fm_fd, fm_log; 1049b50d902SRodney W. Grimes 105df53360cSBrian Somers aflg = kflg = pflg = 0; 106df53360cSBrian Somers usesleep = 1; 107df53360cSBrian Somers rawout = 0; 108*6cff4e07SDavid E. O'Brien flushtime = 30; 109*6cff4e07SDavid E. O'Brien fm_fd = -1; /* Shut up stupid "may be used uninitialized" GCC 110*6cff4e07SDavid E. O'Brien warning. (not needed w/clang) */ 111df53360cSBrian Somers 112*6cff4e07SDavid E. O'Brien while ((ch = getopt(argc, argv, "adfkpqrt:")) != -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*6cff4e07SDavid E. O'Brien case 'f': 121*6cff4e07SDavid E. O'Brien fflg = 1; 122*6cff4e07SDavid E. O'Brien break; 123e8eb82a8SPeter Wemm case 'k': 124e8eb82a8SPeter Wemm kflg = 1; 125e8eb82a8SPeter Wemm break; 126df53360cSBrian Somers case 'p': 127df53360cSBrian Somers pflg = 1; 128df53360cSBrian Somers break; 129df53360cSBrian Somers case 'q': 130df53360cSBrian Somers qflg = 1; 131df53360cSBrian Somers break; 132df53360cSBrian Somers case 'r': 133df53360cSBrian Somers rawout = 1; 134df53360cSBrian Somers break; 135e8eb82a8SPeter Wemm case 't': 136e8eb82a8SPeter Wemm flushtime = atoi(optarg); 137e8eb82a8SPeter Wemm if (flushtime < 0) 138e8eb82a8SPeter Wemm err(1, "invalid flush time %d", flushtime); 139e8eb82a8SPeter Wemm break; 1409b50d902SRodney W. Grimes case '?': 1419b50d902SRodney W. Grimes default: 142236d2f55SPhilippe Charnier usage(); 1439b50d902SRodney W. Grimes } 1449b50d902SRodney W. Grimes argc -= optind; 1459b50d902SRodney W. Grimes argv += optind; 1469b50d902SRodney W. Grimes 14751afb8dfSPeter Wemm if (argc > 0) { 1489b50d902SRodney W. Grimes fname = argv[0]; 14951afb8dfSPeter Wemm argv++; 15051afb8dfSPeter Wemm argc--; 15151afb8dfSPeter Wemm } else 1529b50d902SRodney W. Grimes fname = "typescript"; 1539b50d902SRodney W. Grimes 154df53360cSBrian Somers if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL) 155236d2f55SPhilippe Charnier err(1, "%s", fname); 1569b50d902SRodney W. Grimes 157*6cff4e07SDavid E. O'Brien if (fflg) { 158*6cff4e07SDavid E. O'Brien asprintf(&fmfname, "%s.filemon", fname); 159*6cff4e07SDavid E. O'Brien if (!fmfname) 160*6cff4e07SDavid E. O'Brien err(1, "%s.filemon", fname); 161*6cff4e07SDavid E. O'Brien if ((fm_fd = open("/dev/filemon", O_RDWR)) == -1) 162*6cff4e07SDavid E. O'Brien err(1, "open(\"/dev/filemon\", O_RDWR)"); 163*6cff4e07SDavid E. O'Brien if ((fm_log = open(fmfname, O_WRONLY | O_CREAT | O_TRUNC, 164*6cff4e07SDavid E. O'Brien S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1) 165*6cff4e07SDavid E. O'Brien err(1, "open(%s)", fmfname); 166*6cff4e07SDavid E. O'Brien if (ioctl(fm_fd, FILEMON_SET_FD, &fm_log) < 0) 167*6cff4e07SDavid E. O'Brien err(1, "Cannot set filemon log file descriptor"); 168*6cff4e07SDavid E. O'Brien 169*6cff4e07SDavid E. O'Brien /* Set up these two fd's to close on exec. */ 170*6cff4e07SDavid E. O'Brien (void)fcntl(fm_fd, F_SETFD, FD_CLOEXEC); 171*6cff4e07SDavid E. O'Brien (void)fcntl(fm_log, F_SETFD, FD_CLOEXEC); 172*6cff4e07SDavid E. O'Brien } 173*6cff4e07SDavid E. O'Brien 174df53360cSBrian Somers if (pflg) 175df53360cSBrian Somers playback(fscript); 176df53360cSBrian Somers 177d93708c3SEd Schouten if ((ttyflg = isatty(STDIN_FILENO)) != 0) { 178e292a0e3SColin Percival if (tcgetattr(STDIN_FILENO, &tt) == -1) 179e292a0e3SColin Percival err(1, "tcgetattr"); 180e292a0e3SColin Percival if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) 181e292a0e3SColin Percival err(1, "ioctl"); 1829b50d902SRodney W. Grimes if (openpty(&master, &slave, NULL, &tt, &win) == -1) 183236d2f55SPhilippe Charnier err(1, "openpty"); 184e292a0e3SColin Percival } else { 185e292a0e3SColin Percival if (openpty(&master, &slave, NULL, NULL, NULL) == -1) 186e292a0e3SColin Percival err(1, "openpty"); 187e292a0e3SColin Percival } 1889b50d902SRodney W. Grimes 189df53360cSBrian Somers if (rawout) 190df53360cSBrian Somers record(fscript, NULL, 0, 's'); 191df53360cSBrian Somers 192e8eb82a8SPeter Wemm if (!qflg) { 193e8eb82a8SPeter Wemm tvec = time(NULL); 1949b50d902SRodney W. Grimes (void)printf("Script started, output file is %s\n", fname); 195df53360cSBrian Somers if (!rawout) { 196df53360cSBrian Somers (void)fprintf(fscript, "Script started on %s", 197df53360cSBrian Somers ctime(&tvec)); 198df53360cSBrian Somers if (argv[0]) { 199df53360cSBrian Somers fprintf(fscript, "command: "); 200df53360cSBrian Somers for (k = 0 ; argv[k] ; ++k) 201df53360cSBrian Somers fprintf(fscript, "%s%s", k ? " " : "", 202df53360cSBrian Somers argv[k]); 203df53360cSBrian Somers fprintf(fscript, "\n"); 204df53360cSBrian Somers } 205df53360cSBrian Somers } 206e8eb82a8SPeter Wemm fflush(fscript); 207*6cff4e07SDavid E. O'Brien if (fflg) { 208*6cff4e07SDavid E. O'Brien (void)printf("Filemon started, output file is %s\n", 209*6cff4e07SDavid E. O'Brien fmfname); 210*6cff4e07SDavid E. O'Brien } 211e8eb82a8SPeter Wemm } 212e292a0e3SColin Percival if (ttyflg) { 2139b50d902SRodney W. Grimes rtt = tt; 2149b50d902SRodney W. Grimes cfmakeraw(&rtt); 2159b50d902SRodney W. Grimes rtt.c_lflag &= ~ECHO; 2169b50d902SRodney W. Grimes (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt); 217e292a0e3SColin Percival } 2189b50d902SRodney W. Grimes 2199b50d902SRodney W. Grimes child = fork(); 2209b50d902SRodney W. Grimes if (child < 0) { 221236d2f55SPhilippe Charnier warn("fork"); 222b1c66970SDag-Erling Smørgrav done(1); 2239b50d902SRodney W. Grimes } 224e8eb82a8SPeter Wemm if (child == 0) 22551afb8dfSPeter Wemm doshell(argv); 2268d5e4a14SEd Schouten close(slave); 2279b50d902SRodney W. Grimes 228*6cff4e07SDavid E. O'Brien if (fflg && ioctl(fm_fd, FILEMON_SET_PID, &child) < 0) 229*6cff4e07SDavid E. O'Brien err(1, "Cannot set filemon PID"); 230*6cff4e07SDavid E. O'Brien 23129da7547SMikolaj Golub start = tvec = time(0); 23229da7547SMikolaj Golub readstdin = 1; 233e8eb82a8SPeter Wemm for (;;) { 23429da7547SMikolaj Golub FD_ZERO(&rfd); 235e8eb82a8SPeter Wemm FD_SET(master, &rfd); 23629da7547SMikolaj Golub if (readstdin) 237e8eb82a8SPeter Wemm FD_SET(STDIN_FILENO, &rfd); 238df53360cSBrian Somers if ((!readstdin && ttyflg) || flushtime > 0) { 239df53360cSBrian Somers tv.tv_sec = !readstdin && ttyflg ? 1 : 240df53360cSBrian Somers flushtime - (tvec - start); 241e8eb82a8SPeter Wemm tv.tv_usec = 0; 24229da7547SMikolaj Golub tvp = &tv; 24329da7547SMikolaj Golub readstdin = 1; 24429da7547SMikolaj Golub } else { 24529da7547SMikolaj Golub tvp = NULL; 246e8eb82a8SPeter Wemm } 247e8eb82a8SPeter Wemm n = select(master + 1, &rfd, 0, 0, tvp); 248e8eb82a8SPeter Wemm if (n < 0 && errno != EINTR) 249e8eb82a8SPeter Wemm break; 250e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(STDIN_FILENO, &rfd)) { 251e8eb82a8SPeter Wemm cc = read(STDIN_FILENO, ibuf, BUFSIZ); 252d6a68195SColin Percival if (cc < 0) 253e8eb82a8SPeter Wemm break; 25429da7547SMikolaj Golub if (cc == 0) { 25529da7547SMikolaj Golub if (tcgetattr(master, &stt) == 0 && 25629da7547SMikolaj Golub (stt.c_lflag & ICANON) != 0) { 25729da7547SMikolaj Golub (void)write(master, &stt.c_cc[VEOF], 1); 25829da7547SMikolaj Golub } 25929da7547SMikolaj Golub readstdin = 0; 26029da7547SMikolaj Golub } 261e8eb82a8SPeter Wemm if (cc > 0) { 262df53360cSBrian Somers if (rawout) 263df53360cSBrian Somers record(fscript, ibuf, cc, 'i'); 2649b50d902SRodney W. Grimes (void)write(master, ibuf, cc); 265e8eb82a8SPeter Wemm if (kflg && tcgetattr(master, &stt) >= 0 && 266e8eb82a8SPeter Wemm ((stt.c_lflag & ECHO) == 0)) { 267e8eb82a8SPeter Wemm (void)fwrite(ibuf, 1, cc, fscript); 268e8eb82a8SPeter Wemm } 269e8eb82a8SPeter Wemm } 270e8eb82a8SPeter Wemm } 271e8eb82a8SPeter Wemm if (n > 0 && FD_ISSET(master, &rfd)) { 272e8eb82a8SPeter Wemm cc = read(master, obuf, sizeof (obuf)); 273e8eb82a8SPeter Wemm if (cc <= 0) 274e8eb82a8SPeter Wemm break; 275e1b4d8d0SSheldon Hearn (void)write(STDOUT_FILENO, obuf, cc); 276df53360cSBrian Somers if (rawout) 277df53360cSBrian Somers record(fscript, obuf, cc, 'o'); 278df53360cSBrian Somers else 279e8eb82a8SPeter Wemm (void)fwrite(obuf, 1, cc, fscript); 280e8eb82a8SPeter Wemm } 281e8eb82a8SPeter Wemm tvec = time(0); 282e8eb82a8SPeter Wemm if (tvec - start >= flushtime) { 283e8eb82a8SPeter Wemm fflush(fscript); 284e8eb82a8SPeter Wemm start = tvec; 285e8eb82a8SPeter Wemm } 286e8eb82a8SPeter Wemm } 287b1c66970SDag-Erling Smørgrav finish(); 288b1c66970SDag-Erling Smørgrav done(0); 2899b50d902SRodney W. Grimes } 2909b50d902SRodney W. Grimes 291236d2f55SPhilippe Charnier static void 292f4ac32deSDavid Malone usage(void) 293236d2f55SPhilippe Charnier { 294e8eb82a8SPeter Wemm (void)fprintf(stderr, 295*6cff4e07SDavid E. O'Brien "usage: script [-adfkpqr] [-t time] [file [command ...]]\n"); 296236d2f55SPhilippe Charnier exit(1); 297236d2f55SPhilippe Charnier } 298236d2f55SPhilippe Charnier 299e8efeec6SEd Schouten static void 300f4ac32deSDavid Malone finish(void) 3019b50d902SRodney W. Grimes { 302e1e9ba33SEd Schouten int e, status; 3039b50d902SRodney W. Grimes 304e1e9ba33SEd Schouten if (waitpid(child, &status, 0) == child) { 305b1c66970SDag-Erling Smørgrav if (WIFEXITED(status)) 306b1c66970SDag-Erling Smørgrav e = WEXITSTATUS(status); 307b1c66970SDag-Erling Smørgrav else if (WIFSIGNALED(status)) 308b1c66970SDag-Erling Smørgrav e = WTERMSIG(status); 309b1c66970SDag-Erling Smørgrav else /* can't happen */ 310b1c66970SDag-Erling Smørgrav e = 1; 311b1c66970SDag-Erling Smørgrav done(e); 3129b50d902SRodney W. Grimes } 313e1e9ba33SEd Schouten } 3149b50d902SRodney W. Grimes 315e8efeec6SEd Schouten static void 316f4ac32deSDavid Malone doshell(char **av) 3179b50d902SRodney W. Grimes { 318b139689cSDavid Malone const char *shell; 3199b50d902SRodney W. Grimes 3209b50d902SRodney W. Grimes shell = getenv("SHELL"); 3219b50d902SRodney W. Grimes if (shell == NULL) 3229b50d902SRodney W. Grimes shell = _PATH_BSHELL; 3239b50d902SRodney W. Grimes 3249b50d902SRodney W. Grimes (void)close(master); 3259b50d902SRodney W. Grimes (void)fclose(fscript); 326*6cff4e07SDavid E. O'Brien free(fmfname); 3279b50d902SRodney W. Grimes login_tty(slave); 3289b91846cSDavid E. O'Brien setenv("SCRIPT", fname, 1); 329b1c66970SDag-Erling Smørgrav if (av[0]) { 3300e604e98SPeter Wemm execvp(av[0], av); 331b7ffba17SKris Kennaway warn("%s", av[0]); 332b1c66970SDag-Erling Smørgrav } else { 3337bc6d015SBrian Somers execl(shell, shell, "-i", (char *)NULL); 334b7ffba17SKris Kennaway warn("%s", shell); 335b1c66970SDag-Erling Smørgrav } 3369b50d902SRodney W. Grimes fail(); 3379b50d902SRodney W. Grimes } 3389b50d902SRodney W. Grimes 339e8efeec6SEd Schouten static void 340f4ac32deSDavid Malone fail(void) 3419b50d902SRodney W. Grimes { 3429b50d902SRodney W. Grimes (void)kill(0, SIGTERM); 343b1c66970SDag-Erling Smørgrav done(1); 3449b50d902SRodney W. Grimes } 3459b50d902SRodney W. Grimes 346e8efeec6SEd Schouten static void 347f4ac32deSDavid Malone done(int eno) 3489b50d902SRodney W. Grimes { 3499b50d902SRodney W. Grimes time_t tvec; 3509b50d902SRodney W. Grimes 351e292a0e3SColin Percival if (ttyflg) 352e8eb82a8SPeter Wemm (void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt); 3539b50d902SRodney W. Grimes tvec = time(NULL); 354df53360cSBrian Somers if (rawout) 355df53360cSBrian Somers record(fscript, NULL, 0, 'e'); 356e8eb82a8SPeter Wemm if (!qflg) { 357df53360cSBrian Somers if (!rawout) 358df53360cSBrian Somers (void)fprintf(fscript,"\nScript done on %s", 359df53360cSBrian Somers ctime(&tvec)); 360e8eb82a8SPeter Wemm (void)printf("\nScript done, output file is %s\n", fname); 361*6cff4e07SDavid E. O'Brien if (fflg) { 362*6cff4e07SDavid E. O'Brien (void)printf("Filemon done, output file is %s\n", 363*6cff4e07SDavid E. O'Brien fmfname); 364*6cff4e07SDavid E. O'Brien } 365e8eb82a8SPeter Wemm } 3669b50d902SRodney W. Grimes (void)fclose(fscript); 3679b50d902SRodney W. Grimes (void)close(master); 368b1c66970SDag-Erling Smørgrav exit(eno); 3699b50d902SRodney W. Grimes } 370df53360cSBrian Somers 371df53360cSBrian Somers static void 372df53360cSBrian Somers record(FILE *fp, char *buf, size_t cc, int direction) 373df53360cSBrian Somers { 374df53360cSBrian Somers struct iovec iov[2]; 375df53360cSBrian Somers struct stamp stamp; 376df53360cSBrian Somers struct timeval tv; 377df53360cSBrian Somers 378df53360cSBrian Somers (void)gettimeofday(&tv, NULL); 379df53360cSBrian Somers stamp.scr_len = cc; 380df53360cSBrian Somers stamp.scr_sec = tv.tv_sec; 381df53360cSBrian Somers stamp.scr_usec = tv.tv_usec; 382df53360cSBrian Somers stamp.scr_direction = direction; 383df53360cSBrian Somers iov[0].iov_len = sizeof(stamp); 384df53360cSBrian Somers iov[0].iov_base = &stamp; 385df53360cSBrian Somers iov[1].iov_len = cc; 386df53360cSBrian Somers iov[1].iov_base = buf; 387df53360cSBrian Somers if (writev(fileno(fp), &iov[0], 2) == -1) 388df53360cSBrian Somers err(1, "writev"); 389df53360cSBrian Somers } 390df53360cSBrian Somers 391df53360cSBrian Somers static void 392df53360cSBrian Somers consume(FILE *fp, off_t len, char *buf, int reg) 393df53360cSBrian Somers { 394df53360cSBrian Somers size_t l; 395df53360cSBrian Somers 396df53360cSBrian Somers if (reg) { 397df53360cSBrian Somers if (fseeko(fp, len, SEEK_CUR) == -1) 398df53360cSBrian Somers err(1, NULL); 399df53360cSBrian Somers } 400df53360cSBrian Somers else { 401df53360cSBrian Somers while (len > 0) { 402df53360cSBrian Somers l = MIN(DEF_BUF, len); 403df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l) 404df53360cSBrian Somers err(1, "cannot read buffer"); 405df53360cSBrian Somers len -= l; 406df53360cSBrian Somers } 407df53360cSBrian Somers } 408df53360cSBrian Somers } 409df53360cSBrian Somers 410df53360cSBrian Somers #define swapstamp(stamp) do { \ 411df53360cSBrian Somers if (stamp.scr_direction > 0xff) { \ 412df53360cSBrian Somers stamp.scr_len = bswap64(stamp.scr_len); \ 413df53360cSBrian Somers stamp.scr_sec = bswap64(stamp.scr_sec); \ 414df53360cSBrian Somers stamp.scr_usec = bswap32(stamp.scr_usec); \ 415df53360cSBrian Somers stamp.scr_direction = bswap32(stamp.scr_direction); \ 416df53360cSBrian Somers } \ 417df53360cSBrian Somers } while (0/*CONSTCOND*/) 418df53360cSBrian Somers 419df53360cSBrian Somers static void 420df53360cSBrian Somers playback(FILE *fp) 421df53360cSBrian Somers { 422df53360cSBrian Somers struct timespec tsi, tso; 423df53360cSBrian Somers struct stamp stamp; 424df53360cSBrian Somers struct stat pst; 425df53360cSBrian Somers char buf[DEF_BUF]; 426df53360cSBrian Somers off_t nread, save_len; 427df53360cSBrian Somers size_t l; 428df53360cSBrian Somers time_t tclock; 429df53360cSBrian Somers int reg; 430df53360cSBrian Somers 431df53360cSBrian Somers if (fstat(fileno(fp), &pst) == -1) 432df53360cSBrian Somers err(1, "fstat failed"); 433df53360cSBrian Somers 434df53360cSBrian Somers reg = S_ISREG(pst.st_mode); 435df53360cSBrian Somers 436df53360cSBrian Somers for (nread = 0; !reg || nread < pst.st_size; nread += save_len) { 437df53360cSBrian Somers if (fread(&stamp, sizeof(stamp), 1, fp) != 1) { 438df53360cSBrian Somers if (reg) 439df53360cSBrian Somers err(1, "reading playback header"); 440df53360cSBrian Somers else 441df53360cSBrian Somers break; 442df53360cSBrian Somers } 443df53360cSBrian Somers swapstamp(stamp); 444df53360cSBrian Somers save_len = sizeof(stamp); 445df53360cSBrian Somers 446df53360cSBrian Somers if (reg && stamp.scr_len > 447df53360cSBrian Somers (uint64_t)(pst.st_size - save_len) - nread) 448df53360cSBrian Somers errx(1, "invalid stamp"); 449df53360cSBrian Somers 450df53360cSBrian Somers save_len += stamp.scr_len; 451df53360cSBrian Somers tclock = stamp.scr_sec; 452df53360cSBrian Somers tso.tv_sec = stamp.scr_sec; 453df53360cSBrian Somers tso.tv_nsec = stamp.scr_usec * 1000; 454df53360cSBrian Somers 455df53360cSBrian Somers switch (stamp.scr_direction) { 456df53360cSBrian Somers case 's': 457df53360cSBrian Somers if (!qflg) 458df53360cSBrian Somers (void)printf("Script started on %s", 459df53360cSBrian Somers ctime(&tclock)); 460df53360cSBrian Somers tsi = tso; 461df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 462df53360cSBrian Somers break; 463df53360cSBrian Somers case 'e': 464df53360cSBrian Somers if (!qflg) 465df53360cSBrian Somers (void)printf("\nScript done on %s", 466df53360cSBrian Somers ctime(&tclock)); 467df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 468df53360cSBrian Somers break; 469df53360cSBrian Somers case 'i': 470df53360cSBrian Somers /* throw input away */ 471df53360cSBrian Somers (void)consume(fp, stamp.scr_len, buf, reg); 472df53360cSBrian Somers break; 473df53360cSBrian Somers case 'o': 474df53360cSBrian Somers tsi.tv_sec = tso.tv_sec - tsi.tv_sec; 475df53360cSBrian Somers tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec; 476df53360cSBrian Somers if (tsi.tv_nsec < 0) { 477df53360cSBrian Somers tsi.tv_sec -= 1; 478df53360cSBrian Somers tsi.tv_nsec += 1000000000; 479df53360cSBrian Somers } 480df53360cSBrian Somers if (usesleep) 481df53360cSBrian Somers (void)nanosleep(&tsi, NULL); 482df53360cSBrian Somers tsi = tso; 483df53360cSBrian Somers while (stamp.scr_len > 0) { 484df53360cSBrian Somers l = MIN(DEF_BUF, stamp.scr_len); 485df53360cSBrian Somers if (fread(buf, sizeof(char), l, fp) != l) 486df53360cSBrian Somers err(1, "cannot read buffer"); 487df53360cSBrian Somers 488df53360cSBrian Somers (void)write(STDOUT_FILENO, buf, l); 489df53360cSBrian Somers stamp.scr_len -= l; 490df53360cSBrian Somers } 491df53360cSBrian Somers break; 492df53360cSBrian Somers default: 493df53360cSBrian Somers errx(1, "invalid direction"); 494df53360cSBrian Somers } 495df53360cSBrian Somers } 496df53360cSBrian Somers (void)fclose(fp); 497df53360cSBrian Somers exit(0); 498df53360cSBrian Somers } 499