xref: /illumos-gate/usr/src/cmd/tail/reverse.c (revision 209e49b2)
1*209e49b2SChris Love /*
2*209e49b2SChris Love  * Copyright (c) 1991, 1993
3*209e49b2SChris Love  *	The Regents of the University of California.  All rights reserved.
4*209e49b2SChris Love  *
5*209e49b2SChris Love  * This code is derived from software contributed to Berkeley by
6*209e49b2SChris Love  * Edward Sze-Tyan Wang.
7*209e49b2SChris Love  *
8*209e49b2SChris Love  * Redistribution and use in source and binary forms, with or without
9*209e49b2SChris Love  * modification, are permitted provided that the following conditions
10*209e49b2SChris Love  * are met:
11*209e49b2SChris Love  * 1. Redistributions of source code must retain the above copyright
12*209e49b2SChris Love  *    notice, this list of conditions and the following disclaimer.
13*209e49b2SChris Love  * 2. Redistributions in binary form must reproduce the above copyright
14*209e49b2SChris Love  *    notice, this list of conditions and the following disclaimer in the
15*209e49b2SChris Love  *    documentation and/or other materials provided with the distribution.
16*209e49b2SChris Love  * 4. Neither the name of the University nor the names of its contributors
17*209e49b2SChris Love  *    may be used to endorse or promote products derived from this software
18*209e49b2SChris Love  *    without specific prior written permission.
19*209e49b2SChris Love  *
20*209e49b2SChris Love  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*209e49b2SChris Love  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*209e49b2SChris Love  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*209e49b2SChris Love  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*209e49b2SChris Love  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*209e49b2SChris Love  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*209e49b2SChris Love  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*209e49b2SChris Love  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*209e49b2SChris Love  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*209e49b2SChris Love  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*209e49b2SChris Love  * SUCH DAMAGE.
31*209e49b2SChris Love  */
32*209e49b2SChris Love 
33*209e49b2SChris Love #include <sys/param.h>
34*209e49b2SChris Love #include <sys/stat.h>
35*209e49b2SChris Love #include <sys/mman.h>
36*209e49b2SChris Love 
37*209e49b2SChris Love #include <err.h>
38*209e49b2SChris Love #include <errno.h>
39*209e49b2SChris Love #include <limits.h>
40*209e49b2SChris Love #include <stdint.h>
41*209e49b2SChris Love #include <stdio.h>
42*209e49b2SChris Love #include <stdlib.h>
43*209e49b2SChris Love #include <string.h>
44*209e49b2SChris Love #include <unistd.h>
45*209e49b2SChris Love 
46*209e49b2SChris Love #include "extern.h"
47*209e49b2SChris Love 
48*209e49b2SChris Love static void r_buf(FILE *, const char *);
49*209e49b2SChris Love static void r_reg(FILE *, const char *, enum STYLE, off_t, struct stat *);
50*209e49b2SChris Love 
51*209e49b2SChris Love /*
52*209e49b2SChris Love  * reverse -- display input in reverse order by line.
53*209e49b2SChris Love  *
54*209e49b2SChris Love  * There are six separate cases for this -- regular and non-regular
55*209e49b2SChris Love  * files by bytes, lines or the whole file.
56*209e49b2SChris Love  *
57*209e49b2SChris Love  * BYTES	display N bytes
58*209e49b2SChris Love  *	REG	mmap the file and display the lines
59*209e49b2SChris Love  *	NOREG	cyclically read characters into a wrap-around buffer
60*209e49b2SChris Love  *
61*209e49b2SChris Love  * LINES	display N lines
62*209e49b2SChris Love  *	REG	mmap the file and display the lines
63*209e49b2SChris Love  *	NOREG	cyclically read lines into a wrap-around array of buffers
64*209e49b2SChris Love  *
65*209e49b2SChris Love  * FILE		display the entire file
66*209e49b2SChris Love  *	REG	mmap the file and display the lines
67*209e49b2SChris Love  *	NOREG	cyclically read input into a linked list of buffers
68*209e49b2SChris Love  */
69*209e49b2SChris Love void
reverse(FILE * fp,const char * fn,enum STYLE style,off_t off,struct stat * sbp)70*209e49b2SChris Love reverse(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
71*209e49b2SChris Love {
72*209e49b2SChris Love 	if (style != REVERSE && off == 0)
73*209e49b2SChris Love 		return;
74*209e49b2SChris Love 
75*209e49b2SChris Love 	if (S_ISREG(sbp->st_mode))
76*209e49b2SChris Love 		r_reg(fp, fn, style, off, sbp);
77*209e49b2SChris Love 	else
78*209e49b2SChris Love 		switch (style) {
79*209e49b2SChris Love 		case FBYTES:
80*209e49b2SChris Love 		case RBYTES:
81*209e49b2SChris Love 			(void) bytes(fp, fn, off);
82*209e49b2SChris Love 			break;
83*209e49b2SChris Love 		case FLINES:
84*209e49b2SChris Love 		case RLINES:
85*209e49b2SChris Love 			(void) lines(fp, fn, off);
86*209e49b2SChris Love 			break;
87*209e49b2SChris Love 		case REVERSE:
88*209e49b2SChris Love 			r_buf(fp, fn);
89*209e49b2SChris Love 			break;
90*209e49b2SChris Love 		default:
91*209e49b2SChris Love 			break;
92*209e49b2SChris Love 		}
93*209e49b2SChris Love }
94*209e49b2SChris Love 
95*209e49b2SChris Love /*
96*209e49b2SChris Love  * r_reg -- display a regular file in reverse order by line.
97*209e49b2SChris Love  */
98*209e49b2SChris Love static void
r_reg(FILE * fp,const char * fn,enum STYLE style,off_t off,struct stat * sbp)99*209e49b2SChris Love r_reg(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
100*209e49b2SChris Love {
101*209e49b2SChris Love 	struct mapinfo map;
102*209e49b2SChris Love 	off_t curoff, size, lineend;
103*209e49b2SChris Love 	int i;
104*209e49b2SChris Love 
105*209e49b2SChris Love 	if ((size = sbp->st_size) == 0)
106*209e49b2SChris Love 		return;
107*209e49b2SChris Love 
108*209e49b2SChris Love 	map.start = NULL;
109*209e49b2SChris Love 	map.mapoff = map.maxoff = size;
110*209e49b2SChris Love 	map.fd = fileno(fp);
111*209e49b2SChris Love 
112*209e49b2SChris Love 	/*
113*209e49b2SChris Love 	 * Last char is special, ignore whether newline or not. Note that
114*209e49b2SChris Love 	 * size == 0 is dealt with above, and size == 1 sets curoff to -1.
115*209e49b2SChris Love 	 */
116*209e49b2SChris Love 	curoff = size - 2;
117*209e49b2SChris Love 	lineend = size;
118*209e49b2SChris Love 	while (curoff >= 0) {
119*209e49b2SChris Love 		if (curoff < map.mapoff ||
120*209e49b2SChris Love 		    curoff >= map.mapoff + (off_t)map.maplen) {
121*209e49b2SChris Love 			if (maparound(&map, curoff) != 0) {
122*209e49b2SChris Love 				ierr(fn);
123*209e49b2SChris Love 				return;
124*209e49b2SChris Love 			}
125*209e49b2SChris Love 		}
126*209e49b2SChris Love 		for (i = curoff - map.mapoff; i >= 0; i--) {
127*209e49b2SChris Love 			if (style == RBYTES && --off == 0)
128*209e49b2SChris Love 				break;
129*209e49b2SChris Love 			if (map.start[i] == '\n')
130*209e49b2SChris Love 				break;
131*209e49b2SChris Love 		}
132*209e49b2SChris Love 		/* `i' is either the map offset of a '\n', or -1. */
133*209e49b2SChris Love 		curoff = map.mapoff + i;
134*209e49b2SChris Love 		if (i < 0)
135*209e49b2SChris Love 			continue;
136*209e49b2SChris Love 
137*209e49b2SChris Love 		/* Print the line and update offsets. */
138*209e49b2SChris Love 		if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) {
139*209e49b2SChris Love 			ierr(fn);
140*209e49b2SChris Love 			return;
141*209e49b2SChris Love 		}
142*209e49b2SChris Love 		lineend = curoff + 1;
143*209e49b2SChris Love 		curoff--;
144*209e49b2SChris Love 
145*209e49b2SChris Love 		if (style == RLINES)
146*209e49b2SChris Love 			off--;
147*209e49b2SChris Love 
148*209e49b2SChris Love 		if (off == 0 && style != REVERSE) {
149*209e49b2SChris Love 			/* Avoid printing anything below. */
150*209e49b2SChris Love 			curoff = 0;
151*209e49b2SChris Love 			break;
152*209e49b2SChris Love 		}
153*209e49b2SChris Love 	}
154*209e49b2SChris Love 	if (curoff < 0 && mapprint(&map, 0, lineend) != 0) {
155*209e49b2SChris Love 		ierr(fn);
156*209e49b2SChris Love 		return;
157*209e49b2SChris Love 	}
158*209e49b2SChris Love 	if (map.start != NULL && munmap(map.start, map.maplen))
159*209e49b2SChris Love 		ierr(fn);
160*209e49b2SChris Love }
161*209e49b2SChris Love 
162*209e49b2SChris Love typedef struct bf {
163*209e49b2SChris Love 	struct bf *next;
164*209e49b2SChris Love 	struct bf *prev;
165*209e49b2SChris Love 	int len;
166*209e49b2SChris Love 	char *l;
167*209e49b2SChris Love } BF;
168*209e49b2SChris Love 
169*209e49b2SChris Love /*
170*209e49b2SChris Love  * r_buf -- display a non-regular file in reverse order by line.
171*209e49b2SChris Love  *
172*209e49b2SChris Love  * This is the function that saves the entire input, storing the data in a
173*209e49b2SChris Love  * doubly linked list of buffers and then displays them in reverse order.
174*209e49b2SChris Love  * It has the usual nastiness of trying to find the newlines, as there's no
175*209e49b2SChris Love  * guarantee that a newline occurs anywhere in the file, let alone in any
176*209e49b2SChris Love  * particular buffer.  If we run out of memory, input is discarded (and the
177*209e49b2SChris Love  * user warned).
178*209e49b2SChris Love  */
179*209e49b2SChris Love static void
r_buf(FILE * fp,const char * fn)180*209e49b2SChris Love r_buf(FILE *fp, const char *fn)
181*209e49b2SChris Love {
182*209e49b2SChris Love 	BF *mark, *tl, *tr;
183*209e49b2SChris Love 	int ch, len, llen;
184*209e49b2SChris Love 	char *p;
185*209e49b2SChris Love 	off_t enomem;
186*209e49b2SChris Love 
187*209e49b2SChris Love 	tl = NULL;
188*209e49b2SChris Love #define	BSZ	(128 * 1024)
189*209e49b2SChris Love 	for (mark = NULL, enomem = 0; ; ) {
190*209e49b2SChris Love 		/*
191*209e49b2SChris Love 		 * Allocate a new block and link it into place in a doubly
192*209e49b2SChris Love 		 * linked list.  If out of memory, toss the LRU block and
193*209e49b2SChris Love 		 * keep going.
194*209e49b2SChris Love 		 */
195*209e49b2SChris Love 		if (enomem || (tl = malloc(sizeof (BF))) == NULL ||
196*209e49b2SChris Love 		    (tl->l = malloc(BSZ)) == NULL) {
197*209e49b2SChris Love 			if (!mark)
198*209e49b2SChris Love 				err(1, "malloc");
199*209e49b2SChris Love 			tl = enomem ? tl->next : mark;
200*209e49b2SChris Love 			enomem += tl->len;
201*209e49b2SChris Love 		} else if (mark) {
202*209e49b2SChris Love 			tl->next = mark;
203*209e49b2SChris Love 			tl->prev = mark->prev;
204*209e49b2SChris Love 			mark->prev->next = tl;
205*209e49b2SChris Love 			mark->prev = tl;
206*209e49b2SChris Love 		} else {
207*209e49b2SChris Love 			mark = tl;
208*209e49b2SChris Love 			mark->next = mark->prev = mark;
209*209e49b2SChris Love 		}
210*209e49b2SChris Love 
211*209e49b2SChris Love 		/* Fill the block with input data. */
212*209e49b2SChris Love 		for (p = tl->l, len = 0;
213*209e49b2SChris Love 		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
214*209e49b2SChris Love 			*p++ = ch;
215*209e49b2SChris Love 
216*209e49b2SChris Love 		if (ferror(fp)) {
217*209e49b2SChris Love 			ierr(fn);
218*209e49b2SChris Love 			return;
219*209e49b2SChris Love 		}
220*209e49b2SChris Love 
221*209e49b2SChris Love 		/*
222*209e49b2SChris Love 		 * If no input data for this block and we tossed some data,
223*209e49b2SChris Love 		 * recover it.
224*209e49b2SChris Love 		 */
225*209e49b2SChris Love 		if (!len && enomem) {
226*209e49b2SChris Love 			enomem -= tl->len;
227*209e49b2SChris Love 			tl = tl->prev;
228*209e49b2SChris Love 			break;
229*209e49b2SChris Love 		}
230*209e49b2SChris Love 
231*209e49b2SChris Love 		tl->len = len;
232*209e49b2SChris Love 		if (ch == EOF)
233*209e49b2SChris Love 			break;
234*209e49b2SChris Love 	}
235*209e49b2SChris Love 
236*209e49b2SChris Love 	if (enomem) {
237*209e49b2SChris Love 		warnx("warning: %jd bytes discarded", (intmax_t)enomem);
238*209e49b2SChris Love 		rval = 1;
239*209e49b2SChris Love 	}
240*209e49b2SChris Love 
241*209e49b2SChris Love 	/*
242*209e49b2SChris Love 	 * Step through the blocks in the reverse order read.  The last char
243*209e49b2SChris Love 	 * is special, ignore whether newline or not.
244*209e49b2SChris Love 	 */
245*209e49b2SChris Love 	for (mark = tl; ; ) {
246*209e49b2SChris Love 		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
247*209e49b2SChris Love 		    --p, ++llen)
248*209e49b2SChris Love 			if (*p == '\n') {
249*209e49b2SChris Love 				if (llen) {
250*209e49b2SChris Love 					WR(p + 1, llen);
251*209e49b2SChris Love 					llen = 0;
252*209e49b2SChris Love 				}
253*209e49b2SChris Love 				if (tl == mark)
254*209e49b2SChris Love 					continue;
255*209e49b2SChris Love 				for (tr = tl->next; tr->len; tr = tr->next) {
256*209e49b2SChris Love 					WR(tr->l, tr->len);
257*209e49b2SChris Love 					tr->len = 0;
258*209e49b2SChris Love 					if (tr == mark)
259*209e49b2SChris Love 						break;
260*209e49b2SChris Love 				}
261*209e49b2SChris Love 			}
262*209e49b2SChris Love 		tl->len = llen;
263*209e49b2SChris Love 		if ((tl = tl->prev) == mark)
264*209e49b2SChris Love 			break;
265*209e49b2SChris Love 	}
266*209e49b2SChris Love 	tl = tl->next;
267*209e49b2SChris Love 	if (tl->len) {
268*209e49b2SChris Love 		WR(tl->l, tl->len);
269*209e49b2SChris Love 		tl->len = 0;
270*209e49b2SChris Love 	}
271*209e49b2SChris Love 	while ((tl = tl->next)->len) {
272*209e49b2SChris Love 		WR(tl->l, tl->len);
273*209e49b2SChris Love 		tl->len = 0;
274*209e49b2SChris Love 	}
275*209e49b2SChris Love }
276