Home | History | Annotate | Line # | Download | only in script
script.c revision 1.25
      1  1.25  christos /*	$NetBSD: script.c,v 1.25 2020/08/07 13:36:28 christos Exp $	*/
      2   1.3       jtc 
      3   1.1       cgd /*
      4   1.3       jtc  * Copyright (c) 1980, 1992, 1993
      5   1.3       jtc  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15   1.9       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.5     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.16     lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1992, 1993\
     35  1.16     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39   1.3       jtc #if 0
     40   1.3       jtc static char sccsid[] = "@(#)script.c	8.1 (Berkeley) 6/6/93";
     41   1.3       jtc #endif
     42  1.25  christos __RCSID("$NetBSD: script.c,v 1.25 2020/08/07 13:36:28 christos Exp $");
     43   1.1       cgd #endif /* not lint */
     44   1.1       cgd 
     45   1.1       cgd #include <sys/types.h>
     46   1.3       jtc #include <sys/wait.h>
     47   1.1       cgd #include <sys/stat.h>
     48   1.1       cgd #include <sys/ioctl.h>
     49   1.1       cgd #include <sys/time.h>
     50  1.12  liamjfoy #include <sys/param.h>
     51   1.8    atatat #include <sys/uio.h>
     52   1.3       jtc 
     53   1.5     lukem #include <err.h>
     54   1.3       jtc #include <errno.h>
     55   1.3       jtc #include <fcntl.h>
     56   1.3       jtc #include <paths.h>
     57   1.3       jtc #include <signal.h>
     58   1.1       cgd #include <stdio.h>
     59   1.3       jtc #include <stdlib.h>
     60   1.3       jtc #include <string.h>
     61   1.3       jtc #include <termios.h>
     62   1.6    kleink #include <time.h>
     63   1.3       jtc #include <tzfile.h>
     64   1.3       jtc #include <unistd.h>
     65   1.5     lukem #include <util.h>
     66   1.1       cgd 
     67  1.12  liamjfoy #define	DEF_BUF	65536
     68  1.12  liamjfoy 
     69   1.8    atatat struct stamp {
     70   1.8    atatat 	uint64_t scr_len;	/* amount of data */
     71   1.8    atatat 	uint64_t scr_sec;	/* time it arrived in seconds... */
     72   1.8    atatat 	uint32_t scr_usec;	/* ...and microseconds */
     73   1.8    atatat 	uint32_t scr_direction;	/* 'i', 'o', etc (also indicates endianness) */
     74   1.8    atatat };
     75   1.8    atatat 
     76  1.21     joerg static FILE	*fscript;
     77  1.21     joerg static int	master, slave;
     78  1.21     joerg static int	child, subchild;
     79  1.23  christos static size_t	outcc;
     80  1.21     joerg static int	usesleep, rawout;
     81  1.21     joerg static int	quiet, flush;
     82  1.21     joerg static const char *fname;
     83  1.21     joerg 
     84  1.22  christos static int	isterm;
     85  1.21     joerg static struct	termios tt;
     86  1.21     joerg 
     87  1.21     joerg __dead static void	done(void);
     88  1.21     joerg __dead static void	dooutput(void);
     89  1.21     joerg __dead static void	doshell(const char *);
     90  1.21     joerg __dead static void	fail(void);
     91  1.21     joerg static void	finish(int);
     92  1.21     joerg static void	scriptflush(int);
     93  1.21     joerg static void	record(FILE *, char *, size_t, int);
     94  1.21     joerg static void	consume(FILE *, off_t, char *, int);
     95  1.21     joerg __dead static void	playback(FILE *);
     96   1.3       jtc 
     97   1.3       jtc int
     98  1.11    rpaulo main(int argc, char *argv[])
     99   1.1       cgd {
    100  1.23  christos 	ssize_t scc;
    101  1.23  christos 	size_t cc;
    102   1.3       jtc 	struct termios rtt;
    103   1.3       jtc 	struct winsize win;
    104   1.8    atatat 	int aflg, pflg, ch;
    105   1.3       jtc 	char ibuf[BUFSIZ];
    106  1.18  christos 	const char *command;
    107   1.1       cgd 
    108   1.3       jtc 	aflg = 0;
    109   1.8    atatat 	pflg = 0;
    110   1.8    atatat 	usesleep = 1;
    111   1.8    atatat 	rawout = 0;
    112  1.18  christos 	quiet = 0;
    113  1.18  christos 	flush = 0;
    114  1.18  christos 	command = NULL;
    115  1.18  christos 	while ((ch = getopt(argc, argv, "ac:dfpqr")) != -1)
    116   1.3       jtc 		switch(ch) {
    117   1.1       cgd 		case 'a':
    118   1.3       jtc 			aflg = 1;
    119   1.1       cgd 			break;
    120  1.18  christos 		case 'c':
    121  1.18  christos 			command = optarg;
    122  1.18  christos 			break;
    123   1.8    atatat 		case 'd':
    124   1.8    atatat 			usesleep = 0;
    125   1.8    atatat 			break;
    126  1.18  christos 		case 'f':
    127  1.18  christos 			flush = 1;
    128  1.18  christos 			break;
    129   1.8    atatat 		case 'p':
    130   1.8    atatat 			pflg = 1;
    131   1.8    atatat 			break;
    132  1.18  christos 		case 'q':
    133  1.18  christos 			quiet = 1;
    134  1.18  christos 			break;
    135   1.8    atatat 		case 'r':
    136   1.8    atatat 			rawout = 1;
    137   1.8    atatat 			break;
    138   1.1       cgd 		case '?':
    139   1.1       cgd 		default:
    140  1.18  christos 			(void)fprintf(stderr,
    141  1.18  christos 			    "Usage: %s [-c <command>][-adfpqr] [file]\n",
    142  1.10       wiz 			    getprogname());
    143  1.23  christos 			exit(EXIT_FAILURE);
    144   1.1       cgd 		}
    145   1.1       cgd 	argc -= optind;
    146   1.1       cgd 	argv += optind;
    147   1.1       cgd 
    148   1.1       cgd 	if (argc > 0)
    149   1.1       cgd 		fname = argv[0];
    150   1.1       cgd 	else
    151   1.1       cgd 		fname = "typescript";
    152   1.1       cgd 
    153   1.8    atatat 	if ((fscript = fopen(fname, pflg ? "r" : aflg ? "a" : "w")) == NULL)
    154  1.23  christos 		err(EXIT_FAILURE, "fopen %s", fname);
    155   1.3       jtc 
    156   1.8    atatat 	if (pflg)
    157   1.8    atatat 		playback(fscript);
    158   1.8    atatat 
    159  1.25  christos 	if (tcgetattr(STDIN_FILENO, &tt) == -1 ||
    160  1.25  christos 	    ioctl(STDIN_FILENO, TIOCGWINSZ, &win) == -1) {
    161  1.25  christos 		switch (errno) {
    162  1.25  christos 		case ENOTTY:
    163  1.25  christos 			if (openpty(&master, &slave, NULL, NULL, NULL) == -1)
    164  1.25  christos 				err(EXIT_FAILURE, "openpty");
    165  1.25  christos 			break;
    166  1.25  christos 		case EBADF:
    167  1.25  christos 			err(EXIT_FAILURE, "%d not valid fd", STDIN_FILENO);
    168  1.25  christos 		default: /* errno == EFAULT or EINVAL for ioctl. Not reached in practice. */
    169  1.23  christos 			err(EXIT_FAILURE, "ioctl");
    170  1.25  christos 		}
    171  1.25  christos 	} else {
    172  1.23  christos 		if (openpty(&master, &slave, NULL, &tt, &win) == -1)
    173  1.23  christos 			err(EXIT_FAILURE, "openpty");
    174  1.25  christos 		isterm = 1;
    175  1.23  christos 	}
    176   1.1       cgd 
    177  1.18  christos 	if (!quiet)
    178  1.18  christos 		(void)printf("Script started, output file is %s\n", fname);
    179  1.23  christos 
    180  1.23  christos 	if (isterm) {
    181  1.23  christos 		rtt = tt;
    182  1.23  christos 		cfmakeraw(&rtt);
    183  1.23  christos 		rtt.c_lflag &= ~ECHO;
    184  1.23  christos 		(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &rtt);
    185  1.23  christos 	}
    186   1.1       cgd 
    187   1.3       jtc 	(void)signal(SIGCHLD, finish);
    188   1.1       cgd 	child = fork();
    189  1.23  christos 	if (child == -1) {
    190   1.5     lukem 		warn("fork");
    191   1.1       cgd 		fail();
    192   1.1       cgd 	}
    193   1.1       cgd 	if (child == 0) {
    194   1.1       cgd 		subchild = child = fork();
    195  1.23  christos 		if (child == -1) {
    196   1.5     lukem 			warn("fork");
    197   1.1       cgd 			fail();
    198   1.1       cgd 		}
    199   1.1       cgd 		if (child)
    200   1.1       cgd 			dooutput();
    201   1.1       cgd 		else
    202  1.18  christos 			doshell(command);
    203   1.1       cgd 	}
    204   1.1       cgd 
    205   1.8    atatat 	if (!rawout)
    206   1.8    atatat 		(void)fclose(fscript);
    207  1.23  christos 	while ((scc = read(STDIN_FILENO, ibuf, BUFSIZ)) > 0) {
    208  1.24  christos 		cc = (size_t)scc;
    209   1.8    atatat 		if (rawout)
    210   1.8    atatat 			record(fscript, ibuf, cc, 'i');
    211   1.3       jtc 		(void)write(master, ibuf, cc);
    212   1.8    atatat 	}
    213   1.1       cgd 	done();
    214   1.5     lukem 	/* NOTREACHED */
    215  1.23  christos 	return EXIT_SUCCESS;
    216   1.1       cgd }
    217   1.1       cgd 
    218  1.21     joerg static void
    219  1.11    rpaulo finish(int signo)
    220   1.1       cgd {
    221   1.7  christos 	int die, pid, status;
    222   1.1       cgd 
    223   1.3       jtc 	die = 0;
    224   1.7  christos 	while ((pid = wait3(&status, WNOHANG, 0)) > 0)
    225   1.1       cgd 		if (pid == child)
    226   1.1       cgd 			die = 1;
    227   1.1       cgd 
    228   1.1       cgd 	if (die)
    229   1.1       cgd 		done();
    230   1.1       cgd }
    231   1.1       cgd 
    232  1.21     joerg static void
    233  1.18  christos dooutput(void)
    234   1.1       cgd {
    235   1.3       jtc 	struct itimerval value;
    236  1.23  christos 	ssize_t scc;
    237  1.23  christos 	size_t cc;
    238   1.3       jtc 	time_t tvec;
    239   1.3       jtc 	char obuf[BUFSIZ];
    240   1.1       cgd 
    241   1.3       jtc 	(void)close(STDIN_FILENO);
    242   1.3       jtc 	tvec = time(NULL);
    243   1.8    atatat 	if (rawout)
    244   1.8    atatat 		record(fscript, NULL, 0, 's');
    245  1.18  christos 	else if (!quiet)
    246   1.8    atatat 		(void)fprintf(fscript, "Script started on %s", ctime(&tvec));
    247   1.3       jtc 
    248   1.3       jtc 	(void)signal(SIGALRM, scriptflush);
    249   1.3       jtc 	value.it_interval.tv_sec = SECSPERMIN / 2;
    250   1.3       jtc 	value.it_interval.tv_usec = 0;
    251   1.3       jtc 	value.it_value = value.it_interval;
    252   1.3       jtc 	(void)setitimer(ITIMER_REAL, &value, NULL);
    253   1.1       cgd 	for (;;) {
    254  1.23  christos 		scc = read(master, obuf, sizeof(obuf));
    255  1.23  christos 		if (scc <= 0)
    256   1.1       cgd 			break;
    257  1.23  christos 		cc = (size_t)scc;
    258   1.3       jtc 		(void)write(1, obuf, cc);
    259   1.8    atatat 		if (rawout)
    260   1.8    atatat 			record(fscript, obuf, cc, 'o');
    261   1.8    atatat 		else
    262   1.8    atatat 			(void)fwrite(obuf, 1, cc, fscript);
    263   1.3       jtc 		outcc += cc;
    264  1.18  christos 		if (flush)
    265  1.18  christos 			(void)fflush(fscript);
    266   1.1       cgd 	}
    267   1.1       cgd 	done();
    268   1.1       cgd }
    269   1.1       cgd 
    270  1.21     joerg static void
    271  1.11    rpaulo scriptflush(int signo)
    272   1.1       cgd {
    273   1.3       jtc 	if (outcc) {
    274   1.3       jtc 		(void)fflush(fscript);
    275   1.3       jtc 		outcc = 0;
    276   1.1       cgd 	}
    277   1.1       cgd }
    278   1.1       cgd 
    279  1.21     joerg static void
    280  1.18  christos doshell(const char *command)
    281   1.1       cgd {
    282  1.17     lukem 	const char *shell;
    283   1.3       jtc 
    284   1.3       jtc 	(void)close(master);
    285   1.3       jtc 	(void)fclose(fscript);
    286   1.3       jtc 	login_tty(slave);
    287  1.18  christos 	if (command == NULL) {
    288  1.18  christos 		shell = getenv("SHELL");
    289  1.18  christos 		if (shell == NULL)
    290  1.18  christos 			shell = _PATH_BSHELL;
    291  1.18  christos 		execl(shell, shell, "-i", NULL);
    292  1.19  christos 		warn("execl `%s'", shell);
    293  1.19  christos 	} else {
    294  1.19  christos 		if (system(command) == -1)
    295  1.19  christos 			warn("system `%s'", command);
    296  1.19  christos 	}
    297  1.18  christos 
    298   1.3       jtc 	fail();
    299   1.1       cgd }
    300   1.1       cgd 
    301  1.21     joerg static void
    302  1.21     joerg fail(void)
    303   1.1       cgd {
    304   1.1       cgd 
    305   1.3       jtc 	(void)kill(0, SIGTERM);
    306   1.1       cgd 	done();
    307   1.1       cgd }
    308   1.1       cgd 
    309  1.21     joerg static void
    310  1.21     joerg done(void)
    311   1.1       cgd {
    312   1.3       jtc 	time_t tvec;
    313   1.1       cgd 
    314   1.1       cgd 	if (subchild) {
    315   1.3       jtc 		tvec = time(NULL);
    316   1.8    atatat 		if (rawout)
    317   1.8    atatat 			record(fscript, NULL, 0, 'e');
    318  1.18  christos 		else if (!quiet)
    319   1.8    atatat 			(void)fprintf(fscript,"\nScript done on %s",
    320   1.8    atatat 			    ctime(&tvec));
    321   1.3       jtc 		(void)fclose(fscript);
    322   1.3       jtc 		(void)close(master);
    323   1.1       cgd 	} else {
    324  1.23  christos 		if (isterm)
    325  1.23  christos 			(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &tt);
    326  1.18  christos 		if (!quiet)
    327  1.18  christos 			(void)printf("Script done, output file is %s\n", fname);
    328   1.1       cgd 	}
    329  1.23  christos 	exit(EXIT_SUCCESS);
    330   1.8    atatat }
    331   1.8    atatat 
    332  1.21     joerg static void
    333  1.17     lukem record(FILE *fp, char *buf, size_t cc, int direction)
    334   1.8    atatat {
    335   1.8    atatat 	struct iovec iov[2];
    336   1.8    atatat 	struct stamp stamp;
    337   1.8    atatat 	struct timeval tv;
    338   1.8    atatat 
    339   1.8    atatat 	(void)gettimeofday(&tv, NULL);
    340   1.8    atatat 	stamp.scr_len = cc;
    341   1.8    atatat 	stamp.scr_sec = tv.tv_sec;
    342   1.8    atatat 	stamp.scr_usec = tv.tv_usec;
    343   1.8    atatat 	stamp.scr_direction = direction;
    344   1.8    atatat 	iov[0].iov_len = sizeof(stamp);
    345   1.8    atatat 	iov[0].iov_base = &stamp;
    346   1.8    atatat 	iov[1].iov_len = cc;
    347   1.8    atatat 	iov[1].iov_base = buf;
    348  1.17     lukem 	if (writev(fileno(fp), &iov[0], 2) == -1)
    349  1.23  christos 		err(EXIT_FAILURE, "writev");
    350   1.8    atatat }
    351   1.8    atatat 
    352  1.21     joerg static void
    353  1.17     lukem consume(FILE *fp, off_t len, char *buf, int reg)
    354  1.14  christos {
    355  1.14  christos 	size_t l;
    356  1.14  christos 
    357  1.14  christos 	if (reg) {
    358  1.17     lukem 		if (fseeko(fp, len, SEEK_CUR) == -1)
    359  1.23  christos 			err(EXIT_FAILURE, NULL);
    360  1.14  christos 	}
    361  1.14  christos 	else {
    362  1.14  christos 		while (len > 0) {
    363  1.14  christos 			l = MIN(DEF_BUF, len);
    364  1.17     lukem 			if (fread(buf, sizeof(char), l, fp) != l)
    365  1.23  christos 				err(EXIT_FAILURE, "cannot read buffer");
    366  1.14  christos 			len -= l;
    367  1.14  christos 		}
    368  1.14  christos 	}
    369  1.14  christos }
    370  1.14  christos 
    371   1.8    atatat #define swapstamp(stamp) do { \
    372   1.8    atatat 	if (stamp.scr_direction > 0xff) { \
    373   1.8    atatat 		stamp.scr_len = bswap64(stamp.scr_len); \
    374   1.8    atatat 		stamp.scr_sec = bswap64(stamp.scr_sec); \
    375   1.8    atatat 		stamp.scr_usec = bswap32(stamp.scr_usec); \
    376   1.8    atatat 		stamp.scr_direction = bswap32(stamp.scr_direction); \
    377   1.8    atatat 	} \
    378   1.8    atatat } while (0/*CONSTCOND*/)
    379   1.8    atatat 
    380  1.21     joerg static void
    381  1.22  christos termset(void)
    382  1.22  christos {
    383  1.22  christos 	struct termios traw;
    384  1.22  christos 
    385  1.25  christos 	if (tcgetattr(STDOUT_FILENO, &tt) == -1) {
    386  1.25  christos 		if (errno == EBADF)
    387  1.25  christos 			err(EXIT_FAILURE, "%d not valid fd", STDOUT_FILENO);
    388  1.25  christos 		/* errno == ENOTTY */
    389  1.22  christos 		return;
    390  1.25  christos 	}
    391  1.25  christos 	isterm = 1;
    392  1.22  christos 	traw = tt;
    393  1.22  christos 	cfmakeraw(&traw);
    394  1.22  christos 	traw.c_lflag |= ISIG;
    395  1.25  christos         (void)tcsetattr(STDOUT_FILENO, TCSANOW, &traw);
    396  1.22  christos }
    397  1.22  christos 
    398  1.22  christos static void
    399  1.22  christos termreset(void)
    400  1.22  christos {
    401  1.22  christos 	if (isterm)
    402  1.23  christos 		(void)tcsetattr(STDOUT_FILENO, TCSADRAIN, &tt);
    403  1.22  christos 
    404  1.22  christos 	isterm = 0;
    405  1.22  christos }
    406  1.22  christos 
    407  1.22  christos static void
    408  1.17     lukem playback(FILE *fp)
    409   1.8    atatat {
    410   1.8    atatat 	struct timespec tsi, tso;
    411   1.8    atatat 	struct stamp stamp;
    412  1.13  christos 	struct stat pst;
    413  1.12  liamjfoy 	char buf[DEF_BUF];
    414  1.12  liamjfoy 	off_t nread, save_len;
    415   1.8    atatat 	size_t l;
    416  1.17     lukem 	time_t tclock;
    417  1.13  christos 	int reg;
    418   1.8    atatat 
    419  1.17     lukem 	if (fstat(fileno(fp), &pst) == -1)
    420  1.24  christos 		err(EXIT_FAILURE, "fstat failed");
    421  1.12  liamjfoy 
    422  1.13  christos 	reg = S_ISREG(pst.st_mode);
    423  1.13  christos 
    424  1.13  christos 	for (nread = 0; !reg || nread < pst.st_size; nread += save_len) {
    425  1.17     lukem 		if (fread(&stamp, sizeof(stamp), 1, fp) != 1) {
    426  1.13  christos 			if (reg)
    427  1.23  christos 				err(EXIT_FAILURE, "reading playback header");
    428  1.13  christos 			else
    429  1.13  christos 				break;
    430  1.13  christos 		}
    431  1.12  liamjfoy 		swapstamp(stamp);
    432  1.12  liamjfoy 		save_len = sizeof(stamp);
    433   1.8    atatat 
    434  1.13  christos 		if (reg && stamp.scr_len >
    435  1.13  christos 		    (uint64_t)(pst.st_size - save_len) - nread)
    436  1.23  christos 			errx(EXIT_FAILURE, "invalid stamp");
    437  1.12  liamjfoy 
    438  1.12  liamjfoy 		save_len += stamp.scr_len;
    439  1.17     lukem 		tclock = stamp.scr_sec;
    440   1.8    atatat 		tso.tv_sec = stamp.scr_sec;
    441   1.8    atatat 		tso.tv_nsec = stamp.scr_usec * 1000;
    442   1.8    atatat 
    443   1.8    atatat 		switch (stamp.scr_direction) {
    444   1.8    atatat 		case 's':
    445  1.18  christos 			if (!quiet)
    446  1.18  christos 			    (void)printf("Script started on %s",
    447  1.18  christos 				ctime(&tclock));
    448   1.8    atatat 			tsi = tso;
    449  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    450  1.22  christos 			termset();
    451  1.22  christos 			atexit(termreset);
    452   1.8    atatat 			break;
    453   1.8    atatat 		case 'e':
    454  1.23  christos 			termreset();
    455  1.18  christos 			if (!quiet)
    456  1.18  christos 				(void)printf("\nScript done on %s",
    457  1.18  christos 				    ctime(&tclock));
    458  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    459   1.8    atatat 			break;
    460   1.8    atatat 		case 'i':
    461   1.8    atatat 			/* throw input away */
    462  1.17     lukem 			(void)consume(fp, stamp.scr_len, buf, reg);
    463   1.8    atatat 			break;
    464   1.8    atatat 		case 'o':
    465   1.8    atatat 			tsi.tv_sec = tso.tv_sec - tsi.tv_sec;
    466   1.8    atatat 			tsi.tv_nsec = tso.tv_nsec - tsi.tv_nsec;
    467   1.8    atatat 			if (tsi.tv_nsec < 0) {
    468   1.8    atatat 				tsi.tv_sec -= 1;
    469   1.8    atatat 				tsi.tv_nsec += 1000000000;
    470   1.8    atatat 			}
    471   1.8    atatat 			if (usesleep)
    472   1.8    atatat 				(void)nanosleep(&tsi, NULL);
    473   1.8    atatat 			tsi = tso;
    474  1.12  liamjfoy 			while (stamp.scr_len > 0) {
    475  1.12  liamjfoy 				l = MIN(DEF_BUF, stamp.scr_len);
    476  1.17     lukem 				if (fread(buf, sizeof(char), l, fp) != l)
    477  1.23  christos 					err(EXIT_FAILURE, "cannot read buffer");
    478  1.12  liamjfoy 
    479  1.12  liamjfoy 				(void)write(STDOUT_FILENO, buf, l);
    480  1.12  liamjfoy 				stamp.scr_len -= l;
    481  1.12  liamjfoy 			}
    482   1.8    atatat 			break;
    483  1.12  liamjfoy 		default:
    484  1.23  christos 			errx(EXIT_FAILURE, "invalid direction %u",
    485  1.23  christos 			    stamp.scr_direction);
    486   1.8    atatat 		}
    487  1.12  liamjfoy 	}
    488  1.17     lukem 	(void)fclose(fp);
    489  1.23  christos 	exit(EXIT_SUCCESS);
    490   1.1       cgd }
    491