forward.c revision 69552
1/*-
2 * Copyright (c) 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Edward Sze-Tyan Wang.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
40#endif
41static const char rcsid[] =
42  "$FreeBSD: head/usr.bin/tail/forward.c 69552 2000-12-03 17:05:45Z asmodai $";
43#endif /* not lint */
44
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <sys/time.h>
48#include <sys/mman.h>
49#include <sys/event.h>
50
51#include <limits.h>
52#include <fcntl.h>
53#include <errno.h>
54#include <unistd.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <string.h>
58#include <err.h>
59#include "extern.h"
60
61static void rlines __P((FILE *, long, struct stat *));
62
63/* defines for inner loop actions */
64#define USE_SLEEP	0
65#define USE_KQUEUE	1
66#define ADD_EVENTS	2
67
68/*
69 * forward -- display the file, from an offset, forward.
70 *
71 * There are eight separate cases for this -- regular and non-regular
72 * files, by bytes or lines and from the beginning or end of the file.
73 *
74 * FBYTES	byte offset from the beginning of the file
75 *	REG	seek
76 *	NOREG	read, counting bytes
77 *
78 * FLINES	line offset from the beginning of the file
79 *	REG	read, counting lines
80 *	NOREG	read, counting lines
81 *
82 * RBYTES	byte offset from the end of the file
83 *	REG	seek
84 *	NOREG	cyclically read characters into a wrap-around buffer
85 *
86 * RLINES
87 *	REG	mmap the file and step back until reach the correct offset.
88 *	NOREG	cyclically read lines into a wrap-around array of buffers
89 */
90void
91forward(fp, style, off, sbp)
92	FILE *fp;
93	enum STYLE style;
94	long off;
95	struct stat *sbp;
96{
97	int ch, kq = -1;
98	int action = USE_SLEEP;
99	struct kevent ev[2];
100	struct stat sb2;
101
102	switch(style) {
103	case FBYTES:
104		if (off == 0)
105			break;
106		if (S_ISREG(sbp->st_mode)) {
107			if (sbp->st_size < off)
108				off = sbp->st_size;
109			if (fseek(fp, off, SEEK_SET) == -1) {
110				ierr();
111				return;
112			}
113		} else while (off--)
114			if ((ch = getc(fp)) == EOF) {
115				if (ferror(fp)) {
116					ierr();
117					return;
118				}
119				break;
120			}
121		break;
122	case FLINES:
123		if (off == 0)
124			break;
125		for (;;) {
126			if ((ch = getc(fp)) == EOF) {
127				if (ferror(fp)) {
128					ierr();
129					return;
130				}
131				break;
132			}
133			if (ch == '\n' && !--off)
134				break;
135		}
136		break;
137	case RBYTES:
138		if (S_ISREG(sbp->st_mode)) {
139			if (sbp->st_size >= off &&
140			    fseek(fp, -off, SEEK_END) == -1) {
141				ierr();
142				return;
143			}
144		} else if (off == 0) {
145			while (getc(fp) != EOF);
146			if (ferror(fp)) {
147				ierr();
148				return;
149			}
150		} else
151			if (bytes(fp, off))
152				return;
153		break;
154	case RLINES:
155		if (S_ISREG(sbp->st_mode))
156			if (!off) {
157				if (fseek(fp, 0L, SEEK_END) == -1) {
158					ierr();
159					return;
160				}
161			} else
162				rlines(fp, off, sbp);
163		else if (off == 0) {
164			while (getc(fp) != EOF);
165			if (ferror(fp)) {
166				ierr();
167				return;
168			}
169		} else
170			if (lines(fp, off))
171				return;
172		break;
173	}
174
175	if (fflag) {
176		kq = kqueue();
177		if (kq < 0)
178			err(1, "kqueue");
179		action = ADD_EVENTS;
180	}
181
182	for (;;) {
183		while ((ch = getc(fp)) != EOF)
184			if (putchar(ch) == EOF)
185				oerr();
186		if (ferror(fp)) {
187			ierr();
188			return;
189		}
190		(void)fflush(stdout);
191		if (! fflag)
192			break;
193		clearerr(fp);
194
195		switch (action) {
196		case ADD_EVENTS: {
197			int n = 0;
198			struct timespec ts = { 0, 0 };
199
200			if (Fflag && fileno(fp) != STDIN_FILENO) {
201				ev[n].ident = fileno(fp);
202				ev[n].filter = EVFILT_VNODE;
203				ev[n].flags = EV_ADD | EV_ENABLE | EV_CLEAR;
204				ev[n].fflags = NOTE_DELETE | NOTE_RENAME;
205				n++;
206			}
207			ev[n].ident = fileno(fp);
208			ev[n].filter = EVFILT_READ;
209			ev[n].flags = EV_ADD | EV_ENABLE;
210			n++;
211
212			if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
213				close(kq);
214				kq = -1;
215				action = USE_SLEEP;
216			} else {
217				action = USE_KQUEUE;
218			}
219			break;
220		}
221
222		case USE_KQUEUE:
223			if (kevent(kq, NULL, 0, ev, 1, NULL) < 0)
224				err(1, "kevent");
225
226			if (ev->filter == EVFILT_VNODE) {
227				/* file was rotated, wait until it reappears */
228				action = USE_SLEEP;
229			} else if (ev->data < 0) {
230				/* file shrank, reposition to end */
231				if (fseek(fp, 0L, SEEK_END) == -1) {
232					ierr();
233					return;
234				}
235			}
236			break;
237
238		case USE_SLEEP:
239                	(void) usleep(250000);
240	                clearerr(fp);
241
242			if (Fflag && fileno(fp) != STDIN_FILENO &&
243			    stat(fname, &sb2) != -1) {
244				if (sb2.st_ino != sbp->st_ino ||
245				    sb2.st_dev != sbp->st_dev ||
246				    sb2.st_rdev != sbp->st_rdev ||
247				    sb2.st_nlink == 0) {
248					fp = freopen(fname, "r", fp);
249					if (fp == NULL) {
250						ierr();
251						break;
252					}
253					*sbp = sb2;
254					if (kq != -1)
255						action = ADD_EVENTS;
256				}
257			}
258			break;
259		}
260	}
261}
262
263/*
264 * rlines -- display the last offset lines of the file.
265 */
266static void
267rlines(fp, off, sbp)
268	FILE *fp;
269	long off;
270	struct stat *sbp;
271{
272	off_t size;
273	char *p;
274	char *start;
275
276	if (!(size = sbp->st_size))
277		return;
278
279	if (size > SIZE_T_MAX) {
280		errno = EFBIG;
281		ierr();
282		return;
283	}
284
285	if ((start = mmap(NULL, (size_t)size,
286	    PROT_READ, MAP_SHARED, fileno(fp), (off_t)0)) == MAP_FAILED) {
287		ierr();
288		return;
289	}
290
291	/* Last char is special, ignore whether newline or not. */
292	for (p = start + size - 1; --size;)
293		if (*--p == '\n' && !--off) {
294			++p;
295			break;
296		}
297
298	/* Set the file pointer to reflect the length displayed. */
299	size = sbp->st_size - size;
300	WR(p, size);
301	if (fseek(fp, (long)sbp->st_size, SEEK_SET) == -1) {
302		ierr();
303		return;
304	}
305	if (munmap(start, (size_t)sbp->st_size)) {
306		ierr();
307		return;
308	}
309}
310