xref: /netbsd-src/usr.bin/tail/forward.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: forward.c,v 1.7 1996/02/13 16:49:10 ghudson Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Edward Sze-Tyan Wang.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 #if 0
41 static char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
42 #endif
43 static char rcsid[] = "$NetBSD: forward.c,v 1.7 1996/02/13 16:49:10 ghudson Exp $";
44 #endif /* not lint */
45 
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <sys/time.h>
49 #include <sys/mman.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 "extern.h"
59 
60 static void rlines __P((FILE *, long, struct stat *));
61 
62 /*
63  * forward -- display the file, from an offset, forward.
64  *
65  * There are eight separate cases for this -- regular and non-regular
66  * files, by bytes or lines and from the beginning or end of the file.
67  *
68  * FBYTES	byte offset from the beginning of the file
69  *	REG	seek
70  *	NOREG	read, counting bytes
71  *
72  * FLINES	line offset from the beginning of the file
73  *	REG	read, counting lines
74  *	NOREG	read, counting lines
75  *
76  * RBYTES	byte offset from the end of the file
77  *	REG	seek
78  *	NOREG	cyclically read characters into a wrap-around buffer
79  *
80  * RLINES
81  *	REG	mmap the file and step back until reach the correct offset.
82  *	NOREG	cyclically read lines into a wrap-around array of buffers
83  */
84 void
85 forward(fp, style, off, sbp)
86 	FILE *fp;
87 	enum STYLE style;
88 	long off;
89 	struct stat *sbp;
90 {
91 	register int ch;
92 	struct timeval second;
93 
94 	switch(style) {
95 	case FBYTES:
96 		if (off == 0)
97 			break;
98 		if (S_ISREG(sbp->st_mode)) {
99 			if (sbp->st_size < off)
100 				off = sbp->st_size;
101 			if (fseek(fp, off, SEEK_SET) == -1) {
102 				ierr();
103 				return;
104 			}
105 		} else while (off--)
106 			if ((ch = getc(fp)) == EOF) {
107 				if (ferror(fp)) {
108 					ierr();
109 					return;
110 				}
111 				break;
112 			}
113 		break;
114 	case FLINES:
115 		if (off == 0)
116 			break;
117 		for (;;) {
118 			if ((ch = getc(fp)) == EOF) {
119 				if (ferror(fp)) {
120 					ierr();
121 					return;
122 				}
123 				break;
124 			}
125 			if (ch == '\n' && !--off)
126 				break;
127 		}
128 		break;
129 	case RBYTES:
130 		if (S_ISREG(sbp->st_mode)) {
131 			if (sbp->st_size >= off &&
132 			    fseek(fp, -off, SEEK_END) == -1) {
133 				ierr();
134 				return;
135 			}
136 		} else if (off == 0) {
137 			while (getc(fp) != EOF);
138 			if (ferror(fp)) {
139 				ierr();
140 				return;
141 			}
142 		} else
143 			bytes(fp, off);
144 		break;
145 	case RLINES:
146 		if (S_ISREG(sbp->st_mode))
147 			if (!off) {
148 				if (fseek(fp, 0L, SEEK_END) == -1) {
149 					ierr();
150 					return;
151 				}
152 			} else
153 				rlines(fp, off, sbp);
154 		else if (off == 0) {
155 			while (getc(fp) != EOF);
156 			if (ferror(fp)) {
157 				ierr();
158 				return;
159 			}
160 		} else
161 			lines(fp, off);
162 		break;
163 	}
164 
165 	for (;;) {
166 		while ((ch = getc(fp)) != EOF)
167 			if (putchar(ch) == EOF)
168 				oerr();
169 		if (ferror(fp)) {
170 			ierr();
171 			return;
172 		}
173 		(void)fflush(stdout);
174 		if (!fflag)
175 			break;
176 		/*
177 		 * We pause for one second after displaying any data that has
178 		 * accumulated since we read the file.  Since sleep(3) takes
179 		 * eight system calls, use select() instead.
180 		 */
181 		second.tv_sec = 1;
182 		second.tv_usec = 0;
183 		if (select(0, NULL, NULL, NULL, &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