xref: /freebsd/usr.bin/tail/forward.c (revision 7562eaabc01a48e6b11d5b558c41e3b92dae5c2d)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Sze-Tyan Wang.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/cdefs.h>
38 
39 __FBSDID("$FreeBSD$");
40 
41 #ifndef lint
42 static const char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
43 #endif
44 
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/mman.h>
49 #include <sys/event.h>
50 
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <limits.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 
60 #include "extern.h"
61 
62 static void rlines(FILE *, off_t, struct stat *);
63 
64 /* defines for inner loop actions */
65 #define USE_SLEEP	0
66 #define USE_KQUEUE	1
67 #define ADD_EVENTS	2
68 
69 /*
70  * forward -- display the file, from an offset, forward.
71  *
72  * There are eight separate cases for this -- regular and non-regular
73  * files, by bytes or lines and from the beginning or end of the file.
74  *
75  * FBYTES	byte offset from the beginning of the file
76  *	REG	seek
77  *	NOREG	read, counting bytes
78  *
79  * FLINES	line offset from the beginning of the file
80  *	REG	read, counting lines
81  *	NOREG	read, counting lines
82  *
83  * RBYTES	byte offset from the end of the file
84  *	REG	seek
85  *	NOREG	cyclically read characters into a wrap-around buffer
86  *
87  * RLINES
88  *	REG	mmap the file and step back until reach the correct offset.
89  *	NOREG	cyclically read lines into a wrap-around array of buffers
90  */
91 void
92 forward(FILE *fp, enum STYLE style, off_t off, struct stat *sbp)
93 {
94 	int ch, n, kq = -1;
95 	int action = USE_SLEEP;
96 	struct kevent ev[2];
97 	struct stat sb2;
98 	struct timespec ts;
99 
100 	switch(style) {
101 	case FBYTES:
102 		if (off == 0)
103 			break;
104 		if (S_ISREG(sbp->st_mode)) {
105 			if (sbp->st_size < off)
106 				off = sbp->st_size;
107 			if (fseeko(fp, off, SEEK_SET) == -1) {
108 				ierr();
109 				return;
110 			}
111 		} else while (off--)
112 			if ((ch = getc(fp)) == EOF) {
113 				if (ferror(fp)) {
114 					ierr();
115 					return;
116 				}
117 				break;
118 			}
119 		break;
120 	case FLINES:
121 		if (off == 0)
122 			break;
123 		for (;;) {
124 			if ((ch = getc(fp)) == EOF) {
125 				if (ferror(fp)) {
126 					ierr();
127 					return;
128 				}
129 				break;
130 			}
131 			if (ch == '\n' && !--off)
132 				break;
133 		}
134 		break;
135 	case RBYTES:
136 		if (S_ISREG(sbp->st_mode)) {
137 			if (sbp->st_size >= off &&
138 			    fseeko(fp, -off, SEEK_END) == -1) {
139 				ierr();
140 				return;
141 			}
142 		} else if (off == 0) {
143 			while (getc(fp) != EOF);
144 			if (ferror(fp)) {
145 				ierr();
146 				return;
147 			}
148 		} else
149 			if (bytes(fp, off))
150 				return;
151 		break;
152 	case RLINES:
153 		if (S_ISREG(sbp->st_mode))
154 			if (!off) {
155 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
156 					ierr();
157 					return;
158 				}
159 			} else
160 				rlines(fp, off, sbp);
161 		else if (off == 0) {
162 			while (getc(fp) != EOF);
163 			if (ferror(fp)) {
164 				ierr();
165 				return;
166 			}
167 		} else
168 			if (lines(fp, off))
169 				return;
170 		break;
171 	default:
172 		break;
173 	}
174 
175 	if (fflag) {
176 		kq = kqueue();
177 		if (kq < 0)
178 			err(1, "kqueue");
179 		action = ADD_EVENTS;
180 	}
181 
182 	for (;;) {
183 		while ((ch = getc(fp)) != EOF)
184 			if (putchar(ch) == EOF)
185 				oerr();
186 		if (ferror(fp)) {
187 			ierr();
188 			return;
189 		}
190 		(void)fflush(stdout);
191 		if (! fflag)
192 			break;
193 		clearerr(fp);
194 
195 		switch (action) {
196 		case ADD_EVENTS:
197 			n = 0;
198 			ts.tv_sec = 0;
199 			ts.tv_nsec = 0;
200 
201 			if (Fflag && fileno(fp) != STDIN_FILENO) {
202 				EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
203 				    EV_ADD | EV_ENABLE | EV_CLEAR,
204 				    NOTE_DELETE | NOTE_RENAME, 0, 0);
205 				n++;
206 			}
207 			EV_SET(&ev[n], fileno(fp), EVFILT_READ,
208 			    EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
209 			n++;
210 
211 			if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
212 				action = USE_SLEEP;
213 			} else {
214 				action = USE_KQUEUE;
215 			}
216 			break;
217 
218 		case USE_KQUEUE:
219 			ts.tv_sec = 1;
220 			ts.tv_nsec = 0;
221 			/*
222 			 * In the -F case we set a timeout to ensure that
223 			 * we re-stat the file at least once every second.
224 			 */
225 			n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
226 			if (n < 0)
227 				err(1, "kevent");
228 			if (n == 0) {
229 				/* timeout */
230 				break;
231 			} else if (ev->filter == EVFILT_READ && ev->data < 0) {
232 				 /* file shrank, reposition to end */
233 				if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
234 					ierr();
235 					return;
236 				}
237 			}
238 			break;
239 
240 		case USE_SLEEP:
241                 	(void) usleep(250000);
242 	                clearerr(fp);
243 			break;
244 		}
245 
246 		if (Fflag && fileno(fp) != STDIN_FILENO) {
247 			while (stat(fname, &sb2) != 0)
248 				/* file was rotated, wait until it reappears */
249 				(void)sleep(1);
250 			if (sb2.st_ino != sbp->st_ino ||
251 			    sb2.st_dev != sbp->st_dev ||
252 			    sb2.st_nlink == 0) {
253 				fp = freopen(fname, "r", fp);
254 				if (fp == NULL) {
255 					ierr();
256 					return;
257 				} else {
258 					*sbp = sb2;
259 					action = ADD_EVENTS;
260 				}
261 			}
262 		}
263 	}
264 }
265 
266 /*
267  * rlines -- display the last offset lines of the file.
268  */
269 static void
270 rlines(fp, off, sbp)
271 	FILE *fp;
272 	off_t off;
273 	struct stat *sbp;
274 {
275 	struct mapinfo map;
276 	off_t curoff, size;
277 	int i;
278 
279 	if (!(size = sbp->st_size))
280 		return;
281 	map.start = NULL;
282 	map.fd = fileno(fp);
283 	map.mapoff = map.maxoff = size;
284 
285 	/*
286 	 * Last char is special, ignore whether newline or not. Note that
287 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
288 	 */
289 	curoff = size - 2;
290 	while (curoff >= 0) {
291 		if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
292 			ierr();
293 			return;
294 		}
295 		for (i = curoff - map.mapoff; i >= 0; i--)
296 			if (map.start[i] == '\n' && --off == 0)
297 				break;
298 		/* `i' is either the map offset of a '\n', or -1. */
299 		curoff = map.mapoff + i;
300 		if (i >= 0)
301 			break;
302 	}
303 	curoff++;
304 	if (mapprint(&map, curoff, size - curoff) != 0) {
305 		ierr();
306 		exit(1);
307 	}
308 
309 	/* Set the file pointer to reflect the length displayed. */
310 	if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
311 		ierr();
312 		return;
313 	}
314 	if (map.start != NULL && munmap(map.start, map.maplen)) {
315 		ierr();
316 		return;
317 	}
318 }
319