19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1991, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * Edward Sze-Tyan Wang. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 169b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 179b50d902SRodney W. Grimes * must display the following acknowledgement: 189b50d902SRodney W. Grimes * This product includes software developed by the University of 199b50d902SRodney W. Grimes * California, Berkeley and its contributors. 209b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 219b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 229b50d902SRodney W. Grimes * without specific prior written permission. 239b50d902SRodney W. Grimes * 249b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 259b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 269b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 279b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 289b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 299b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 309b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 329b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 339b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 349b50d902SRodney W. Grimes * SUCH DAMAGE. 359b50d902SRodney W. Grimes */ 369b50d902SRodney W. Grimes 37814e3a92SMark Murray #include <sys/cdefs.h> 38814e3a92SMark Murray 39814e3a92SMark Murray __FBSDID("$FreeBSD$"); 40814e3a92SMark Murray 419b50d902SRodney W. Grimes #ifndef lint 42814e3a92SMark Murray static const char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93"; 43247e7cb1SJeroen Ruigrok van der Werven #endif 449b50d902SRodney W. Grimes 459b50d902SRodney W. Grimes #include <sys/types.h> 469b50d902SRodney W. Grimes #include <sys/stat.h> 479b50d902SRodney W. Grimes #include <sys/time.h> 489b50d902SRodney W. Grimes #include <sys/mman.h> 4932462e82SJonathan Lemon #include <sys/event.h> 509b50d902SRodney W. Grimes 51814e3a92SMark Murray #include <err.h> 529b50d902SRodney W. Grimes #include <errno.h> 53814e3a92SMark Murray #include <fcntl.h> 54814e3a92SMark Murray #include <limits.h> 559b50d902SRodney W. Grimes #include <stdio.h> 569b50d902SRodney W. Grimes #include <stdlib.h> 579b50d902SRodney W. Grimes #include <string.h> 58814e3a92SMark Murray #include <unistd.h> 59814e3a92SMark Murray 609b50d902SRodney W. Grimes #include "extern.h" 619b50d902SRodney W. Grimes 623f330d7dSWarner Losh static void rlines(FILE *, off_t, struct stat *); 63a2fb00d9SDavid Malone static void show(file_info_t *); 64a2fb00d9SDavid Malone static void set_events(file_info_t *files); 659b50d902SRodney W. Grimes 66ea9bd2dfSJonathan Lemon /* defines for inner loop actions */ 67ea9bd2dfSJonathan Lemon #define USE_SLEEP 0 68ea9bd2dfSJonathan Lemon #define USE_KQUEUE 1 69ea9bd2dfSJonathan Lemon #define ADD_EVENTS 2 70ea9bd2dfSJonathan Lemon 7115a55f79SPaul Richards struct kevent *ev; 7215a55f79SPaul Richards int action = USE_SLEEP; 7315a55f79SPaul Richards int kq; 7415a55f79SPaul Richards 759b50d902SRodney W. Grimes /* 769b50d902SRodney W. Grimes * forward -- display the file, from an offset, forward. 779b50d902SRodney W. Grimes * 789b50d902SRodney W. Grimes * There are eight separate cases for this -- regular and non-regular 799b50d902SRodney W. Grimes * files, by bytes or lines and from the beginning or end of the file. 809b50d902SRodney W. Grimes * 819b50d902SRodney W. Grimes * FBYTES byte offset from the beginning of the file 829b50d902SRodney W. Grimes * REG seek 839b50d902SRodney W. Grimes * NOREG read, counting bytes 849b50d902SRodney W. Grimes * 859b50d902SRodney W. Grimes * FLINES line offset from the beginning of the file 869b50d902SRodney W. Grimes * REG read, counting lines 879b50d902SRodney W. Grimes * NOREG read, counting lines 889b50d902SRodney W. Grimes * 899b50d902SRodney W. Grimes * RBYTES byte offset from the end of the file 909b50d902SRodney W. Grimes * REG seek 919b50d902SRodney W. Grimes * NOREG cyclically read characters into a wrap-around buffer 929b50d902SRodney W. Grimes * 939b50d902SRodney W. Grimes * RLINES 949b50d902SRodney W. Grimes * REG mmap the file and step back until reach the correct offset. 959b50d902SRodney W. Grimes * NOREG cyclically read lines into a wrap-around array of buffers 969b50d902SRodney W. Grimes */ 979b50d902SRodney W. Grimes void 984bba8e59SPaul Richards forward(FILE *fp, enum STYLE style, off_t off, struct stat *sbp) 999b50d902SRodney W. Grimes { 100a2fb00d9SDavid Malone int ch; 1019b50d902SRodney W. Grimes 1029b50d902SRodney W. Grimes switch(style) { 1039b50d902SRodney W. Grimes case FBYTES: 1049b50d902SRodney W. Grimes if (off == 0) 1059b50d902SRodney W. Grimes break; 1069b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) { 1079b50d902SRodney W. Grimes if (sbp->st_size < off) 1089b50d902SRodney W. Grimes off = sbp->st_size; 109bd9dc975SAndrey A. Chernov if (fseeko(fp, off, SEEK_SET) == -1) { 1109b50d902SRodney W. Grimes ierr(); 1119b50d902SRodney W. Grimes return; 1129b50d902SRodney W. Grimes } 1139b50d902SRodney W. Grimes } else while (off--) 1149b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) { 1159b50d902SRodney W. Grimes if (ferror(fp)) { 1169b50d902SRodney W. Grimes ierr(); 1179b50d902SRodney W. Grimes return; 1189b50d902SRodney W. Grimes } 1199b50d902SRodney W. Grimes break; 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes break; 1229b50d902SRodney W. Grimes case FLINES: 1239b50d902SRodney W. Grimes if (off == 0) 1249b50d902SRodney W. Grimes break; 1259b50d902SRodney W. Grimes for (;;) { 1269b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) { 1279b50d902SRodney W. Grimes if (ferror(fp)) { 1289b50d902SRodney W. Grimes ierr(); 1299b50d902SRodney W. Grimes return; 1309b50d902SRodney W. Grimes } 1319b50d902SRodney W. Grimes break; 1329b50d902SRodney W. Grimes } 1339b50d902SRodney W. Grimes if (ch == '\n' && !--off) 1349b50d902SRodney W. Grimes break; 1359b50d902SRodney W. Grimes } 1369b50d902SRodney W. Grimes break; 1379b50d902SRodney W. Grimes case RBYTES: 1389b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) { 1399b50d902SRodney W. Grimes if (sbp->st_size >= off && 140bd9dc975SAndrey A. Chernov fseeko(fp, -off, SEEK_END) == -1) { 1419b50d902SRodney W. Grimes ierr(); 1429b50d902SRodney W. Grimes return; 1439b50d902SRodney W. Grimes } 1449b50d902SRodney W. Grimes } else if (off == 0) { 1459b50d902SRodney W. Grimes while (getc(fp) != EOF); 1469b50d902SRodney W. Grimes if (ferror(fp)) { 1479b50d902SRodney W. Grimes ierr(); 1489b50d902SRodney W. Grimes return; 1499b50d902SRodney W. Grimes } 1509b50d902SRodney W. Grimes } else 1516439f56eSAdam David if (bytes(fp, off)) 1526439f56eSAdam David return; 1539b50d902SRodney W. Grimes break; 1549b50d902SRodney W. Grimes case RLINES: 1559b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) 1569b50d902SRodney W. Grimes if (!off) { 157bd9dc975SAndrey A. Chernov if (fseeko(fp, (off_t)0, SEEK_END) == -1) { 1589b50d902SRodney W. Grimes ierr(); 1599b50d902SRodney W. Grimes return; 1609b50d902SRodney W. Grimes } 1619b50d902SRodney W. Grimes } else 1629b50d902SRodney W. Grimes rlines(fp, off, sbp); 1639b50d902SRodney W. Grimes else if (off == 0) { 1649b50d902SRodney W. Grimes while (getc(fp) != EOF); 1659b50d902SRodney W. Grimes if (ferror(fp)) { 1669b50d902SRodney W. Grimes ierr(); 1679b50d902SRodney W. Grimes return; 1689b50d902SRodney W. Grimes } 1699b50d902SRodney W. Grimes } else 1706439f56eSAdam David if (lines(fp, off)) 1716439f56eSAdam David return; 1729b50d902SRodney W. Grimes break; 173814e3a92SMark Murray default: 174b77b9b9aSMurray Stokely break; 1759b50d902SRodney W. Grimes } 1769b50d902SRodney W. Grimes 1779b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF) 1789b50d902SRodney W. Grimes if (putchar(ch) == EOF) 1799b50d902SRodney W. Grimes oerr(); 1809b50d902SRodney W. Grimes if (ferror(fp)) { 1819b50d902SRodney W. Grimes ierr(); 1829b50d902SRodney W. Grimes return; 1839b50d902SRodney W. Grimes } 1849b50d902SRodney W. Grimes (void)fflush(stdout); 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes 1879b50d902SRodney W. Grimes /* 1889b50d902SRodney W. Grimes * rlines -- display the last offset lines of the file. 1899b50d902SRodney W. Grimes */ 1909b50d902SRodney W. Grimes static void 1919b50d902SRodney W. Grimes rlines(fp, off, sbp) 1929b50d902SRodney W. Grimes FILE *fp; 193bd9dc975SAndrey A. Chernov off_t off; 1949b50d902SRodney W. Grimes struct stat *sbp; 1959b50d902SRodney W. Grimes { 196726098d3SDavid Malone struct mapinfo map; 197726098d3SDavid Malone off_t curoff, size; 198726098d3SDavid Malone int i; 1999b50d902SRodney W. Grimes 2009b50d902SRodney W. Grimes if (!(size = sbp->st_size)) 2019b50d902SRodney W. Grimes return; 202726098d3SDavid Malone map.start = NULL; 203726098d3SDavid Malone map.fd = fileno(fp); 204726098d3SDavid Malone map.mapoff = map.maxoff = size; 2059b50d902SRodney W. Grimes 2066bea9ab4SAndrey A. Chernov /* 207726098d3SDavid Malone * Last char is special, ignore whether newline or not. Note that 208726098d3SDavid Malone * size == 0 is dealt with above, and size == 1 sets curoff to -1. 2096bea9ab4SAndrey A. Chernov */ 210726098d3SDavid Malone curoff = size - 2; 211726098d3SDavid Malone while (curoff >= 0) { 212726098d3SDavid Malone if (curoff < map.mapoff && maparound(&map, curoff) != 0) { 2139b50d902SRodney W. Grimes ierr(); 2149b50d902SRodney W. Grimes return; 2159b50d902SRodney W. Grimes } 216726098d3SDavid Malone for (i = curoff - map.mapoff; i >= 0; i--) 217726098d3SDavid Malone if (map.start[i] == '\n' && --off == 0) 218726098d3SDavid Malone break; 219726098d3SDavid Malone /* `i' is either the map offset of a '\n', or -1. */ 220726098d3SDavid Malone curoff = map.mapoff + i; 221726098d3SDavid Malone if (i >= 0) 222726098d3SDavid Malone break; 223726098d3SDavid Malone } 224726098d3SDavid Malone curoff++; 225726098d3SDavid Malone if (mapprint(&map, curoff, size - curoff) != 0) { 226726098d3SDavid Malone ierr(); 227726098d3SDavid Malone exit(1); 228726098d3SDavid Malone } 229726098d3SDavid Malone 230726098d3SDavid Malone /* Set the file pointer to reflect the length displayed. */ 231a74da62eSAndrey A. Chernov if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) { 232726098d3SDavid Malone ierr(); 233726098d3SDavid Malone return; 234726098d3SDavid Malone } 235726098d3SDavid Malone if (map.start != NULL && munmap(map.start, map.maplen)) { 23644cf272fSAdam David ierr(); 2379b50d902SRodney W. Grimes return; 2389b50d902SRodney W. Grimes } 2399b50d902SRodney W. Grimes } 24015a55f79SPaul Richards 24115a55f79SPaul Richards /* 24215a55f79SPaul Richards * follow -- display the file, from an offset, forward. 24315a55f79SPaul Richards * 24415a55f79SPaul Richards */ 24515a55f79SPaul Richards 246a2fb00d9SDavid Malone static void 24715a55f79SPaul Richards show(file_info_t *file) 24815a55f79SPaul Richards { 2499e777bc6SBrian Somers int ch; 2509e777bc6SBrian Somers static file_info_t *last; 25115a55f79SPaul Richards 25215a55f79SPaul Richards while ((ch = getc(file->fp)) != EOF) { 2539e777bc6SBrian Somers if (last != file && no_files > 1) { 25415a55f79SPaul Richards (void)printf("\n==> %s <==\n", file->file_name); 2559e777bc6SBrian Somers last = file; 25615a55f79SPaul Richards } 25715a55f79SPaul Richards if (putchar(ch) == EOF) 25815a55f79SPaul Richards oerr(); 25915a55f79SPaul Richards } 26015a55f79SPaul Richards (void)fflush(stdout); 26115a55f79SPaul Richards if (ferror(file->fp)) { 26215a55f79SPaul Richards file->fp = NULL; 26315a55f79SPaul Richards ierr(); 26415a55f79SPaul Richards } else 26515a55f79SPaul Richards clearerr(file->fp); 26615a55f79SPaul Richards } 26715a55f79SPaul Richards 268a2fb00d9SDavid Malone static void 26915a55f79SPaul Richards set_events(file_info_t *files) 27015a55f79SPaul Richards { 27115a55f79SPaul Richards int i, n = 0; 27215a55f79SPaul Richards file_info_t *file; 27315a55f79SPaul Richards struct timespec ts; 27415a55f79SPaul Richards 27515a55f79SPaul Richards ts.tv_sec = 0; 27615a55f79SPaul Richards ts.tv_nsec = 0; 27715a55f79SPaul Richards 27815a55f79SPaul Richards action = USE_KQUEUE; 27915a55f79SPaul Richards for (i = 0, file = files; i < no_files; i++, file++) { 28015a55f79SPaul Richards if (! file->fp) 28115a55f79SPaul Richards continue; 28215a55f79SPaul Richards if (Fflag && fileno(file->fp) != STDIN_FILENO) { 28315a55f79SPaul Richards EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE, 28415a55f79SPaul Richards EV_ADD | EV_ENABLE | EV_CLEAR, 28515a55f79SPaul Richards NOTE_DELETE | NOTE_RENAME, 0, 0); 28615a55f79SPaul Richards n++; 28715a55f79SPaul Richards } 28815a55f79SPaul Richards EV_SET(&ev[n], fileno(file->fp), EVFILT_READ, 28915a55f79SPaul Richards EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); 29015a55f79SPaul Richards n++; 29115a55f79SPaul Richards } 29215a55f79SPaul Richards 29315a55f79SPaul Richards if (kevent(kq, ev, n, NULL, 0, &ts) < 0) { 29415a55f79SPaul Richards action = USE_SLEEP; 29515a55f79SPaul Richards } 29615a55f79SPaul Richards } 29715a55f79SPaul Richards 29815a55f79SPaul Richards void 29915a55f79SPaul Richards follow(file_info_t *files, enum STYLE style, off_t off) 30015a55f79SPaul Richards { 30115a55f79SPaul Richards int active, i, n = -1; 30215a55f79SPaul Richards struct stat sb2; 30315a55f79SPaul Richards file_info_t *file; 30415a55f79SPaul Richards struct timespec ts; 30515a55f79SPaul Richards 30615a55f79SPaul Richards /* Position each of the files */ 30715a55f79SPaul Richards 30815a55f79SPaul Richards file = files; 30915a55f79SPaul Richards active = 0; 31015a55f79SPaul Richards n = 0; 31115a55f79SPaul Richards for (i = 0; i < no_files; i++, file++) { 31215a55f79SPaul Richards if (file->fp) { 31315a55f79SPaul Richards active = 1; 31415a55f79SPaul Richards n++; 31515a55f79SPaul Richards if (no_files > 1) 31615a55f79SPaul Richards (void)printf("\n==> %s <==\n", file->file_name); 31715a55f79SPaul Richards forward(file->fp, style, off, &file->st); 31815a55f79SPaul Richards if (Fflag && fileno(file->fp) != STDIN_FILENO) 31915a55f79SPaul Richards n++; 32015a55f79SPaul Richards } 32115a55f79SPaul Richards } 32215a55f79SPaul Richards if (! active) 32315a55f79SPaul Richards return; 32415a55f79SPaul Richards 32515a55f79SPaul Richards kq = kqueue(); 32615a55f79SPaul Richards if (kq < 0) 32715a55f79SPaul Richards err(1, "kqueue"); 32815a55f79SPaul Richards ev = malloc(n * sizeof(struct kevent)); 32915a55f79SPaul Richards if (! ev) 33015a55f79SPaul Richards err(1, "Couldn't allocate memory for kevents."); 33115a55f79SPaul Richards set_events(files); 33215a55f79SPaul Richards 33315a55f79SPaul Richards for (;;) { 33415a55f79SPaul Richards for (i = 0, file = files; i < no_files; i++, file++) { 33515a55f79SPaul Richards if (! file->fp) 33615a55f79SPaul Richards continue; 33715a55f79SPaul Richards if (Fflag && file->fp && fileno(file->fp) != STDIN_FILENO) { 33815a55f79SPaul Richards if (stat(file->file_name, &sb2) != 0) { 33915a55f79SPaul Richards /* file was rotated, skip it until it reappears */ 34015a55f79SPaul Richards continue; 34115a55f79SPaul Richards } 34215a55f79SPaul Richards if (sb2.st_ino != file->st.st_ino || 34315a55f79SPaul Richards sb2.st_dev != file->st.st_dev || 34415a55f79SPaul Richards sb2.st_nlink == 0) { 34515a55f79SPaul Richards file->fp = freopen(file->file_name, "r", file->fp); 34615a55f79SPaul Richards if (file->fp == NULL) { 34715a55f79SPaul Richards ierr(); 34815a55f79SPaul Richards continue; 34915a55f79SPaul Richards } else { 35015a55f79SPaul Richards memcpy(&file->st, &sb2, sizeof(struct stat)); 35115a55f79SPaul Richards set_events(files); 35215a55f79SPaul Richards } 35315a55f79SPaul Richards } 35415a55f79SPaul Richards } 35515a55f79SPaul Richards show(file); 35615a55f79SPaul Richards } 35715a55f79SPaul Richards 35815a55f79SPaul Richards switch (action) { 35915a55f79SPaul Richards case USE_KQUEUE: 36015a55f79SPaul Richards ts.tv_sec = 1; 36115a55f79SPaul Richards ts.tv_nsec = 0; 36215a55f79SPaul Richards /* 36315a55f79SPaul Richards * In the -F case we set a timeout to ensure that 36415a55f79SPaul Richards * we re-stat the file at least once every second. 36515a55f79SPaul Richards */ 36615a55f79SPaul Richards n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL); 36715a55f79SPaul Richards if (n < 0) 36815a55f79SPaul Richards err(1, "kevent"); 36915a55f79SPaul Richards if (n == 0) { 37015a55f79SPaul Richards /* timeout */ 37115a55f79SPaul Richards break; 37215a55f79SPaul Richards } else if (ev->filter == EVFILT_READ && ev->data < 0) { 37315a55f79SPaul Richards /* file shrank, reposition to end */ 37415a55f79SPaul Richards if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) { 37515a55f79SPaul Richards ierr(); 37615a55f79SPaul Richards continue; 37715a55f79SPaul Richards } 37815a55f79SPaul Richards } 37915a55f79SPaul Richards break; 38015a55f79SPaul Richards 38115a55f79SPaul Richards case USE_SLEEP: 38215a55f79SPaul Richards (void) usleep(250000); 38315a55f79SPaul Richards break; 38415a55f79SPaul Richards } 38515a55f79SPaul Richards } 38615a55f79SPaul Richards } 387