Initial import from FreeBSD RELENG_4:
[games.git] / usr.bin / tail / forward.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Sze-Tyan Wang.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)forward.c   8.1 (Berkeley) 6/6/93";
40 #endif
41 static const char rcsid[] =
42   "$FreeBSD: src/usr.bin/tail/forward.c,v 1.11.6.7 2003/01/07 05:26:22 tjr Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <sys/time.h>
48 #include <sys/mman.h>
49 #include <sys/event.h>
50
51 #include <limits.h>
52 #include <fcntl.h>
53 #include <errno.h>
54 #include <unistd.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <err.h>
59 #include "extern.h"
60
61 static void rlines __P((FILE *, off_t, struct stat *));
62
63 /* defines for inner loop actions */
64 #define USE_SLEEP       0
65 #define USE_KQUEUE      1
66 #define ADD_EVENTS      2
67
68 /*
69  * forward -- display the file, from an offset, forward.
70  *
71  * There are eight separate cases for this -- regular and non-regular
72  * files, by bytes or lines and from the beginning or end of the file.
73  *
74  * FBYTES       byte offset from the beginning of the file
75  *      REG     seek
76  *      NOREG   read, counting bytes
77  *
78  * FLINES       line offset from the beginning of the file
79  *      REG     read, counting lines
80  *      NOREG   read, counting lines
81  *
82  * RBYTES       byte offset from the end of the file
83  *      REG     seek
84  *      NOREG   cyclically read characters into a wrap-around buffer
85  *
86  * RLINES
87  *      REG     mmap the file and step back until reach the correct offset.
88  *      NOREG   cyclically read lines into a wrap-around array of buffers
89  */
90 void
91 forward(fp, style, off, sbp)
92         FILE *fp;
93         enum STYLE style;
94         off_t off;
95         struct stat *sbp;
96 {
97         int ch, n, kq = -1;
98         int action = USE_SLEEP;
99         struct kevent ev[2];
100         struct stat sb2;
101         struct timespec ts;
102
103         switch(style) {
104         case FBYTES:
105                 if (off == 0)
106                         break;
107                 if (S_ISREG(sbp->st_mode)) {
108                         if (sbp->st_size < off)
109                                 off = sbp->st_size;
110                         if (fseeko(fp, off, SEEK_SET) == -1) {
111                                 ierr();
112                                 return;
113                         }
114                 } else while (off--)
115                         if ((ch = getc(fp)) == EOF) {
116                                 if (ferror(fp)) {
117                                         ierr();
118                                         return;
119                                 }
120                                 break;
121                         }
122                 break;
123         case FLINES:
124                 if (off == 0)
125                         break;
126                 for (;;) {
127                         if ((ch = getc(fp)) == EOF) {
128                                 if (ferror(fp)) {
129                                         ierr();
130                                         return;
131                                 }
132                                 break;
133                         }
134                         if (ch == '\n' && !--off)
135                                 break;
136                 }
137                 break;
138         case RBYTES:
139                 if (S_ISREG(sbp->st_mode)) {
140                         if (sbp->st_size >= off &&
141                             fseeko(fp, -off, SEEK_END) == -1) {
142                                 ierr();
143                                 return;
144                         }
145                 } else if (off == 0) {
146                         while (getc(fp) != EOF);
147                         if (ferror(fp)) {
148                                 ierr();
149                                 return;
150                         }
151                 } else
152                         if (bytes(fp, off))
153                                 return;
154                 break;
155         case RLINES:
156                 if (S_ISREG(sbp->st_mode))
157                         if (!off) {
158                                 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
159                                         ierr();
160                                         return;
161                                 }
162                         } else
163                                 rlines(fp, off, sbp);
164                 else if (off == 0) {
165                         while (getc(fp) != EOF);
166                         if (ferror(fp)) {
167                                 ierr();
168                                 return;
169                         }
170                 } else
171                         if (lines(fp, off))
172                                 return;
173                 break;
174         }
175
176         if (fflag) {
177                 kq = kqueue();
178                 if (kq < 0)
179                         err(1, "kqueue");
180                 action = ADD_EVENTS;
181         }
182
183         for (;;) {
184                 while ((ch = getc(fp)) != EOF)
185                         if (putchar(ch) == EOF)
186                                 oerr();
187                 if (ferror(fp)) {
188                         ierr();
189                         return;
190                 }
191                 (void)fflush(stdout);
192                 if (! fflag)
193                         break;
194                 clearerr(fp);
195
196                 switch (action) {
197                 case ADD_EVENTS:
198                         n = 0;
199                         ts.tv_sec = 0;
200                         ts.tv_nsec = 0;
201
202                         if (Fflag && fileno(fp) != STDIN_FILENO) {
203                                 EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
204                                     EV_ADD | EV_ENABLE | EV_CLEAR,
205                                     NOTE_DELETE | NOTE_RENAME, 0, 0);
206                                 n++;
207                         }
208                         EV_SET(&ev[n], fileno(fp), EVFILT_READ,
209                             EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
210                         n++;
211
212                         if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
213                                 action = USE_SLEEP;
214                         } else {
215                                 action = USE_KQUEUE;
216                         }
217                         break;
218
219                 case USE_KQUEUE:
220                         ts.tv_sec = 1;
221                         ts.tv_nsec = 0;
222                         /*
223                          * In the -F case we set a timeout to ensure that
224                          * we re-stat the file at least once every second.
225                          */
226                         n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
227                         if (n < 0)
228                                 err(1, "kevent");
229                         if (n == 0) {
230                                 /* timeout */
231                                 break;
232                         } else if (ev->filter == EVFILT_READ && ev->data < 0) {
233                                  /* file shrank, reposition to end */
234                                 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
235                                         ierr();
236                                         return;
237                                 }
238                         }
239                         break;
240
241                 case USE_SLEEP:
242                         (void) usleep(250000);
243                         clearerr(fp);
244                         break;
245                 }
246
247                 if (Fflag && fileno(fp) != STDIN_FILENO) {
248                         while (stat(fname, &sb2) != 0)
249                                 /* file was rotated, wait until it reappears */
250                                 (void)sleep(1);
251                         if (sb2.st_ino != sbp->st_ino ||
252                             sb2.st_dev != sbp->st_dev ||
253                             sb2.st_rdev != sbp->st_rdev ||
254                             sb2.st_nlink == 0) {
255                                 fp = freopen(fname, "r", fp);
256                                 if (fp == NULL) {
257                                         ierr();
258                                         return;
259                                 } else {
260                                         *sbp = sb2;
261                                         action = ADD_EVENTS;
262                                 }
263                         }
264                 }
265         }
266 }
267
268 /*
269  * rlines -- display the last offset lines of the file.
270  */
271 static void
272 rlines(fp, off, sbp)
273         FILE *fp;
274         off_t off;
275         struct stat *sbp;
276 {
277         struct mapinfo map;
278         off_t curoff, size;
279         int i;
280
281         if (!(size = sbp->st_size))
282                 return;
283         map.start = NULL;
284         map.fd = fileno(fp);
285         map.mapoff = map.maxoff = size;
286
287         /*
288          * Last char is special, ignore whether newline or not. Note that
289          * size == 0 is dealt with above, and size == 1 sets curoff to -1.
290          */
291         curoff = size - 2;
292         while (curoff >= 0) {
293                 if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
294                         ierr();
295                         return;
296                 }
297                 for (i = curoff - map.mapoff; i >= 0; i--)
298                         if (map.start[i] == '\n' && --off == 0)
299                                 break;
300                 /* `i' is either the map offset of a '\n', or -1. */
301                 curoff = map.mapoff + i;
302                 if (i >= 0)
303                         break;
304         }
305         curoff++;
306         if (mapprint(&map, curoff, size - curoff) != 0) {
307                 ierr();
308                 exit(1);
309         }
310
311         /* Set the file pointer to reflect the length displayed. */
312         if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
313                 ierr();
314                 return;
315         }
316         if (map.start != NULL && munmap(map.start, map.maplen)) {
317                 ierr();
318                 return;
319         }
320 }