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 7576628ce7SXin LI static const file_info_t *last; 7676628ce7SXin LI 779b50d902SRodney W. Grimes /* 789b50d902SRodney W. Grimes * forward -- display the file, from an offset, forward. 799b50d902SRodney W. Grimes * 809b50d902SRodney W. Grimes * There are eight separate cases for this -- regular and non-regular 819b50d902SRodney W. Grimes * files, by bytes or lines and from the beginning or end of the file. 829b50d902SRodney W. Grimes * 839b50d902SRodney W. Grimes * FBYTES byte offset from the beginning of the file 849b50d902SRodney W. Grimes * REG seek 859b50d902SRodney W. Grimes * NOREG read, counting bytes 869b50d902SRodney W. Grimes * 879b50d902SRodney W. Grimes * FLINES line offset from the beginning of the file 889b50d902SRodney W. Grimes * REG read, counting lines 899b50d902SRodney W. Grimes * NOREG read, counting lines 909b50d902SRodney W. Grimes * 919b50d902SRodney W. Grimes * RBYTES byte offset from the end of the file 929b50d902SRodney W. Grimes * REG seek 939b50d902SRodney W. Grimes * NOREG cyclically read characters into a wrap-around buffer 949b50d902SRodney W. Grimes * 959b50d902SRodney W. Grimes * RLINES 969b50d902SRodney W. Grimes * REG mmap the file and step back until reach the correct offset. 979b50d902SRodney W. Grimes * NOREG cyclically read lines into a wrap-around array of buffers 989b50d902SRodney W. Grimes */ 999b50d902SRodney W. Grimes void 1004bba8e59SPaul Richards forward(FILE *fp, enum STYLE style, off_t off, struct stat *sbp) 1019b50d902SRodney W. Grimes { 102a2fb00d9SDavid Malone int ch; 1039b50d902SRodney W. Grimes 1049b50d902SRodney W. Grimes switch(style) { 1059b50d902SRodney W. Grimes case FBYTES: 1069b50d902SRodney W. Grimes if (off == 0) 1079b50d902SRodney W. Grimes break; 1089b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) { 1099b50d902SRodney W. Grimes if (sbp->st_size < off) 1109b50d902SRodney W. Grimes off = sbp->st_size; 111bd9dc975SAndrey A. Chernov if (fseeko(fp, off, SEEK_SET) == -1) { 1129b50d902SRodney W. Grimes ierr(); 1139b50d902SRodney W. Grimes return; 1149b50d902SRodney W. Grimes } 1159b50d902SRodney W. Grimes } else while (off--) 1169b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) { 1179b50d902SRodney W. Grimes if (ferror(fp)) { 1189b50d902SRodney W. Grimes ierr(); 1199b50d902SRodney W. Grimes return; 1209b50d902SRodney W. Grimes } 1219b50d902SRodney W. Grimes break; 1229b50d902SRodney W. Grimes } 1239b50d902SRodney W. Grimes break; 1249b50d902SRodney W. Grimes case FLINES: 1259b50d902SRodney W. Grimes if (off == 0) 1269b50d902SRodney W. Grimes break; 1279b50d902SRodney W. Grimes for (;;) { 1289b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) { 1299b50d902SRodney W. Grimes if (ferror(fp)) { 1309b50d902SRodney W. Grimes ierr(); 1319b50d902SRodney W. Grimes return; 1329b50d902SRodney W. Grimes } 1339b50d902SRodney W. Grimes break; 1349b50d902SRodney W. Grimes } 1359b50d902SRodney W. Grimes if (ch == '\n' && !--off) 1369b50d902SRodney W. Grimes break; 1379b50d902SRodney W. Grimes } 1389b50d902SRodney W. Grimes break; 1399b50d902SRodney W. Grimes case RBYTES: 1409b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) { 1419b50d902SRodney W. Grimes if (sbp->st_size >= off && 142bd9dc975SAndrey A. Chernov fseeko(fp, -off, SEEK_END) == -1) { 1439b50d902SRodney W. Grimes ierr(); 1449b50d902SRodney W. Grimes return; 1459b50d902SRodney W. Grimes } 1469b50d902SRodney W. Grimes } else if (off == 0) { 1479b50d902SRodney W. Grimes while (getc(fp) != EOF); 1489b50d902SRodney W. Grimes if (ferror(fp)) { 1499b50d902SRodney W. Grimes ierr(); 1509b50d902SRodney W. Grimes return; 1519b50d902SRodney W. Grimes } 1529b50d902SRodney W. Grimes } else 1536439f56eSAdam David if (bytes(fp, off)) 1546439f56eSAdam David return; 1559b50d902SRodney W. Grimes break; 1569b50d902SRodney W. Grimes case RLINES: 1579b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) 1589b50d902SRodney W. Grimes if (!off) { 159bd9dc975SAndrey A. Chernov if (fseeko(fp, (off_t)0, SEEK_END) == -1) { 1609b50d902SRodney W. Grimes ierr(); 1619b50d902SRodney W. Grimes return; 1629b50d902SRodney W. Grimes } 1639b50d902SRodney W. Grimes } else 1649b50d902SRodney W. Grimes rlines(fp, off, sbp); 1659b50d902SRodney W. Grimes else if (off == 0) { 1669b50d902SRodney W. Grimes while (getc(fp) != EOF); 1679b50d902SRodney W. Grimes if (ferror(fp)) { 1689b50d902SRodney W. Grimes ierr(); 1699b50d902SRodney W. Grimes return; 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes } else 1726439f56eSAdam David if (lines(fp, off)) 1736439f56eSAdam David return; 1749b50d902SRodney W. Grimes break; 175814e3a92SMark Murray default: 176b77b9b9aSMurray Stokely break; 1779b50d902SRodney W. Grimes } 1789b50d902SRodney W. Grimes 1799b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF) 1809b50d902SRodney W. Grimes if (putchar(ch) == EOF) 1819b50d902SRodney W. Grimes oerr(); 1829b50d902SRodney W. Grimes if (ferror(fp)) { 1839b50d902SRodney W. Grimes ierr(); 1849b50d902SRodney W. Grimes return; 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes (void)fflush(stdout); 1879b50d902SRodney W. Grimes } 1889b50d902SRodney W. Grimes 1899b50d902SRodney W. Grimes /* 1909b50d902SRodney W. Grimes * rlines -- display the last offset lines of the file. 1919b50d902SRodney W. Grimes */ 1929b50d902SRodney W. Grimes static void 1939b50d902SRodney W. Grimes rlines(fp, off, sbp) 1949b50d902SRodney W. Grimes FILE *fp; 195bd9dc975SAndrey A. Chernov off_t off; 1969b50d902SRodney W. Grimes struct stat *sbp; 1979b50d902SRodney W. Grimes { 198726098d3SDavid Malone struct mapinfo map; 199726098d3SDavid Malone off_t curoff, size; 200726098d3SDavid Malone int i; 2019b50d902SRodney W. Grimes 2029b50d902SRodney W. Grimes if (!(size = sbp->st_size)) 2039b50d902SRodney W. Grimes return; 204726098d3SDavid Malone map.start = NULL; 205726098d3SDavid Malone map.fd = fileno(fp); 206726098d3SDavid Malone map.mapoff = map.maxoff = size; 2079b50d902SRodney W. Grimes 2086bea9ab4SAndrey A. Chernov /* 209726098d3SDavid Malone * Last char is special, ignore whether newline or not. Note that 210726098d3SDavid Malone * size == 0 is dealt with above, and size == 1 sets curoff to -1. 2116bea9ab4SAndrey A. Chernov */ 212726098d3SDavid Malone curoff = size - 2; 213726098d3SDavid Malone while (curoff >= 0) { 214726098d3SDavid Malone if (curoff < map.mapoff && maparound(&map, curoff) != 0) { 2159b50d902SRodney W. Grimes ierr(); 2169b50d902SRodney W. Grimes return; 2179b50d902SRodney W. Grimes } 218726098d3SDavid Malone for (i = curoff - map.mapoff; i >= 0; i--) 219726098d3SDavid Malone if (map.start[i] == '\n' && --off == 0) 220726098d3SDavid Malone break; 221726098d3SDavid Malone /* `i' is either the map offset of a '\n', or -1. */ 222726098d3SDavid Malone curoff = map.mapoff + i; 223726098d3SDavid Malone if (i >= 0) 224726098d3SDavid Malone break; 225726098d3SDavid Malone } 226726098d3SDavid Malone curoff++; 227726098d3SDavid Malone if (mapprint(&map, curoff, size - curoff) != 0) { 228726098d3SDavid Malone ierr(); 229726098d3SDavid Malone exit(1); 230726098d3SDavid Malone } 231726098d3SDavid Malone 232726098d3SDavid Malone /* Set the file pointer to reflect the length displayed. */ 233a74da62eSAndrey A. Chernov if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) { 234726098d3SDavid Malone ierr(); 235726098d3SDavid Malone return; 236726098d3SDavid Malone } 237726098d3SDavid Malone if (map.start != NULL && munmap(map.start, map.maplen)) { 23844cf272fSAdam David ierr(); 2399b50d902SRodney W. Grimes return; 2409b50d902SRodney W. Grimes } 2419b50d902SRodney W. Grimes } 24215a55f79SPaul Richards 243a2fb00d9SDavid Malone static void 24415a55f79SPaul Richards show(file_info_t *file) 24515a55f79SPaul Richards { 2469e777bc6SBrian Somers int ch; 24715a55f79SPaul Richards 24815a55f79SPaul Richards while ((ch = getc(file->fp)) != EOF) { 2499e777bc6SBrian Somers if (last != file && no_files > 1) { 25015a55f79SPaul Richards (void)printf("\n==> %s <==\n", file->file_name); 2519e777bc6SBrian Somers last = file; 25215a55f79SPaul Richards } 25315a55f79SPaul Richards if (putchar(ch) == EOF) 25415a55f79SPaul Richards oerr(); 25515a55f79SPaul Richards } 25615a55f79SPaul Richards (void)fflush(stdout); 25715a55f79SPaul Richards if (ferror(file->fp)) { 25815a55f79SPaul Richards file->fp = NULL; 25915a55f79SPaul Richards ierr(); 26015a55f79SPaul Richards } else 26115a55f79SPaul Richards clearerr(file->fp); 26215a55f79SPaul Richards } 26315a55f79SPaul Richards 264a2fb00d9SDavid Malone static void 26515a55f79SPaul Richards set_events(file_info_t *files) 26615a55f79SPaul Richards { 26715a55f79SPaul Richards int i, n = 0; 26815a55f79SPaul Richards file_info_t *file; 26915a55f79SPaul Richards struct timespec ts; 27015a55f79SPaul Richards 27115a55f79SPaul Richards ts.tv_sec = 0; 27215a55f79SPaul Richards ts.tv_nsec = 0; 27315a55f79SPaul Richards 27415a55f79SPaul Richards action = USE_KQUEUE; 27515a55f79SPaul Richards for (i = 0, file = files; i < no_files; i++, file++) { 27615a55f79SPaul Richards if (! file->fp) 27715a55f79SPaul Richards continue; 27815a55f79SPaul Richards if (Fflag && fileno(file->fp) != STDIN_FILENO) { 27915a55f79SPaul Richards EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE, 28015a55f79SPaul Richards EV_ADD | EV_ENABLE | EV_CLEAR, 28115a55f79SPaul Richards NOTE_DELETE | NOTE_RENAME, 0, 0); 28215a55f79SPaul Richards n++; 28315a55f79SPaul Richards } 28415a55f79SPaul Richards EV_SET(&ev[n], fileno(file->fp), EVFILT_READ, 28515a55f79SPaul Richards EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); 28615a55f79SPaul Richards n++; 28715a55f79SPaul Richards } 28815a55f79SPaul Richards 28915a55f79SPaul Richards if (kevent(kq, ev, n, NULL, 0, &ts) < 0) { 29015a55f79SPaul Richards action = USE_SLEEP; 29115a55f79SPaul Richards } 29215a55f79SPaul Richards } 29315a55f79SPaul Richards 29476628ce7SXin LI /* 29576628ce7SXin LI * follow -- display the file, from an offset, forward. 29676628ce7SXin LI * 29776628ce7SXin LI */ 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 32576628ce7SXin LI last = --file; 32676628ce7SXin LI 32715a55f79SPaul Richards kq = kqueue(); 32815a55f79SPaul Richards if (kq < 0) 32915a55f79SPaul Richards err(1, "kqueue"); 33015a55f79SPaul Richards ev = malloc(n * sizeof(struct kevent)); 33115a55f79SPaul Richards if (! ev) 33215a55f79SPaul Richards err(1, "Couldn't allocate memory for kevents."); 33315a55f79SPaul Richards set_events(files); 33415a55f79SPaul Richards 33515a55f79SPaul Richards for (;;) { 33615a55f79SPaul Richards for (i = 0, file = files; i < no_files; i++, file++) { 33715a55f79SPaul Richards if (! file->fp) 33815a55f79SPaul Richards continue; 33915a55f79SPaul Richards if (Fflag && file->fp && fileno(file->fp) != STDIN_FILENO) { 34015a55f79SPaul Richards if (stat(file->file_name, &sb2) != 0) { 34115a55f79SPaul Richards /* file was rotated, skip it until it reappears */ 34215a55f79SPaul Richards continue; 34315a55f79SPaul Richards } 34415a55f79SPaul Richards if (sb2.st_ino != file->st.st_ino || 34515a55f79SPaul Richards sb2.st_dev != file->st.st_dev || 34615a55f79SPaul Richards sb2.st_nlink == 0) { 34715a55f79SPaul Richards file->fp = freopen(file->file_name, "r", file->fp); 34815a55f79SPaul Richards if (file->fp == NULL) { 34915a55f79SPaul Richards ierr(); 35015a55f79SPaul Richards continue; 35115a55f79SPaul Richards } else { 35215a55f79SPaul Richards memcpy(&file->st, &sb2, sizeof(struct stat)); 35315a55f79SPaul Richards set_events(files); 35415a55f79SPaul Richards } 35515a55f79SPaul Richards } 35615a55f79SPaul Richards } 35715a55f79SPaul Richards show(file); 35815a55f79SPaul Richards } 35915a55f79SPaul Richards 36015a55f79SPaul Richards switch (action) { 36115a55f79SPaul Richards case USE_KQUEUE: 36215a55f79SPaul Richards ts.tv_sec = 1; 36315a55f79SPaul Richards ts.tv_nsec = 0; 36415a55f79SPaul Richards /* 36515a55f79SPaul Richards * In the -F case we set a timeout to ensure that 36615a55f79SPaul Richards * we re-stat the file at least once every second. 36715a55f79SPaul Richards */ 36815a55f79SPaul Richards n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL); 36915a55f79SPaul Richards if (n < 0) 37015a55f79SPaul Richards err(1, "kevent"); 37115a55f79SPaul Richards if (n == 0) { 37215a55f79SPaul Richards /* timeout */ 37315a55f79SPaul Richards break; 37415a55f79SPaul Richards } else if (ev->filter == EVFILT_READ && ev->data < 0) { 37515a55f79SPaul Richards /* file shrank, reposition to end */ 37615a55f79SPaul Richards if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) { 37715a55f79SPaul Richards ierr(); 37815a55f79SPaul Richards continue; 37915a55f79SPaul Richards } 38015a55f79SPaul Richards } 38115a55f79SPaul Richards break; 38215a55f79SPaul Richards 38315a55f79SPaul Richards case USE_SLEEP: 38415a55f79SPaul Richards (void) usleep(250000); 38515a55f79SPaul Richards break; 38615a55f79SPaul Richards } 38715a55f79SPaul Richards } 38815a55f79SPaul Richards } 389