read.c revision 1.4 1 /* $NetBSD: read.c,v 1.4 1994/11/23 07:42:07 jtc 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[] = "@(#)read.c 8.1 (Berkeley) 6/6/93";
42 #endif
43 static char rcsid[] = "$NetBSD: read.c,v 1.4 1994/11/23 07:42:07 jtc Exp $";
44 #endif /* not lint */
45
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <unistd.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include "extern.h"
55
56 /*
57 * bytes -- read bytes to an offset from the end and display.
58 *
59 * This is the function that reads to a byte offset from the end of the input,
60 * storing the data in a wrap-around buffer which is then displayed. If the
61 * rflag is set, the data is displayed in lines in reverse order, and this
62 * routine has the usual nastiness of trying to find the newlines. Otherwise,
63 * it is displayed from the character closest to the beginning of the input to
64 * the end.
65 */
66 void
67 bytes(fp, off)
68 register FILE *fp;
69 off_t off;
70 {
71 register int ch, len, tlen;
72 register char *ep, *p, *t;
73 int wrap;
74 char *sp;
75
76 if ((sp = p = malloc(off)) == NULL)
77 err(1, "%s", strerror(errno));
78
79 for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
80 *p = ch;
81 if (++p == ep) {
82 wrap = 1;
83 p = sp;
84 }
85 }
86 if (ferror(fp)) {
87 ierr();
88 return;
89 }
90
91 if (rflag) {
92 for (t = p - 1, len = 0; t >= sp; --t, ++len)
93 if (*t == '\n' && len) {
94 WR(t + 1, len);
95 len = 0;
96 }
97 if (wrap) {
98 tlen = len;
99 for (t = ep - 1, len = 0; t >= p; --t, ++len)
100 if (*t == '\n') {
101 if (len) {
102 WR(t + 1, len);
103 len = 0;
104 }
105 if (tlen) {
106 WR(sp, tlen);
107 tlen = 0;
108 }
109 }
110 if (len)
111 WR(t + 1, len);
112 if (tlen)
113 WR(sp, tlen);
114 }
115 } else {
116 if (wrap && (len = ep - p))
117 WR(p, len);
118 if (len = p - sp)
119 WR(sp, len);
120 }
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 void
134 lines(fp, off)
135 register FILE *fp;
136 off_t off;
137 {
138 struct {
139 u_int blen;
140 u_int len;
141 char *l;
142 } *lines;
143 register int ch;
144 register char *p;
145 int blen, cnt, recno, wrap;
146 char *sp;
147
148 if ((lines = malloc(off * sizeof(*lines))) == NULL)
149 err(1, "%s", strerror(errno));
150
151 sp = NULL;
152 blen = cnt = recno = wrap = 0;
153
154 while ((ch = getc(fp)) != EOF) {
155 if (++cnt > blen) {
156 if ((sp = realloc(sp, blen += 1024)) == NULL)
157 err(1, "%s", strerror(errno));
158 p = sp + cnt - 1;
159 }
160 *p++ = ch;
161 if (ch == '\n') {
162 if (lines[recno].blen < cnt) {
163 lines[recno].blen = cnt + 256;
164 if ((lines[recno].l = realloc(lines[recno].l,
165 lines[recno].blen)) == NULL)
166 err(1, "%s", strerror(errno));
167 }
168 bcopy(sp, lines[recno].l, lines[recno].len = cnt);
169 cnt = 0;
170 p = sp;
171 if (++recno == off) {
172 wrap = 1;
173 recno = 0;
174 }
175 }
176 }
177 if (ferror(fp)) {
178 ierr();
179 return;
180 }
181 if (cnt) {
182 lines[recno].l = sp;
183 lines[recno].len = cnt;
184 if (++recno == off) {
185 wrap = 1;
186 recno = 0;
187 }
188 }
189
190 if (rflag) {
191 for (cnt = recno - 1; cnt >= 0; --cnt)
192 WR(lines[cnt].l, lines[cnt].len);
193 if (wrap)
194 for (cnt = off - 1; cnt >= recno; --cnt)
195 WR(lines[cnt].l, lines[cnt].len);
196 } else {
197 if (wrap)
198 for (cnt = recno; cnt < off; ++cnt)
199 WR(lines[cnt].l, lines[cnt].len);
200 for (cnt = 0; cnt < recno; ++cnt)
201 WR(lines[cnt].l, lines[cnt].len);
202 }
203 }
204