Annotation of src/usr.bin/tail/read.c, Revision 1.11
1.11 ! otto 1: /* $OpenBSD: read.c,v 1.10 2007/09/13 19:59:18 otto Exp $ */
1.1 deraadt 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.
1.7 millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 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: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)read.c 8.1 (Berkeley) 6/6/93";
39: #endif
1.11 ! otto 40: static char rcsid[] = "$OpenBSD: read.c,v 1.10 2007/09/13 19:59:18 otto Exp $";
1.1 deraadt 41: #endif /* not lint */
42:
43: #include <sys/types.h>
44: #include <sys/stat.h>
1.8 kjell 45: #include <sys/limits.h>
1.3 millert 46:
47: #include <err.h>
48: #include <errno.h>
1.1 deraadt 49: #include <fcntl.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
1.3 millert 53: #include <unistd.h>
54:
1.1 deraadt 55: #include "extern.h"
56:
57: /*
58: * bytes -- read bytes to an offset from the end and display.
59: *
60: * This is the function that reads to a byte offset from the end of the input,
61: * storing the data in a wrap-around buffer which is then displayed. If the
62: * rflag is set, the data is displayed in lines in reverse order, and this
63: * routine has the usual nastiness of trying to find the newlines. Otherwise,
64: * it is displayed from the character closest to the beginning of the input to
65: * the end.
1.4 ericj 66: *
1.5 mpech 67: * A non-zero return means an (non-fatal) error occurred.
1.4 ericj 68: *
1.1 deraadt 69: */
1.4 ericj 70: int
1.9 kjell 71: bytes(FILE *fp, off_t off)
1.1 deraadt 72: {
1.8 kjell 73: int ch;
74: size_t len, tlen;
1.6 mpech 75: char *ep, *p, *t;
1.1 deraadt 76: int wrap;
77: char *sp;
78:
1.8 kjell 79: if (off > SIZE_T_MAX)
80: errx(1, "offset too large");
81:
1.1 deraadt 82: if ((sp = p = malloc(off)) == NULL)
1.3 millert 83: err(1, NULL);
1.1 deraadt 84:
85: for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
86: *p = ch;
87: if (++p == ep) {
88: wrap = 1;
89: p = sp;
90: }
91: }
92: if (ferror(fp)) {
93: ierr();
1.10 otto 94: free(sp);
1.4 ericj 95: return(1);
1.1 deraadt 96: }
97:
98: if (rflag) {
99: for (t = p - 1, len = 0; t >= sp; --t, ++len)
100: if (*t == '\n' && len) {
101: WR(t + 1, len);
102: len = 0;
103: }
104: if (wrap) {
105: tlen = len;
106: for (t = ep - 1, len = 0; t >= p; --t, ++len)
107: if (*t == '\n') {
108: if (len) {
109: WR(t + 1, len);
110: len = 0;
111: }
112: if (tlen) {
113: WR(sp, tlen);
114: tlen = 0;
115: }
116: }
117: if (len)
118: WR(t + 1, len);
119: if (tlen)
120: WR(sp, tlen);
121: }
122: } else {
123: if (wrap && (len = ep - p))
124: WR(p, len);
1.3 millert 125: if ((len = p - sp))
1.1 deraadt 126: WR(sp, len);
127: }
1.10 otto 128:
129: free(sp);
1.4 ericj 130: return(0);
1.1 deraadt 131: }
132:
133: /*
134: * lines -- read lines to an offset from the end and display.
135: *
136: * This is the function that reads to a line offset from the end of the input,
137: * storing the data in an array of buffers which is then displayed. If the
138: * rflag is set, the data is displayed in lines in reverse order, and this
139: * routine has the usual nastiness of trying to find the newlines. Otherwise,
140: * it is displayed from the line closest to the beginning of the input to
141: * the end.
1.4 ericj 142: *
1.5 mpech 143: * A non-zero return means an (non-fatal) error occurred.
1.4 ericj 144: *
1.1 deraadt 145: */
1.4 ericj 146: int
1.9 kjell 147: lines(FILE *fp, off_t off)
1.1 deraadt 148: {
149: struct {
1.8 kjell 150: size_t blen;
151: size_t len;
1.1 deraadt 152: char *l;
153: } *lines;
1.11 ! otto 154: int ch, rc = 0;
1.6 mpech 155: char *p = NULL;
1.8 kjell 156: int wrap;
157: size_t cnt, recno, blen, newsize;
158: char *sp = NULL, *newp = NULL;
159:
160: if (off > SIZE_T_MAX)
161: errx(1, "offset too large");
1.1 deraadt 162:
1.3 millert 163: if ((lines = calloc(off, sizeof(*lines))) == NULL)
164: err(1, NULL);
1.1 deraadt 165:
166: blen = cnt = recno = wrap = 0;
167:
168: while ((ch = getc(fp)) != EOF) {
169: if (++cnt > blen) {
1.8 kjell 170: newsize = blen + 1024;
171: if ((newp = realloc(sp, newsize)) == NULL)
1.3 millert 172: err(1, NULL);
1.8 kjell 173: sp = newp;
174: blen = newsize;
1.1 deraadt 175: p = sp + cnt - 1;
176: }
177: *p++ = ch;
178: if (ch == '\n') {
179: if (lines[recno].blen < cnt) {
1.8 kjell 180: newsize = cnt + 256;
181: if ((newp = realloc(lines[recno].l,
182: newsize)) == NULL)
1.3 millert 183: err(1, NULL);
1.8 kjell 184: lines[recno].l = newp;
185: lines[recno].blen = newsize;
1.1 deraadt 186: }
1.3 millert 187: memcpy(lines[recno].l, sp, (lines[recno].len = cnt));
1.1 deraadt 188: cnt = 0;
189: p = sp;
190: if (++recno == off) {
191: wrap = 1;
192: recno = 0;
193: }
194: }
195: }
196: if (ferror(fp)) {
197: ierr();
1.11 ! otto 198: rc = 1;
! 199: goto done;
1.1 deraadt 200: }
201: if (cnt) {
202: lines[recno].l = sp;
203: lines[recno].len = cnt;
204: if (++recno == off) {
205: wrap = 1;
206: recno = 0;
207: }
208: }
209:
210: if (rflag) {
1.8 kjell 211: for (cnt = recno; cnt > 0; --cnt)
212: WR(lines[cnt - 1].l, lines[cnt - 1].len);
1.1 deraadt 213: if (wrap)
1.8 kjell 214: for (cnt = off; cnt > recno; --cnt)
215: WR(lines[cnt - 1].l, lines[cnt - 1].len);
1.1 deraadt 216: } else {
217: if (wrap)
218: for (cnt = recno; cnt < off; ++cnt)
219: WR(lines[cnt].l, lines[cnt].len);
220: for (cnt = 0; cnt < recno; ++cnt)
221: WR(lines[cnt].l, lines[cnt].len);
222: }
1.11 ! otto 223: done:
! 224: for (cnt = 0; cnt < off; cnt++)
! 225: free(lines[cnt].l);
! 226: free(sp);
! 227: free(lines);
! 228: return(rc);
1.1 deraadt 229: }