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
37 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
42 static const char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93";
45 #include <sys/param.h>
46 #include <sys/mount.h>
47 #include <sys/types.h>
51 #include <sys/event.h>
64 static void rlines(FILE *, const char *fn, off_t, struct stat *);
65 static int show(file_info_t *);
66 static void set_events(file_info_t *files);
68 /* defines for inner loop actions */
74 int action = USE_SLEEP;
77 static const file_info_t *last;
80 * forward -- display the file, from an offset, forward.
82 * There are eight separate cases for this -- regular and non-regular
83 * files, by bytes or lines and from the beginning or end of the file.
85 * FBYTES byte offset from the beginning of the file
87 * NOREG read, counting bytes
89 * FLINES line offset from the beginning of the file
90 * REG read, counting lines
91 * NOREG read, counting lines
93 * RBYTES byte offset from the end of the file
95 * NOREG cyclically read characters into a wrap-around buffer
98 * REG mmap the file and step back until reach the correct offset.
99 * NOREG cyclically read lines into a wrap-around array of buffers
102 forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
110 if (S_ISREG(sbp->st_mode)) {
111 if (sbp->st_size < off)
113 if (fseeko(fp, off, SEEK_SET) == -1) {
118 if ((ch = getc(fp)) == EOF) {
130 if ((ch = getc(fp)) == EOF) {
137 if (ch == '\n' && !--off)
142 if (S_ISREG(sbp->st_mode)) {
143 if (sbp->st_size >= off &&
144 fseeko(fp, -off, SEEK_END) == -1) {
148 } else if (off == 0) {
149 while (getc(fp) != EOF);
155 if (bytes(fp, fn, off))
159 if (S_ISREG(sbp->st_mode))
161 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
166 rlines(fp, fn, off, sbp);
168 while (getc(fp) != EOF);
174 if (lines(fp, fn, off))
181 while ((ch = getc(fp)) != EOF)
182 if (putchar(ch) == EOF)
188 (void)fflush(stdout);
192 * rlines -- display the last offset lines of the file.
195 rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp)
201 if (!(size = sbp->st_size))
205 map.mapoff = map.maxoff = size;
208 * Last char is special, ignore whether newline or not. Note that
209 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
212 while (curoff >= 0) {
213 if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
217 for (i = curoff - map.mapoff; i >= 0; i--)
218 if (map.start[i] == '\n' && --off == 0)
220 /* `i' is either the map offset of a '\n', or -1. */
221 curoff = map.mapoff + i;
226 if (mapprint(&map, curoff, size - curoff) != 0) {
231 /* Set the file pointer to reflect the length displayed. */
232 if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
236 if (map.start != NULL && munmap(map.start, map.maplen)) {
243 show(file_info_t *file)
247 while ((ch = getc(file->fp)) != EOF) {
248 if (last != file && no_files > 1) {
250 (void)printf("\n==> %s <==\n", file->file_name);
253 if (putchar(ch) == EOF)
256 (void)fflush(stdout);
257 if (ferror(file->fp)) {
260 ierr(file->file_name);
268 set_events(file_info_t *files)
279 for (i = 0, file = files; i < no_files; i++, file++) {
283 if (fstatfs(fileno(file->fp), &sf) == 0 &&
284 (sf.f_flags & MNT_LOCAL) == 0) {
289 if (Fflag && fileno(file->fp) != STDIN_FILENO) {
290 EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE,
291 EV_ADD | EV_ENABLE | EV_CLEAR,
292 NOTE_DELETE | NOTE_RENAME, 0, 0);
295 EV_SET(&ev[n], fileno(file->fp), EVFILT_READ,
296 EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
300 if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
306 * follow -- display the file, from an offset, forward.
310 follow(file_info_t *files, enum STYLE style, off_t off)
312 int active, ev_change, i, n = -1;
317 /* Position each of the files */
322 for (i = 0; i < no_files; i++, file++) {
326 if (no_files > 1 && !qflag)
327 (void)printf("\n==> %s <==\n", file->file_name);
328 forward(file->fp, file->file_name, style, off, &file->st);
329 if (Fflag && fileno(file->fp) != STDIN_FILENO)
333 if (!Fflag && !active)
341 ev = malloc(n * sizeof(struct kevent));
343 err(1, "Couldn't allocate memory for kevents.");
349 for (i = 0, file = files; i < no_files; i++, file++) {
351 file->fp = fopen(file->file_name, "r");
352 if (file->fp != NULL &&
353 fstat(fileno(file->fp), &file->st)
358 if (file->fp != NULL)
362 if (fileno(file->fp) == STDIN_FILENO)
364 if (stat(file->file_name, &sb2) == -1) {
366 ierr(file->file_name);
374 if (sb2.st_ino != file->st.st_ino ||
375 sb2.st_dev != file->st.st_dev ||
378 file->fp = freopen(file->file_name, "r",
380 if (file->fp != NULL)
381 memcpy(&file->st, &sb2,
382 sizeof(struct stat));
383 else if (errno != ENOENT)
384 ierr(file->file_name);
390 for (i = 0, file = files; i < no_files; i++, file++)
391 if (file->fp && !show(file))
402 * In the -F case we set a timeout to ensure that
403 * we re-stat the file at least once every second.
405 n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
411 } else if (ev->filter == EVFILT_READ && ev->data < 0) {
412 /* file shrank, reposition to end */
413 if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) {
414 ierr(file->file_name);
421 (void) usleep(250000);