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 * @(#)reverse.c 8.1 (Berkeley) 6/6/93
33 * $FreeBSD: src/usr.bin/tail/reverse.c,v 1.9.2.3 2001/12/19 20:29:31 iedowse Exp $
34 * $DragonFly: src/usr.bin/tail/reverse.c,v 1.5 2006/08/13 02:12:18 swildner Exp $
37 #include <sys/param.h>
50 static void r_buf(FILE *);
51 static void r_reg(FILE *, enum STYLE, off_t, struct stat *);
54 * reverse -- display input in reverse order by line.
56 * There are six separate cases for this -- regular and non-regular
57 * files by bytes, lines or the whole file.
59 * BYTES display N bytes
60 * REG mmap the file and display the lines
61 * NOREG cyclically read characters into a wrap-around buffer
63 * LINES display N lines
64 * REG mmap the file and display the lines
65 * NOREG cyclically read lines into a wrap-around array of buffers
67 * FILE display the entire file
68 * REG mmap the file and display the lines
69 * NOREG cyclically read input into a linked list of buffers
72 reverse(FILE *fp, enum STYLE style, off_t off, struct stat *sbp)
74 if (style != REVERSE && off == 0)
77 if (S_ISREG(sbp->st_mode))
78 r_reg(fp, style, off, sbp);
83 display_bytes(fp, off);
87 display_lines(fp, off);
96 * r_reg -- display a regular file in reverse order by line.
99 r_reg(FILE *fp, enum STYLE style, off_t off, struct stat *sbp)
102 off_t curoff, size, lineend;
105 if (!(size = sbp->st_size))
109 map.mapoff = map.maxoff = size;
113 * Last char is special, ignore whether newline or not. Note that
114 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
118 while (curoff >= 0) {
119 if (curoff < map.mapoff ||
120 curoff >= (intmax_t)(map.mapoff + map.maplen)) {
121 if (maparound(&map, curoff) != 0) {
126 for (i = curoff - map.mapoff; i >= 0; i--) {
127 if (style == RBYTES && --off == 0)
129 if (map.start[i] == '\n')
132 /* `i' is either the map offset of a '\n', or -1. */
133 curoff = map.mapoff + i;
137 /* Print the line and update offsets. */
138 if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) {
142 lineend = curoff + 1;
148 if (off == 0 && style != REVERSE) {
149 /* Avoid printing anything below. */
154 if (curoff < 0 && mapprint(&map, 0, lineend) != 0) {
158 if (map.start != NULL && munmap(map.start, map.maplen))
170 * r_buf -- display a non-regular file in reverse order by line.
172 * This is the function that saves the entire input, storing the data in a
173 * doubly linked list of buffers and then displays them in reverse order.
174 * It has the usual nastiness of trying to find the newlines, as there's no
175 * guarantee that a newline occurs anywhere in the file, let alone in any
176 * particular buffer. If we run out of memory, input is discarded (and the
182 BF *mark, *tl = NULL, *tr = NULL;
183 int ch = 0, len, llen;
187 #define BSZ (128 * 1024)
188 for (mark = NULL, enomem = 0;;) {
190 * Allocate a new block and link it into place in a doubly
191 * linked list. If out of memory, toss the LRU block and
194 if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
195 (tl->l = malloc(BSZ)) == NULL) {
198 tl = enomem ? tl->next : mark;
202 tl->prev = mark->prev;
203 mark->prev->next = tl;
207 mark->next = mark->prev = mark;
210 /* Fill the block with input data. */
211 for (p = tl->l, len = 0;
212 len < BSZ && (ch = getc(fp)) != EOF; ++len)
221 * If no input data for this block and we tossed some data,
237 warnx("warning: %jd bytes discarded", (intmax_t)enomem);
242 * Step through the blocks in the reverse order read. The last char
243 * is special, ignore whether newline or not.
246 for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
255 for (tr = tl->next; tr->len; tr = tr->next) {
263 if ((tl = tl->prev) == mark)
271 while ((tl = tl->next)->len) {