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