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