1 |
|
|
/* $OpenBSD: read.c,v 1.20 2017/03/26 19:55:07 martijn Exp $ */ |
2 |
|
|
/* $NetBSD: read.c,v 1.4 1994/11/23 07:42:07 jtc Exp $ */ |
3 |
|
|
|
4 |
|
|
/*- |
5 |
|
|
* Copyright (c) 1991, 1993 |
6 |
|
|
* The Regents of the University of California. All rights reserved. |
7 |
|
|
* |
8 |
|
|
* This code is derived from software contributed to Berkeley by |
9 |
|
|
* Edward Sze-Tyan Wang. |
10 |
|
|
* |
11 |
|
|
* Redistribution and use in source and binary forms, with or without |
12 |
|
|
* modification, are permitted provided that the following conditions |
13 |
|
|
* are met: |
14 |
|
|
* 1. Redistributions of source code must retain the above copyright |
15 |
|
|
* notice, this list of conditions and the following disclaimer. |
16 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
17 |
|
|
* notice, this list of conditions and the following disclaimer in the |
18 |
|
|
* documentation and/or other materials provided with the distribution. |
19 |
|
|
* 3. Neither the name of the University nor the names of its contributors |
20 |
|
|
* may be used to endorse or promote products derived from this software |
21 |
|
|
* without specific prior written permission. |
22 |
|
|
* |
23 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
24 |
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
25 |
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
26 |
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
27 |
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
28 |
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
29 |
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
30 |
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
31 |
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
32 |
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
33 |
|
|
* SUCH DAMAGE. |
34 |
|
|
*/ |
35 |
|
|
|
36 |
|
|
#include <sys/types.h> |
37 |
|
|
#include <sys/stat.h> |
38 |
|
|
|
39 |
|
|
#include <err.h> |
40 |
|
|
#include <stdio.h> |
41 |
|
|
#include <stdint.h> |
42 |
|
|
#include <stdlib.h> |
43 |
|
|
#include <string.h> |
44 |
|
|
#include <unistd.h> |
45 |
|
|
|
46 |
|
|
#include "extern.h" |
47 |
|
|
|
48 |
|
|
/* |
49 |
|
|
* bytes -- read bytes to an offset from the end and display. |
50 |
|
|
* |
51 |
|
|
* This is the function that reads to a byte offset from the end of the input, |
52 |
|
|
* storing the data in a wrap-around buffer which is then displayed. If the |
53 |
|
|
* rflag is set, the data is displayed in lines in reverse order, and this |
54 |
|
|
* routine has the usual nastiness of trying to find the newlines. Otherwise, |
55 |
|
|
* it is displayed from the character closest to the beginning of the input to |
56 |
|
|
* the end. |
57 |
|
|
* |
58 |
|
|
* A non-zero return means an (non-fatal) error occurred. |
59 |
|
|
* |
60 |
|
|
*/ |
61 |
|
|
int |
62 |
|
|
bytes(struct tailfile *tf, off_t off) |
63 |
|
|
{ |
64 |
|
|
int ch; |
65 |
|
|
size_t len, tlen; |
66 |
|
|
char *ep, *p, *t; |
67 |
|
|
int wrap; |
68 |
|
|
char *sp; |
69 |
|
|
|
70 |
|
|
if (off > SIZE_MAX) |
71 |
|
|
errx(1, "offset too large"); |
72 |
|
|
|
73 |
|
|
if ((sp = p = malloc(off)) == NULL) |
74 |
|
|
err(1, NULL); |
75 |
|
|
|
76 |
|
|
for (wrap = 0, ep = p + off; (ch = getc(tf->fp)) != EOF;) { |
77 |
|
|
*p = ch; |
78 |
|
|
if (++p == ep) { |
79 |
|
|
wrap = 1; |
80 |
|
|
p = sp; |
81 |
|
|
} |
82 |
|
|
} |
83 |
|
|
if (ferror(tf->fp)) { |
84 |
|
|
ierr(tf->fname); |
85 |
|
|
free(sp); |
86 |
|
|
return(1); |
87 |
|
|
} |
88 |
|
|
|
89 |
|
|
if (rflag) { |
90 |
|
|
for (t = p - 1, len = 0; t >= sp; --t, ++len) |
91 |
|
|
if (*t == '\n' && len) { |
92 |
|
|
WR(t + 1, len); |
93 |
|
|
len = 0; |
94 |
|
|
} |
95 |
|
|
if (wrap) { |
96 |
|
|
tlen = len; |
97 |
|
|
for (t = ep - 1, len = 0; t >= p; --t, ++len) |
98 |
|
|
if (*t == '\n') { |
99 |
|
|
if (len) { |
100 |
|
|
WR(t + 1, len); |
101 |
|
|
len = 0; |
102 |
|
|
} |
103 |
|
|
if (tlen) { |
104 |
|
|
WR(sp, tlen); |
105 |
|
|
tlen = 0; |
106 |
|
|
} |
107 |
|
|
} |
108 |
|
|
if (len) |
109 |
|
|
WR(t + 1, len); |
110 |
|
|
if (tlen) |
111 |
|
|
WR(sp, tlen); |
112 |
|
|
} |
113 |
|
|
} else { |
114 |
|
|
if (wrap && (len = ep - p)) |
115 |
|
|
WR(p, len); |
116 |
|
|
if ((len = p - sp)) |
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 |
|
|
* A non-zero return means an (non-fatal) error occurred. |
135 |
|
|
* |
136 |
|
|
*/ |
137 |
|
|
int |
138 |
|
|
lines(struct tailfile *tf, off_t off) |
139 |
|
|
{ |
140 |
|
|
struct { |
141 |
|
|
size_t blen; |
142 |
|
|
size_t len; |
143 |
|
|
char *l; |
144 |
|
|
} *lines = NULL; |
145 |
|
|
int ch, rc = 0; |
146 |
|
|
char *p = NULL; |
147 |
|
|
int wrap; |
148 |
|
|
size_t cnt, lineno, nlineno, recno, blen, newsize; |
149 |
|
|
char *sp = NULL, *newp = NULL; |
150 |
|
|
|
151 |
✗✓ |
138 |
if (off > SIZE_MAX) |
152 |
|
|
errx(1, "offset too large"); |
153 |
|
|
|
154 |
|
|
lineno = blen = cnt = recno = wrap = 0; |
155 |
|
|
|
156 |
✓✗✓✓ ✓✓ |
61450 |
while ((ch = getc(tf->fp)) != EOF) { |
157 |
✓✓ |
12221 |
if (++cnt > blen) { |
158 |
|
69 |
newsize = blen + 1024; |
159 |
✗✓ |
69 |
if ((newp = realloc(sp, newsize)) == NULL) |
160 |
|
|
err(1, NULL); |
161 |
|
|
sp = newp; |
162 |
|
|
blen = newsize; |
163 |
|
69 |
p = sp + cnt - 1; |
164 |
|
69 |
} |
165 |
|
12221 |
*p++ = ch; |
166 |
✓✓ |
12221 |
if (recno >= lineno) { |
167 |
|
69 |
nlineno = (lineno + 1024) > off ? |
168 |
|
|
(size_t) off : lineno + 1024; |
169 |
✗✓ |
138 |
if ((lines = recallocarray(lines, lineno, nlineno, |
170 |
|
69 |
sizeof(*lines))) == NULL) |
171 |
|
|
err(1, NULL); |
172 |
|
|
lineno = nlineno; |
173 |
|
69 |
} |
174 |
✓✓ |
12221 |
if (ch == '\n') { |
175 |
✓✓ |
724 |
if (lines[recno].blen < cnt) { |
176 |
|
69 |
newsize = cnt + 256; |
177 |
✗✓ |
138 |
if ((newp = realloc(lines[recno].l, |
178 |
|
69 |
newsize)) == NULL) |
179 |
|
|
err(1, NULL); |
180 |
|
69 |
lines[recno].l = newp; |
181 |
|
69 |
lines[recno].blen = newsize; |
182 |
|
69 |
} |
183 |
|
724 |
memcpy(lines[recno].l, sp, (lines[recno].len = cnt)); |
184 |
|
|
cnt = 0; |
185 |
|
|
p = sp; |
186 |
✓✗ |
724 |
if (++recno == off) { |
187 |
|
|
wrap = 1; |
188 |
|
|
recno = 0; |
189 |
|
724 |
} |
190 |
|
|
} |
191 |
|
|
} |
192 |
✓✗✗✓ ✗✗ |
138 |
if (ferror(tf->fp)) { |
193 |
|
|
ierr(tf->fname); |
194 |
|
|
rc = 1; |
195 |
|
|
goto done; |
196 |
|
|
} |
197 |
✗✓ |
69 |
if (cnt) { |
198 |
|
|
lines[recno].l = sp; |
199 |
|
|
lines[recno].len = cnt; |
200 |
|
|
sp = NULL; |
201 |
|
|
if (++recno == off) { |
202 |
|
|
wrap = 1; |
203 |
|
|
recno = 0; |
204 |
|
|
} |
205 |
|
|
} |
206 |
|
|
|
207 |
✗✓ |
69 |
if (rflag) { |
208 |
|
|
for (cnt = recno; cnt > 0; --cnt) |
209 |
|
|
WR(lines[cnt - 1].l, lines[cnt - 1].len); |
210 |
|
|
if (wrap) |
211 |
|
|
for (cnt = off; cnt > recno; --cnt) |
212 |
|
|
WR(lines[cnt - 1].l, lines[cnt - 1].len); |
213 |
|
|
} else { |
214 |
✓✗ |
69 |
if (wrap) |
215 |
✓✓ |
276 |
for (cnt = recno; cnt < off; ++cnt) |
216 |
✗✓ |
69 |
WR(lines[cnt].l, lines[cnt].len); |
217 |
✗✓ |
138 |
for (cnt = 0; cnt < recno; ++cnt) |
218 |
|
|
WR(lines[cnt].l, lines[cnt].len); |
219 |
|
|
} |
220 |
|
|
done: |
221 |
✓✓ |
276 |
for (cnt = 0; cnt < lineno; cnt++) |
222 |
|
69 |
free(lines[cnt].l); |
223 |
|
69 |
free(sp); |
224 |
|
69 |
free(lines); |
225 |
|
69 |
return(rc); |
226 |
|
|
} |