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 4513829828SPaul Saab #include <sys/param.h> 4613829828SPaul Saab #include <sys/mount.h> 479b50d902SRodney W. Grimes #include <sys/types.h> 489b50d902SRodney W. Grimes #include <sys/stat.h> 499b50d902SRodney W. Grimes #include <sys/time.h> 509b50d902SRodney W. Grimes #include <sys/mman.h> 5132462e82SJonathan Lemon #include <sys/event.h> 529b50d902SRodney W. Grimes 53814e3a92SMark Murray #include <err.h> 549b50d902SRodney W. Grimes #include <errno.h> 55814e3a92SMark Murray #include <fcntl.h> 56814e3a92SMark Murray #include <limits.h> 579b50d902SRodney W. Grimes #include <stdio.h> 589b50d902SRodney W. Grimes #include <stdlib.h> 599b50d902SRodney W. Grimes #include <string.h> 60814e3a92SMark Murray #include <unistd.h> 61814e3a92SMark Murray 629b50d902SRodney W. Grimes #include "extern.h" 639b50d902SRodney W. Grimes 643f330d7dSWarner Losh static void rlines(FILE *, off_t, struct stat *); 65a2fb00d9SDavid Malone static void show(file_info_t *); 66a2fb00d9SDavid Malone static void set_events(file_info_t *files); 679b50d902SRodney W. Grimes 68ea9bd2dfSJonathan Lemon /* defines for inner loop actions */ 69ea9bd2dfSJonathan Lemon #define USE_SLEEP 0 70ea9bd2dfSJonathan Lemon #define USE_KQUEUE 1 71ea9bd2dfSJonathan Lemon #define ADD_EVENTS 2 72ea9bd2dfSJonathan Lemon 7315a55f79SPaul Richards struct kevent *ev; 7415a55f79SPaul Richards int action = USE_SLEEP; 7515a55f79SPaul Richards int kq; 7615a55f79SPaul Richards 7776628ce7SXin LI static const file_info_t *last; 7876628ce7SXin LI 799b50d902SRodney W. Grimes /* 809b50d902SRodney W. Grimes * forward -- display the file, from an offset, forward. 819b50d902SRodney W. Grimes * 829b50d902SRodney W. Grimes * There are eight separate cases for this -- regular and non-regular 839b50d902SRodney W. Grimes * files, by bytes or lines and from the beginning or end of the file. 849b50d902SRodney W. Grimes * 859b50d902SRodney W. Grimes * FBYTES byte offset from the beginning of the file 869b50d902SRodney W. Grimes * REG seek 879b50d902SRodney W. Grimes * NOREG read, counting bytes 889b50d902SRodney W. Grimes * 899b50d902SRodney W. Grimes * FLINES line offset from the beginning of the file 909b50d902SRodney W. Grimes * REG read, counting lines 919b50d902SRodney W. Grimes * NOREG read, counting lines 929b50d902SRodney W. Grimes * 939b50d902SRodney W. Grimes * RBYTES byte offset from the end of the file 949b50d902SRodney W. Grimes * REG seek 959b50d902SRodney W. Grimes * NOREG cyclically read characters into a wrap-around buffer 969b50d902SRodney W. Grimes * 979b50d902SRodney W. Grimes * RLINES 989b50d902SRodney W. Grimes * REG mmap the file and step back until reach the correct offset. 999b50d902SRodney W. Grimes * NOREG cyclically read lines into a wrap-around array of buffers 1009b50d902SRodney W. Grimes */ 1019b50d902SRodney W. Grimes void 1024bba8e59SPaul Richards forward(FILE *fp, enum STYLE style, off_t off, struct stat *sbp) 1039b50d902SRodney W. Grimes { 104a2fb00d9SDavid Malone int ch; 1059b50d902SRodney W. Grimes 1069b50d902SRodney W. Grimes switch(style) { 1079b50d902SRodney W. Grimes case FBYTES: 1089b50d902SRodney W. Grimes if (off == 0) 1099b50d902SRodney W. Grimes break; 1109b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) { 1119b50d902SRodney W. Grimes if (sbp->st_size < off) 1129b50d902SRodney W. Grimes off = sbp->st_size; 113bd9dc975SAndrey A. Chernov if (fseeko(fp, off, SEEK_SET) == -1) { 1149b50d902SRodney W. Grimes ierr(); 1159b50d902SRodney W. Grimes return; 1169b50d902SRodney W. Grimes } 1179b50d902SRodney W. Grimes } else while (off--) 1189b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) { 1199b50d902SRodney W. Grimes if (ferror(fp)) { 1209b50d902SRodney W. Grimes ierr(); 1219b50d902SRodney W. Grimes return; 1229b50d902SRodney W. Grimes } 1239b50d902SRodney W. Grimes break; 1249b50d902SRodney W. Grimes } 1259b50d902SRodney W. Grimes break; 1269b50d902SRodney W. Grimes case FLINES: 1279b50d902SRodney W. Grimes if (off == 0) 1289b50d902SRodney W. Grimes break; 1299b50d902SRodney W. Grimes for (;;) { 1309b50d902SRodney W. Grimes if ((ch = getc(fp)) == EOF) { 1319b50d902SRodney W. Grimes if (ferror(fp)) { 1329b50d902SRodney W. Grimes ierr(); 1339b50d902SRodney W. Grimes return; 1349b50d902SRodney W. Grimes } 1359b50d902SRodney W. Grimes break; 1369b50d902SRodney W. Grimes } 1379b50d902SRodney W. Grimes if (ch == '\n' && !--off) 1389b50d902SRodney W. Grimes break; 1399b50d902SRodney W. Grimes } 1409b50d902SRodney W. Grimes break; 1419b50d902SRodney W. Grimes case RBYTES: 1429b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) { 1439b50d902SRodney W. Grimes if (sbp->st_size >= off && 144bd9dc975SAndrey A. Chernov fseeko(fp, -off, SEEK_END) == -1) { 1459b50d902SRodney W. Grimes ierr(); 1469b50d902SRodney W. Grimes return; 1479b50d902SRodney W. Grimes } 1489b50d902SRodney W. Grimes } else if (off == 0) { 1499b50d902SRodney W. Grimes while (getc(fp) != EOF); 1509b50d902SRodney W. Grimes if (ferror(fp)) { 1519b50d902SRodney W. Grimes ierr(); 1529b50d902SRodney W. Grimes return; 1539b50d902SRodney W. Grimes } 1549b50d902SRodney W. Grimes } else 1556439f56eSAdam David if (bytes(fp, off)) 1566439f56eSAdam David return; 1579b50d902SRodney W. Grimes break; 1589b50d902SRodney W. Grimes case RLINES: 1599b50d902SRodney W. Grimes if (S_ISREG(sbp->st_mode)) 1609b50d902SRodney W. Grimes if (!off) { 161bd9dc975SAndrey A. Chernov if (fseeko(fp, (off_t)0, SEEK_END) == -1) { 1629b50d902SRodney W. Grimes ierr(); 1639b50d902SRodney W. Grimes return; 1649b50d902SRodney W. Grimes } 1659b50d902SRodney W. Grimes } else 1669b50d902SRodney W. Grimes rlines(fp, off, sbp); 1679b50d902SRodney W. Grimes else if (off == 0) { 1689b50d902SRodney W. Grimes while (getc(fp) != EOF); 1699b50d902SRodney W. Grimes if (ferror(fp)) { 1709b50d902SRodney W. Grimes ierr(); 1719b50d902SRodney W. Grimes return; 1729b50d902SRodney W. Grimes } 1739b50d902SRodney W. Grimes } else 1746439f56eSAdam David if (lines(fp, off)) 1756439f56eSAdam David return; 1769b50d902SRodney W. Grimes break; 177814e3a92SMark Murray default: 178b77b9b9aSMurray Stokely break; 1799b50d902SRodney W. Grimes } 1809b50d902SRodney W. Grimes 1819b50d902SRodney W. Grimes while ((ch = getc(fp)) != EOF) 1829b50d902SRodney W. Grimes if (putchar(ch) == EOF) 1839b50d902SRodney W. Grimes oerr(); 1849b50d902SRodney W. Grimes if (ferror(fp)) { 1859b50d902SRodney W. Grimes ierr(); 1869b50d902SRodney W. Grimes return; 1879b50d902SRodney W. Grimes } 1889b50d902SRodney W. Grimes (void)fflush(stdout); 1899b50d902SRodney W. Grimes } 1909b50d902SRodney W. Grimes 1919b50d902SRodney W. Grimes /* 1929b50d902SRodney W. Grimes * rlines -- display the last offset lines of the file. 1939b50d902SRodney W. Grimes */ 1949b50d902SRodney W. Grimes static void 1959b50d902SRodney W. Grimes rlines(fp, off, sbp) 1969b50d902SRodney W. Grimes FILE *fp; 197bd9dc975SAndrey A. Chernov off_t off; 1989b50d902SRodney W. Grimes struct stat *sbp; 1999b50d902SRodney W. Grimes { 200726098d3SDavid Malone struct mapinfo map; 201726098d3SDavid Malone off_t curoff, size; 202726098d3SDavid Malone int i; 2039b50d902SRodney W. Grimes 2049b50d902SRodney W. Grimes if (!(size = sbp->st_size)) 2059b50d902SRodney W. Grimes return; 206726098d3SDavid Malone map.start = NULL; 207726098d3SDavid Malone map.fd = fileno(fp); 208726098d3SDavid Malone map.mapoff = map.maxoff = size; 2099b50d902SRodney W. Grimes 2106bea9ab4SAndrey A. Chernov /* 211726098d3SDavid Malone * Last char is special, ignore whether newline or not. Note that 212726098d3SDavid Malone * size == 0 is dealt with above, and size == 1 sets curoff to -1. 2136bea9ab4SAndrey A. Chernov */ 214726098d3SDavid Malone curoff = size - 2; 215726098d3SDavid Malone while (curoff >= 0) { 216726098d3SDavid Malone if (curoff < map.mapoff && maparound(&map, curoff) != 0) { 2179b50d902SRodney W. Grimes ierr(); 2189b50d902SRodney W. Grimes return; 2199b50d902SRodney W. Grimes } 220726098d3SDavid Malone for (i = curoff - map.mapoff; i >= 0; i--) 221726098d3SDavid Malone if (map.start[i] == '\n' && --off == 0) 222726098d3SDavid Malone break; 223726098d3SDavid Malone /* `i' is either the map offset of a '\n', or -1. */ 224726098d3SDavid Malone curoff = map.mapoff + i; 225726098d3SDavid Malone if (i >= 0) 226726098d3SDavid Malone break; 227726098d3SDavid Malone } 228726098d3SDavid Malone curoff++; 229726098d3SDavid Malone if (mapprint(&map, curoff, size - curoff) != 0) { 230726098d3SDavid Malone ierr(); 231726098d3SDavid Malone exit(1); 232726098d3SDavid Malone } 233726098d3SDavid Malone 234726098d3SDavid Malone /* Set the file pointer to reflect the length displayed. */ 235a74da62eSAndrey A. Chernov if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) { 236726098d3SDavid Malone ierr(); 237726098d3SDavid Malone return; 238726098d3SDavid Malone } 239726098d3SDavid Malone if (map.start != NULL && munmap(map.start, map.maplen)) { 24044cf272fSAdam David ierr(); 2419b50d902SRodney W. Grimes return; 2429b50d902SRodney W. Grimes } 2439b50d902SRodney W. Grimes } 24415a55f79SPaul Richards 245a2fb00d9SDavid Malone static void 24615a55f79SPaul Richards show(file_info_t *file) 24715a55f79SPaul Richards { 2489e777bc6SBrian Somers int ch; 24915a55f79SPaul Richards 25015a55f79SPaul Richards while ((ch = getc(file->fp)) != EOF) { 2519e777bc6SBrian Somers if (last != file && no_files > 1) { 25215a55f79SPaul Richards (void)printf("\n==> %s <==\n", file->file_name); 2539e777bc6SBrian Somers last = file; 25415a55f79SPaul Richards } 25515a55f79SPaul Richards if (putchar(ch) == EOF) 25615a55f79SPaul Richards oerr(); 25715a55f79SPaul Richards } 25815a55f79SPaul Richards (void)fflush(stdout); 25915a55f79SPaul Richards if (ferror(file->fp)) { 26015a55f79SPaul Richards file->fp = NULL; 26115a55f79SPaul Richards ierr(); 26215a55f79SPaul Richards } else 26315a55f79SPaul Richards clearerr(file->fp); 26415a55f79SPaul Richards } 26515a55f79SPaul Richards 266a2fb00d9SDavid Malone static void 26715a55f79SPaul Richards set_events(file_info_t *files) 26815a55f79SPaul Richards { 26915a55f79SPaul Richards int i, n = 0; 27015a55f79SPaul Richards file_info_t *file; 27115a55f79SPaul Richards struct timespec ts; 27213829828SPaul Saab struct statfs sf; 27315a55f79SPaul Richards 27415a55f79SPaul Richards ts.tv_sec = 0; 27515a55f79SPaul Richards ts.tv_nsec = 0; 27615a55f79SPaul Richards 27715a55f79SPaul Richards action = USE_KQUEUE; 27815a55f79SPaul Richards for (i = 0, file = files; i < no_files; i++, file++) { 27915a55f79SPaul Richards if (! file->fp) 28015a55f79SPaul Richards continue; 28113829828SPaul Saab 28213829828SPaul Saab if (fstatfs(fileno(file->fp), &sf) == 0 && 28313829828SPaul Saab (sf.f_flags & MNT_LOCAL) == 0) { 28413829828SPaul Saab action = USE_SLEEP; 28513829828SPaul Saab return; 28613829828SPaul Saab } 28713829828SPaul Saab 28815a55f79SPaul Richards if (Fflag && fileno(file->fp) != STDIN_FILENO) { 28915a55f79SPaul Richards EV_SET(&ev[n], fileno(file->fp), EVFILT_VNODE, 29015a55f79SPaul Richards EV_ADD | EV_ENABLE | EV_CLEAR, 29115a55f79SPaul Richards NOTE_DELETE | NOTE_RENAME, 0, 0); 29215a55f79SPaul Richards n++; 29315a55f79SPaul Richards } 29415a55f79SPaul Richards EV_SET(&ev[n], fileno(file->fp), EVFILT_READ, 29515a55f79SPaul Richards EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0); 29615a55f79SPaul Richards n++; 29715a55f79SPaul Richards } 29815a55f79SPaul Richards 29915a55f79SPaul Richards if (kevent(kq, ev, n, NULL, 0, &ts) < 0) { 30015a55f79SPaul Richards action = USE_SLEEP; 30115a55f79SPaul Richards } 30215a55f79SPaul Richards } 30315a55f79SPaul Richards 30476628ce7SXin LI /* 30576628ce7SXin LI * follow -- display the file, from an offset, forward. 30676628ce7SXin LI * 30776628ce7SXin LI */ 30815a55f79SPaul Richards void 30915a55f79SPaul Richards follow(file_info_t *files, enum STYLE style, off_t off) 31015a55f79SPaul Richards { 31115a55f79SPaul Richards int active, i, n = -1; 31215a55f79SPaul Richards struct stat sb2; 31315a55f79SPaul Richards file_info_t *file; 31415a55f79SPaul Richards struct timespec ts; 31515a55f79SPaul Richards 31615a55f79SPaul Richards /* Position each of the files */ 31715a55f79SPaul Richards 31815a55f79SPaul Richards file = files; 31915a55f79SPaul Richards active = 0; 32015a55f79SPaul Richards n = 0; 32115a55f79SPaul Richards for (i = 0; i < no_files; i++, file++) { 32215a55f79SPaul Richards if (file->fp) { 32315a55f79SPaul Richards active = 1; 32415a55f79SPaul Richards n++; 32515a55f79SPaul Richards if (no_files > 1) 32615a55f79SPaul Richards (void)printf("\n==> %s <==\n", file->file_name); 32715a55f79SPaul Richards forward(file->fp, style, off, &file->st); 32815a55f79SPaul Richards if (Fflag && fileno(file->fp) != STDIN_FILENO) 32915a55f79SPaul Richards n++; 33015a55f79SPaul Richards } 33115a55f79SPaul Richards } 33215a55f79SPaul Richards if (! active) 33315a55f79SPaul Richards return; 33415a55f79SPaul Richards 33576628ce7SXin LI last = --file; 33676628ce7SXin LI 33715a55f79SPaul Richards kq = kqueue(); 33815a55f79SPaul Richards if (kq < 0) 33915a55f79SPaul Richards err(1, "kqueue"); 34015a55f79SPaul Richards ev = malloc(n * sizeof(struct kevent)); 34115a55f79SPaul Richards if (! ev) 34215a55f79SPaul Richards err(1, "Couldn't allocate memory for kevents."); 34315a55f79SPaul Richards set_events(files); 34415a55f79SPaul Richards 34515a55f79SPaul Richards for (;;) { 34615a55f79SPaul Richards for (i = 0, file = files; i < no_files; i++, file++) { 34715a55f79SPaul Richards if (! file->fp) 34815a55f79SPaul Richards continue; 34915a55f79SPaul Richards if (Fflag && file->fp && fileno(file->fp) != STDIN_FILENO) { 35015a55f79SPaul Richards if (stat(file->file_name, &sb2) != 0) { 35115a55f79SPaul Richards /* file was rotated, skip it until it reappears */ 35215a55f79SPaul Richards continue; 35315a55f79SPaul Richards } 35415a55f79SPaul Richards if (sb2.st_ino != file->st.st_ino || 35515a55f79SPaul Richards sb2.st_dev != file->st.st_dev || 35615a55f79SPaul Richards sb2.st_nlink == 0) { 35715a55f79SPaul Richards file->fp = freopen(file->file_name, "r", file->fp); 35815a55f79SPaul Richards if (file->fp == NULL) { 35915a55f79SPaul Richards ierr(); 36015a55f79SPaul Richards continue; 36115a55f79SPaul Richards } else { 36215a55f79SPaul Richards memcpy(&file->st, &sb2, sizeof(struct stat)); 36315a55f79SPaul Richards set_events(files); 36415a55f79SPaul Richards } 36515a55f79SPaul Richards } 36615a55f79SPaul Richards } 36715a55f79SPaul Richards show(file); 36815a55f79SPaul Richards } 36915a55f79SPaul Richards 37015a55f79SPaul Richards switch (action) { 37115a55f79SPaul Richards case USE_KQUEUE: 37215a55f79SPaul Richards ts.tv_sec = 1; 37315a55f79SPaul Richards ts.tv_nsec = 0; 37415a55f79SPaul Richards /* 37515a55f79SPaul Richards * In the -F case we set a timeout to ensure that 37615a55f79SPaul Richards * we re-stat the file at least once every second. 37715a55f79SPaul Richards */ 37815a55f79SPaul Richards n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL); 37915a55f79SPaul Richards if (n < 0) 38015a55f79SPaul Richards err(1, "kevent"); 38115a55f79SPaul Richards if (n == 0) { 38215a55f79SPaul Richards /* timeout */ 38315a55f79SPaul Richards break; 38415a55f79SPaul Richards } else if (ev->filter == EVFILT_READ && ev->data < 0) { 38515a55f79SPaul Richards /* file shrank, reposition to end */ 38615a55f79SPaul Richards if (lseek(ev->ident, (off_t)0, SEEK_END) == -1) { 38715a55f79SPaul Richards ierr(); 38815a55f79SPaul Richards continue; 38915a55f79SPaul Richards } 39015a55f79SPaul Richards } 39115a55f79SPaul Richards break; 39215a55f79SPaul Richards 39315a55f79SPaul Richards case USE_SLEEP: 39415a55f79SPaul Richards (void) usleep(250000); 39515a55f79SPaul Richards break; 39615a55f79SPaul Richards } 39715a55f79SPaul Richards } 39815a55f79SPaul Richards } 399