reverse.c revision 1590
11592Srgrimes/*-
21592Srgrimes * Copyright (c) 1991, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * This code is derived from software contributed to Berkeley by
61592Srgrimes * Edward Sze-Tyan Wang.
71592Srgrimes *
81592Srgrimes * Redistribution and use in source and binary forms, with or without
91592Srgrimes * modification, are permitted provided that the following conditions
101592Srgrimes * are met:
111592Srgrimes * 1. Redistributions of source code must retain the above copyright
121592Srgrimes *    notice, this list of conditions and the following disclaimer.
131592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141592Srgrimes *    notice, this list of conditions and the following disclaimer in the
151592Srgrimes *    documentation and/or other materials provided with the distribution.
161592Srgrimes * 3. All advertising materials mentioning features or use of this software
171592Srgrimes *    must display the following acknowledgement:
181592Srgrimes *	This product includes software developed by the University of
191592Srgrimes *	California, Berkeley and its contributors.
201592Srgrimes * 4. Neither the name of the University nor the names of its contributors
211592Srgrimes *    may be used to endorse or promote products derived from this software
221592Srgrimes *    without specific prior written permission.
231592Srgrimes *
241592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341592Srgrimes * SUCH DAMAGE.
3531329Scharnier */
361592Srgrimes
3731329Scharnier#ifndef lint
3831329Scharnierstatic char sccsid[] = "@(#)reverse.c	8.1 (Berkeley) 6/6/93";
3950476Speter#endif /* not lint */
401592Srgrimes
411592Srgrimes#include <sys/param.h>
421592Srgrimes#include <sys/stat.h>
431592Srgrimes#include <sys/mman.h>
4416433Sache
4516433Sache#include <limits.h>
4656668Sshin#include <errno.h>
471592Srgrimes#include <unistd.h>
481592Srgrimes#include <stdio.h>
4916433Sache#include <stdlib.h>
50107030Speter#include <string.h>
5116433Sache#include "extern.h"
521592Srgrimes
531592Srgrimesstatic void r_buf __P((FILE *));
541592Srgrimesstatic void r_reg __P((FILE *, enum STYLE, long, struct stat *));
551592Srgrimes
5689920Sume/*
571592Srgrimes * reverse -- display input in reverse order by line.
581592Srgrimes *
591592Srgrimes * There are six separate cases for this -- regular and non-regular
601592Srgrimes * files by bytes, lines or the whole file.
611592Srgrimes *
621592Srgrimes * BYTES	display N bytes
631592Srgrimes *	REG	mmap the file and display the lines
641592Srgrimes *	NOREG	cyclically read characters into a wrap-around buffer
651592Srgrimes *
661592Srgrimes * LINES	display N lines
6789920Sume *	REG	mmap the file and display the lines
6889920Sume *	NOREG	cyclically read lines into a wrap-around array of buffers
6989920Sume *
701592Srgrimes * FILE		display the entire file
711592Srgrimes *	REG	mmap the file and display the lines
721592Srgrimes *	NOREG	cyclically read input into a linked list of buffers
7389920Sume */
741592Srgrimesvoid
7589920Sumereverse(fp, style, off, sbp)
7689920Sume	FILE *fp;
7789920Sume	enum STYLE style;
7889920Sume	long off;
7916433Sache	struct stat *sbp;
801592Srgrimes{
811592Srgrimes	if (style != REVERSE && off == 0)
821592Srgrimes		return;
831592Srgrimes
841592Srgrimes	if (S_ISREG(sbp->st_mode))
851592Srgrimes		r_reg(fp, style, off, sbp);
86107030Speter	else
87100612Syar		switch(style) {
881592Srgrimes		case FBYTES:
891592Srgrimes		case RBYTES:
901592Srgrimes			bytes(fp, off);
911592Srgrimes			break;
92		case FLINES:
93		case RLINES:
94			lines(fp, off);
95			break;
96		case REVERSE:
97			r_buf(fp);
98			break;
99		}
100}
101
102/*
103 * r_reg -- display a regular file in reverse order by line.
104 */
105static void
106r_reg(fp, style, off, sbp)
107	FILE *fp;
108	register enum STYLE style;
109	long off;
110	struct stat *sbp;
111{
112	register off_t size;
113	register int llen;
114	register char *p;
115	char *start;
116
117	if (!(size = sbp->st_size))
118		return;
119
120	if (size > SIZE_T_MAX) {
121		err(0, "%s: %s", fname, strerror(EFBIG));
122		return;
123	}
124
125	if ((start = mmap(NULL, (size_t)size,
126	    PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) {
127		err(0, "%s: %s", fname, strerror(EFBIG));
128		return;
129	}
130	p = start + size - 1;
131
132	if (style == RBYTES && off < size)
133		size = off;
134
135	/* Last char is special, ignore whether newline or not. */
136	for (llen = 1; --size; ++llen)
137		if (*--p == '\n') {
138			WR(p + 1, llen);
139			llen = 0;
140			if (style == RLINES && !--off) {
141				++p;
142				break;
143			}
144		}
145	if (llen)
146		WR(p, llen);
147	if (munmap(start, (size_t)sbp->st_size))
148		err(0, "%s: %s", fname, strerror(errno));
149}
150
151typedef struct bf {
152	struct bf *next;
153	struct bf *prev;
154	int len;
155	char *l;
156} BF;
157
158/*
159 * r_buf -- display a non-regular file in reverse order by line.
160 *
161 * This is the function that saves the entire input, storing the data in a
162 * doubly linked list of buffers and then displays them in reverse order.
163 * It has the usual nastiness of trying to find the newlines, as there's no
164 * guarantee that a newline occurs anywhere in the file, let alone in any
165 * particular buffer.  If we run out of memory, input is discarded (and the
166 * user warned).
167 */
168static void
169r_buf(fp)
170	FILE *fp;
171{
172	register BF *mark, *tl, *tr;
173	register int ch, len, llen;
174	register char *p;
175	off_t enomem;
176
177#define	BSZ	(128 * 1024)
178	for (mark = NULL, enomem = 0;;) {
179		/*
180		 * Allocate a new block and link it into place in a doubly
181		 * linked list.  If out of memory, toss the LRU block and
182		 * keep going.
183		 */
184		if (enomem || (tl = malloc(sizeof(BF))) == NULL ||
185		    (tl->l = malloc(BSZ)) == NULL) {
186			if (!mark)
187				err(1, "%s", strerror(errno));
188			tl = enomem ? tl->next : mark;
189			enomem += tl->len;
190		} else if (mark) {
191			tl->next = mark;
192			tl->prev = mark->prev;
193			mark->prev->next = tl;
194			mark->prev = tl;
195		} else
196			mark->next = mark->prev = (mark = tl);
197
198		/* Fill the block with input data. */
199		for (p = tl->l, len = 0;
200		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
201			*p++ = ch;
202
203		/*
204		 * If no input data for this block and we tossed some data,
205		 * recover it.
206		 */
207		if (!len) {
208			if (enomem)
209				enomem -= tl->len;
210			tl = tl->prev;
211			break;
212		}
213
214		tl->len = len;
215		if (ch == EOF)
216			break;
217	}
218
219	if (enomem) {
220		(void)fprintf(stderr,
221		    "tail: warning: %ld bytes discarded\n", enomem);
222		rval = 1;
223	}
224
225	/*
226	 * Step through the blocks in the reverse order read.  The last char
227	 * is special, ignore whether newline or not.
228	 */
229	for (mark = tl;;) {
230		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
231		    --p, ++llen)
232			if (*p == '\n') {
233				if (llen) {
234					WR(p + 1, llen);
235					llen = 0;
236				}
237				if (tl == mark)
238					continue;
239				for (tr = tl->next; tr->len; tr = tr->next) {
240					WR(tr->l, tr->len);
241					tr->len = 0;
242					if (tr == mark)
243						break;
244				}
245			}
246		tl->len = llen;
247		if ((tl = tl->prev) == mark)
248			break;
249	}
250	tl = tl->next;
251	if (tl->len) {
252		WR(tl->l, tl->len);
253		tl->len = 0;
254	}
255	while ((tl = tl->next)->len) {
256		WR(tl->l, tl->len);
257		tl->len = 0;
258	}
259}
260