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
37 static const char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93";
40 #include <sys/param.h>
41 #include <sys/mount.h>
42 #include <sys/types.h>
46 #include <sys/event.h>
57 #include <libcasper.h>
58 #include <casper/cap_fileargs.h>
62 static void rlines(FILE *, const char *fn, off_t, struct stat *);
63 static int show(file_info_t *);
64 static void set_events(file_info_t *files);
66 /* defines for inner loop actions */
71 static struct kevent *ev;
72 static int action = USE_SLEEP;
75 static const file_info_t *last;
78 * forward -- display the file, from an offset, forward.
80 * There are eight separate cases for this -- regular and non-regular
81 * files, by bytes or lines and from the beginning or end of the file.
83 * FBYTES byte offset from the beginning of the file
85 * NOREG read, counting bytes
87 * FLINES line offset from the beginning of the file
88 * REG read, counting lines
89 * NOREG read, counting lines
91 * RBYTES byte offset from the end of the file
93 * NOREG cyclically read characters into a wrap-around buffer
96 * REG mmap the file and step back until reach the correct offset.
97 * NOREG cyclically read lines into a wrap-around array of buffers
100 forward(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
108 if (S_ISREG(sbp->st_mode)) {
109 if (sbp->st_size < off)
111 if (fseeko(fp, off, SEEK_SET) == -1) {
116 if ((ch = getc(fp)) == EOF) {
128 if ((ch = getc(fp)) == EOF) {
135 if (ch == '\n' && !--off)
140 if (S_ISREG(sbp->st_mode)) {
141 if (sbp->st_size >= off &&
142 fseeko(fp, -off, SEEK_END) == -1) {
146 } else if (off == 0) {
147 while (getc(fp) != EOF);
153 if (bytes(fp, fn, off))
157 if (S_ISREG(sbp->st_mode))
159 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
164 rlines(fp, fn, off, sbp);
166 while (getc(fp) != EOF);
172 if (lines(fp, fn, off))
179 while ((ch = getc(fp)) != EOF)
180 if (putchar(ch) == EOF)
186 (void)fflush(stdout);
190 * rlines -- display the last offset lines of the file.
193 rlines(FILE *fp, const char *fn, off_t off, struct stat *sbp)
199 if (!(size = sbp->st_size))
203 map.mapoff = map.maxoff = size;
206 * Last char is special, ignore whether newline or not. Note that
207 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
210 while (curoff >= 0) {
211 if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
215 for (i = curoff - map.mapoff; i >= 0; i--)
216 if (map.start[i] == '\n' && --off == 0)
218 /* `i' is either the map offset of a '\n', or -1. */
219 curoff = map.mapoff + i;
224 if (mapprint(&map, curoff, size - curoff) != 0) {
229 /* Set the file pointer to reflect the length displayed. */
230 if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
234 if (map.start != NULL && munmap(map.start, map.maplen)) {
241 show(file_info_t *file)
245 while ((ch = getc(file->fp)) != EOF) {
247 if (vflag || (qflag == 0 && no_files > 1))
248 printfn(file->file_name, 1);
251 if (putchar(ch) == EOF)
254 (void)fflush(stdout);
255 if (ferror(file->fp)) {
258 ierr(file->file_name);
266 set_events(file_info_t *files)
277 for (i = 0, file = files; i < no_files; i++, file++) {
281 if (fstatfs(fileno(file->fp), &sf) == 0 &&
282 (sf.f_flags & MNT_LOCAL) == 0) {
287 if (Fflag && fileno(file->fp) != STDIN_FILENO) {
288 EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE,
289 EV_ADD | EV_ENABLE | EV_CLEAR,
290 NOTE_DELETE | NOTE_RENAME, 0, 0);
293 EV_SET(&ev[n], fileno(file->fp), EVFILT_READ,
294 EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
298 if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
304 * follow -- display the file, from an offset, forward.
308 follow(file_info_t *files, enum STYLE style, off_t off)
310 int active, ev_change, i, n = -1;
316 /* Position each of the files */
321 for (i = 0; i < no_files; i++, file++) {
325 if (vflag || (qflag == 0 && no_files > 1))
326 printfn(file->file_name, 1);
327 forward(file->fp, file->file_name, style, off, &file->st);
328 if (Fflag && fileno(file->fp) != STDIN_FILENO)
332 if (!Fflag && !active)
340 ev = malloc(n * sizeof(struct kevent));
342 err(1, "Couldn't allocate memory for kevents.");
348 for (i = 0, file = files; i < no_files; i++, file++) {
351 fileargs_fopen(fa, file->file_name,
353 if (file->fp != NULL &&
354 fstat(fileno(file->fp), &file->st)
359 if (file->fp != NULL)
363 if (fileno(file->fp) == STDIN_FILENO)
365 ftmp = fileargs_fopen(fa, file->file_name, "r");
367 fstat(fileno(ftmp), &sb2) == -1) {
369 ierr(file->file_name);
371 if (file->fp != NULL) {
382 if (sb2.st_ino != file->st.st_ino ||
383 sb2.st_dev != file->st.st_dev ||
388 memcpy(&file->st, &sb2,
389 sizeof(struct stat));
397 for (i = 0, file = files; i < no_files; i++, file++)
398 if (file->fp && !show(file))
409 * In the -F case we set a timeout to ensure that
410 * we re-stat the file at least once every second.
411 * If we've recieved EINTR, ignore it. Both reasons
412 * for its generation are transient.
415 n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
416 if (n < 0 && errno == EINTR)
424 } else if (ev->filter == EVFILT_READ && ev->data < 0) {
425 /* file shrank, reposition to end */
426 if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) {
427 ierr(file->file_name);
434 (void) usleep(250000);