2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Sze-Tyan Wang.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 static const char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93";
43 #include <sys/param.h>
44 #include <sys/mount.h>
45 #include <sys/types.h>
49 #include <sys/event.h>
60 #include <libcasper.h>
61 #include <casper/cap_fileargs.h>
65 static void rlines(FILE *, const char *fn, off_t, struct stat *);
66 static int show(file_info_t *);
67 static void set_events(file_info_t *files);
69 /* defines for inner loop actions */
74 static struct kevent *ev;
75 static int action = USE_SLEEP;
78 static const file_info_t *last;
81 * forward -- display the file, from an offset, forward.
83 * There are eight separate cases for this -- regular and non-regular
84 * files, by bytes or lines and from the beginning or end of the file.
86 * FBYTES byte offset from the beginning of the file
88 * NOREG read, counting bytes
90 * FLINES line offset from the beginning of the file
91 * REG read, counting lines
92 * NOREG read, counting lines
94 * RBYTES byte offset from the end of the file
96 * NOREG cyclically read characters into a wrap-around buffer
99 * REG mmap the file and step back until reach the correct offset.
100 * NOREG cyclically read lines into a wrap-around array of buffers
103 forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
111 if (S_ISREG(sbp->st_mode)) {
112 if (sbp->st_size < off)
114 if (fseeko(fp, off, SEEK_SET) == -1) {
119 if ((ch = getc(fp)) == EOF) {
131 if ((ch = getc(fp)) == EOF) {
138 if (ch == '\n' && !--off)
143 if (S_ISREG(sbp->st_mode)) {
144 if (sbp->st_size >= off &&
145 fseeko(fp, -off, SEEK_END) == -1) {
149 } else if (off == 0) {
150 while (getc(fp) != EOF);
156 if (bytes(fp, fn, off))
160 if (S_ISREG(sbp->st_mode))
162 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
167 rlines(fp, fn, off, sbp);
169 while (getc(fp) != EOF);
175 if (lines(fp, fn, off))
182 while ((ch = getc(fp)) != EOF)
183 if (putchar(ch) == EOF)
189 (void)fflush(stdout);
193 * rlines -- display the last offset lines of the file.
196 rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp)
202 if (!(size = sbp->st_size))
206 map.mapoff = map.maxoff = size;
209 * Last char is special, ignore whether newline or not. Note that
210 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
213 while (curoff >= 0) {
214 if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
218 for (i = curoff - map.mapoff; i >= 0; i--)
219 if (map.start[i] == '\n' && --off == 0)
221 /* `i' is either the map offset of a '\n', or -1. */
222 curoff = map.mapoff + i;
227 if (mapprint(&map, curoff, size - curoff) != 0) {
232 /* Set the file pointer to reflect the length displayed. */
233 if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
237 if (map.start != NULL && munmap(map.start, map.maplen)) {
244 show(file_info_t *file)
248 while ((ch = getc(file->fp)) != EOF) {
249 if (last != file && no_files > 1) {
251 printfn(file->file_name, 1);
254 if (putchar(ch) == EOF)
257 (void)fflush(stdout);
258 if (ferror(file->fp)) {
261 ierr(file->file_name);
269 set_events(file_info_t *files)
280 for (i = 0, file = files; i < no_files; i++, file++) {
284 if (fstatfs(fileno(file->fp), &sf) == 0 &&
285 (sf.f_flags & MNT_LOCAL) == 0) {
290 if (Fflag && fileno(file->fp) != STDIN_FILENO) {
291 EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE,
292 EV_ADD | EV_ENABLE | EV_CLEAR,
293 NOTE_DELETE | NOTE_RENAME, 0, 0);
296 EV_SET(&ev[n], fileno(file->fp), EVFILT_READ,
297 EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
301 if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
307 * follow -- display the file, from an offset, forward.
311 follow(file_info_t *files, enum STYLE style, off_t off)
313 int active, ev_change, i, n = -1;
319 /* Position each of the files */
324 for (i = 0; i < no_files; i++, file++) {
328 if (no_files > 1 && !qflag)
329 printfn(file->file_name, 1);
330 forward(file->fp, file->file_name, style, off, &file->st);
331 if (Fflag && fileno(file->fp) != STDIN_FILENO)
335 if (!Fflag && !active)
343 ev = malloc(n * sizeof(struct kevent));
345 err(1, "Couldn't allocate memory for kevents.");
351 for (i = 0, file = files; i < no_files; i++, file++) {
354 fileargs_fopen(fa, file->file_name,
356 if (file->fp != NULL &&
357 fstat(fileno(file->fp), &file->st)
362 if (file->fp != NULL)
366 if (fileno(file->fp) == STDIN_FILENO)
368 ftmp = fileargs_fopen(fa, file->file_name, "r");
370 fstat(fileno(file->fp), &sb2) == -1) {
372 ierr(file->file_name);
374 if (file->fp != NULL) {
385 if (sb2.st_ino != file->st.st_ino ||
386 sb2.st_dev != file->st.st_dev ||
391 memcpy(&file->st, &sb2,
392 sizeof(struct stat));
400 for (i = 0, file = files; i < no_files; i++, file++)
401 if (file->fp && !show(file))
412 * In the -F case we set a timeout to ensure that
413 * we re-stat the file at least once every second.
415 n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
421 } else if (ev->filter == EVFILT_READ && ev->data < 0) {
422 /* file shrank, reposition to end */
423 if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) {
424 ierr(file->file_name);
431 (void) usleep(250000);