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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static char sccsid[] = "@(#)reverse.c 8.1 (Berkeley) 6/6/93";
41 static const char rcsid[] =
45 #include <sys/param.h>
58 static void r_buf __P((FILE *));
59 static void r_reg __P((FILE *, enum STYLE, long, struct stat *));
62 * reverse -- display input in reverse order by line.
64 * There are six separate cases for this -- regular and non-regular
65 * files by bytes, lines or the whole file.
67 * BYTES display N bytes
68 * REG mmap the file and display the lines
69 * NOREG cyclically read characters into a wrap-around buffer
71 * LINES display N lines
72 * REG mmap the file and display the lines
73 * NOREG cyclically read lines into a wrap-around array of buffers
75 * FILE display the entire file
76 * REG mmap the file and display the lines
77 * NOREG cyclically read input into a linked list of buffers
80 reverse(fp, style, off, sbp)
86 if (style != REVERSE && off == 0)
89 if (S_ISREG(sbp->st_mode))
90 r_reg(fp, style, off, sbp);
108 * r_reg -- display a regular file in reverse order by line.
111 r_reg(fp, style, off, sbp)
113 register enum STYLE style;
122 if (!(size = sbp->st_size))
125 if (size > SIZE_T_MAX) {
131 if ((start = mmap(NULL, (size_t)size,
132 PROT_READ, MAP_SHARED, fileno(fp), (off_t)0)) == MAP_FAILED) {
136 p = start + size - 1;
138 if (style == RBYTES && off < size)
141 /* Last char is special, ignore whether newline or not. */
142 for (llen = 1; --size; ++llen)
146 if (style == RLINES && !--off) {
153 if (munmap(start, (size_t)sbp->st_size))
165 * r_buf -- display a non-regular file in reverse order by line.
167 * This is the function that saves the entire input, storing the data in a
168 * doubly linked list of buffers and then displays them in reverse order.
169 * It has the usual nastiness of trying to find the newlines, as there's no
170 * guarantee that a newline occurs anywhere in the file, let alone in any
171 * particular buffer. If we run out of memory, input is discarded (and the
178 register BF *mark, *tl, *tr;
179 register int ch, len, llen;
183 #define BSZ (128 * 1024)
184 for (mark = NULL, enomem = 0;;) {
186 * Allocate a new block and link it into place in a doubly
187 * linked list. If out of memory, toss the LRU block and
190 if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
191 (tl->l = malloc(BSZ)) == NULL) {
194 tl = enomem ? tl->next : mark;
198 tl->prev = mark->prev;
199 mark->prev->next = tl;
202 mark->next = mark->prev = (mark = tl);
204 /* Fill the block with input data. */
205 for (p = tl->l, len = 0;
206 len < BSZ && (ch = getc(fp)) != EOF; ++len)
215 * If no input data for this block and we tossed some data,
231 warnx("warning: %qd bytes discarded", enomem);
236 * Step through the blocks in the reverse order read. The last char
237 * is special, ignore whether newline or not.
240 for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
249 for (tr = tl->next; tr->len; tr = tr->next) {
257 if ((tl = tl->prev) == mark)
265 while ((tl = tl->next)->len) {