Fix OPSYS constant in pkg_install tools
[minix3.git] / usr.bin / tail / forward.c
blob2af32e922d7c4bfda1613f99d3feaa00bd269f1a
1 /* $NetBSD: forward.c,v 1.33 2015/10/09 17:51:26 christos Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Sze-Tyan Wang.
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.
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.
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.33 2015/10/09 17:51:26 christos Exp $");
41 #endif /* not lint */
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>
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"
58 static int rlines(FILE *, off_t, struct stat *);
60 /* defines for inner loop actions */
61 #define USE_SLEEP 0
62 #define USE_KQUEUE 1
63 #define ADD_EVENTS 2
66 * forward -- display the file, from an offset, forward.
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.
71 * FBYTES byte offset from the beginning of the file
72 * REG seek
73 * NOREG read, counting bytes
75 * FLINES line offset from the beginning of the file
76 * REG read, counting lines
77 * NOREG read, counting lines
79 * RBYTES byte offset from the end of the file
80 * REG seek
81 * NOREG cyclically read characters into a wrap-around buffer
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
87 void
88 forward(FILE *fp, enum STYLE style, off_t off, struct stat *sbp)
90 #if !defined(__minix)
91 int ch, n;
92 #else
93 int ch;
94 #endif /* !defined(__minix) */
95 int kq=-1, action=USE_SLEEP;
96 struct stat statbuf;
97 #if !defined(__minix)
98 struct kevent ev[2];
99 #endif /* !defined(__minix) */
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;
112 } else while (off--)
113 if ((ch = getc(fp)) == EOF) {
114 if (ferror(fp)) {
115 ierr();
116 return;
118 break;
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;
130 break;
132 if (ch == '\n' && !--off)
133 break;
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;
143 } else if (off == 0) {
144 while (getc(fp) != EOF);
145 if (ferror(fp)) {
146 ierr();
147 return;
149 } else {
150 if (displaybytes(fp, off))
151 return;
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;
161 } else {
162 if (rlines(fp, off, sbp))
163 return;
165 } else if (off == 0) {
166 while (getc(fp) != EOF);
167 if (ferror(fp)) {
168 ierr();
169 return;
171 } else {
172 if (displaylines(fp, off))
173 return;
175 break;
176 default:
177 break;
180 if (fflag) {
181 #if !defined(__minix)
182 kq = kqueue();
183 if (kq < 0)
184 xerr(1, "kqueue");
185 action = ADD_EVENTS;
186 #else
187 action = USE_SLEEP;
188 #endif /* !defined(__minix) */
191 for (;;) {
192 while ((ch = getc(fp)) != EOF) {
193 if (putchar(ch) == EOF)
194 oerr();
196 if (ferror(fp)) {
197 ierr();
198 return;
200 (void)fflush(stdout);
201 if (!fflag)
202 break;
204 clearerr(fp);
206 switch (action) {
207 #if !defined(__minix)
208 case ADD_EVENTS:
209 n = 0;
211 memset(ev, 0, sizeof(ev));
212 if (fflag == 2 && fp != stdin) {
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++;
218 EV_SET(&ev[n], fileno(fp), EVFILT_READ,
219 EV_ADD | EV_ENABLE, 0, 0, 0);
220 n++;
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;
229 break;
231 case USE_KQUEUE:
232 if (kevent(kq, NULL, 0, ev, 1, NULL) == -1)
233 xerr(1, "kevent");
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;
245 break;
246 #endif /* !defined(__minix) */
248 case USE_SLEEP:
250 * We pause for one second after displaying any data
251 * that has accumulated since we read the file.
253 (void) sleep(1);
255 if (fflag == 2 && fp != stdin &&
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;
266 *sbp = statbuf;
267 if (kq != -1)
268 action = ADD_EVENTS;
269 } else if (kq != -1)
270 action = USE_KQUEUE;
272 break;
275 out:
276 if (fflag && kq != -1)
277 close(kq);
281 * rlines -- display the last offset lines of the file.
283 * Non-zero return means than a (non-fatal) error occurred.
285 static int
286 rlines(FILE *fp, off_t off, struct stat *sbp)
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;
296 #define MMAP_MAXSIZE (10 * 1024 * 1024)
298 if (!(file_size = sbp->st_size))
299 return 0;
300 file_remaining = file_size;
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;
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;
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;
326 file_remaining -= mmap_size - mmap_remaining;
328 if (off == 0)
329 break;
331 if (file_remaining == 0)
332 break;
334 if (munmap(start, mmap_size)) {
335 xerr(0, "%s", fname);
336 return 1;
339 if (mmap_offset >= MMAP_MAXSIZE) {
340 mmap_offset -= MMAP_MAXSIZE;
341 } else {
342 mmap_offset = 0;
343 mmap_size = file_remaining;
348 * Output the (perhaps partial) data in this mmap'd block.
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;
358 * Set the file pointer to reflect the length displayed.
359 * This will cause the caller to redisplay the data if/when
360 * needed.
362 if (fseeko(fp, file_remaining, SEEK_SET) == -1) {
363 ierr();
364 return 1;
366 return 0;