xref: /freebsd/usr.bin/tail/forward.c (revision afe61c15161c324a7af299a9b8457aba5afc92db)
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 "extern.h"
54 
55 static void rlines __P((FILE *, long, struct stat *));
56 
57 /*
58  * forward -- display the file, from an offset, forward.
59  *
60  * There are eight separate cases for this -- regular and non-regular
61  * files, by bytes or lines and from the beginning or end of the file.
62  *
63  * FBYTES	byte offset from the beginning of the file
64  *	REG	seek
65  *	NOREG	read, counting bytes
66  *
67  * FLINES	line offset from the beginning of the file
68  *	REG	read, counting lines
69  *	NOREG	read, counting lines
70  *
71  * RBYTES	byte offset from the end of the file
72  *	REG	seek
73  *	NOREG	cyclically read characters into a wrap-around buffer
74  *
75  * RLINES
76  *	REG	mmap the file and step back until reach the correct offset.
77  *	NOREG	cyclically read lines into a wrap-around array of buffers
78  */
79 void
80 forward(fp, style, off, sbp)
81 	FILE *fp;
82 	enum STYLE style;
83 	long off;
84 	struct stat *sbp;
85 {
86 	register int ch;
87 	struct timeval second;
88 	fd_set zero;
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 			bytes(fp, off);
140 		break;
141 	case RLINES:
142 		if (S_ISREG(sbp->st_mode))
143 			if (!off) {
144 				if (fseek(fp, 0L, SEEK_END) == -1) {
145 					ierr();
146 					return;
147 				}
148 			} else
149 				rlines(fp, off, sbp);
150 		else if (off == 0) {
151 			while (getc(fp) != EOF);
152 			if (ferror(fp)) {
153 				ierr();
154 				return;
155 			}
156 		} else
157 			lines(fp, off);
158 		break;
159 	}
160 
161 	/*
162 	 * We pause for one second after displaying any data that has
163 	 * accumulated since we read the file.
164 	 */
165 	if (fflag) {
166 		FD_ZERO(&zero);
167 		second.tv_sec = 1;
168 		second.tv_usec = 0;
169 	}
170 
171 	for (;;) {
172 		while ((ch = getc(fp)) != EOF)
173 			if (putchar(ch) == EOF)
174 				oerr();
175 		if (ferror(fp)) {
176 			ierr();
177 			return;
178 		}
179 		(void)fflush(stdout);
180 		if (!fflag)
181 			break;
182 		/* Sleep(3) is eight system calls.  Do it fast. */
183 		if (select(0, &zero, &zero, &zero, &second) == -1)
184 			err(1, "select: %s", strerror(errno));
185 		clearerr(fp);
186 	}
187 }
188 
189 /*
190  * rlines -- display the last offset lines of the file.
191  */
192 static void
193 rlines(fp, off, sbp)
194 	FILE *fp;
195 	long off;
196 	struct stat *sbp;
197 {
198 	register off_t size;
199 	register char *p;
200 	char *start;
201 
202 	if (!(size = sbp->st_size))
203 		return;
204 
205 	if (size > SIZE_T_MAX) {
206 		err(0, "%s: %s", fname, strerror(EFBIG));
207 		return;
208 	}
209 
210 	if ((start = mmap(NULL, (size_t)size,
211 	    PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) {
212 		err(0, "%s: %s", fname, strerror(EFBIG));
213 		return;
214 	}
215 
216 	/* Last char is special, ignore whether newline or not. */
217 	for (p = start + size - 1; --size;)
218 		if (*--p == '\n' && !--off) {
219 			++p;
220 			break;
221 		}
222 
223 	/* Set the file pointer to reflect the length displayed. */
224 	size = sbp->st_size - size;
225 	WR(p, size);
226 	if (fseek(fp, (long)sbp->st_size, SEEK_SET) == -1) {
227 		ierr();
228 		return;
229 	}
230 	if (munmap(start, (size_t)sbp->st_size)) {
231 		err(0, "%s: %s", fname, strerror(errno));
232 		return;
233 	}
234 }
235