xref: /minix/usr.bin/tail/forward.c (revision 7b1dfc68)
1 /*	$NetBSD: forward.c,v 1.32 2013/10/18 20:47:07 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Edward Sze-Tyan Wang.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
21  *
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
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 __RCSID("$NetBSD: forward.c,v 1.32 2013/10/18 20:47:07 christos Exp $");
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/mman.h>
47 #include <sys/event.h>
48 
49 #include <limits.h>
50 #include <fcntl.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include "extern.h"
57 
58 static int rlines(FILE *, off_t, struct stat *);
59 
60 /* defines for inner loop actions */
61 #define	USE_SLEEP	0
62 #define	USE_KQUEUE	1
63 #define	ADD_EVENTS	2
64 
65 /*
66  * forward -- display the file, from an offset, forward.
67  *
68  * There are eight separate cases for this -- regular and non-regular
69  * files, by bytes or lines and from the beginning or end of the file.
70  *
71  * FBYTES	byte offset from the beginning of the file
72  *	REG	seek
73  *	NOREG	read, counting bytes
74  *
75  * FLINES	line offset from the beginning of the file
76  *	REG	read, counting lines
77  *	NOREG	read, counting lines
78  *
79  * RBYTES	byte offset from the end of the file
80  *	REG	seek
81  *	NOREG	cyclically read characters into a wrap-around buffer
82  *
83  * RLINES
84  *	REG	mmap the file and step back until reach the correct offset.
85  *	NOREG	cyclically read lines into a wrap-around array of buffers
86  */
87 void
88 forward(FILE *fp, enum STYLE style, off_t off, struct stat *sbp)
89 {
90 #ifndef __minix
91 	int ch, n;
92 #else
93 	int ch;
94 #endif
95 	int kq=-1, action=USE_SLEEP;
96 	struct stat statbuf;
97 #ifndef __minix
98 	struct kevent ev[2];
99 #endif
100 
101 	switch(style) {
102 	case FBYTES:
103 		if (off == 0)
104 			break;
105 		if (S_ISREG(sbp->st_mode)) {
106 			if (sbp->st_size < off)
107 				off = sbp->st_size;
108 			if (fseeko(fp, off, SEEK_SET) == -1) {
109 				ierr();
110 				return;
111 			}
112 		} else while (off--)
113 			if ((ch = getc(fp)) == EOF) {
114 				if (ferror(fp)) {
115 					ierr();
116 					return;
117 				}
118 				break;
119 			}
120 		break;
121 	case FLINES:
122 		if (off == 0)
123 			break;
124 		for (;;) {
125 			if ((ch = getc(fp)) == EOF) {
126 				if (ferror(fp)) {
127 					ierr();
128 					return;
129 				}
130 				break;
131 			}
132 			if (ch == '\n' && !--off)
133 				break;
134 		}
135 		break;
136 	case RBYTES:
137 		if (S_ISREG(sbp->st_mode)) {
138 			if (sbp->st_size >= off &&
139 			    fseeko(fp, -off, SEEK_END) == -1) {
140 				ierr();
141 				return;
142 			}
143 		} else if (off == 0) {
144 			while (getc(fp) != EOF);
145 			if (ferror(fp)) {
146 				ierr();
147 				return;
148 			}
149 		} else {
150 			if (displaybytes(fp, off))
151 				return;
152 		}
153 		break;
154 	case RLINES:
155 		if (S_ISREG(sbp->st_mode)) {
156 			if (!off) {
157 				if (fseek(fp, 0L, SEEK_END) == -1) {
158 					ierr();
159 					return;
160 				}
161 			} else {
162 				if (rlines(fp, off, sbp))
163 					return;
164 			}
165 		} else if (off == 0) {
166 			while (getc(fp) != EOF);
167 			if (ferror(fp)) {
168 				ierr();
169 				return;
170 			}
171 		} else {
172 			if (displaylines(fp, off))
173 				return;
174 		}
175 		break;
176 	default:
177 		break;
178 	}
179 
180 	if (fflag) {
181 #ifndef __minix
182 		kq = kqueue();
183 		if (kq < 0)
184 			xerr(1, "kqueue");
185 		action = ADD_EVENTS;
186 #else
187         action = USE_SLEEP;
188 #endif
189 	}
190 
191 	for (;;) {
192 		while ((ch = getc(fp)) != EOF)  {
193 			if (putchar(ch) == EOF)
194 				oerr();
195 		}
196 		if (ferror(fp)) {
197 			ierr();
198 			return;
199 		}
200 		(void)fflush(stdout);
201 		if (!fflag)
202 			break;
203 
204 		clearerr(fp);
205 
206 		switch (action) {
207 #ifndef __minix
208 		case ADD_EVENTS:
209 			n = 0;
210 
211 			memset(ev, 0, sizeof(ev));
212 			if (fflag == 2 && fileno(fp) != STDIN_FILENO) {
213 				EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
214 				    EV_ADD | EV_ENABLE | EV_CLEAR,
215 				    NOTE_DELETE | NOTE_RENAME, 0, 0);
216 				n++;
217 			}
218 			EV_SET(&ev[n], fileno(fp), EVFILT_READ,
219 			    EV_ADD | EV_ENABLE, 0, 0, 0);
220 			n++;
221 
222 			if (kevent(kq, ev, n, NULL, 0, NULL) == -1) {
223 				close(kq);
224 				kq = -1;
225 				action = USE_SLEEP;
226 			} else {
227 				action = USE_KQUEUE;
228 			}
229 			break;
230 
231 		case USE_KQUEUE:
232 			if (kevent(kq, NULL, 0, ev, 1, NULL) == -1)
233 				xerr(1, "kevent");
234 
235 			if (ev[0].filter == EVFILT_VNODE) {
236 				/* file was rotated, wait until it reappears */
237 				action = USE_SLEEP;
238 			} else if (ev[0].data < 0) {
239 				/* file shrank, reposition to end */
240 				if (fseek(fp, 0L, SEEK_END) == -1) {
241 					ierr();
242 					return;
243 				}
244 			}
245 			break;
246 #endif
247 
248 		case USE_SLEEP:
249 			/*
250 			 * We pause for one second after displaying any data
251 			 * that has accumulated since we read the file.
252 			 */
253                 	(void) sleep(1);
254 
255 			if (fflag == 2 && fileno(fp) != STDIN_FILENO &&
256 			    stat(fname, &statbuf) != -1) {
257 				if (statbuf.st_ino != sbp->st_ino ||
258 				    statbuf.st_dev != sbp->st_dev ||
259 				    statbuf.st_rdev != sbp->st_rdev ||
260 				    statbuf.st_nlink == 0) {
261 					fp = freopen(fname, "r", fp);
262 					if (fp == NULL) {
263 						ierr();
264 						goto out;
265 					}
266 					*sbp = statbuf;
267 					if (kq != -1)
268 						action = ADD_EVENTS;
269 				} else if (kq != -1)
270 					action = USE_KQUEUE;
271 			}
272 			break;
273 		}
274 	}
275 out:
276 	if (fflag && kq != -1)
277 		close(kq);
278 }
279 
280 /*
281  * rlines -- display the last offset lines of the file.
282  *
283  * Non-zero return means than a (non-fatal) error occurred.
284  */
285 static int
286 rlines(FILE *fp, off_t off, struct stat *sbp)
287 {
288 	off_t file_size;
289 	off_t file_remaining;
290 	char *p = NULL;
291 	char *start = NULL;
292 	off_t mmap_size;
293 	off_t mmap_offset;
294 	off_t mmap_remaining = 0;
295 
296 #define MMAP_MAXSIZE  (10 * 1024 * 1024)
297 
298 	if (!(file_size = sbp->st_size))
299 		return 0;
300 	file_remaining = file_size;
301 
302 	if (file_remaining > MMAP_MAXSIZE) {
303 		mmap_size = MMAP_MAXSIZE;
304 		mmap_offset = file_remaining - MMAP_MAXSIZE;
305 	} else {
306 		mmap_size = file_remaining;
307 		mmap_offset = 0;
308 	}
309 
310 	while (off) {
311 		start = mmap(NULL, (size_t)mmap_size, PROT_READ,
312 			     MAP_FILE|MAP_SHARED, fileno(fp), mmap_offset);
313 		if (start == MAP_FAILED) {
314 			xerr(0, "%s", fname);
315 			return 1;
316 		}
317 
318 		mmap_remaining = mmap_size;
319 		/* Last char is special, ignore whether newline or not. */
320 		for (p = start + mmap_remaining - 1 ; --mmap_remaining ; )
321 			if (*--p == '\n' && !--off) {
322 				++p;
323 				break;
324 			}
325 
326 		file_remaining -= mmap_size - mmap_remaining;
327 
328 		if (off == 0)
329 			break;
330 
331 		if (file_remaining == 0)
332 			break;
333 
334 		if (munmap(start, mmap_size)) {
335 			xerr(0, "%s", fname);
336 			return 1;
337 		}
338 
339 		if (mmap_offset >= MMAP_MAXSIZE) {
340 			mmap_offset -= MMAP_MAXSIZE;
341 		} else {
342 			mmap_offset = 0;
343 			mmap_size = file_remaining;
344 		}
345 	}
346 
347 	/*
348 	 * Output the (perhaps partial) data in this mmap'd block.
349 	 */
350 	WR(p, mmap_size - mmap_remaining);
351 	file_remaining += mmap_size - mmap_remaining;
352 	if (munmap(start, mmap_size)) {
353 		xerr(0, "%s", fname);
354 		return 1;
355 	}
356 
357 	/*
358 	 * Set the file pointer to reflect the length displayed.
359 	 * This will cause the caller to redisplay the data if/when
360 	 * needed.
361 	 */
362 	if (fseeko(fp, file_remaining, SEEK_SET) == -1) {
363 		ierr();
364 		return 1;
365 	}
366 	return 0;
367 }
368