2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Sze-Tyan Wang.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * @(#)forward.c 8.1 (Berkeley) 6/6/93
37 * $FreeBSD: src/usr.bin/tail/forward.c,v 1.11.6.7 2003/01/07 05:26:22 tjr Exp $
38 * $DragonFly: src/usr.bin/tail/forward.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
41 #include <sys/types.h>
45 #include <sys/event.h>
57 static void rlines __P((FILE *, off_t, struct stat *));
59 /* defines for inner loop actions */
65 * forward -- display the file, from an offset, forward.
67 * There are eight separate cases for this -- regular and non-regular
68 * files, by bytes or lines and from the beginning or end of the file.
70 * FBYTES byte offset from the beginning of the file
72 * NOREG read, counting bytes
74 * FLINES line offset from the beginning of the file
75 * REG read, counting lines
76 * NOREG read, counting lines
78 * RBYTES byte offset from the end of the file
80 * NOREG cyclically read characters into a wrap-around buffer
83 * REG mmap the file and step back until reach the correct offset.
84 * NOREG cyclically read lines into a wrap-around array of buffers
87 forward(fp, style, off, sbp)
94 int action = USE_SLEEP;
103 if (S_ISREG(sbp->st_mode)) {
104 if (sbp->st_size < off)
106 if (fseeko(fp, off, SEEK_SET) == -1) {
111 if ((ch = getc(fp)) == EOF) {
123 if ((ch = getc(fp)) == EOF) {
130 if (ch == '\n' && !--off)
135 if (S_ISREG(sbp->st_mode)) {
136 if (sbp->st_size >= off &&
137 fseeko(fp, -off, SEEK_END) == -1) {
141 } else if (off == 0) {
142 while (getc(fp) != EOF);
152 if (S_ISREG(sbp->st_mode))
154 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
159 rlines(fp, off, sbp);
161 while (getc(fp) != EOF);
180 while ((ch = getc(fp)) != EOF)
181 if (putchar(ch) == EOF)
187 (void)fflush(stdout);
198 if (Fflag && fileno(fp) != STDIN_FILENO) {
199 EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
200 EV_ADD | EV_ENABLE | EV_CLEAR,
201 NOTE_DELETE | NOTE_RENAME, 0, 0);
204 EV_SET(&ev[n], fileno(fp), EVFILT_READ,
205 EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
208 if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
219 * In the -F case we set a timeout to ensure that
220 * we re-stat the file at least once every second.
222 n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
228 } else if (ev->filter == EVFILT_READ && ev->data < 0) {
229 /* file shrank, reposition to end */
230 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
238 (void) usleep(250000);
243 if (Fflag && fileno(fp) != STDIN_FILENO) {
244 while (stat(fname, &sb2) != 0)
245 /* file was rotated, wait until it reappears */
247 if (sb2.st_ino != sbp->st_ino ||
248 sb2.st_dev != sbp->st_dev ||
249 sb2.st_rdev != sbp->st_rdev ||
251 fp = freopen(fname, "r", fp);
265 * rlines -- display the last offset lines of the file.
277 if (!(size = sbp->st_size))
281 map.mapoff = map.maxoff = size;
284 * Last char is special, ignore whether newline or not. Note that
285 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
288 while (curoff >= 0) {
289 if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
293 for (i = curoff - map.mapoff; i >= 0; i--)
294 if (map.start[i] == '\n' && --off == 0)
296 /* `i' is either the map offset of a '\n', or -1. */
297 curoff = map.mapoff + i;
302 if (mapprint(&map, curoff, size - curoff) != 0) {
307 /* Set the file pointer to reflect the length displayed. */
308 if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
312 if (map.start != NULL && munmap(map.start, map.maplen)) {