xref: /openbsd/usr.bin/tail/read.c (revision 17df1aa7)
1 /*	$OpenBSD: read.c,v 1.14 2009/10/27 23:59:44 deraadt Exp $	*/
2 /*	$NetBSD: read.c,v 1.4 1994/11/23 07:42:07 jtc Exp $	*/
3 
4 /*-
5  * Copyright (c) 1991, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Edward Sze-Tyan Wang.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/limits.h>
39 
40 #include <err.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "extern.h"
47 
48 /*
49  * bytes -- read bytes to an offset from the end and display.
50  *
51  * This is the function that reads to a byte offset from the end of the input,
52  * storing the data in a wrap-around buffer which is then displayed.  If the
53  * rflag is set, the data is displayed in lines in reverse order, and this
54  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
55  * it is displayed from the character closest to the beginning of the input to
56  * the end.
57  *
58  * A non-zero return means an (non-fatal) error occurred.
59  *
60  */
61 int
62 bytes(FILE *fp, off_t off)
63 {
64 	int ch;
65 	size_t len, tlen;
66 	char *ep, *p, *t;
67 	int wrap;
68 	char *sp;
69 
70 	if (off > SIZE_T_MAX)
71 		errx(1, "offset too large");
72 
73 	if ((sp = p = malloc(off)) == NULL)
74 		err(1, NULL);
75 
76 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
77 		*p = ch;
78 		if (++p == ep) {
79 			wrap = 1;
80 			p = sp;
81 		}
82 	}
83 	if (ferror(fp)) {
84 		ierr();
85 		free(sp);
86 		return(1);
87 	}
88 
89 	if (rflag) {
90 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
91 			if (*t == '\n' && len) {
92 				WR(t + 1, len);
93 				len = 0;
94 		}
95 		if (wrap) {
96 			tlen = len;
97 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
98 				if (*t == '\n') {
99 					if (len) {
100 						WR(t + 1, len);
101 						len = 0;
102 					}
103 					if (tlen) {
104 						WR(sp, tlen);
105 						tlen = 0;
106 					}
107 				}
108 			if (len)
109 				WR(t + 1, len);
110 			if (tlen)
111 				WR(sp, tlen);
112 		}
113 	} else {
114 		if (wrap && (len = ep - p))
115 			WR(p, len);
116 		if ((len = p - sp))
117 			WR(sp, len);
118 	}
119 
120 	free(sp);
121 	return(0);
122 }
123 
124 /*
125  * lines -- read lines to an offset from the end and display.
126  *
127  * This is the function that reads to a line offset from the end of the input,
128  * storing the data in an array of buffers which is then displayed.  If the
129  * rflag is set, the data is displayed in lines in reverse order, and this
130  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
131  * it is displayed from the line closest to the beginning of the input to
132  * the end.
133  *
134  * A non-zero return means an (non-fatal) error occurred.
135  *
136  */
137 int
138 lines(FILE *fp, off_t off)
139 {
140 	struct {
141 		size_t blen;
142 		size_t len;
143 		char *l;
144 	} *lines;
145 	int ch, rc = 0;
146 	char *p = NULL;
147 	int wrap;
148 	size_t cnt, recno, blen, newsize;
149 	char *sp = NULL, *newp = NULL;
150 
151 	if (off > SIZE_T_MAX)
152 		errx(1, "offset too large");
153 
154 	if ((lines = calloc(off, sizeof(*lines))) == NULL)
155 		err(1, NULL);
156 
157 	blen = cnt = recno = wrap = 0;
158 
159 	while ((ch = getc(fp)) != EOF) {
160 		if (++cnt > blen) {
161 			newsize = blen + 1024;
162 			if ((newp = realloc(sp, newsize)) == NULL)
163 				err(1, NULL);
164 			sp = newp;
165 			blen = newsize;
166 			p = sp + cnt - 1;
167 		}
168 		*p++ = ch;
169 		if (ch == '\n') {
170 			if (lines[recno].blen < cnt) {
171 				newsize = cnt + 256;
172 				if ((newp = realloc(lines[recno].l,
173 				    newsize)) == NULL)
174 					err(1, NULL);
175 				lines[recno].l = newp;
176 				lines[recno].blen = newsize;
177 			}
178 			memcpy(lines[recno].l, sp, (lines[recno].len = cnt));
179 			cnt = 0;
180 			p = sp;
181 			if (++recno == off) {
182 				wrap = 1;
183 				recno = 0;
184 			}
185 		}
186 	}
187 	if (ferror(fp)) {
188 		ierr();
189 		rc = 1;
190 		goto done;
191 	}
192 	if (cnt) {
193 		lines[recno].l = sp;
194 		lines[recno].len = cnt;
195 		sp = NULL;
196 		if (++recno == off) {
197 			wrap = 1;
198 			recno = 0;
199 		}
200 	}
201 
202 	if (rflag) {
203 		for (cnt = recno; cnt > 0; --cnt)
204 			WR(lines[cnt - 1].l, lines[cnt - 1].len);
205 		if (wrap)
206 			for (cnt = off; cnt > recno; --cnt)
207 				WR(lines[cnt - 1].l, lines[cnt - 1].len);
208 	} else {
209 		if (wrap)
210 			for (cnt = recno; cnt < off; ++cnt)
211 				WR(lines[cnt].l, lines[cnt].len);
212 		for (cnt = 0; cnt < recno; ++cnt)
213 			WR(lines[cnt].l, lines[cnt].len);
214 	}
215 done:
216 	for (cnt = 0; cnt < off; cnt++)
217 		free(lines[cnt].l);
218 	free(sp);
219 	free(lines);
220 	return(rc);
221 }
222