Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.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  * @(#)forward.c        8.1 (Berkeley) 6/6/93
37  * $FreeBSD: src/usr.bin/tail/forward.c,v 1.11.6.7 2003/01/07 05:26:22 tjr Exp $
38  * $DragonFly: src/usr.bin/tail/forward.c,v 1.2 2003/06/17 04:29:32 dillon Exp $
39  */
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/mman.h>
45 #include <sys/event.h>
46
47 #include <limits.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <err.h>
55 #include "extern.h"
56
57 static void rlines __P((FILE *, off_t, struct stat *));
58
59 /* defines for inner loop actions */
60 #define USE_SLEEP       0
61 #define USE_KQUEUE      1
62 #define ADD_EVENTS      2
63
64 /*
65  * forward -- display the file, from an offset, forward.
66  *
67  * There are eight separate cases for this -- regular and non-regular
68  * files, by bytes or lines and from the beginning or end of the file.
69  *
70  * FBYTES       byte offset from the beginning of the file
71  *      REG     seek
72  *      NOREG   read, counting bytes
73  *
74  * FLINES       line offset from the beginning of the file
75  *      REG     read, counting lines
76  *      NOREG   read, counting lines
77  *
78  * RBYTES       byte offset from the end of the file
79  *      REG     seek
80  *      NOREG   cyclically read characters into a wrap-around buffer
81  *
82  * RLINES
83  *      REG     mmap the file and step back until reach the correct offset.
84  *      NOREG   cyclically read lines into a wrap-around array of buffers
85  */
86 void
87 forward(fp, style, off, sbp)
88         FILE *fp;
89         enum STYLE style;
90         off_t off;
91         struct stat *sbp;
92 {
93         int ch, n, kq = -1;
94         int action = USE_SLEEP;
95         struct kevent ev[2];
96         struct stat sb2;
97         struct timespec ts;
98
99         switch(style) {
100         case FBYTES:
101                 if (off == 0)
102                         break;
103                 if (S_ISREG(sbp->st_mode)) {
104                         if (sbp->st_size < off)
105                                 off = sbp->st_size;
106                         if (fseeko(fp, off, SEEK_SET) == -1) {
107                                 ierr();
108                                 return;
109                         }
110                 } else while (off--)
111                         if ((ch = getc(fp)) == EOF) {
112                                 if (ferror(fp)) {
113                                         ierr();
114                                         return;
115                                 }
116                                 break;
117                         }
118                 break;
119         case FLINES:
120                 if (off == 0)
121                         break;
122                 for (;;) {
123                         if ((ch = getc(fp)) == EOF) {
124                                 if (ferror(fp)) {
125                                         ierr();
126                                         return;
127                                 }
128                                 break;
129                         }
130                         if (ch == '\n' && !--off)
131                                 break;
132                 }
133                 break;
134         case RBYTES:
135                 if (S_ISREG(sbp->st_mode)) {
136                         if (sbp->st_size >= off &&
137                             fseeko(fp, -off, SEEK_END) == -1) {
138                                 ierr();
139                                 return;
140                         }
141                 } else if (off == 0) {
142                         while (getc(fp) != EOF);
143                         if (ferror(fp)) {
144                                 ierr();
145                                 return;
146                         }
147                 } else
148                         if (bytes(fp, off))
149                                 return;
150                 break;
151         case RLINES:
152                 if (S_ISREG(sbp->st_mode))
153                         if (!off) {
154                                 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
155                                         ierr();
156                                         return;
157                                 }
158                         } else
159                                 rlines(fp, off, sbp);
160                 else if (off == 0) {
161                         while (getc(fp) != EOF);
162                         if (ferror(fp)) {
163                                 ierr();
164                                 return;
165                         }
166                 } else
167                         if (lines(fp, off))
168                                 return;
169                 break;
170         }
171
172         if (fflag) {
173                 kq = kqueue();
174                 if (kq < 0)
175                         err(1, "kqueue");
176                 action = ADD_EVENTS;
177         }
178
179         for (;;) {
180                 while ((ch = getc(fp)) != EOF)
181                         if (putchar(ch) == EOF)
182                                 oerr();
183                 if (ferror(fp)) {
184                         ierr();
185                         return;
186                 }
187                 (void)fflush(stdout);
188                 if (! fflag)
189                         break;
190                 clearerr(fp);
191
192                 switch (action) {
193                 case ADD_EVENTS:
194                         n = 0;
195                         ts.tv_sec = 0;
196                         ts.tv_nsec = 0;
197
198                         if (Fflag && fileno(fp) != STDIN_FILENO) {
199                                 EV_SET(&ev[n], fileno(fp), EVFILT_VNODE,
200                                     EV_ADD | EV_ENABLE | EV_CLEAR,
201                                     NOTE_DELETE | NOTE_RENAME, 0, 0);
202                                 n++;
203                         }
204                         EV_SET(&ev[n], fileno(fp), EVFILT_READ,
205                             EV_ADD | EV_ENABLE | EV_CLEAR, 0, 0, 0);
206                         n++;
207
208                         if (kevent(kq, ev, n, NULL, 0, &ts) < 0) {
209                                 action = USE_SLEEP;
210                         } else {
211                                 action = USE_KQUEUE;
212                         }
213                         break;
214
215                 case USE_KQUEUE:
216                         ts.tv_sec = 1;
217                         ts.tv_nsec = 0;
218                         /*
219                          * In the -F case we set a timeout to ensure that
220                          * we re-stat the file at least once every second.
221                          */
222                         n = kevent(kq, NULL, 0, ev, 1, Fflag ? &ts : NULL);
223                         if (n < 0)
224                                 err(1, "kevent");
225                         if (n == 0) {
226                                 /* timeout */
227                                 break;
228                         } else if (ev->filter == EVFILT_READ && ev->data < 0) {
229                                  /* file shrank, reposition to end */
230                                 if (fseeko(fp, (off_t)0, SEEK_END) == -1) {
231                                         ierr();
232                                         return;
233                                 }
234                         }
235                         break;
236
237                 case USE_SLEEP:
238                         (void) usleep(250000);
239                         clearerr(fp);
240                         break;
241                 }
242
243                 if (Fflag && fileno(fp) != STDIN_FILENO) {
244                         while (stat(fname, &sb2) != 0)
245                                 /* file was rotated, wait until it reappears */
246                                 (void)sleep(1);
247                         if (sb2.st_ino != sbp->st_ino ||
248                             sb2.st_dev != sbp->st_dev ||
249                             sb2.st_rdev != sbp->st_rdev ||
250                             sb2.st_nlink == 0) {
251                                 fp = freopen(fname, "r", fp);
252                                 if (fp == NULL) {
253                                         ierr();
254                                         return;
255                                 } else {
256                                         *sbp = sb2;
257                                         action = ADD_EVENTS;
258                                 }
259                         }
260                 }
261         }
262 }
263
264 /*
265  * rlines -- display the last offset lines of the file.
266  */
267 static void
268 rlines(fp, off, sbp)
269         FILE *fp;
270         off_t off;
271         struct stat *sbp;
272 {
273         struct mapinfo map;
274         off_t curoff, size;
275         int i;
276
277         if (!(size = sbp->st_size))
278                 return;
279         map.start = NULL;
280         map.fd = fileno(fp);
281         map.mapoff = map.maxoff = size;
282
283         /*
284          * Last char is special, ignore whether newline or not. Note that
285          * size == 0 is dealt with above, and size == 1 sets curoff to -1.
286          */
287         curoff = size - 2;
288         while (curoff >= 0) {
289                 if (curoff < map.mapoff && maparound(&map, curoff) != 0) {
290                         ierr();
291                         return;
292                 }
293                 for (i = curoff - map.mapoff; i >= 0; i--)
294                         if (map.start[i] == '\n' && --off == 0)
295                                 break;
296                 /* `i' is either the map offset of a '\n', or -1. */
297                 curoff = map.mapoff + i;
298                 if (i >= 0)
299                         break;
300         }
301         curoff++;
302         if (mapprint(&map, curoff, size - curoff) != 0) {
303                 ierr();
304                 exit(1);
305         }
306
307         /* Set the file pointer to reflect the length displayed. */
308         if (fseeko(fp, sbp->st_size, SEEK_SET) == -1) {
309                 ierr();
310                 return;
311         }
312         if (map.start != NULL && munmap(map.start, map.maplen)) {
313                 ierr();
314                 return;
315         }
316 }