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