2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Sze-Tyan Wang.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)read.c 8.1 (Berkeley) 6/6/93
33 * $FreeBSD: src/usr.bin/tail/read.c,v 1.6.8.1 2001/01/24 08:41:14 ru Exp $
34 * $DragonFly: src/usr.bin/tail/read.c,v 1.6 2008/02/05 14:35:43 matthias Exp $
37 #include <sys/types.h>
49 * display_bytes -- read bytes to an offset from the end and display.
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
59 display_bytes(FILE *fp, off_t off)
66 if ((sp = p = malloc(off)) == NULL)
69 for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
83 for (t = p - 1, len = 0; t >= sp; --t, ++len)
84 if (*t == '\n' && len) {
90 for (t = ep - 1, len = 0; t >= p; --t, ++len)
107 if (wrap && (len = ep - p))
109 if ((len = p - sp) != 0)
117 * display_lines -- read lines to an offset from the end and display.
119 * This is the function that reads to a line offset from the end of the input,
120 * storing the data in an array of buffers which is then displayed. If the
121 * rflag is set, the data is displayed in lines in reverse order, and this
122 * routine has the usual nastiness of trying to find the newlines. Otherwise,
123 * it is displayed from the line closest to the beginning of the input to
127 display_lines(FILE *fp, off_t off)
136 int blen, cnt, recno, wrap;
139 if ((lines = malloc(off * sizeof(*lines))) == NULL)
141 bzero(lines, off * sizeof(*lines));
143 blen = cnt = recno = wrap = 0;
145 while ((ch = getc(fp)) != EOF) {
147 if ((sp = realloc(sp, blen += 1024)) == NULL)
153 if (lines[recno].blen < cnt) {
154 lines[recno].blen = cnt + 256;
155 if ((lines[recno].l = realloc(lines[recno].l,
156 lines[recno].blen)) == NULL)
159 bcopy(sp, lines[recno].l, lines[recno].len = cnt);
162 if (++recno == off) {
176 lines[recno].len = cnt;
177 if (++recno == off) {
184 for (cnt = recno - 1; cnt >= 0; --cnt)
185 WR(lines[cnt].l, lines[cnt].len);
187 for (cnt = off - 1; cnt >= recno; --cnt)
188 WR(lines[cnt].l, lines[cnt].len);
191 for (cnt = recno; cnt < off; ++cnt)
192 WR(lines[cnt].l, lines[cnt].len);
193 for (cnt = 0; cnt < recno; ++cnt)
194 WR(lines[cnt].l, lines[cnt].len);
197 for (cnt = 0; cnt < off; cnt++)