xref: /freebsd/usr.bin/tail/forward.c (revision 6bea9ab43b92f0e9a94ca208064fd1a3f02c3ca2)
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 
619b50d902SRodney W. Grimes static void rlines __P((FILE *, long, 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;
949b50d902SRodney W. Grimes 	long 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;
1099b50d902SRodney W. Grimes 			if (fseek(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 &&
1409b50d902SRodney W. Grimes 			    fseek(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) {
1579b50d902SRodney W. Grimes 				if (fseek(fp, 0L, 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,
2076029e693SJonathan Lemon 			    EV_ADD | EV_ENABLE, 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 */
229b446630fSJonathan Lemon 				if (fseek(fp, 0L, 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;
254ea9bd2dfSJonathan Lemon 				}
255ea9bd2dfSJonathan Lemon 			}
256ea9bd2dfSJonathan Lemon 			break;
257ea9bd2dfSJonathan Lemon 		}
2589b50d902SRodney W. Grimes 	}
2599b50d902SRodney W. Grimes }
2609b50d902SRodney W. Grimes 
2619b50d902SRodney W. Grimes /*
2629b50d902SRodney W. Grimes  * rlines -- display the last offset lines of the file.
2639b50d902SRodney W. Grimes  */
2649b50d902SRodney W. Grimes static void
2659b50d902SRodney W. Grimes rlines(fp, off, sbp)
2669b50d902SRodney W. Grimes 	FILE *fp;
2679b50d902SRodney W. Grimes 	long off;
2689b50d902SRodney W. Grimes 	struct stat *sbp;
2699b50d902SRodney W. Grimes {
27048a1ef22SJeroen Ruigrok van der Werven 	off_t size;
27148a1ef22SJeroen Ruigrok van der Werven 	char *p;
2729b50d902SRodney W. Grimes 	char *start;
2739b50d902SRodney W. Grimes 
2749b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
2759b50d902SRodney W. Grimes 		return;
2769b50d902SRodney W. Grimes 
2776bea9ab4SAndrey A. Chernov 	/*
2786bea9ab4SAndrey A. Chernov 	 * size not passed directly to mmap() below because unclear error
2796bea9ab4SAndrey A. Chernov 	 * diagnostic "Invalid argument".
2806bea9ab4SAndrey A. Chernov 	 */
2814f2554b7SAndrey A. Chernov 	if (size > SIZE_T_MAX || size < 0) {
2820a6f6077SPeter Wemm 		errno = EFBIG;
28344cf272fSAdam David 		ierr();
2844f2554b7SAndrey A. Chernov 		exit(1);
2854f2554b7SAndrey A. Chernov 	}
2864f2554b7SAndrey A. Chernov 
2876bea9ab4SAndrey A. Chernov 	/*
2886bea9ab4SAndrey A. Chernov 	 * XXX: FIXME - mmap() not support files over 2GB
2896bea9ab4SAndrey A. Chernov 	 * Large file processing require alternative implementation,
2906bea9ab4SAndrey A. Chernov 	 * for now print nice error diagnostic at least.
2916bea9ab4SAndrey A. Chernov 	 */
2921410c7ccSAndrey A. Chernov 	if (size > SSIZE_MAX) {
2934f2554b7SAndrey A. Chernov 		errno = EFBIG;
2944f2554b7SAndrey A. Chernov 		ierr();
2954f2554b7SAndrey A. Chernov 		exit(1);
2969b50d902SRodney W. Grimes 	}
2979b50d902SRodney W. Grimes 
2989b50d902SRodney W. Grimes 	if ((start = mmap(NULL, (size_t)size,
2998abdc2ebSAlexander Langer 	    PROT_READ, MAP_SHARED, fileno(fp), (off_t)0)) == MAP_FAILED) {
30044cf272fSAdam David 		ierr();
301ad859a47SAndrey A. Chernov 		exit(1);
3029b50d902SRodney W. Grimes 	}
3039b50d902SRodney W. Grimes 
3049b50d902SRodney W. Grimes 	/* Last char is special, ignore whether newline or not. */
3059b50d902SRodney W. Grimes 	for (p = start + size - 1; --size;)
3069b50d902SRodney W. Grimes 		if (*--p == '\n' && !--off) {
3079b50d902SRodney W. Grimes 			++p;
3089b50d902SRodney W. Grimes 			break;
3099b50d902SRodney W. Grimes 		}
3109b50d902SRodney W. Grimes 
3119b50d902SRodney W. Grimes 	/* Set the file pointer to reflect the length displayed. */
3129b50d902SRodney W. Grimes 	size = sbp->st_size - size;
3139b50d902SRodney W. Grimes 	WR(p, size);
3144f2554b7SAndrey A. Chernov 	if (fseek(fp, 0L, SEEK_END) == -1) {
3159b50d902SRodney W. Grimes 		ierr();
3169b50d902SRodney W. Grimes 		return;
3179b50d902SRodney W. Grimes 	}
3189b50d902SRodney W. Grimes 	if (munmap(start, (size_t)sbp->st_size)) {
31944cf272fSAdam David 		ierr();
3209b50d902SRodney W. Grimes 		return;
3219b50d902SRodney W. Grimes 	}
3229b50d902SRodney W. Grimes }
323