xref: /freebsd/usr.bin/tail/forward.c (revision ef6f20ce47613db7cc615b45f4b6fa1eb99ae0ba)
19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1991, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * Edward Sze-Tyan Wang.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
35814e3a92SMark Murray #include <sys/cdefs.h>
36814e3a92SMark Murray 
37814e3a92SMark Murray __FBSDID("$FreeBSD$");
38814e3a92SMark Murray 
399b50d902SRodney W. Grimes #ifndef lint
40814e3a92SMark Murray static const char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
41247e7cb1SJeroen Ruigrok van der Werven #endif
429b50d902SRodney W. Grimes 
4313829828SPaul Saab #include <sys/param.h>
4413829828SPaul Saab #include <sys/mount.h>
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 
60c851fce6SMariusz Zaborski #include <libcasper.h>
61c851fce6SMariusz Zaborski #include <casper/cap_fileargs.h>
62c851fce6SMariusz Zaborski 
639b50d902SRodney W. Grimes #include "extern.h"
649b50d902SRodney W. Grimes 
6522da50cfSBrian Somers static void rlines(FILE *, const char *fn, off_t, struct stat *);
6622da50cfSBrian Somers static int show(file_info_t *);
67a2fb00d9SDavid Malone static void set_events(file_info_t *files);
689b50d902SRodney W. Grimes 
69ea9bd2dfSJonathan Lemon /* defines for inner loop actions */
70ea9bd2dfSJonathan Lemon #define USE_SLEEP	0
71ea9bd2dfSJonathan Lemon #define USE_KQUEUE	1
72ea9bd2dfSJonathan Lemon #define ADD_EVENTS	2
73ea9bd2dfSJonathan Lemon 
74c7c497f1SEd Schouten static struct kevent *ev;
75c7c497f1SEd Schouten static int action = USE_SLEEP;
76c7c497f1SEd Schouten static int kq;
7715a55f79SPaul Richards 
7876628ce7SXin LI static const file_info_t *last;
7976628ce7SXin LI 
809b50d902SRodney W. Grimes /*
819b50d902SRodney W. Grimes  * forward -- display the file, from an offset, forward.
829b50d902SRodney W. Grimes  *
839b50d902SRodney W. Grimes  * There are eight separate cases for this -- regular and non-regular
849b50d902SRodney W. Grimes  * files, by bytes or lines and from the beginning or end of the file.
859b50d902SRodney W. Grimes  *
869b50d902SRodney W. Grimes  * FBYTES	byte offset from the beginning of the file
879b50d902SRodney W. Grimes  *	REG	seek
889b50d902SRodney W. Grimes  *	NOREG	read, counting bytes
899b50d902SRodney W. Grimes  *
909b50d902SRodney W. Grimes  * FLINES	line offset from the beginning of the file
919b50d902SRodney W. Grimes  *	REG	read, counting lines
929b50d902SRodney W. Grimes  *	NOREG	read, counting lines
939b50d902SRodney W. Grimes  *
949b50d902SRodney W. Grimes  * RBYTES	byte offset from the end of the file
959b50d902SRodney W. Grimes  *	REG	seek
969b50d902SRodney W. Grimes  *	NOREG	cyclically read characters into a wrap-around buffer
979b50d902SRodney W. Grimes  *
989b50d902SRodney W. Grimes  * RLINES
999b50d902SRodney W. Grimes  *	REG	mmap the file and step back until reach the correct offset.
1009b50d902SRodney W. Grimes  *	NOREG	cyclically read lines into a wrap-around array of buffers
1019b50d902SRodney W. Grimes  */
1029b50d902SRodney W. Grimes void
10322da50cfSBrian Somers forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
1049b50d902SRodney W. Grimes {
105a2fb00d9SDavid Malone 	int ch;
1069b50d902SRodney W. Grimes 
1079b50d902SRodney W. Grimes 	switch(style) {
1089b50d902SRodney W. Grimes 	case FBYTES:
1099b50d902SRodney W. Grimes 		if (off == 0)
1109b50d902SRodney W. Grimes 			break;
1119b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode)) {
1129b50d902SRodney W. Grimes 			if (sbp->st_size < off)
1139b50d902SRodney W. Grimes 				off = sbp->st_size;
114bd9dc975SAndrey A. Chernov 			if (fseeko(fp, off, SEEK_SET) == -1) {
11522da50cfSBrian Somers 				ierr(fn);
1169b50d902SRodney W. Grimes 				return;
1179b50d902SRodney W. Grimes 			}
1189b50d902SRodney W. Grimes 		} else while (off--)
1199b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF) {
1209b50d902SRodney W. Grimes 				if (ferror(fp)) {
12122da50cfSBrian Somers 					ierr(fn);
1229b50d902SRodney W. Grimes 					return;
1239b50d902SRodney W. Grimes 				}
1249b50d902SRodney W. Grimes 				break;
1259b50d902SRodney W. Grimes 			}
1269b50d902SRodney W. Grimes 		break;
1279b50d902SRodney W. Grimes 	case FLINES:
1289b50d902SRodney W. Grimes 		if (off == 0)
1299b50d902SRodney W. Grimes 			break;
1309b50d902SRodney W. Grimes 		for (;;) {
1319b50d902SRodney W. Grimes 			if ((ch = getc(fp)) == EOF) {
1329b50d902SRodney W. Grimes 				if (ferror(fp)) {
13322da50cfSBrian Somers 					ierr(fn);
1349b50d902SRodney W. Grimes 					return;
1359b50d902SRodney W. Grimes 				}
1369b50d902SRodney W. Grimes 				break;
1379b50d902SRodney W. Grimes 			}
1389b50d902SRodney W. Grimes 			if (ch == '\n' && !--off)
1399b50d902SRodney W. Grimes 				break;
1409b50d902SRodney W. Grimes 		}
1419b50d902SRodney W. Grimes 		break;
1429b50d902SRodney W. Grimes 	case RBYTES:
1439b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode)) {
1449b50d902SRodney W. Grimes 			if (sbp->st_size >= off &&
145bd9dc975SAndrey A. Chernov 			    fseeko(fp, -off, SEEK_END) == -1) {
14622da50cfSBrian Somers 				ierr(fn);
1479b50d902SRodney W. Grimes 				return;
1489b50d902SRodney W. Grimes 			}
1499b50d902SRodney W. Grimes 		} else if (off == 0) {
1509b50d902SRodney W. Grimes 			while (getc(fp) != EOF);
1519b50d902SRodney W. Grimes 			if (ferror(fp)) {
15222da50cfSBrian Somers 				ierr(fn);
1539b50d902SRodney W. Grimes 				return;
1549b50d902SRodney W. Grimes 			}
1559b50d902SRodney W. Grimes 		} else
15622da50cfSBrian Somers 			if (bytes(fp, fn, off))
1576439f56eSAdam David 				return;
1589b50d902SRodney W. Grimes 		break;
1599b50d902SRodney W. Grimes 	case RLINES:
1609b50d902SRodney W. Grimes 		if (S_ISREG(sbp->st_mode))
1619b50d902SRodney W. Grimes 			if (!off) {
162bd9dc975SAndrey A. Chernov 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
16322da50cfSBrian Somers 					ierr(fn);
1649b50d902SRodney W. Grimes 					return;
1659b50d902SRodney W. Grimes 				}
1669b50d902SRodney W. Grimes 			} else
16722da50cfSBrian Somers 				rlines(fp, fn, off, sbp);
1689b50d902SRodney W. Grimes 		else if (off == 0) {
1699b50d902SRodney W. Grimes 			while (getc(fp) != EOF);
1709b50d902SRodney W. Grimes 			if (ferror(fp)) {
17122da50cfSBrian Somers 				ierr(fn);
1729b50d902SRodney W. Grimes 				return;
1739b50d902SRodney W. Grimes 			}
1749b50d902SRodney W. Grimes 		} else
17522da50cfSBrian Somers 			if (lines(fp, fn, off))
1766439f56eSAdam David 				return;
1779b50d902SRodney W. Grimes 		break;
178814e3a92SMark Murray 	default:
179b77b9b9aSMurray Stokely 		break;
1809b50d902SRodney W. Grimes 	}
1819b50d902SRodney W. Grimes 
1829b50d902SRodney W. Grimes 	while ((ch = getc(fp)) != EOF)
1839b50d902SRodney W. Grimes 		if (putchar(ch) == EOF)
1849b50d902SRodney W. Grimes 			oerr();
1859b50d902SRodney W. Grimes 	if (ferror(fp)) {
18622da50cfSBrian Somers 		ierr(fn);
1879b50d902SRodney W. Grimes 		return;
1889b50d902SRodney W. Grimes 	}
1899b50d902SRodney W. Grimes 	(void)fflush(stdout);
1909b50d902SRodney W. Grimes }
1919b50d902SRodney W. Grimes 
1929b50d902SRodney W. Grimes /*
1939b50d902SRodney W. Grimes  * rlines -- display the last offset lines of the file.
1949b50d902SRodney W. Grimes  */
1959b50d902SRodney W. Grimes static void
19622da50cfSBrian Somers rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp)
1979b50d902SRodney W. Grimes {
198726098d3SDavid Malone 	struct mapinfo map;
199726098d3SDavid Malone 	off_t curoff, size;
200726098d3SDavid Malone 	int i;
2019b50d902SRodney W. Grimes 
2029b50d902SRodney W. Grimes 	if (!(size = sbp->st_size))
2039b50d902SRodney W. Grimes 		return;
204726098d3SDavid Malone 	map.start = NULL;
205726098d3SDavid Malone 	map.fd = fileno(fp);
206726098d3SDavid Malone 	map.mapoff = map.maxoff = size;
2079b50d902SRodney W. Grimes 
2086bea9ab4SAndrey A. Chernov 	/*
209726098d3SDavid Malone 	 * Last char is special, ignore whether newline or not. Note that
210726098d3SDavid Malone 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
2116bea9ab4SAndrey A. Chernov 	 */
212726098d3SDavid Malone 	curoff = size - 2;
213726098d3SDavid Malone 	while (curoff >= 0) {
214726098d3SDavid Malone 		if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
21522da50cfSBrian Somers 			ierr(fn);
2169b50d902SRodney W. Grimes 			return;
2179b50d902SRodney W. Grimes 		}
218726098d3SDavid Malone 		for (i = curoff - map.mapoff; i >= 0; i--)
219726098d3SDavid Malone 			if (map.start[i] == '\n' && --off == 0)
220726098d3SDavid Malone 				break;
221726098d3SDavid Malone 		/* `i' is either the map offset of a '\n', or -1. */
222726098d3SDavid Malone 		curoff = map.mapoff + i;
223726098d3SDavid Malone 		if (i >= 0)
224726098d3SDavid Malone 			break;
225726098d3SDavid Malone 	}
226726098d3SDavid Malone 	curoff++;
227726098d3SDavid Malone 	if (mapprint(&map, curoff, size - curoff) != 0) {
22822da50cfSBrian Somers 		ierr(fn);
229726098d3SDavid Malone 		exit(1);
230726098d3SDavid Malone 	}
231726098d3SDavid Malone 
232726098d3SDavid Malone 	/* Set the file pointer to reflect the length displayed. */
233a74da62eSAndrey A. Chernov 	if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
23422da50cfSBrian Somers 		ierr(fn);
235726098d3SDavid Malone 		return;
236726098d3SDavid Malone 	}
237726098d3SDavid Malone 	if (map.start != NULL && munmap(map.start, map.maplen)) {
23822da50cfSBrian Somers 		ierr(fn);
2399b50d902SRodney W. Grimes 		return;
2409b50d902SRodney W. Grimes 	}
2419b50d902SRodney W. Grimes }
24215a55f79SPaul Richards 
24322da50cfSBrian Somers static int
24415a55f79SPaul Richards show(file_info_t *file)
24515a55f79SPaul Richards {
2469e777bc6SBrian Somers 	int ch;
24715a55f79SPaul Richards 
24815a55f79SPaul Richards 	while ((ch = getc(file->fp)) != EOF) {
249643ac419SXin LI 		if (last != file) {
250643ac419SXin LI 			if (vflag || (qflag == 0 && no_files > 1))
251849d265dSJaakko Heinonen 				printfn(file->file_name, 1);
2529e777bc6SBrian Somers 			last = file;
25315a55f79SPaul Richards 		}
25415a55f79SPaul Richards 		if (putchar(ch) == EOF)
25515a55f79SPaul Richards 			oerr();
25615a55f79SPaul Richards 	}
25715a55f79SPaul Richards 	(void)fflush(stdout);
25815a55f79SPaul Richards 	if (ferror(file->fp)) {
25922da50cfSBrian Somers 		fclose(file->fp);
26015a55f79SPaul Richards 		file->fp = NULL;
26122da50cfSBrian Somers 		ierr(file->file_name);
26222da50cfSBrian Somers 		return 0;
26322da50cfSBrian Somers 	}
26415a55f79SPaul Richards 	clearerr(file->fp);
26522da50cfSBrian Somers 	return 1;
26615a55f79SPaul Richards }
26715a55f79SPaul Richards 
268a2fb00d9SDavid Malone static void
26915a55f79SPaul Richards set_events(file_info_t *files)
27015a55f79SPaul Richards {
27115a55f79SPaul Richards 	int i, n = 0;
27215a55f79SPaul Richards 	file_info_t *file;
27315a55f79SPaul Richards 	struct timespec ts;
27413829828SPaul Saab 	struct statfs sf;
27515a55f79SPaul Richards 
27615a55f79SPaul Richards 	ts.tv_sec = 0;
27715a55f79SPaul Richards 	ts.tv_nsec = 0;
27815a55f79SPaul Richards 
27915a55f79SPaul Richards 	action = USE_KQUEUE;
28015a55f79SPaul Richards 	for (i = 0, file = files; i < no_files; i++, file++) {
28115a55f79SPaul Richards 		if (! file->fp)
28215a55f79SPaul Richards 			continue;
28313829828SPaul Saab 
28413829828SPaul Saab 		if (fstatfs(fileno(file->fp), &sf) == 0 &&
28513829828SPaul Saab 		    (sf.f_flags & MNT_LOCAL) == 0) {
28613829828SPaul Saab 			action = USE_SLEEP;
28713829828SPaul Saab 			return;
28813829828SPaul Saab 		}
28913829828SPaul Saab 
29015a55f79SPaul Richards 		if (Fflag && fileno(file->fp) != STDIN_FILENO) {
29115a55f79SPaul Richards 			EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE,
29215a55f79SPaul Richards 			    EV_ADD | EV_ENABLE | EV_CLEAR,
29315a55f79SPaul Richards 			    NOTE_DELETE | NOTE_RENAME, 0, 0);
29415a55f79SPaul Richards 			n++;
29515a55f79SPaul Richards 		}
29615a55f79SPaul Richards 		EV_SET(&ev[n], fileno(file->fp), EVFILT_READ,
29715a55f79SPaul Richards 		    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
29815a55f79SPaul Richards 		n++;
29915a55f79SPaul Richards 	}
30015a55f79SPaul Richards 
30115a55f79SPaul Richards 	if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
30215a55f79SPaul Richards 		action = USE_SLEEP;
30315a55f79SPaul Richards 	}
30415a55f79SPaul Richards }
30515a55f79SPaul Richards 
30676628ce7SXin LI /*
30776628ce7SXin LI  * follow -- display the file, from an offset, forward.
30876628ce7SXin LI  *
30976628ce7SXin LI  */
31015a55f79SPaul Richards void
31115a55f79SPaul Richards follow(file_info_t *files, enum STYLE style, off_t off)
31215a55f79SPaul Richards {
31322da50cfSBrian Somers 	int active, ev_change, i, n = -1;
31415a55f79SPaul Richards 	struct stat sb2;
31515a55f79SPaul Richards 	file_info_t *file;
316c851fce6SMariusz Zaborski 	FILE *ftmp;
31715a55f79SPaul Richards 	struct timespec ts;
31815a55f79SPaul Richards 
31915a55f79SPaul Richards 	/* Position each of the files */
32015a55f79SPaul Richards 
32115a55f79SPaul Richards 	file = files;
32215a55f79SPaul Richards 	active = 0;
32315a55f79SPaul Richards 	n = 0;
32415a55f79SPaul Richards 	for (i = 0; i < no_files; i++, file++) {
32515a55f79SPaul Richards 		if (file->fp) {
32615a55f79SPaul Richards 			active = 1;
32715a55f79SPaul Richards 			n++;
328643ac419SXin LI 			if (vflag || (qflag == 0 && no_files > 1))
329849d265dSJaakko Heinonen 				printfn(file->file_name, 1);
33022da50cfSBrian Somers 			forward(file->fp, file->file_name, style, off, &file->st);
33115a55f79SPaul Richards 			if (Fflag && fileno(file->fp) != STDIN_FILENO)
33215a55f79SPaul Richards 				n++;
33315a55f79SPaul Richards 		}
33415a55f79SPaul Richards 	}
33522da50cfSBrian Somers 	if (!Fflag && !active)
33615a55f79SPaul Richards 		return;
33715a55f79SPaul Richards 
33876628ce7SXin LI 	last = --file;
33976628ce7SXin LI 
34015a55f79SPaul Richards 	kq = kqueue();
34115a55f79SPaul Richards 	if (kq < 0)
34215a55f79SPaul Richards 		err(1, "kqueue");
34315a55f79SPaul Richards 	ev = malloc(n * sizeof(struct kevent));
34415a55f79SPaul Richards 	if (! ev)
34515a55f79SPaul Richards 	    err(1, "Couldn't allocate memory for kevents.");
34615a55f79SPaul Richards 	set_events(files);
34715a55f79SPaul Richards 
34815a55f79SPaul Richards 	for (;;) {
34922da50cfSBrian Somers 		ev_change = 0;
35022da50cfSBrian Somers 		if (Fflag) {
35115a55f79SPaul Richards 			for (i = 0, file = files; i < no_files; i++, file++) {
35222da50cfSBrian Somers 				if (!file->fp) {
353c851fce6SMariusz Zaborski 					file->fp =
354c851fce6SMariusz Zaborski 					    fileargs_fopen(fa, file->file_name,
355c851fce6SMariusz Zaborski 					    "r");
35622da50cfSBrian Somers 					if (file->fp != NULL &&
35722da50cfSBrian Somers 					    fstat(fileno(file->fp), &file->st)
35822da50cfSBrian Somers 					    == -1) {
35922da50cfSBrian Somers 						fclose(file->fp);
36022da50cfSBrian Somers 						file->fp = NULL;
36122da50cfSBrian Somers 					}
36222da50cfSBrian Somers 					if (file->fp != NULL)
36322da50cfSBrian Somers 						ev_change++;
36415a55f79SPaul Richards 					continue;
36522da50cfSBrian Somers 				}
36622da50cfSBrian Somers 				if (fileno(file->fp) == STDIN_FILENO)
36722da50cfSBrian Somers 					continue;
368c851fce6SMariusz Zaborski 				ftmp = fileargs_fopen(fa, file->file_name, "r");
369c851fce6SMariusz Zaborski 				if (ftmp == NULL ||
3707787e7eeSChuck Silvers 				    fstat(fileno(ftmp), &sb2) == -1) {
37122da50cfSBrian Somers 					if (errno != ENOENT)
37222da50cfSBrian Somers 						ierr(file->file_name);
37322da50cfSBrian Somers 					show(file);
37454d34ff6SJilles Tjoelker 					if (file->fp != NULL) {
37522da50cfSBrian Somers 						fclose(file->fp);
37622da50cfSBrian Somers 						file->fp = NULL;
37754d34ff6SJilles Tjoelker 					}
378c851fce6SMariusz Zaborski 					if (ftmp != NULL) {
379c851fce6SMariusz Zaborski 						fclose(ftmp);
380c851fce6SMariusz Zaborski 					}
38122da50cfSBrian Somers 					ev_change++;
38222da50cfSBrian Somers 					continue;
38322da50cfSBrian Somers 				}
38422da50cfSBrian Somers 
38522da50cfSBrian Somers 				if (sb2.st_ino != file->st.st_ino ||
38615a55f79SPaul Richards 				    sb2.st_dev != file->st.st_dev ||
38722da50cfSBrian Somers 				    sb2.st_nlink == 0) {
388d5d2cea1SMarcel Moolenaar 					show(file);
389c851fce6SMariusz Zaborski 					fclose(file->fp);
390c851fce6SMariusz Zaborski 					file->fp = ftmp;
39122da50cfSBrian Somers 					memcpy(&file->st, &sb2,
39222da50cfSBrian Somers 					    sizeof(struct stat));
39322da50cfSBrian Somers 					ev_change++;
394c851fce6SMariusz Zaborski 				} else {
395c851fce6SMariusz Zaborski 					fclose(ftmp);
39622da50cfSBrian Somers 				}
39722da50cfSBrian Somers 			}
39822da50cfSBrian Somers 		}
39922da50cfSBrian Somers 
40022da50cfSBrian Somers 		for (i = 0, file = files; i < no_files; i++, file++)
40122da50cfSBrian Somers 			if (file->fp && !show(file))
40222da50cfSBrian Somers 				ev_change++;
40322da50cfSBrian Somers 
40422da50cfSBrian Somers 		if (ev_change)
40515a55f79SPaul Richards 			set_events(files);
40615a55f79SPaul Richards 
40715a55f79SPaul Richards 		switch (action) {
40815a55f79SPaul Richards 		case USE_KQUEUE:
40915a55f79SPaul Richards 			ts.tv_sec = 1;
41015a55f79SPaul Richards 			ts.tv_nsec = 0;
41115a55f79SPaul Richards 			/*
41215a55f79SPaul Richards 			 * In the -F case we set a timeout to ensure that
41315a55f79SPaul Richards 			 * we re-stat the file at least once every second.
414*ef6f20ceSWarner Losh 			 * If we've recieved EINTR, ignore it. Both reasons
415*ef6f20ceSWarner Losh 			 * for its generation are transient.
41615a55f79SPaul Richards 			 */
417*ef6f20ceSWarner Losh 			do {
41815a55f79SPaul Richards 				n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
419*ef6f20ceSWarner Losh 				if (n < 0 && errno == EINTR)
420*ef6f20ceSWarner Losh 					continue;
42115a55f79SPaul Richards 				if (n < 0)
42215a55f79SPaul Richards 					err(1, "kevent");
423*ef6f20ceSWarner Losh 			} while (n < 0);
42415a55f79SPaul Richards 			if (n == 0) {
42515a55f79SPaul Richards 				/* timeout */
42615a55f79SPaul Richards 				break;
42715a55f79SPaul Richards 			} else if (ev->filter == EVFILT_READ && ev->data < 0) {
42815a55f79SPaul Richards 				/* file shrank, reposition to end */
42915a55f79SPaul Richards 				if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) {
43022da50cfSBrian Somers 					ierr(file->file_name);
43115a55f79SPaul Richards 					continue;
43215a55f79SPaul Richards 				}
43315a55f79SPaul Richards 			}
43415a55f79SPaul Richards 			break;
43515a55f79SPaul Richards 
43615a55f79SPaul Richards 		case USE_SLEEP:
43715a55f79SPaul Richards 			(void) usleep(250000);
43815a55f79SPaul Richards 			break;
43915a55f79SPaul Richards 		}
44015a55f79SPaul Richards 	}
44115a55f79SPaul Richards }
442