1 /* $NetBSD: forward.c,v 1.33 2015/10/09 17:51:26 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.33 2015/10/09 17:51:26 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
)
94 #endif /* !defined(__minix) */
95 int kq
=-1, action
=USE_SLEEP
;
99 #endif /* !defined(__minix) */
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
))
181 #if !defined(__minix)
188 #endif /* !defined(__minix) */
192 while ((ch
= getc(fp
)) != EOF
) {
193 if (putchar(ch
) == EOF
)
200 (void)fflush(stdout
);
207 #if !defined(__minix)
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);
218 EV_SET(&ev
[n
], fileno(fp
), EVFILT_READ
,
219 EV_ADD
| EV_ENABLE
, 0, 0, 0);
222 if (kevent(kq
, ev
, n
, NULL
, 0, NULL
) == -1) {
232 if (kevent(kq
, NULL
, 0, ev
, 1, NULL
) == -1)
235 if (ev
[0].filter
== EVFILT_VNODE
) {
236 /* file was rotated, wait until it reappears */
238 } else if (ev
[0].data
< 0) {
239 /* file shrank, reposition to end */
240 if (fseek(fp
, 0L, SEEK_END
) == -1) {
246 #endif /* !defined(__minix) */
250 * We pause for one second after displaying any data
251 * that has accumulated since we read the file.
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
);
276 if (fflag
&& kq
!= -1)
281 * rlines -- display the last offset lines of the file.
283 * Non-zero return means than a (non-fatal) error occurred.
286 rlines(FILE *fp
, off_t off
, struct stat
*sbp
)
289 off_t file_remaining
;
294 off_t mmap_remaining
= 0;
296 #define MMAP_MAXSIZE (10 * 1024 * 1024)
298 if (!(file_size
= sbp
->st_size
))
300 file_remaining
= file_size
;
302 if (file_remaining
> MMAP_MAXSIZE
) {
303 mmap_size
= MMAP_MAXSIZE
;
304 mmap_offset
= file_remaining
- MMAP_MAXSIZE
;
306 mmap_size
= file_remaining
;
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
);
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
) {
326 file_remaining
-= mmap_size
- mmap_remaining
;
331 if (file_remaining
== 0)
334 if (munmap(start
, mmap_size
)) {
335 xerr(0, "%s", fname
);
339 if (mmap_offset
>= MMAP_MAXSIZE
) {
340 mmap_offset
-= MMAP_MAXSIZE
;
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
);
358 * Set the file pointer to reflect the length displayed.
359 * This will cause the caller to redisplay the data if/when
362 if (fseeko(fp
, file_remaining
, SEEK_SET
) == -1) {