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