xref: /minix3/usr.bin/tail/read.c (revision a967d739abf6b9e18d75e99925030c41b4f075ca)
1 /*	$NetBSD: read.c,v 1.17 2011/09/03 10:59:10 christos 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. 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 #include <sys/cdefs.h>
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
39 #endif
40 __RCSID("$NetBSD: read.c,v 1.17 2011/09/03 10:59:10 christos Exp $");
41 #endif /* not lint */
42 
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 #include <fcntl.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include "extern.h"
52 
53 /*
54  * displaybytes -- 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  * Non-zero return means than a (non-fatal) error occurred.
64  */
65 int
displaybytes(FILE * fp,off_t off)66 displaybytes(FILE *fp, off_t off)
67 {
68 	int ch, len, tlen;
69 	char *ep, *p, *t;
70 	int wrap;
71 	char *sp;
72 
73 	if ((sp = p = malloc(off)) == NULL)
74 		xerr(1, "malloc");
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 		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 		if ((len = p - sp) != 0)
116 			WR(sp, len);
117 	}
118 	return 0;
119 }
120 
121 /*
122  * displaylines -- read lines to an offset from the end and display.
123  *
124  * This is the function that reads to a line offset from the end of the input,
125  * storing the data in an array of buffers which is then displayed.  If the
126  * rflag is set, the data is displayed in lines in reverse order, and this
127  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
128  * it is displayed from the line closest to the beginning of the input to
129  * the end.
130  *
131  * Non-zero return means than a (non-fatal) error occurred.
132  */
133 int
displaylines(FILE * fp,off_t off)134 displaylines(FILE *fp, off_t off)
135 {
136 	struct {
137 		int blen;
138 		int len;
139 		char *l;
140 	} *lines;
141 	int ch;
142 	char *p;
143 	int blen, cnt, recno, wrap;
144 	char *sp, *n;
145 
146 	p = NULL;
147 	if ((lines = malloc(off * sizeof(*lines))) == NULL)
148 		xerr(1, "malloc");
149 
150 	memset(lines, 0, sizeof(*lines) * off);
151 
152 	sp = NULL;
153 	blen = cnt = recno = wrap = 0;
154 
155 	while ((ch = getc(fp)) != EOF) {
156 		if (++cnt > blen) {
157 			if ((n = realloc(sp, blen + 1024)) == NULL)
158 				xerr(1, "realloc");
159 			sp = n;
160 			blen += 1024;
161 			p = sp + cnt - 1;
162 		}
163 		*p++ = ch;
164 		if (ch == '\n') {
165 			if (lines[recno].blen < cnt) {
166 				if ((n = realloc(lines[recno].l,
167 				    cnt + 256)) == NULL)
168 					xerr(1, "realloc");
169 				lines[recno].l = n;
170 				lines[recno].blen = cnt + 256;
171 			}
172 			memmove(lines[recno].l, sp, lines[recno].len = cnt);
173 			cnt = 0;
174 			p = sp;
175 			if (++recno == off) {
176 				wrap = 1;
177 				recno = 0;
178 			}
179 		}
180 	}
181 	if (ferror(fp)) {
182 		free(lines);
183 		ierr();
184 		return 1;
185 	}
186 	if (cnt) {
187 		lines[recno].l = sp;
188 		lines[recno].len = cnt;
189 		if (++recno == off) {
190 			wrap = 1;
191 			recno = 0;
192 		}
193 	}
194 
195 	if (rflag) {
196 		for (cnt = recno - 1; cnt >= 0; --cnt)
197 			WR(lines[cnt].l, lines[cnt].len);
198 		if (wrap)
199 			for (cnt = off - 1; cnt >= recno; --cnt)
200 				WR(lines[cnt].l, lines[cnt].len);
201 	} else {
202 		if (wrap)
203 			for (cnt = recno; cnt < off; ++cnt)
204 				WR(lines[cnt].l, lines[cnt].len);
205 		for (cnt = 0; cnt < recno; ++cnt)
206 			WR(lines[cnt].l, lines[cnt].len);
207 	}
208 	free(lines);
209 	return 0;
210 }
211