xref: /freebsd/usr.bin/tail/forward.c (revision 54d34ff63deb6b929444de83db4c9fa96e7ba6e5)
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  * 4. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes  *    without specific prior written permission.
199b50d902SRodney W. Grimes  *
209b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes  * SUCH DAMAGE.
319b50d902SRodney W. Grimes  */
329b50d902SRodney W. Grimes 
33814e3a92SMark Murray #include <sys/cdefs.h>
34814e3a92SMark Murray 
35814e3a92SMark Murray __FBSDID("$FreeBSD$");
36814e3a92SMark Murray 
379b50d902SRodney W. Grimes #ifndef lint
38814e3a92SMark Murray static const char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
39247e7cb1SJeroen Ruigrok van der Werven #endif
409b50d902SRodney W. Grimes 
4113829828SPaul Saab #include <sys/param.h>
4213829828SPaul Saab #include <sys/mount.h>
439b50d902SRodney W. Grimes #include <sys/types.h>
449b50d902SRodney W. Grimes #include <sys/stat.h>
459b50d902SRodney W. Grimes #include <sys/time.h>
469b50d902SRodney W. Grimes #include <sys/mman.h>
4732462e82SJonathan Lemon #include <sys/event.h>
489b50d902SRodney W. Grimes 
49814e3a92SMark Murray #include <err.h>
509b50d902SRodney W. Grimes #include <errno.h>
51814e3a92SMark Murray #include <fcntl.h>
52814e3a92SMark Murray #include <limits.h>
539b50d902SRodney W. Grimes #include <stdio.h>
549b50d902SRodney W. Grimes #include <stdlib.h>
559b50d902SRodney W. Grimes #include <string.h>
56814e3a92SMark Murray #include <unistd.h>
57814e3a92SMark Murray 
589b50d902SRodney W. Grimes #include "extern.h"
599b50d902SRodney W. Grimes 
6022da50cfSBrian Somers static void rlines(FILE *, const char *fn, off_t, struct stat *);
6122da50cfSBrian Somers static int show(file_info_t *);
62a2fb00d9SDavid Malone static void set_events(file_info_t *files);
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 
6915a55f79SPaul Richards struct kevent *ev;
7015a55f79SPaul Richards int action = USE_SLEEP;
7115a55f79SPaul Richards int kq;
7215a55f79SPaul Richards 
7376628ce7SXin LI static const file_info_t *last;
7476628ce7SXin LI 
759b50d902SRodney W. Grimes /*
769b50d902SRodney W. Grimes  * forward -- display the file, from an offset, forward.
779b50d902SRodney W. Grimes  *
789b50d902SRodney W. Grimes  * There are eight separate cases for this -- regular and non-regular
799b50d902SRodney W. Grimes  * files, by bytes or lines and from the beginning or end of the file.
809b50d902SRodney W. Grimes  *
819b50d902SRodney W. Grimes  * FBYTES	byte offset from the beginning of the file
829b50d902SRodney W. Grimes  *	REG	seek
839b50d902SRodney W. Grimes  *	NOREG	read, counting bytes
849b50d902SRodney W. Grimes  *
859b50d902SRodney W. Grimes  * FLINES	line offset from the beginning of the file
869b50d902SRodney W. Grimes  *	REG	read, counting lines
879b50d902SRodney W. Grimes  *	NOREG	read, counting lines
889b50d902SRodney W. Grimes  *
899b50d902SRodney W. Grimes  * RBYTES	byte offset from the end of the file
909b50d902SRodney W. Grimes  *	REG	seek
919b50d902SRodney W. Grimes  *	NOREG	cyclically read characters into a wrap-around buffer
929b50d902SRodney W. Grimes  *
939b50d902SRodney W. Grimes  * RLINES
949b50d902SRodney W. Grimes  *	REG	mmap the file and step back until reach the correct offset.
959b50d902SRodney W. Grimes  *	NOREG	cyclically read lines into a wrap-around array of buffers
969b50d902SRodney W. Grimes  */
979b50d902SRodney W. Grimes void
9822da50cfSBrian Somers forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
999b50d902SRodney W. Grimes {
100a2fb00d9SDavid Malone 	int ch;
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) {
11022da50cfSBrian Somers 				ierr(fn);
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)) {
11622da50cfSBrian Somers 					ierr(fn);
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)) {
12822da50cfSBrian Somers 					ierr(fn);
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) {
14122da50cfSBrian Somers 				ierr(fn);
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)) {
14722da50cfSBrian Somers 				ierr(fn);
1489b50d902SRodney W. Grimes 				return;
1499b50d902SRodney W. Grimes 			}
1509b50d902SRodney W. Grimes 		} else
15122da50cfSBrian Somers 			if (bytes(fp, fn, 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) {
15822da50cfSBrian Somers 					ierr(fn);
1599b50d902SRodney W. Grimes 					return;
1609b50d902SRodney W. Grimes 				}
1619b50d902SRodney W. Grimes 			} else
16222da50cfSBrian Somers 				rlines(fp, fn, off, sbp);
1639b50d902SRodney W. Grimes 		else if (off == 0) {
1649b50d902SRodney W. Grimes 			while (getc(fp) != EOF);
1659b50d902SRodney W. Grimes 			if (ferror(fp)) {
16622da50cfSBrian Somers 				ierr(fn);
1679b50d902SRodney W. Grimes 				return;
1689b50d902SRodney W. Grimes 			}
1699b50d902SRodney W. Grimes 		} else
17022da50cfSBrian Somers 			if (lines(fp, fn, off))
1716439f56eSAdam David 				return;
1729b50d902SRodney W. Grimes 		break;
173814e3a92SMark Murray 	default:
174b77b9b9aSMurray Stokely 		break;
1759b50d902SRodney W. Grimes 	}
1769b50d902SRodney W. Grimes 
1779b50d902SRodney W. Grimes 	while ((ch = getc(fp)) != EOF)
1789b50d902SRodney W. Grimes 		if (putchar(ch) == EOF)
1799b50d902SRodney W. Grimes 			oerr();
1809b50d902SRodney W. Grimes 	if (ferror(fp)) {
18122da50cfSBrian Somers 		ierr(fn);
1829b50d902SRodney W. Grimes 		return;
1839b50d902SRodney W. Grimes 	}
1849b50d902SRodney W. Grimes 	(void)fflush(stdout);
1859b50d902SRodney W. Grimes }
1869b50d902SRodney W. Grimes 
1879b50d902SRodney W. Grimes /*
1889b50d902SRodney W. Grimes  * rlines -- display the last offset lines of the file.
1899b50d902SRodney W. Grimes  */
1909b50d902SRodney W. Grimes static void
19122da50cfSBrian Somers rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp)
1929b50d902SRodney W. Grimes {
193726098d3SDavid Malone 	struct mapinfo map;
194726098d3SDavid Malone 	off_t curoff, size;
195726098d3SDavid Malone 	int i;
1969b50d902SRodney W. Grimes 
1979b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
1989b50d902SRodney W. Grimes 		return;
199726098d3SDavid Malone 	map.start = NULL;
200726098d3SDavid Malone 	map.fd = fileno(fp);
201726098d3SDavid Malone 	map.mapoff = map.maxoff = size;
2029b50d902SRodney W. Grimes 
2036bea9ab4SAndrey A. Chernov 	/*
204726098d3SDavid Malone 	 * Last char is special, ignore whether newline or not. Note that
205726098d3SDavid Malone 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
2066bea9ab4SAndrey A. Chernov 	 */
207726098d3SDavid Malone 	curoff = size - 2;
208726098d3SDavid Malone 	while (curoff >= 0) {
209726098d3SDavid Malone 		if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
21022da50cfSBrian Somers 			ierr(fn);
2119b50d902SRodney W. Grimes 			return;
2129b50d902SRodney W. Grimes 		}
213726098d3SDavid Malone 		for (i = curoff - map.mapoff; i >= 0; i--)
214726098d3SDavid Malone 			if (map.start[i] == '\n' && --off == 0)
215726098d3SDavid Malone 				break;
216726098d3SDavid Malone 		/* `i' is either the map offset of a '\n', or -1. */
217726098d3SDavid Malone 		curoff = map.mapoff + i;
218726098d3SDavid Malone 		if (i >= 0)
219726098d3SDavid Malone 			break;
220726098d3SDavid Malone 	}
221726098d3SDavid Malone 	curoff++;
222726098d3SDavid Malone 	if (mapprint(&map, curoff, size - curoff) != 0) {
22322da50cfSBrian Somers 		ierr(fn);
224726098d3SDavid Malone 		exit(1);
225726098d3SDavid Malone 	}
226726098d3SDavid Malone 
227726098d3SDavid Malone 	/* Set the file pointer to reflect the length displayed. */
228a74da62eSAndrey A. Chernov 	if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
22922da50cfSBrian Somers 		ierr(fn);
230726098d3SDavid Malone 		return;
231726098d3SDavid Malone 	}
232726098d3SDavid Malone 	if (map.start != NULL && munmap(map.start, map.maplen)) {
23322da50cfSBrian Somers 		ierr(fn);
2349b50d902SRodney W. Grimes 		return;
2359b50d902SRodney W. Grimes 	}
2369b50d902SRodney W. Grimes }
23715a55f79SPaul Richards 
23822da50cfSBrian Somers static int
23915a55f79SPaul Richards show(file_info_t *file)
24015a55f79SPaul Richards {
2419e777bc6SBrian Somers 	int ch;
24215a55f79SPaul Richards 
24315a55f79SPaul Richards 	while ((ch = getc(file->fp)) != EOF) {
2449e777bc6SBrian Somers 		if (last != file && no_files > 1) {
245aa1d7ce7SFlorent Thoumie 			if (!qflag)
24615a55f79SPaul Richards 				(void)printf("\n==> %s <==\n", file->file_name);
2479e777bc6SBrian Somers 			last = file;
24815a55f79SPaul Richards 		}
24915a55f79SPaul Richards 		if (putchar(ch) == EOF)
25015a55f79SPaul Richards 			oerr();
25115a55f79SPaul Richards 	}
25215a55f79SPaul Richards 	(void)fflush(stdout);
25315a55f79SPaul Richards 	if (ferror(file->fp)) {
25422da50cfSBrian Somers 		fclose(file->fp);
25515a55f79SPaul Richards 		file->fp = NULL;
25622da50cfSBrian Somers 		ierr(file->file_name);
25722da50cfSBrian Somers 		return 0;
25822da50cfSBrian Somers 	}
25915a55f79SPaul Richards 	clearerr(file->fp);
26022da50cfSBrian Somers 	return 1;
26115a55f79SPaul Richards }
26215a55f79SPaul Richards 
263a2fb00d9SDavid Malone static void
26415a55f79SPaul Richards set_events(file_info_t *files)
26515a55f79SPaul Richards {
26615a55f79SPaul Richards 	int i, n = 0;
26715a55f79SPaul Richards 	file_info_t *file;
26815a55f79SPaul Richards 	struct timespec ts;
26913829828SPaul Saab 	struct statfs sf;
27015a55f79SPaul Richards 
27115a55f79SPaul Richards 	ts.tv_sec = 0;
27215a55f79SPaul Richards 	ts.tv_nsec = 0;
27315a55f79SPaul Richards 
27415a55f79SPaul Richards 	action = USE_KQUEUE;
27515a55f79SPaul Richards 	for (i = 0, file = files; i < no_files; i++, file++) {
27615a55f79SPaul Richards 		if (! file->fp)
27715a55f79SPaul Richards 			continue;
27813829828SPaul Saab 
27913829828SPaul Saab 		if (fstatfs(fileno(file->fp), &sf) == 0 &&
28013829828SPaul Saab 		    (sf.f_flags & MNT_LOCAL) == 0) {
28113829828SPaul Saab 			action = USE_SLEEP;
28213829828SPaul Saab 			return;
28313829828SPaul Saab 		}
28413829828SPaul Saab 
28515a55f79SPaul Richards 		if (Fflag && fileno(file->fp) != STDIN_FILENO) {
28615a55f79SPaul Richards 			EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE,
28715a55f79SPaul Richards 			    EV_ADD | EV_ENABLE | EV_CLEAR,
28815a55f79SPaul Richards 			    NOTE_DELETE | NOTE_RENAME, 0, 0);
28915a55f79SPaul Richards 			n++;
29015a55f79SPaul Richards 		}
29115a55f79SPaul Richards 		EV_SET(&ev[n], fileno(file->fp), EVFILT_READ,
29215a55f79SPaul Richards 		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
29315a55f79SPaul Richards 		n++;
29415a55f79SPaul Richards 	}
29515a55f79SPaul Richards 
29615a55f79SPaul Richards 	if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
29715a55f79SPaul Richards 		action = USE_SLEEP;
29815a55f79SPaul Richards 	}
29915a55f79SPaul Richards }
30015a55f79SPaul Richards 
30176628ce7SXin LI /*
30276628ce7SXin LI  * follow -- display the file, from an offset, forward.
30376628ce7SXin LI  *
30476628ce7SXin LI  */
30515a55f79SPaul Richards void
30615a55f79SPaul Richards follow(file_info_t *files, enum STYLE style, off_t off)
30715a55f79SPaul Richards {
30822da50cfSBrian Somers 	int active, ev_change, i, n = -1;
30915a55f79SPaul Richards 	struct stat sb2;
31015a55f79SPaul Richards 	file_info_t *file;
31115a55f79SPaul Richards 	struct timespec ts;
31215a55f79SPaul Richards 
31315a55f79SPaul Richards 	/* Position each of the files */
31415a55f79SPaul Richards 
31515a55f79SPaul Richards 	file = files;
31615a55f79SPaul Richards 	active = 0;
31715a55f79SPaul Richards 	n = 0;
31815a55f79SPaul Richards 	for (i = 0; i < no_files; i++, file++) {
31915a55f79SPaul Richards 		if (file->fp) {
32015a55f79SPaul Richards 			active = 1;
32115a55f79SPaul Richards 			n++;
322aa1d7ce7SFlorent Thoumie 			if (no_files > 1 && !qflag)
32315a55f79SPaul Richards 				(void)printf("\n==> %s <==\n", file->file_name);
32422da50cfSBrian Somers 			forward(file->fp, file->file_name, style, off, &file->st);
32515a55f79SPaul Richards 			if (Fflag && fileno(file->fp) != STDIN_FILENO)
32615a55f79SPaul Richards 				n++;
32715a55f79SPaul Richards 		}
32815a55f79SPaul Richards 	}
32922da50cfSBrian Somers 	if (!Fflag && !active)
33015a55f79SPaul Richards 		return;
33115a55f79SPaul Richards 
33276628ce7SXin LI 	last = --file;
33376628ce7SXin LI 
33415a55f79SPaul Richards 	kq = kqueue();
33515a55f79SPaul Richards 	if (kq < 0)
33615a55f79SPaul Richards 		err(1, "kqueue");
33715a55f79SPaul Richards 	ev = malloc(n * sizeof(struct kevent));
33815a55f79SPaul Richards 	if (! ev)
33915a55f79SPaul Richards 	    err(1, "Couldn't allocate memory for kevents.");
34015a55f79SPaul Richards 	set_events(files);
34115a55f79SPaul Richards 
34215a55f79SPaul Richards 	for (;;) {
34322da50cfSBrian Somers 		ev_change = 0;
34422da50cfSBrian Somers 		if (Fflag) {
34515a55f79SPaul Richards 			for (i = 0, file = files; i < no_files; i++, file++) {
34622da50cfSBrian Somers 				if (!file->fp) {
34722da50cfSBrian Somers 					file->fp = fopen(file->file_name, "r");
34822da50cfSBrian Somers 					if (file->fp != NULL &&
34922da50cfSBrian Somers 					    fstat(fileno(file->fp), &file->st)
35022da50cfSBrian Somers 					    == -1) {
35122da50cfSBrian Somers 						fclose(file->fp);
35222da50cfSBrian Somers 						file->fp = NULL;
35322da50cfSBrian Somers 					}
35422da50cfSBrian Somers 					if (file->fp != NULL)
35522da50cfSBrian Somers 						ev_change++;
35615a55f79SPaul Richards 					continue;
35722da50cfSBrian Somers 				}
35822da50cfSBrian Somers 				if (fileno(file->fp) == STDIN_FILENO)
35922da50cfSBrian Somers 					continue;
36022da50cfSBrian Somers 				if (stat(file->file_name, &sb2) == -1) {
36122da50cfSBrian Somers 					if (errno != ENOENT)
36222da50cfSBrian Somers 						ierr(file->file_name);
36322da50cfSBrian Somers 					show(file);
364*54d34ff6SJilles Tjoelker 					if (file->fp != NULL) {
36522da50cfSBrian Somers 						fclose(file->fp);
36622da50cfSBrian Somers 						file->fp = NULL;
367*54d34ff6SJilles Tjoelker 					}
36822da50cfSBrian Somers 					ev_change++;
36922da50cfSBrian Somers 					continue;
37022da50cfSBrian Somers 				}
37122da50cfSBrian Somers 
37222da50cfSBrian Somers 				if (sb2.st_ino != file->st.st_ino ||
37315a55f79SPaul Richards 				    sb2.st_dev != file->st.st_dev ||
37422da50cfSBrian Somers 				    sb2.st_nlink == 0) {
375d5d2cea1SMarcel Moolenaar 					show(file);
37622da50cfSBrian Somers 					file->fp = freopen(file->file_name, "r",
37722da50cfSBrian Somers 					    file->fp);
37822da50cfSBrian Somers 					if (file->fp != NULL)
37922da50cfSBrian Somers 						memcpy(&file->st, &sb2,
38022da50cfSBrian Somers 						    sizeof(struct stat));
38122da50cfSBrian Somers 					else if (errno != ENOENT)
38222da50cfSBrian Somers 						ierr(file->file_name);
38322da50cfSBrian Somers 					ev_change++;
38422da50cfSBrian Somers 				}
38522da50cfSBrian Somers 			}
38622da50cfSBrian Somers 		}
38722da50cfSBrian Somers 
38822da50cfSBrian Somers 		for (i = 0, file = files; i < no_files; i++, file++)
38922da50cfSBrian Somers 			if (file->fp && !show(file))
39022da50cfSBrian Somers 				ev_change++;
39122da50cfSBrian Somers 
39222da50cfSBrian Somers 		if (ev_change)
39315a55f79SPaul Richards 			set_events(files);
39415a55f79SPaul Richards 
39515a55f79SPaul Richards 		switch (action) {
39615a55f79SPaul Richards 		case USE_KQUEUE:
39715a55f79SPaul Richards 			ts.tv_sec = 1;
39815a55f79SPaul Richards 			ts.tv_nsec = 0;
39915a55f79SPaul Richards 			/*
40015a55f79SPaul Richards 			 * In the -F case we set a timeout to ensure that
40115a55f79SPaul Richards 			 * we re-stat the file at least once every second.
40215a55f79SPaul Richards 			 */
40315a55f79SPaul Richards 			n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
40415a55f79SPaul Richards 			if (n < 0)
40515a55f79SPaul Richards 				err(1, "kevent");
40615a55f79SPaul Richards 			if (n == 0) {
40715a55f79SPaul Richards 				/* timeout */
40815a55f79SPaul Richards 				break;
40915a55f79SPaul Richards 			} else if (ev->filter == EVFILT_READ && ev->data < 0) {
41015a55f79SPaul Richards 				/* file shrank, reposition to end */
41115a55f79SPaul Richards 				if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) {
41222da50cfSBrian Somers 					ierr(file->file_name);
41315a55f79SPaul Richards 					continue;
41415a55f79SPaul Richards 				}
41515a55f79SPaul Richards 			}
41615a55f79SPaul Richards 			break;
41715a55f79SPaul Richards 
41815a55f79SPaul Richards 		case USE_SLEEP:
41915a55f79SPaul Richards 			(void) usleep(250000);
42015a55f79SPaul Richards 			break;
42115a55f79SPaul Richards 		}
42215a55f79SPaul Richards 	}
42315a55f79SPaul Richards }
424