2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Sze-Tyan Wang.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)forward.c 8.1 (Berkeley) 6/6/93
33 * $FreeBSD: src/usr.bin/tail/forward.c,v 1.11.6.7 2003/01/07 05:26:22 tjr Exp $
36 #include <sys/types.h>
40 #include <sys/event.h>
52 static void rlines(FILE *, off_t
, struct stat
*);
54 /* defines for inner loop actions */
60 int action
= USE_SLEEP
;
64 * forward -- display the file, from an offset, forward.
66 * There are eight separate cases for this -- regular and non-regular
67 * files, by bytes or lines and from the beginning or end of the file.
69 * FBYTES byte offset from the beginning of the file
71 * NOREG read, counting bytes
73 * FLINES line offset from the beginning of the file
74 * REG read, counting lines
75 * NOREG read, counting lines
77 * RBYTES byte offset from the end of the file
79 * NOREG cyclically read characters into a wrap-around buffer
82 * REG mmap the file and step back until reach the correct offset.
83 * NOREG cyclically read lines into a wrap-around array of buffers
86 forward(FILE *fp
, enum STYLE style
, off_t off
, struct stat
*sbp
)
94 if (S_ISREG(sbp
->st_mode
)) {
95 if (sbp
->st_size
< off
)
97 if (fseeko(fp
, off
, SEEK_SET
) == -1) {
102 if ((ch
= getc(fp
)) == EOF
) {
114 if ((ch
= getc(fp
)) == EOF
) {
121 if (ch
== '\n' && !--off
)
126 if (S_ISREG(sbp
->st_mode
)) {
127 if (sbp
->st_size
>= off
&&
128 fseeko(fp
, -off
, SEEK_END
) == -1) {
132 } else if (off
== 0) {
133 while (getc(fp
) != EOF
);
139 if (display_bytes(fp
, off
))
143 if (S_ISREG(sbp
->st_mode
))
145 if (fseeko(fp
, (off_t
)0, SEEK_END
) == -1) {
150 rlines(fp
, off
, sbp
);
152 while (getc(fp
) != EOF
);
158 if (display_lines(fp
, off
))
162 errx(1, "internal error: forward style cannot be REVERSE");
166 while ((ch
= getc(fp
)) != EOF
) {
167 if (putchar(ch
) == EOF
)
178 * rlines -- display the last offset lines of the file.
181 rlines(FILE *fp
, off_t off
, struct stat
*sbp
)
187 if (!(size
= sbp
->st_size
))
191 map
.mapoff
= map
.maxoff
= size
;
194 * Last char is special, ignore whether newline or not. Note that
195 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
198 while (curoff
>= 0) {
199 if (curoff
< map
.mapoff
&& maparound(&map
, curoff
) != 0) {
203 for (i
= curoff
- map
.mapoff
; i
>= 0; i
--)
204 if (map
.start
[i
] == '\n' && --off
== 0)
206 /* `i' is either the map offset of a '\n', or -1. */
207 curoff
= map
.mapoff
+ i
;
212 if (mapprint(&map
, curoff
, size
- curoff
) != 0) {
217 /* Set the file pointer to reflect the length displayed. */
218 if (fseeko(fp
, sbp
->st_size
, SEEK_SET
) == -1) {
222 if (map
.start
!= NULL
&& munmap(map
.start
, map
.maplen
)) {
229 * follow -- display the file, from an offset, forward.
233 show(file_info_t
*file
, int at_index
)
238 while ((ch
= getc(file
->fp
)) != EOF
) {
239 if (first
&& no_files
> 1) {
240 showfilename(at_index
, file
->file_name
);
243 if (putchar(ch
) == EOF
)
247 if (ferror(file
->fp
)) {
256 showfilename(int at_index
, const char *filename
)
258 static int last_index
= -1;
259 static int continuing
= 0;
261 if (last_index
== at_index
)
266 printf("==> %s <==\n", filename
);
269 last_index
= at_index
;
273 set_events(file_info_t
*files
)
284 for (i
= 0, file
= files
; i
< no_files
; i
++, file
++) {
285 if (file
->fp
== NULL
)
287 if (Fflag
&& fileno(file
->fp
) != STDIN_FILENO
) {
288 EV_SET(&ev
[n
], fileno(file
->fp
), EVFILT_VNODE
,
289 EV_ADD
| EV_ENABLE
| EV_CLEAR
,
290 NOTE_DELETE
| NOTE_RENAME
, 0, 0);
293 EV_SET(&ev
[n
], fileno(file
->fp
), EVFILT_READ
,
294 EV_ADD
| EV_ENABLE
| EV_CLEAR
, 0, 0, 0);
298 if (kevent(kq
, ev
, n
, NULL
, 0, &ts
) < 0)
303 follow(file_info_t
*files
, enum STYLE style
, off_t off
)
310 /* Position each of the files */
314 for (i
= 0; i
< no_files
; i
++, file
++) {
319 showfilename(i
, file
->file_name
);
320 forward(file
->fp
, style
, off
, &file
->st
);
321 if (Fflag
&& fileno(file
->fp
) != STDIN_FILENO
)
332 ev
= malloc(n
* sizeof(struct kevent
));
334 err(1, "Couldn't allocate memory for kevents.");
338 for (i
= 0, file
= files
; i
< no_files
; i
++, file
++) {
339 if (file
->fp
== NULL
)
341 if (Fflag
&& fileno(file
->fp
) != STDIN_FILENO
) {
342 if (stat(file
->file_name
, &sb2
) == -1) {
344 * file was rotated, skip it until it
349 if (sb2
.st_ino
!= file
->st
.st_ino
||
350 sb2
.st_dev
!= file
->st
.st_dev
||
352 file
->fp
= freopen(file
->file_name
, "r",
354 if (file
->fp
== NULL
) {
358 memcpy(&file
->st
, &sb2
,
359 sizeof(struct stat
));
372 * In the -F case, we set a timeout to ensure that
373 * we re-stat the file at least once every second.
375 n
= kevent(kq
, NULL
, 0, ev
, 1, Fflag
? &ts
: NULL
);
381 } else if (ev
->filter
== EVFILT_READ
&& ev
->data
< 0) {
382 /* file shrank, reposition to end */
383 if (lseek(ev
->ident
, 0, SEEK_END
) == -1) {