xref: /freebsd/usr.bin/tail/forward.c (revision 814e3a92a9538486cd1621e811c475c932e32227)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1991, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * Edward Sze-Tyan Wang.
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.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
37814e3a92SMark Murray #include <sys/cdefs.h>
38814e3a92SMark Murray 
39814e3a92SMark Murray __FBSDID("$FreeBSD$");
40814e3a92SMark Murray 
419b50d902SRodney W. Grimes #ifndef lint
42814e3a92SMark Murray static const char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
43247e7cb1SJeroen Ruigrok van der Werven #endif
449b50d902SRodney W. Grimes 
459b50d902SRodney W. Grimes #include <sys/types.h>
469b50d902SRodney W. Grimes #include <sys/stat.h>
479b50d902SRodney W. Grimes #include <sys/time.h>
489b50d902SRodney W. Grimes #include <sys/mman.h>
4932462e82SJonathan Lemon #include <sys/event.h>
509b50d902SRodney W. Grimes 
51814e3a92SMark Murray #include <err.h>
529b50d902SRodney W. Grimes #include <errno.h>
53814e3a92SMark Murray #include <fcntl.h>
54814e3a92SMark Murray #include <limits.h>
559b50d902SRodney W. Grimes #include <stdio.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
58814e3a92SMark Murray #include <unistd.h>
59814e3a92SMark Murray 
609b50d902SRodney W. Grimes #include "extern.h"
619b50d902SRodney W. Grimes 
62bd9dc975SAndrey A. Chernov static void rlines __P((FILE *, off_t, struct stat *));
639b50d902SRodney W. Grimes 
64ea9bd2dfSJonathan Lemon /* defines for inner loop actions */
65ea9bd2dfSJonathan Lemon #define USE_SLEEP	0
66ea9bd2dfSJonathan Lemon #define USE_KQUEUE	1
67ea9bd2dfSJonathan Lemon #define ADD_EVENTS	2
68ea9bd2dfSJonathan Lemon 
699b50d902SRodney W. Grimes /*
709b50d902SRodney W. Grimes  * forward -- display the file, from an offset, forward.
719b50d902SRodney W. Grimes  *
729b50d902SRodney W. Grimes  * There are eight separate cases for this -- regular and non-regular
739b50d902SRodney W. Grimes  * files, by bytes or lines and from the beginning or end of the file.
749b50d902SRodney W. Grimes  *
759b50d902SRodney W. Grimes  * FBYTES	byte offset from the beginning of the file
769b50d902SRodney W. Grimes  *	REG	seek
779b50d902SRodney W. Grimes  *	NOREG	read, counting bytes
789b50d902SRodney W. Grimes  *
799b50d902SRodney W. Grimes  * FLINES	line offset from the beginning of the file
809b50d902SRodney W. Grimes  *	REG	read, counting lines
819b50d902SRodney W. Grimes  *	NOREG	read, counting lines
829b50d902SRodney W. Grimes  *
839b50d902SRodney W. Grimes  * RBYTES	byte offset from the end of the file
849b50d902SRodney W. Grimes  *	REG	seek
859b50d902SRodney W. Grimes  *	NOREG	cyclically read characters into a wrap-around buffer
869b50d902SRodney W. Grimes  *
879b50d902SRodney W. Grimes  * RLINES
889b50d902SRodney W. Grimes  *	REG	mmap the file and step back until reach the correct offset.
899b50d902SRodney W. Grimes  *	NOREG	cyclically read lines into a wrap-around array of buffers
909b50d902SRodney W. Grimes  */
919b50d902SRodney W. Grimes void
929b50d902SRodney W. Grimes forward(fp, style, off, sbp)
939b50d902SRodney W. Grimes 	FILE *fp;
949b50d902SRodney W. Grimes 	enum STYLE style;
95bd9dc975SAndrey A. Chernov 	off_t off;
969b50d902SRodney W. Grimes 	struct stat *sbp;
979b50d902SRodney W. Grimes {
98aee1bac7SIan Dowse 	int ch, n, kq = -1;
99ea9bd2dfSJonathan Lemon 	int action = USE_SLEEP;
100b446630fSJonathan Lemon 	struct kevent ev[2];
101ea9bd2dfSJonathan Lemon 	struct stat sb2;
102aee1bac7SIan Dowse 	struct timespec ts;
1039b50d902SRodney W. Grimes 
1049b50d902SRodney W. Grimes 	switch(style) {
1059b50d902SRodney W. Grimes 	case FBYTES:
1069b50d902SRodney W. Grimes 		if (off == 0)
1079b50d902SRodney W. Grimes 			break;
1089b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode)) {
1099b50d902SRodney W. Grimes 			if (sbp->st_size < off)
1109b50d902SRodney W. Grimes 				off = sbp->st_size;
111bd9dc975SAndrey A. Chernov 			if (fseeko(fp, off, SEEK_SET) == -1) {
1129b50d902SRodney W. Grimes 				ierr();
1139b50d902SRodney W. Grimes 				return;
1149b50d902SRodney W. Grimes 			}
1159b50d902SRodney W. Grimes 		} else while (off--)
1169b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF) {
1179b50d902SRodney W. Grimes 				if (ferror(fp)) {
1189b50d902SRodney W. Grimes 					ierr();
1199b50d902SRodney W. Grimes 					return;
1209b50d902SRodney W. Grimes 				}
1219b50d902SRodney W. Grimes 				break;
1229b50d902SRodney W. Grimes 			}
1239b50d902SRodney W. Grimes 		break;
1249b50d902SRodney W. Grimes 	case FLINES:
1259b50d902SRodney W. Grimes 		if (off == 0)
1269b50d902SRodney W. Grimes 			break;
1279b50d902SRodney W. Grimes 		for (;;) {
1289b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF) {
1299b50d902SRodney W. Grimes 				if (ferror(fp)) {
1309b50d902SRodney W. Grimes 					ierr();
1319b50d902SRodney W. Grimes 					return;
1329b50d902SRodney W. Grimes 				}
1339b50d902SRodney W. Grimes 				break;
1349b50d902SRodney W. Grimes 			}
1359b50d902SRodney W. Grimes 			if (ch == '\n' && !--off)
1369b50d902SRodney W. Grimes 				break;
1379b50d902SRodney W. Grimes 		}
1389b50d902SRodney W. Grimes 		break;
1399b50d902SRodney W. Grimes 	case RBYTES:
1409b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode)) {
1419b50d902SRodney W. Grimes 			if (sbp->st_size >= off &&
142bd9dc975SAndrey A. Chernov 			    fseeko(fp, -off, SEEK_END) == -1) {
1439b50d902SRodney W. Grimes 				ierr();
1449b50d902SRodney W. Grimes 				return;
1459b50d902SRodney W. Grimes 			}
1469b50d902SRodney W. Grimes 		} else if (off == 0) {
1479b50d902SRodney W. Grimes 			while (getc(fp) != EOF);
1489b50d902SRodney W. Grimes 			if (ferror(fp)) {
1499b50d902SRodney W. Grimes 				ierr();
1509b50d902SRodney W. Grimes 				return;
1519b50d902SRodney W. Grimes 			}
1529b50d902SRodney W. Grimes 		} else
1536439f56eSAdam David 			if (bytes(fp, off))
1546439f56eSAdam David 				return;
1559b50d902SRodney W. Grimes 		break;
1569b50d902SRodney W. Grimes 	case RLINES:
1579b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode))
1589b50d902SRodney W. Grimes 			if (!off) {
159bd9dc975SAndrey A. Chernov 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
1609b50d902SRodney W. Grimes 					ierr();
1619b50d902SRodney W. Grimes 					return;
1629b50d902SRodney W. Grimes 				}
1639b50d902SRodney W. Grimes 			} else
1649b50d902SRodney W. Grimes 				rlines(fp, off, sbp);
1659b50d902SRodney W. Grimes 		else if (off == 0) {
1669b50d902SRodney W. Grimes 			while (getc(fp) != EOF);
1679b50d902SRodney W. Grimes 			if (ferror(fp)) {
1689b50d902SRodney W. Grimes 				ierr();
1699b50d902SRodney W. Grimes 				return;
1709b50d902SRodney W. Grimes 			}
1719b50d902SRodney W. Grimes 		} else
1726439f56eSAdam David 			if (lines(fp, off))
1736439f56eSAdam David 				return;
1749b50d902SRodney W. Grimes 		break;
175814e3a92SMark Murray 	default:
1769b50d902SRodney W. Grimes 	}
1779b50d902SRodney W. Grimes 
178b446630fSJonathan Lemon 	if (fflag) {
179b446630fSJonathan Lemon 		kq = kqueue();
180b446630fSJonathan Lemon 		if (kq < 0)
181b446630fSJonathan Lemon 			err(1, "kqueue");
182ea9bd2dfSJonathan Lemon 		action = ADD_EVENTS;
183b446630fSJonathan Lemon 	}
1849b50d902SRodney W. Grimes 
1859b50d902SRodney W. Grimes 	for (;;) {
1869b50d902SRodney W. Grimes 		while ((ch = getc(fp)) != EOF)
1879b50d902SRodney W. Grimes 			if (putchar(ch) == EOF)
1889b50d902SRodney W. Grimes 				oerr();
1899b50d902SRodney W. Grimes 		if (ferror(fp)) {
1909b50d902SRodney W. Grimes 			ierr();
1919b50d902SRodney W. Grimes 			return;
1929b50d902SRodney W. Grimes 		}
1939b50d902SRodney W. Grimes 		(void)fflush(stdout);
1949b50d902SRodney W. Grimes 		if (! fflag)
1959b50d902SRodney W. Grimes 			break;
1969b50d902SRodney W. Grimes 		clearerr(fp);
197eb1c9439SPeter Wemm 
198ea9bd2dfSJonathan Lemon 		switch (action) {
199aee1bac7SIan Dowse 		case ADD_EVENTS:
200aee1bac7SIan Dowse 			n = 0;
201aee1bac7SIan Dowse 			ts.tv_sec = 0;
202aee1bac7SIan Dowse 			ts.tv_nsec = 0;
203b446630fSJonathan Lemon 
204b446630fSJonathan Lemon 			if (Fflag && fileno(fp) != STDIN_FILENO) {
2056029e693SJonathan Lemon 				EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
2066029e693SJonathan Lemon 				    EV_ADD | EV_ENABLE | EV_CLEAR,
2076029e693SJonathan Lemon 				    NOTE_DELETE | NOTE_RENAME, 0, 0);
208b446630fSJonathan Lemon 				n++;
209b446630fSJonathan Lemon 			}
2106029e693SJonathan Lemon 			EV_SET(&ev[n], fileno(fp), EVFILT_READ,
211a8fb4e28SJonathan Lemon 			    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
212b446630fSJonathan Lemon 			n++;
213b446630fSJonathan Lemon 
214727a4ed6SJonathan Lemon 			if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
215ea9bd2dfSJonathan Lemon 				action = USE_SLEEP;
216ea9bd2dfSJonathan Lemon 			} else {
217ea9bd2dfSJonathan Lemon 				action = USE_KQUEUE;
218ea9bd2dfSJonathan Lemon 			}
219ea9bd2dfSJonathan Lemon 			break;
220b446630fSJonathan Lemon 
221ea9bd2dfSJonathan Lemon 		case USE_KQUEUE:
222aee1bac7SIan Dowse 			ts.tv_sec = 1;
223aee1bac7SIan Dowse 			ts.tv_nsec = 0;
224aee1bac7SIan Dowse 			/*
225aee1bac7SIan Dowse 			 * In the -F case we set a timeout to ensure that
226aee1bac7SIan Dowse 			 * we re-stat the file at least once every second.
227aee1bac7SIan Dowse 			 */
228aee1bac7SIan Dowse 			n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
229aee1bac7SIan Dowse 			if (n < 0)
230b446630fSJonathan Lemon 				err(1, "kevent");
231aee1bac7SIan Dowse 			if (n == 0) {
232aee1bac7SIan Dowse 				/* timeout */
233aee1bac7SIan Dowse 				break;
234aee1bac7SIan Dowse 			} else if (ev->filter == EVFILT_READ && ev->data < 0) {
235b446630fSJonathan Lemon 				 /* file shrank, reposition to end */
236bd9dc975SAndrey A. Chernov 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
237b446630fSJonathan Lemon 					ierr();
238b446630fSJonathan Lemon 					return;
239eb1c9439SPeter Wemm 				}
240eb1c9439SPeter Wemm 			}
241ea9bd2dfSJonathan Lemon 			break;
242ea9bd2dfSJonathan Lemon 
243ea9bd2dfSJonathan Lemon 		case USE_SLEEP:
244ea9bd2dfSJonathan Lemon                 	(void) usleep(250000);
245ea9bd2dfSJonathan Lemon 	                clearerr(fp);
246aee1bac7SIan Dowse 			break;
247aee1bac7SIan Dowse 		}
248ea9bd2dfSJonathan Lemon 
249aee1bac7SIan Dowse 		if (Fflag && fileno(fp) != STDIN_FILENO) {
250aee1bac7SIan Dowse 			while (stat(fname, &sb2) != 0)
251aee1bac7SIan Dowse 				/* file was rotated, wait until it reappears */
252aee1bac7SIan Dowse 				(void)sleep(1);
253ea9bd2dfSJonathan Lemon 			if (sb2.st_ino != sbp->st_ino ||
254ea9bd2dfSJonathan Lemon 			    sb2.st_dev != sbp->st_dev ||
255ea9bd2dfSJonathan Lemon 			    sb2.st_rdev != sbp->st_rdev ||
256ea9bd2dfSJonathan Lemon 			    sb2.st_nlink == 0) {
257ea9bd2dfSJonathan Lemon 				fp = freopen(fname, "r", fp);
258ea9bd2dfSJonathan Lemon 				if (fp == NULL) {
259ea9bd2dfSJonathan Lemon 					ierr();
260aee1bac7SIan Dowse 				} else {
261ea9bd2dfSJonathan Lemon 					*sbp = sb2;
262ea9bd2dfSJonathan Lemon 					action = ADD_EVENTS;
263ea9bd2dfSJonathan Lemon 				}
264ea9bd2dfSJonathan Lemon 			}
265ea9bd2dfSJonathan Lemon 		}
2669b50d902SRodney W. Grimes 	}
2679b50d902SRodney W. Grimes }
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes /*
2709b50d902SRodney W. Grimes  * rlines -- display the last offset lines of the file.
2719b50d902SRodney W. Grimes  */
2729b50d902SRodney W. Grimes static void
2739b50d902SRodney W. Grimes rlines(fp, off, sbp)
2749b50d902SRodney W. Grimes 	FILE *fp;
275bd9dc975SAndrey A. Chernov 	off_t off;
2769b50d902SRodney W. Grimes 	struct stat *sbp;
2779b50d902SRodney W. Grimes {
278726098d3SDavid Malone 	struct mapinfo map;
279726098d3SDavid Malone 	off_t curoff, size;
280726098d3SDavid Malone 	int i;
2819b50d902SRodney W. Grimes 
2829b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
2839b50d902SRodney W. Grimes 		return;
284726098d3SDavid Malone 	map.start = NULL;
285726098d3SDavid Malone 	map.fd = fileno(fp);
286726098d3SDavid Malone 	map.mapoff = map.maxoff = size;
2879b50d902SRodney W. Grimes 
2886bea9ab4SAndrey A. Chernov 	/*
289726098d3SDavid Malone 	 * Last char is special, ignore whether newline or not. Note that
290726098d3SDavid Malone 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
2916bea9ab4SAndrey A. Chernov 	 */
292726098d3SDavid Malone 	curoff = size - 2;
293726098d3SDavid Malone 	while (curoff >= 0) {
294726098d3SDavid Malone 		if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
2959b50d902SRodney W. Grimes 			ierr();
2969b50d902SRodney W. Grimes 			return;
2979b50d902SRodney W. Grimes 		}
298726098d3SDavid Malone 		for (i = curoff - map.mapoff; i >= 0; i--)
299726098d3SDavid Malone 			if (map.start[i] == '\n' && --off == 0)
300726098d3SDavid Malone 				break;
301726098d3SDavid Malone 		/* `i' is either the map offset of a '\n', or -1. */
302726098d3SDavid Malone 		curoff = map.mapoff + i;
303726098d3SDavid Malone 		if (i >= 0)
304726098d3SDavid Malone 			break;
305726098d3SDavid Malone 	}
306726098d3SDavid Malone 	curoff++;
307726098d3SDavid Malone 	if (mapprint(&map, curoff, size - curoff) != 0) {
308726098d3SDavid Malone 		ierr();
309726098d3SDavid Malone 		exit(1);
310726098d3SDavid Malone 	}
311726098d3SDavid Malone 
312726098d3SDavid Malone 	/* Set the file pointer to reflect the length displayed. */
313a74da62eSAndrey A. Chernov 	if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
314726098d3SDavid Malone 		ierr();
315726098d3SDavid Malone 		return;
316726098d3SDavid Malone 	}
317726098d3SDavid Malone 	if (map.start != NULL && munmap(map.start, map.maplen)) {
31844cf272fSAdam David 		ierr();
3199b50d902SRodney W. Grimes 		return;
3209b50d902SRodney W. Grimes 	}
3219b50d902SRodney W. Grimes }
322