1 /* $NetBSD: forward.c,v 1.28 2006/05/24 16:34:25 christos Exp $ */
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
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
35 #include <sys/cdefs.h>
38 static char sccsid
[] = "@(#)forward.c 8.1 (Berkeley) 6/6/93";
40 __RCSID("$NetBSD: forward.c,v 1.28 2006/05/24 16:34:25 christos Exp $");
43 #include <sys/types.h>
47 #include <sys/event.h>
58 static int rlines(FILE *, off_t
, struct stat
*);
60 /* defines for inner loop actions */
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
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
81 * NOREG cyclically read characters into a wrap-around buffer
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
88 forward(FILE *fp
, enum STYLE style
, off_t off
, struct stat
*sbp
)
91 int kq
=-1, action
=USE_SLEEP
;
97 /* Keep track of file's previous incarnation. */
98 lastdev
= sbp
->st_dev
;
99 lastino
= sbp
->st_ino
;
105 if (S_ISREG(sbp
->st_mode
)) {
106 if (sbp
->st_size
< off
)
108 if (fseeko(fp
, off
, SEEK_SET
) == -1) {
113 if ((ch
= getc(fp
)) == EOF
) {
125 if ((ch
= getc(fp
)) == EOF
) {
132 if (ch
== '\n' && !--off
)
137 if (S_ISREG(sbp
->st_mode
)) {
138 if (sbp
->st_size
>= off
&&
139 fseeko(fp
, -off
, SEEK_END
) == -1) {
143 } else if (off
== 0) {
144 while (getc(fp
) != EOF
);
150 if (displaybytes(fp
, off
))
155 if (S_ISREG(sbp
->st_mode
)) {
157 if (fseek(fp
, 0L, SEEK_END
) == -1) {
162 if (rlines(fp
, off
, sbp
))
165 } else if (off
== 0) {
166 while (getc(fp
) != EOF
);
172 if (displaylines(fp
, off
))
188 while ((ch
= getc(fp
)) != EOF
) {
189 if (putchar(ch
) == EOF
)
196 (void)fflush(stdout
);
206 memset(ev
, 0, sizeof(ev
));
207 if (fflag
== 2 && fileno(fp
) != STDIN_FILENO
) {
208 EV_SET(&ev
[n
], fileno(fp
), EVFILT_VNODE
,
209 EV_ADD
| EV_ENABLE
| EV_CLEAR
,
210 NOTE_DELETE
| NOTE_RENAME
, 0, 0);
213 EV_SET(&ev
[n
], fileno(fp
), EVFILT_READ
,
214 EV_ADD
| EV_ENABLE
, 0, 0, 0);
217 if (kevent(kq
, ev
, n
, NULL
, 0, NULL
) < 0) {
227 if (kevent(kq
, NULL
, 0, ev
, 1, NULL
) < 0)
230 if (ev
[0].filter
== EVFILT_VNODE
) {
231 /* file was rotated, wait until it reappears */
233 } else if (ev
[0].data
< 0) {
234 /* file shrank, reposition to end */
235 if (fseek(fp
, 0L, SEEK_END
) == -1) {
244 * We pause for one second after displaying any data
245 * that has accumulated since we read the file.
249 if (fflag
== 2 && fileno(fp
) != STDIN_FILENO
&&
250 stat(fname
, &statbuf
) != -1) {
251 if (statbuf
.st_ino
!= sbp
->st_ino
||
252 statbuf
.st_dev
!= sbp
->st_dev
||
253 statbuf
.st_rdev
!= sbp
->st_rdev
||
254 statbuf
.st_nlink
== 0) {
255 fp
= freopen(fname
, "r", fp
);
270 if (fflag
&& kq
!= -1)
275 * rlines -- display the last offset lines of the file.
277 * Non-zero return means than a (non-fatal) error occurred.
280 rlines(FILE *fp
, off_t off
, struct stat
*sbp
)
283 off_t file_remaining
;
288 off_t mmap_remaining
= 0;
290 #define MMAP_MAXSIZE (10 * 1024 * 1024)
292 if (!(file_size
= sbp
->st_size
))
294 file_remaining
= file_size
;
296 if (file_remaining
> MMAP_MAXSIZE
) {
297 mmap_size
= MMAP_MAXSIZE
;
298 mmap_offset
= file_remaining
- MMAP_MAXSIZE
;
300 mmap_size
= file_remaining
;
305 start
= mmap(NULL
, (size_t)mmap_size
, PROT_READ
,
306 MAP_FILE
|MAP_SHARED
, fileno(fp
), mmap_offset
);
307 if (start
== MAP_FAILED
) {
308 err(0, "%s: %s", fname
, strerror(EFBIG
));
312 mmap_remaining
= mmap_size
;
313 /* Last char is special, ignore whether newline or not. */
314 for (p
= start
+ mmap_remaining
- 1 ; --mmap_remaining
; )
315 if (*--p
== '\n' && !--off
) {
320 file_remaining
-= mmap_size
- mmap_remaining
;
325 if (file_remaining
== 0)
328 if (munmap(start
, mmap_size
)) {
329 err(0, "%s: %s", fname
, strerror(errno
));
333 if (mmap_offset
>= MMAP_MAXSIZE
) {
334 mmap_offset
-= MMAP_MAXSIZE
;
337 mmap_size
= file_remaining
;
342 * Output the (perhaps partial) data in this mmap'd block.
344 WR(p
, mmap_size
- mmap_remaining
);
345 file_remaining
+= mmap_size
- mmap_remaining
;
346 if (munmap(start
, mmap_size
)) {
347 err(0, "%s: %s", fname
, strerror(errno
));
352 * Set the file pointer to reflect the length displayed.
353 * This will cause the caller to redisplay the data if/when
356 if (fseeko(fp
, file_remaining
, SEEK_SET
) == -1) {