xref: /freebsd/usr.bin/tail/forward.c (revision bd9dc97512fff91e14353a573b5ffbb200bf16bf)
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 
379b50d902SRodney W. Grimes #ifndef lint
38247e7cb1SJeroen Ruigrok van der Werven #if 0
399b50d902SRodney W. Grimes static char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
40247e7cb1SJeroen Ruigrok van der Werven #endif
41247e7cb1SJeroen Ruigrok van der Werven static const char rcsid[] =
42247e7cb1SJeroen Ruigrok van der Werven   "$FreeBSD$";
439b50d902SRodney W. Grimes #endif /* not lint */
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 
519b50d902SRodney W. Grimes #include <limits.h>
529b50d902SRodney W. Grimes #include <fcntl.h>
539b50d902SRodney W. Grimes #include <errno.h>
549b50d902SRodney W. Grimes #include <unistd.h>
559b50d902SRodney W. Grimes #include <stdio.h>
569b50d902SRodney W. Grimes #include <stdlib.h>
579b50d902SRodney W. Grimes #include <string.h>
58c3fd5728SPeter Wemm #include <err.h>
599b50d902SRodney W. Grimes #include "extern.h"
609b50d902SRodney W. Grimes 
61bd9dc975SAndrey A. Chernov static void rlines __P((FILE *, off_t, struct stat *));
629b50d902SRodney W. Grimes 
63ea9bd2dfSJonathan Lemon /* defines for inner loop actions */
64ea9bd2dfSJonathan Lemon #define USE_SLEEP	0
65ea9bd2dfSJonathan Lemon #define USE_KQUEUE	1
66ea9bd2dfSJonathan Lemon #define ADD_EVENTS	2
67ea9bd2dfSJonathan Lemon 
689b50d902SRodney W. Grimes /*
699b50d902SRodney W. Grimes  * forward -- display the file, from an offset, forward.
709b50d902SRodney W. Grimes  *
719b50d902SRodney W. Grimes  * There are eight separate cases for this -- regular and non-regular
729b50d902SRodney W. Grimes  * files, by bytes or lines and from the beginning or end of the file.
739b50d902SRodney W. Grimes  *
749b50d902SRodney W. Grimes  * FBYTES	byte offset from the beginning of the file
759b50d902SRodney W. Grimes  *	REG	seek
769b50d902SRodney W. Grimes  *	NOREG	read, counting bytes
779b50d902SRodney W. Grimes  *
789b50d902SRodney W. Grimes  * FLINES	line offset from the beginning of the file
799b50d902SRodney W. Grimes  *	REG	read, counting lines
809b50d902SRodney W. Grimes  *	NOREG	read, counting lines
819b50d902SRodney W. Grimes  *
829b50d902SRodney W. Grimes  * RBYTES	byte offset from the end of the file
839b50d902SRodney W. Grimes  *	REG	seek
849b50d902SRodney W. Grimes  *	NOREG	cyclically read characters into a wrap-around buffer
859b50d902SRodney W. Grimes  *
869b50d902SRodney W. Grimes  * RLINES
879b50d902SRodney W. Grimes  *	REG	mmap the file and step back until reach the correct offset.
889b50d902SRodney W. Grimes  *	NOREG	cyclically read lines into a wrap-around array of buffers
899b50d902SRodney W. Grimes  */
909b50d902SRodney W. Grimes void
919b50d902SRodney W. Grimes forward(fp, style, off, sbp)
929b50d902SRodney W. Grimes 	FILE *fp;
939b50d902SRodney W. Grimes 	enum STYLE style;
94bd9dc975SAndrey A. Chernov 	off_t off;
959b50d902SRodney W. Grimes 	struct stat *sbp;
969b50d902SRodney W. Grimes {
97ea9bd2dfSJonathan Lemon 	int ch, kq = -1;
98ea9bd2dfSJonathan Lemon 	int action = USE_SLEEP;
99b446630fSJonathan Lemon 	struct kevent ev[2];
100ea9bd2dfSJonathan Lemon 	struct stat sb2;
1019b50d902SRodney W. Grimes 
1029b50d902SRodney W. Grimes 	switch(style) {
1039b50d902SRodney W. Grimes 	case FBYTES:
1049b50d902SRodney W. Grimes 		if (off == 0)
1059b50d902SRodney W. Grimes 			break;
1069b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode)) {
1079b50d902SRodney W. Grimes 			if (sbp->st_size < off)
1089b50d902SRodney W. Grimes 				off = sbp->st_size;
109bd9dc975SAndrey A. Chernov 			if (fseeko(fp, off, SEEK_SET) == -1) {
1109b50d902SRodney W. Grimes 				ierr();
1119b50d902SRodney W. Grimes 				return;
1129b50d902SRodney W. Grimes 			}
1139b50d902SRodney W. Grimes 		} else while (off--)
1149b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF) {
1159b50d902SRodney W. Grimes 				if (ferror(fp)) {
1169b50d902SRodney W. Grimes 					ierr();
1179b50d902SRodney W. Grimes 					return;
1189b50d902SRodney W. Grimes 				}
1199b50d902SRodney W. Grimes 				break;
1209b50d902SRodney W. Grimes 			}
1219b50d902SRodney W. Grimes 		break;
1229b50d902SRodney W. Grimes 	case FLINES:
1239b50d902SRodney W. Grimes 		if (off == 0)
1249b50d902SRodney W. Grimes 			break;
1259b50d902SRodney W. Grimes 		for (;;) {
1269b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF) {
1279b50d902SRodney W. Grimes 				if (ferror(fp)) {
1289b50d902SRodney W. Grimes 					ierr();
1299b50d902SRodney W. Grimes 					return;
1309b50d902SRodney W. Grimes 				}
1319b50d902SRodney W. Grimes 				break;
1329b50d902SRodney W. Grimes 			}
1339b50d902SRodney W. Grimes 			if (ch == '\n' && !--off)
1349b50d902SRodney W. Grimes 				break;
1359b50d902SRodney W. Grimes 		}
1369b50d902SRodney W. Grimes 		break;
1379b50d902SRodney W. Grimes 	case RBYTES:
1389b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode)) {
1399b50d902SRodney W. Grimes 			if (sbp->st_size >= off &&
140bd9dc975SAndrey A. Chernov 			    fseeko(fp, -off, SEEK_END) == -1) {
1419b50d902SRodney W. Grimes 				ierr();
1429b50d902SRodney W. Grimes 				return;
1439b50d902SRodney W. Grimes 			}
1449b50d902SRodney W. Grimes 		} else if (off == 0) {
1459b50d902SRodney W. Grimes 			while (getc(fp) != EOF);
1469b50d902SRodney W. Grimes 			if (ferror(fp)) {
1479b50d902SRodney W. Grimes 				ierr();
1489b50d902SRodney W. Grimes 				return;
1499b50d902SRodney W. Grimes 			}
1509b50d902SRodney W. Grimes 		} else
1516439f56eSAdam David 			if (bytes(fp, off))
1526439f56eSAdam David 				return;
1539b50d902SRodney W. Grimes 		break;
1549b50d902SRodney W. Grimes 	case RLINES:
1559b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode))
1569b50d902SRodney W. Grimes 			if (!off) {
157bd9dc975SAndrey A. Chernov 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
1589b50d902SRodney W. Grimes 					ierr();
1599b50d902SRodney W. Grimes 					return;
1609b50d902SRodney W. Grimes 				}
1619b50d902SRodney W. Grimes 			} else
1629b50d902SRodney W. Grimes 				rlines(fp, off, sbp);
1639b50d902SRodney W. Grimes 		else if (off == 0) {
1649b50d902SRodney W. Grimes 			while (getc(fp) != EOF);
1659b50d902SRodney W. Grimes 			if (ferror(fp)) {
1669b50d902SRodney W. Grimes 				ierr();
1679b50d902SRodney W. Grimes 				return;
1689b50d902SRodney W. Grimes 			}
1699b50d902SRodney W. Grimes 		} else
1706439f56eSAdam David 			if (lines(fp, off))
1716439f56eSAdam David 				return;
1729b50d902SRodney W. Grimes 		break;
1739b50d902SRodney W. Grimes 	}
1749b50d902SRodney W. Grimes 
175b446630fSJonathan Lemon 	if (fflag) {
176b446630fSJonathan Lemon 		kq = kqueue();
177b446630fSJonathan Lemon 		if (kq < 0)
178b446630fSJonathan Lemon 			err(1, "kqueue");
179ea9bd2dfSJonathan Lemon 		action = ADD_EVENTS;
180b446630fSJonathan Lemon 	}
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes 	for (;;) {
1839b50d902SRodney W. Grimes 		while ((ch = getc(fp)) != EOF)
1849b50d902SRodney W. Grimes 			if (putchar(ch) == EOF)
1859b50d902SRodney W. Grimes 				oerr();
1869b50d902SRodney W. Grimes 		if (ferror(fp)) {
1879b50d902SRodney W. Grimes 			ierr();
1889b50d902SRodney W. Grimes 			return;
1899b50d902SRodney W. Grimes 		}
1909b50d902SRodney W. Grimes 		(void)fflush(stdout);
1919b50d902SRodney W. Grimes 		if (! fflag)
1929b50d902SRodney W. Grimes 			break;
1939b50d902SRodney W. Grimes 		clearerr(fp);
194eb1c9439SPeter Wemm 
195ea9bd2dfSJonathan Lemon 		switch (action) {
196ea9bd2dfSJonathan Lemon 		case ADD_EVENTS: {
197b446630fSJonathan Lemon 			int n = 0;
198b446630fSJonathan Lemon 			struct timespec ts = { 0, 0 };
199b446630fSJonathan Lemon 
200b446630fSJonathan Lemon 			if (Fflag && fileno(fp) != STDIN_FILENO) {
2016029e693SJonathan Lemon 				EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
2026029e693SJonathan Lemon 				    EV_ADD | EV_ENABLE | EV_CLEAR,
2036029e693SJonathan Lemon 				    NOTE_DELETE | NOTE_RENAME, 0, 0);
204b446630fSJonathan Lemon 				n++;
205b446630fSJonathan Lemon 			}
2066029e693SJonathan Lemon 			EV_SET(&ev[n], fileno(fp), EVFILT_READ,
207a8fb4e28SJonathan Lemon 			    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
208b446630fSJonathan Lemon 			n++;
209b446630fSJonathan Lemon 
210727a4ed6SJonathan Lemon 			if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
211ea9bd2dfSJonathan Lemon 				close(kq);
212ea9bd2dfSJonathan Lemon 				kq = -1;
213ea9bd2dfSJonathan Lemon 				action = USE_SLEEP;
214ea9bd2dfSJonathan Lemon 			} else {
215ea9bd2dfSJonathan Lemon 				action = USE_KQUEUE;
216ea9bd2dfSJonathan Lemon 			}
217ea9bd2dfSJonathan Lemon 			break;
218b446630fSJonathan Lemon 		}
219b446630fSJonathan Lemon 
220ea9bd2dfSJonathan Lemon 		case USE_KQUEUE:
221727a4ed6SJonathan Lemon 			if (kevent(kq, NULL, 0, ev, 1, NULL) < 0)
222b446630fSJonathan Lemon 				err(1, "kevent");
223b446630fSJonathan Lemon 
224b446630fSJonathan Lemon 			if (ev->filter == EVFILT_VNODE) {
225ea9bd2dfSJonathan Lemon 				/* file was rotated, wait until it reappears */
226ea9bd2dfSJonathan Lemon 				action = USE_SLEEP;
227b446630fSJonathan Lemon 			} else if (ev->data < 0) {
228b446630fSJonathan Lemon 				/* file shrank, reposition to end */
229bd9dc975SAndrey A. Chernov 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
230b446630fSJonathan Lemon 					ierr();
231b446630fSJonathan Lemon 					return;
232eb1c9439SPeter Wemm 				}
233eb1c9439SPeter Wemm 			}
234ea9bd2dfSJonathan Lemon 			break;
235ea9bd2dfSJonathan Lemon 
236ea9bd2dfSJonathan Lemon 		case USE_SLEEP:
237ea9bd2dfSJonathan Lemon                 	(void) usleep(250000);
238ea9bd2dfSJonathan Lemon 	                clearerr(fp);
239ea9bd2dfSJonathan Lemon 
240ea9bd2dfSJonathan Lemon 			if (Fflag && fileno(fp) != STDIN_FILENO &&
241ea9bd2dfSJonathan Lemon 			    stat(fname, &sb2) != -1) {
242ea9bd2dfSJonathan Lemon 				if (sb2.st_ino != sbp->st_ino ||
243ea9bd2dfSJonathan Lemon 				    sb2.st_dev != sbp->st_dev ||
244ea9bd2dfSJonathan Lemon 				    sb2.st_rdev != sbp->st_rdev ||
245ea9bd2dfSJonathan Lemon 				    sb2.st_nlink == 0) {
246ea9bd2dfSJonathan Lemon 					fp = freopen(fname, "r", fp);
247ea9bd2dfSJonathan Lemon 					if (fp == NULL) {
248ea9bd2dfSJonathan Lemon 						ierr();
249ea9bd2dfSJonathan Lemon 						break;
250ea9bd2dfSJonathan Lemon 					}
251ea9bd2dfSJonathan Lemon 					*sbp = sb2;
252ea9bd2dfSJonathan Lemon 					if (kq != -1)
253ea9bd2dfSJonathan Lemon 						action = ADD_EVENTS;
254a8fb4e28SJonathan Lemon 				} else if (kq != -1) {
255a8fb4e28SJonathan Lemon 					action = USE_KQUEUE;
256ea9bd2dfSJonathan Lemon 				}
257ea9bd2dfSJonathan Lemon 			}
258ea9bd2dfSJonathan Lemon 			break;
259ea9bd2dfSJonathan Lemon 		}
2609b50d902SRodney W. Grimes 	}
2619b50d902SRodney W. Grimes }
2629b50d902SRodney W. Grimes 
2639b50d902SRodney W. Grimes /*
2649b50d902SRodney W. Grimes  * rlines -- display the last offset lines of the file.
2659b50d902SRodney W. Grimes  */
2669b50d902SRodney W. Grimes static void
2679b50d902SRodney W. Grimes rlines(fp, off, sbp)
2689b50d902SRodney W. Grimes 	FILE *fp;
269bd9dc975SAndrey A. Chernov 	off_t off;
2709b50d902SRodney W. Grimes 	struct stat *sbp;
2719b50d902SRodney W. Grimes {
272726098d3SDavid Malone 	struct mapinfo map;
273726098d3SDavid Malone 	off_t curoff, size;
274726098d3SDavid Malone 	int i;
2759b50d902SRodney W. Grimes 
2769b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
2779b50d902SRodney W. Grimes 		return;
278726098d3SDavid Malone 	map.start = NULL;
279726098d3SDavid Malone 	map.fd = fileno(fp);
280726098d3SDavid Malone 	map.mapoff = map.maxoff = size;
2819b50d902SRodney W. Grimes 
2826bea9ab4SAndrey A. Chernov 	/*
283726098d3SDavid Malone 	 * Last char is special, ignore whether newline or not. Note that
284726098d3SDavid Malone 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
2856bea9ab4SAndrey A. Chernov 	 */
286726098d3SDavid Malone 	curoff = size - 2;
287726098d3SDavid Malone 	while (curoff >= 0) {
288726098d3SDavid Malone 		if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
2899b50d902SRodney W. Grimes 			ierr();
2909b50d902SRodney W. Grimes 			return;
2919b50d902SRodney W. Grimes 		}
292726098d3SDavid Malone 		for (i = curoff - map.mapoff; i >= 0; i--)
293726098d3SDavid Malone 			if (map.start[i] == '\n' && --off == 0)
294726098d3SDavid Malone 				break;
295726098d3SDavid Malone 		/* `i' is either the map offset of a '\n', or -1. */
296726098d3SDavid Malone 		curoff = map.mapoff + i;
297726098d3SDavid Malone 		if (i >= 0)
298726098d3SDavid Malone 			break;
299726098d3SDavid Malone 	}
300726098d3SDavid Malone 	curoff++;
301726098d3SDavid Malone 	if (mapprint(&map, curoff, size - curoff) != 0) {
302726098d3SDavid Malone 		ierr();
303726098d3SDavid Malone 		exit(1);
304726098d3SDavid Malone 	}
305726098d3SDavid Malone 
306726098d3SDavid Malone 	/* Set the file pointer to reflect the length displayed. */
307a74da62eSAndrey A. Chernov 	if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
308726098d3SDavid Malone 		ierr();
309726098d3SDavid Malone 		return;
310726098d3SDavid Malone 	}
311726098d3SDavid Malone 	if (map.start != NULL && munmap(map.start, map.maplen)) {
31244cf272fSAdam David 		ierr();
3139b50d902SRodney W. Grimes 		return;
3149b50d902SRodney W. Grimes 	}
3159b50d902SRodney W. Grimes }
316