xref: /titanic_41/usr/src/cmd/tail/reverse.c (revision 9ea11b96f9f71eef4f1bd2062ea054451a32a216)
1 /*
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Sze-Tyan Wang.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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  * 4. 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.
19  *
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
30  * SUCH DAMAGE.
31  */
32 
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <sys/mman.h>
36 
37 #include <err.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdint.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <unistd.h>
45 
46 #include "extern.h"
47 
48 static void r_buf(FILE *, const char *);
49 static void r_reg(FILE *, const char *, enum STYLE, off_t, struct stat *);
50 
51 /*
52  * reverse -- display input in reverse order by line.
53  *
54  * There are six separate cases for this -- regular and non-regular
55  * files by bytes, lines or the whole file.
56  *
57  * BYTES	display N bytes
58  *	REG	mmap the file and display the lines
59  *	NOREG	cyclically read characters into a wrap-around buffer
60  *
61  * LINES	display N lines
62  *	REG	mmap the file and display the lines
63  *	NOREG	cyclically read lines into a wrap-around array of buffers
64  *
65  * FILE		display the entire file
66  *	REG	mmap the file and display the lines
67  *	NOREG	cyclically read input into a linked list of buffers
68  */
69 void
70 reverse(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
71 {
72 	if (style != REVERSE && off == 0)
73 		return;
74 
75 	if (S_ISREG(sbp->st_mode))
76 		r_reg(fp, fn, style, off, sbp);
77 	else
78 		switch (style) {
79 		case FBYTES:
80 		case RBYTES:
81 			(void) bytes(fp, fn, off);
82 			break;
83 		case FLINES:
84 		case RLINES:
85 			(void) lines(fp, fn, off);
86 			break;
87 		case REVERSE:
88 			r_buf(fp, fn);
89 			break;
90 		default:
91 			break;
92 		}
93 }
94 
95 /*
96  * r_reg -- display a regular file in reverse order by line.
97  */
98 static void
99 r_reg(FILE *fp, const char *fn, enum STYLE style, off_t off, struct stat *sbp)
100 {
101 	struct mapinfo map;
102 	off_t curoff, size, lineend;
103 	int i;
104 
105 	if ((size = sbp->st_size) == 0)
106 		return;
107 
108 	map.start = NULL;
109 	map.mapoff = map.maxoff = size;
110 	map.fd = fileno(fp);
111 
112 	/*
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.
115 	 */
116 	curoff = size - 2;
117 	lineend = size;
118 	while (curoff >= 0) {
119 		if (curoff < map.mapoff ||
120 		    curoff >= map.mapoff + (off_t)map.maplen) {
121 			if (maparound(&map, curoff) != 0) {
122 				ierr(fn);
123 				return;
124 			}
125 		}
126 		for (i = curoff - map.mapoff; i >= 0; i--) {
127 			if (style == RBYTES && --off == 0)
128 				break;
129 			if (map.start[i] == '\n')
130 				break;
131 		}
132 		/* `i' is either the map offset of a '\n', or -1. */
133 		curoff = map.mapoff + i;
134 		if (i < 0)
135 			continue;
136 
137 		/* Print the line and update offsets. */
138 		if (mapprint(&map, curoff + 1, lineend - curoff - 1) != 0) {
139 			ierr(fn);
140 			return;
141 		}
142 		lineend = curoff + 1;
143 		curoff--;
144 
145 		if (style == RLINES)
146 			off--;
147 
148 		if (off == 0 && style != REVERSE) {
149 			/* Avoid printing anything below. */
150 			curoff = 0;
151 			break;
152 		}
153 	}
154 	if (curoff < 0 && mapprint(&map, 0, lineend) != 0) {
155 		ierr(fn);
156 		return;
157 	}
158 	if (map.start != NULL && munmap(map.start, map.maplen))
159 		ierr(fn);
160 }
161 
162 typedef struct bf {
163 	struct bf *next;
164 	struct bf *prev;
165 	int len;
166 	char *l;
167 } BF;
168 
169 /*
170  * r_buf -- display a non-regular file in reverse order by line.
171  *
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
177  * user warned).
178  */
179 static void
180 r_buf(FILE *fp, const char *fn)
181 {
182 	BF *mark, *tl, *tr;
183 	int ch, len, llen;
184 	char *p;
185 	off_t enomem;
186 
187 	tl = NULL;
188 #define	BSZ	(128 * 1024)
189 	for (mark = NULL, enomem = 0; ; ) {
190 		/*
191 		 * Allocate a new block and link it into place in a doubly
192 		 * linked list.  If out of memory, toss the LRU block and
193 		 * keep going.
194 		 */
195 		if (enomem || (tl = malloc(sizeof (BF))) == NULL ||
196 		    (tl->l = malloc(BSZ)) == NULL) {
197 			if (!mark)
198 				err(1, "malloc");
199 			tl = enomem ? tl->next : mark;
200 			enomem += tl->len;
201 		} else if (mark) {
202 			tl->next = mark;
203 			tl->prev = mark->prev;
204 			mark->prev->next = tl;
205 			mark->prev = tl;
206 		} else {
207 			mark = tl;
208 			mark->next = mark->prev = mark;
209 		}
210 
211 		/* Fill the block with input data. */
212 		for (p = tl->l, len = 0;
213 		    len < BSZ && (ch = getc(fp)) != EOF; ++len)
214 			*p++ = ch;
215 
216 		if (ferror(fp)) {
217 			ierr(fn);
218 			return;
219 		}
220 
221 		/*
222 		 * If no input data for this block and we tossed some data,
223 		 * recover it.
224 		 */
225 		if (!len && enomem) {
226 			enomem -= tl->len;
227 			tl = tl->prev;
228 			break;
229 		}
230 
231 		tl->len = len;
232 		if (ch == EOF)
233 			break;
234 	}
235 
236 	if (enomem) {
237 		warnx("warning: %jd bytes discarded", (intmax_t)enomem);
238 		rval = 1;
239 	}
240 
241 	/*
242 	 * Step through the blocks in the reverse order read.  The last char
243 	 * is special, ignore whether newline or not.
244 	 */
245 	for (mark = tl; ; ) {
246 		for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
247 		    --p, ++llen)
248 			if (*p == '\n') {
249 				if (llen) {
250 					WR(p + 1, llen);
251 					llen = 0;
252 				}
253 				if (tl == mark)
254 					continue;
255 				for (tr = tl->next; tr->len; tr = tr->next) {
256 					WR(tr->l, tr->len);
257 					tr->len = 0;
258 					if (tr == mark)
259 						break;
260 				}
261 			}
262 		tl->len = llen;
263 		if ((tl = tl->prev) == mark)
264 			break;
265 	}
266 	tl = tl->next;
267 	if (tl->len) {
268 		WR(tl->l, tl->len);
269 		tl->len = 0;
270 	}
271 	while ((tl = tl->next)->len) {
272 		WR(tl->l, tl->len);
273 		tl->len = 0;
274 	}
275 }
276