xref: /freebsd/usr.bin/tail/forward.c (revision 3e0f6b97b257a96f7275e4442204263e44b16686)
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 static char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
39 #endif /* not lint */
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 #include <sys/time.h>
44 #include <sys/mman.h>
45 
46 #include <limits.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <err.h>
54 #include "extern.h"
55 
56 static void rlines __P((FILE *, long, struct stat *));
57 
58 /*
59  * forward -- display the file, from an offset, forward.
60  *
61  * There are eight separate cases for this -- regular and non-regular
62  * files, by bytes or lines and from the beginning or end of the file.
63  *
64  * FBYTES	byte offset from the beginning of the file
65  *	REG	seek
66  *	NOREG	read, counting bytes
67  *
68  * FLINES	line offset from the beginning of the file
69  *	REG	read, counting lines
70  *	NOREG	read, counting lines
71  *
72  * RBYTES	byte offset from the end of the file
73  *	REG	seek
74  *	NOREG	cyclically read characters into a wrap-around buffer
75  *
76  * RLINES
77  *	REG	mmap the file and step back until reach the correct offset.
78  *	NOREG	cyclically read lines into a wrap-around array of buffers
79  */
80 void
81 forward(fp, style, off, sbp)
82 	FILE *fp;
83 	enum STYLE style;
84 	long off;
85 	struct stat *sbp;
86 {
87 	register int ch;
88 	struct timeval second;
89 
90 	switch(style) {
91 	case FBYTES:
92 		if (off == 0)
93 			break;
94 		if (S_ISREG(sbp->st_mode)) {
95 			if (sbp->st_size < off)
96 				off = sbp->st_size;
97 			if (fseek(fp, off, SEEK_SET) == -1) {
98 				ierr();
99 				return;
100 			}
101 		} else while (off--)
102 			if ((ch = getc(fp)) == EOF) {
103 				if (ferror(fp)) {
104 					ierr();
105 					return;
106 				}
107 				break;
108 			}
109 		break;
110 	case FLINES:
111 		if (off == 0)
112 			break;
113 		for (;;) {
114 			if ((ch = getc(fp)) == EOF) {
115 				if (ferror(fp)) {
116 					ierr();
117 					return;
118 				}
119 				break;
120 			}
121 			if (ch == '\n' && !--off)
122 				break;
123 		}
124 		break;
125 	case RBYTES:
126 		if (S_ISREG(sbp->st_mode)) {
127 			if (sbp->st_size >= off &&
128 			    fseek(fp, -off, SEEK_END) == -1) {
129 				ierr();
130 				return;
131 			}
132 		} else if (off == 0) {
133 			while (getc(fp) != EOF);
134 			if (ferror(fp)) {
135 				ierr();
136 				return;
137 			}
138 		} else
139 			if (bytes(fp, off))
140 				return;
141 		break;
142 	case RLINES:
143 		if (S_ISREG(sbp->st_mode))
144 			if (!off) {
145 				if (fseek(fp, 0L, SEEK_END) == -1) {
146 					ierr();
147 					return;
148 				}
149 			} else
150 				rlines(fp, off, sbp);
151 		else if (off == 0) {
152 			while (getc(fp) != EOF);
153 			if (ferror(fp)) {
154 				ierr();
155 				return;
156 			}
157 		} else
158 			if (lines(fp, off))
159 				return;
160 		break;
161 	}
162 
163 	/*
164 	 * We pause for one second after displaying any data that has
165 	 * accumulated since we read the file.
166 	 */
167 
168 	for (;;) {
169 		while ((ch = getc(fp)) != EOF)
170 			if (putchar(ch) == EOF)
171 				oerr();
172 		if (ferror(fp)) {
173 			ierr();
174 			return;
175 		}
176 		(void)fflush(stdout);
177 		if (!fflag)
178 			break;
179 
180 		/* Sleep(3) is eight system calls.  Do it fast. */
181 		second.tv_sec = 1;
182 		second.tv_usec = 0;
183 		if (select(0, NULL, NULL, NULL, &second) == -1)
184 			if (errno != EINTR)
185 				err(1, "select");
186 		clearerr(fp);
187 	}
188 }
189 
190 /*
191  * rlines -- display the last offset lines of the file.
192  */
193 static void
194 rlines(fp, off, sbp)
195 	FILE *fp;
196 	long off;
197 	struct stat *sbp;
198 {
199 	register off_t size;
200 	register char *p;
201 	char *start;
202 
203 	if (!(size = sbp->st_size))
204 		return;
205 
206 	if (size > SIZE_T_MAX) {
207 		errno = EFBIG;
208 		ierr();
209 		return;
210 	}
211 
212 	if ((start = mmap(NULL, (size_t)size,
213 	    PROT_READ, MAP_SHARED, fileno(fp), (off_t)0)) == MAP_FAILED) {
214 		ierr();
215 		return;
216 	}
217 
218 	/* Last char is special, ignore whether newline or not. */
219 	for (p = start + size - 1; --size;)
220 		if (*--p == '\n' && !--off) {
221 			++p;
222 			break;
223 		}
224 
225 	/* Set the file pointer to reflect the length displayed. */
226 	size = sbp->st_size - size;
227 	WR(p, size);
228 	if (fseek(fp, (long)sbp->st_size, SEEK_SET) == -1) {
229 		ierr();
230 		return;
231 	}
232 	if (munmap(start, (size_t)sbp->st_size)) {
233 		ierr();
234 		return;
235 	}
236 }
237