forward.c revision 1.23 1 1.23 jdolecek /* $NetBSD: forward.c,v 1.23 2002/10/30 21:48:50 jdolecek Exp $ */
2 1.6 jtc
3 1.1 glass /*-
4 1.6 jtc * Copyright (c) 1991, 1993
5 1.6 jtc * The Regents of the University of California. All rights reserved.
6 1.1 glass *
7 1.1 glass * This code is derived from software contributed to Berkeley by
8 1.1 glass * Edward Sze-Tyan Wang.
9 1.1 glass *
10 1.1 glass * Redistribution and use in source and binary forms, with or without
11 1.1 glass * modification, are permitted provided that the following conditions
12 1.1 glass * are met:
13 1.1 glass * 1. Redistributions of source code must retain the above copyright
14 1.1 glass * notice, this list of conditions and the following disclaimer.
15 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 glass * notice, this list of conditions and the following disclaimer in the
17 1.1 glass * documentation and/or other materials provided with the distribution.
18 1.1 glass * 3. All advertising materials mentioning features or use of this software
19 1.1 glass * must display the following acknowledgement:
20 1.1 glass * This product includes software developed by the University of
21 1.1 glass * California, Berkeley and its contributors.
22 1.1 glass * 4. Neither the name of the University nor the names of its contributors
23 1.1 glass * may be used to endorse or promote products derived from this software
24 1.1 glass * without specific prior written permission.
25 1.1 glass *
26 1.1 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 1.1 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 1.1 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 1.1 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 1.1 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 1.1 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 1.1 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 1.1 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 1.1 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 1.1 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 1.1 glass * SUCH DAMAGE.
37 1.1 glass */
38 1.1 glass
39 1.8 lukem #include <sys/cdefs.h>
40 1.1 glass #ifndef lint
41 1.6 jtc #if 0
42 1.6 jtc static char sccsid[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93";
43 1.6 jtc #endif
44 1.23 jdolecek __RCSID("$NetBSD: forward.c,v 1.23 2002/10/30 21:48:50 jdolecek Exp $");
45 1.1 glass #endif /* not lint */
46 1.1 glass
47 1.1 glass #include <sys/types.h>
48 1.1 glass #include <sys/stat.h>
49 1.1 glass #include <sys/time.h>
50 1.1 glass #include <sys/mman.h>
51 1.23 jdolecek #include <sys/event.h>
52 1.6 jtc
53 1.6 jtc #include <limits.h>
54 1.1 glass #include <fcntl.h>
55 1.1 glass #include <errno.h>
56 1.1 glass #include <unistd.h>
57 1.1 glass #include <stdio.h>
58 1.1 glass #include <stdlib.h>
59 1.1 glass #include <string.h>
60 1.1 glass #include "extern.h"
61 1.1 glass
62 1.19 wiz static int rlines(FILE *, long, struct stat *);
63 1.1 glass
64 1.23 jdolecek /* defines for inner loop actions */
65 1.23 jdolecek #define USE_SLEEP 0
66 1.23 jdolecek #define USE_KQUEUE 1
67 1.23 jdolecek #define ADD_EVENTS 2
68 1.23 jdolecek
69 1.1 glass /*
70 1.1 glass * forward -- display the file, from an offset, forward.
71 1.1 glass *
72 1.1 glass * There are eight separate cases for this -- regular and non-regular
73 1.1 glass * files, by bytes or lines and from the beginning or end of the file.
74 1.1 glass *
75 1.1 glass * FBYTES byte offset from the beginning of the file
76 1.1 glass * REG seek
77 1.1 glass * NOREG read, counting bytes
78 1.1 glass *
79 1.1 glass * FLINES line offset from the beginning of the file
80 1.1 glass * REG read, counting lines
81 1.1 glass * NOREG read, counting lines
82 1.1 glass *
83 1.1 glass * RBYTES byte offset from the end of the file
84 1.1 glass * REG seek
85 1.1 glass * NOREG cyclically read characters into a wrap-around buffer
86 1.1 glass *
87 1.1 glass * RLINES
88 1.1 glass * REG mmap the file and step back until reach the correct offset.
89 1.1 glass * NOREG cyclically read lines into a wrap-around array of buffers
90 1.1 glass */
91 1.1 glass void
92 1.19 wiz forward(FILE *fp, enum STYLE style, long int off, struct stat *sbp)
93 1.1 glass {
94 1.23 jdolecek int ch, n;
95 1.23 jdolecek int kq=-1, action=USE_SLEEP;
96 1.9 cjs struct stat statbuf;
97 1.9 cjs dev_t lastdev;
98 1.9 cjs ino_t lastino;
99 1.23 jdolecek struct kevent ev[2];
100 1.9 cjs
101 1.9 cjs /* Keep track of file's previous incarnation. */
102 1.9 cjs lastdev = sbp->st_dev;
103 1.9 cjs lastino = sbp->st_ino;
104 1.1 glass
105 1.1 glass switch(style) {
106 1.1 glass case FBYTES:
107 1.1 glass if (off == 0)
108 1.1 glass break;
109 1.1 glass if (S_ISREG(sbp->st_mode)) {
110 1.1 glass if (sbp->st_size < off)
111 1.1 glass off = sbp->st_size;
112 1.6 jtc if (fseek(fp, off, SEEK_SET) == -1) {
113 1.1 glass ierr();
114 1.6 jtc return;
115 1.6 jtc }
116 1.1 glass } else while (off--)
117 1.1 glass if ((ch = getc(fp)) == EOF) {
118 1.6 jtc if (ferror(fp)) {
119 1.1 glass ierr();
120 1.6 jtc return;
121 1.1 glass }
122 1.6 jtc break;
123 1.6 jtc }
124 1.1 glass break;
125 1.1 glass case FLINES:
126 1.1 glass if (off == 0)
127 1.1 glass break;
128 1.1 glass for (;;) {
129 1.1 glass if ((ch = getc(fp)) == EOF) {
130 1.6 jtc if (ferror(fp)) {
131 1.1 glass ierr();
132 1.6 jtc return;
133 1.6 jtc }
134 1.1 glass break;
135 1.1 glass }
136 1.1 glass if (ch == '\n' && !--off)
137 1.1 glass break;
138 1.1 glass }
139 1.1 glass break;
140 1.1 glass case RBYTES:
141 1.1 glass if (S_ISREG(sbp->st_mode)) {
142 1.1 glass if (sbp->st_size >= off &&
143 1.6 jtc fseek(fp, -off, SEEK_END) == -1) {
144 1.1 glass ierr();
145 1.6 jtc return;
146 1.6 jtc }
147 1.1 glass } else if (off == 0) {
148 1.1 glass while (getc(fp) != EOF);
149 1.6 jtc if (ferror(fp)) {
150 1.1 glass ierr();
151 1.6 jtc return;
152 1.6 jtc }
153 1.16 cgd } else {
154 1.16 cgd if (bytes(fp, off))
155 1.16 cgd return;
156 1.16 cgd }
157 1.1 glass break;
158 1.1 glass case RLINES:
159 1.14 christos if (S_ISREG(sbp->st_mode)) {
160 1.1 glass if (!off) {
161 1.6 jtc if (fseek(fp, 0L, SEEK_END) == -1) {
162 1.1 glass ierr();
163 1.6 jtc return;
164 1.6 jtc }
165 1.16 cgd } else {
166 1.16 cgd if (rlines(fp, off, sbp))
167 1.16 cgd return;
168 1.16 cgd }
169 1.15 christos } else if (off == 0) {
170 1.1 glass while (getc(fp) != EOF);
171 1.6 jtc if (ferror(fp)) {
172 1.1 glass ierr();
173 1.6 jtc return;
174 1.6 jtc }
175 1.16 cgd } else {
176 1.16 cgd if (lines(fp, off))
177 1.16 cgd return;
178 1.16 cgd }
179 1.1 glass break;
180 1.8 lukem default:
181 1.8 lukem break;
182 1.1 glass }
183 1.1 glass
184 1.23 jdolecek if (fflag) {
185 1.23 jdolecek kq = kqueue();
186 1.23 jdolecek if (kq < 0)
187 1.23 jdolecek err(1, "kqueue");
188 1.23 jdolecek action = ADD_EVENTS;
189 1.23 jdolecek }
190 1.23 jdolecek
191 1.1 glass for (;;) {
192 1.9 cjs while ((ch = getc(fp)) != EOF) {
193 1.1 glass if (putchar(ch) == EOF)
194 1.1 glass oerr();
195 1.9 cjs }
196 1.6 jtc if (ferror(fp)) {
197 1.1 glass ierr();
198 1.6 jtc return;
199 1.6 jtc }
200 1.1 glass (void)fflush(stdout);
201 1.1 glass if (!fflag)
202 1.1 glass break;
203 1.23 jdolecek
204 1.1 glass clearerr(fp);
205 1.9 cjs
206 1.23 jdolecek switch (action) {
207 1.23 jdolecek case ADD_EVENTS:
208 1.23 jdolecek n = 0;
209 1.23 jdolecek
210 1.23 jdolecek memset(ev, 0, sizeof(ev));
211 1.23 jdolecek if (fflag == 2 && fileno(fp) != STDIN_FILENO) {
212 1.23 jdolecek EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
213 1.23 jdolecek EV_ADD | EV_ENABLE | EV_CLEAR,
214 1.23 jdolecek NOTE_DELETE | NOTE_RENAME, 0, 0);
215 1.23 jdolecek n++;
216 1.23 jdolecek }
217 1.23 jdolecek EV_SET(&ev[n], fileno(fp), EVFILT_READ,
218 1.23 jdolecek EV_ADD | EV_ENABLE, 0, 0, 0);
219 1.23 jdolecek n++;
220 1.23 jdolecek
221 1.23 jdolecek if (kevent(kq, ev, n, NULL, 0, NULL) < 0) {
222 1.23 jdolecek close(kq);
223 1.23 jdolecek kq = -1;
224 1.23 jdolecek action = USE_SLEEP;
225 1.23 jdolecek } else {
226 1.23 jdolecek action = USE_KQUEUE;
227 1.23 jdolecek }
228 1.23 jdolecek break;
229 1.23 jdolecek
230 1.23 jdolecek case USE_KQUEUE:
231 1.23 jdolecek if (kevent(kq, NULL, 0, ev, 1, NULL) < 0)
232 1.23 jdolecek err(1, "kevent");
233 1.23 jdolecek
234 1.23 jdolecek if (ev[0].filter == EVFILT_VNODE) {
235 1.23 jdolecek /* file was rotated, wait until it reappears */
236 1.23 jdolecek action = USE_SLEEP;
237 1.23 jdolecek } else if (ev[0].data < 0) {
238 1.23 jdolecek /* file shrank, reposition to end */
239 1.23 jdolecek if (fseek(fp, 0L, SEEK_END) == -1) {
240 1.23 jdolecek ierr();
241 1.23 jdolecek return;
242 1.9 cjs }
243 1.9 cjs }
244 1.23 jdolecek break;
245 1.23 jdolecek
246 1.23 jdolecek case USE_SLEEP:
247 1.23 jdolecek /*
248 1.23 jdolecek * We pause for one second after displaying any data
249 1.23 jdolecek * that has accumulated since we read the file.
250 1.23 jdolecek */
251 1.23 jdolecek (void) usleep(1000000);
252 1.23 jdolecek
253 1.23 jdolecek if (fflag == 2 && fileno(fp) != STDIN_FILENO &&
254 1.23 jdolecek stat(fname, &statbuf) != -1) {
255 1.23 jdolecek if (statbuf.st_ino != sbp->st_ino ||
256 1.23 jdolecek statbuf.st_dev != sbp->st_dev ||
257 1.23 jdolecek statbuf.st_rdev != sbp->st_rdev ||
258 1.23 jdolecek statbuf.st_nlink == 0) {
259 1.23 jdolecek fp = freopen(fname, "r", fp);
260 1.23 jdolecek if (fp == NULL) {
261 1.23 jdolecek ierr();
262 1.23 jdolecek break;
263 1.23 jdolecek }
264 1.23 jdolecek *sbp = statbuf;
265 1.23 jdolecek if (kq != -1)
266 1.23 jdolecek action = ADD_EVENTS;
267 1.23 jdolecek } else if (kq != -1)
268 1.23 jdolecek action = USE_KQUEUE;
269 1.23 jdolecek }
270 1.23 jdolecek break;
271 1.9 cjs }
272 1.1 glass }
273 1.23 jdolecek
274 1.23 jdolecek if (fflag && kq != -1)
275 1.23 jdolecek close(kq);
276 1.1 glass }
277 1.1 glass
278 1.1 glass /*
279 1.1 glass * rlines -- display the last offset lines of the file.
280 1.16 cgd *
281 1.16 cgd * Non-zero return means than a (non-fatal) error occurred.
282 1.1 glass */
283 1.16 cgd static int
284 1.19 wiz rlines(FILE *fp, long int off, struct stat *sbp)
285 1.1 glass {
286 1.17 explorer off_t file_size;
287 1.17 explorer off_t file_remaining;
288 1.8 lukem char *p;
289 1.5 jtc char *start;
290 1.17 explorer off_t mmap_size;
291 1.17 explorer off_t mmap_offset;
292 1.17 explorer off_t mmap_remaining;
293 1.1 glass
294 1.17 explorer #define MMAP_MAXSIZE (10 * 1024 * 1024)
295 1.17 explorer
296 1.17 explorer if (!(file_size = sbp->st_size))
297 1.16 cgd return (0);
298 1.17 explorer file_remaining = file_size;
299 1.1 glass
300 1.18 explorer if (file_remaining > MMAP_MAXSIZE) {
301 1.17 explorer mmap_size = MMAP_MAXSIZE;
302 1.18 explorer mmap_offset = file_remaining - MMAP_MAXSIZE;
303 1.17 explorer } else {
304 1.18 explorer mmap_size = file_remaining;
305 1.17 explorer mmap_offset = 0;
306 1.6 jtc }
307 1.6 jtc
308 1.17 explorer while (off) {
309 1.17 explorer start = mmap(NULL, (size_t)mmap_size, PROT_READ,
310 1.17 explorer MAP_FILE|MAP_SHARED, fileno(fp), mmap_offset);
311 1.17 explorer if (start == MAP_FAILED) {
312 1.17 explorer err(0, "%s: %s", fname, strerror(EFBIG));
313 1.17 explorer return (1);
314 1.17 explorer }
315 1.17 explorer
316 1.17 explorer mmap_remaining = mmap_size;
317 1.17 explorer /* Last char is special, ignore whether newline or not. */
318 1.17 explorer for (p = start + mmap_remaining - 1 ; --mmap_remaining ; )
319 1.17 explorer if (*--p == '\n' && !--off) {
320 1.17 explorer ++p;
321 1.17 explorer break;
322 1.17 explorer }
323 1.17 explorer
324 1.17 explorer file_remaining -= mmap_size - mmap_remaining;
325 1.1 glass
326 1.17 explorer if (off == 0)
327 1.18 explorer break;
328 1.18 explorer
329 1.18 explorer if (file_remaining == 0)
330 1.1 glass break;
331 1.17 explorer
332 1.17 explorer if (munmap(start, mmap_size)) {
333 1.17 explorer err(0, "%s: %s", fname, strerror(errno));
334 1.17 explorer return (1);
335 1.1 glass }
336 1.1 glass
337 1.17 explorer if (mmap_offset >= MMAP_MAXSIZE) {
338 1.17 explorer mmap_offset -= MMAP_MAXSIZE;
339 1.17 explorer } else {
340 1.17 explorer mmap_offset = 0;
341 1.17 explorer mmap_size = file_remaining;
342 1.17 explorer }
343 1.17 explorer }
344 1.17 explorer
345 1.17 explorer /*
346 1.17 explorer * Output the (perhaps partial) data in this mmap'd block.
347 1.17 explorer */
348 1.17 explorer WR(p, mmap_size - mmap_remaining);
349 1.17 explorer file_remaining += mmap_size - mmap_remaining;
350 1.17 explorer if (munmap(start, mmap_size)) {
351 1.17 explorer err(0, "%s: %s", fname, strerror(errno));
352 1.16 cgd return (1);
353 1.6 jtc }
354 1.17 explorer
355 1.17 explorer /*
356 1.17 explorer * Set the file pointer to reflect the length displayed.
357 1.17 explorer * This will cause the caller to redisplay the data if/when
358 1.17 explorer * needed.
359 1.17 explorer */
360 1.17 explorer if (fseeko(fp, file_remaining, SEEK_SET) == -1) {
361 1.17 explorer ierr();
362 1.16 cgd return (1);
363 1.5 jtc }
364 1.16 cgd return (0);
365 1.1 glass }
366