xref: /freebsd/usr.bin/tail/read.c (revision 10b9d77bf1ccf2f3affafa6261692cb92cf7e992)
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  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 
35 __FBSDID("$FreeBSD$");
36 
37 #ifndef lint
38 static const char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 
41 #include <sys/types.h>
42 #include <sys/stat.h>
43 
44 #include <err.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
51 
52 #include "extern.h"
53 
54 /*
55  * bytes -- read bytes to an offset from the end and display.
56  *
57  * This is the function that reads to a byte offset from the end of the input,
58  * storing the data in a wrap-around buffer which is then displayed.  If the
59  * rflag is set, the data is displayed in lines in reverse order, and this
60  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
61  * it is displayed from the character closest to the beginning of the input to
62  * the end.
63  */
64 int
65 bytes(FILE *fp, const char *fn, off_t off)
66 {
67 	int ch, len, tlen;
68 	char *ep, *p, *t;
69 	int wrap;
70 	char *sp;
71 
72 	if ((sp = p = malloc(off)) == NULL)
73 		err(1, "malloc");
74 
75 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
76 		*p = ch;
77 		if (++p == ep) {
78 			wrap = 1;
79 			p = sp;
80 		}
81 	}
82 	if (ferror(fp)) {
83 		ierr(fn);
84 		free(sp);
85 		return 1;
86 	}
87 
88 	if (rflag) {
89 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
90 			if (*t == '\n' && len) {
91 				WR(t + 1, len);
92 				len = 0;
93 		}
94 		if (wrap) {
95 			tlen = len;
96 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
97 				if (*t == '\n') {
98 					if (len) {
99 						WR(t + 1, len);
100 						len = 0;
101 					}
102 					if (tlen) {
103 						WR(sp, tlen);
104 						tlen = 0;
105 					}
106 				}
107 			if (len)
108 				WR(t + 1, len);
109 			if (tlen)
110 				WR(sp, tlen);
111 		}
112 	} else {
113 		if (wrap && (len = ep - p))
114 			WR(p, len);
115 		len = p - sp;
116 		if (len)
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 int
135 lines(FILE *fp, const char *fn, off_t off)
136 {
137 	struct {
138 		int blen;
139 		u_int len;
140 		char *l;
141 	} *llines;
142 	int ch, rc;
143 	char *p, *sp;
144 	int blen, cnt, recno, wrap;
145 
146 	if ((llines = malloc(off * sizeof(*llines))) == NULL)
147 		err(1, "malloc");
148 	bzero(llines, off * sizeof(*llines));
149 	p = sp = NULL;
150 	blen = cnt = recno = wrap = 0;
151 	rc = 0;
152 
153 	while ((ch = getc(fp)) != EOF) {
154 		if (++cnt > blen) {
155 			if ((sp = realloc(sp, blen += 1024)) == NULL)
156 				err(1, "realloc");
157 			p = sp + cnt - 1;
158 		}
159 		*p++ = ch;
160 		if (ch == '\n') {
161 			if ((int)llines[recno].blen < cnt) {
162 				llines[recno].blen = cnt + 256;
163 				if ((llines[recno].l = realloc(llines[recno].l,
164 				    llines[recno].blen)) == NULL)
165 					err(1, "realloc");
166 			}
167 			bcopy(sp, llines[recno].l, llines[recno].len = cnt);
168 			cnt = 0;
169 			p = sp;
170 			if (++recno == off) {
171 				wrap = 1;
172 				recno = 0;
173 			}
174 		}
175 	}
176 	if (ferror(fp)) {
177 		ierr(fn);
178 		rc = 1;
179 		goto done;
180 	}
181 	if (cnt) {
182 		llines[recno].l = sp;
183 		sp = NULL;
184 		llines[recno].len = cnt;
185 		if (++recno == off) {
186 			wrap = 1;
187 			recno = 0;
188 		}
189 	}
190 
191 	if (rflag) {
192 		for (cnt = recno - 1; cnt >= 0; --cnt)
193 			WR(llines[cnt].l, llines[cnt].len);
194 		if (wrap)
195 			for (cnt = off - 1; cnt >= recno; --cnt)
196 				WR(llines[cnt].l, llines[cnt].len);
197 	} else {
198 		if (wrap)
199 			for (cnt = recno; cnt < off; ++cnt)
200 				WR(llines[cnt].l, llines[cnt].len);
201 		for (cnt = 0; cnt < recno; ++cnt)
202 			WR(llines[cnt].l, llines[cnt].len);
203 	}
204 done:
205 	for (cnt = 0; cnt < off; cnt++)
206 		free(llines[cnt].l);
207 	free(sp);
208 	free(llines);
209 	return (rc);
210 }
211