Annotation of src/usr.bin/tail/reverse.c, Revision 1.13
1.13 ! millert 1: /* $OpenBSD: reverse.c,v 1.12 2002/02/16 21:27:54 millert 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.
1.13 ! millert 19: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 20: * may be used to endorse or promote products derived from this software
21: * without specific prior written permission.
22: *
23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33: * SUCH DAMAGE.
34: */
35:
36: #ifndef lint
37: #if 0
38: static char sccsid[] = "@(#)reverse.c 8.1 (Berkeley) 6/6/93";
39: #endif
1.13 ! millert 40: static char rcsid[] = "$OpenBSD: reverse.c,v 1.12 2002/02/16 21:27:54 millert Exp $";
1.1 deraadt 41: #endif /* not lint */
42:
43: #include <sys/param.h>
44: #include <sys/stat.h>
45: #include <sys/mman.h>
46:
1.3 millert 47: #include <err.h>
48: #include <errno.h>
1.1 deraadt 49: #include <limits.h>
50: #include <stdio.h>
51: #include <stdlib.h>
52: #include <string.h>
1.3 millert 53: #include <unistd.h>
54:
1.1 deraadt 55: #include "extern.h"
56:
1.12 millert 57: static void r_buf(FILE *);
58: static int r_reg(FILE *, enum STYLE, long, struct stat *);
1.1 deraadt 59:
60: /*
61: * reverse -- display input in reverse order by line.
62: *
63: * There are six separate cases for this -- regular and non-regular
64: * files by bytes, lines or the whole file.
65: *
66: * BYTES display N bytes
67: * REG mmap the file and display the lines
68: * NOREG cyclically read characters into a wrap-around buffer
69: *
70: * LINES display N lines
71: * REG mmap the file and display the lines
72: * NOREG cyclically read lines into a wrap-around array of buffers
73: *
74: * FILE display the entire file
75: * REG mmap the file and display the lines
76: * NOREG cyclically read input into a linked list of buffers
77: */
78: void
79: reverse(fp, style, off, sbp)
80: FILE *fp;
81: enum STYLE style;
82: long off;
83: struct stat *sbp;
84: {
85: if (style != REVERSE && off == 0)
86: return;
87:
1.4 millert 88: if (!S_ISREG(sbp->st_mode) || r_reg(fp, style, off, sbp) != 0)
1.1 deraadt 89: switch(style) {
90: case FBYTES:
91: case RBYTES:
1.7 ericj 92: (void)bytes(fp, off);
1.1 deraadt 93: break;
94: case FLINES:
95: case RLINES:
1.7 ericj 96: (void)lines(fp, off);
1.1 deraadt 97: break;
98: case REVERSE:
99: r_buf(fp);
100: break;
101: }
102: }
103:
104: /*
105: * r_reg -- display a regular file in reverse order by line.
106: */
1.4 millert 107: static int
1.1 deraadt 108: r_reg(fp, style, off, sbp)
109: FILE *fp;
1.11 mpech 110: enum STYLE style;
1.1 deraadt 111: long off;
112: struct stat *sbp;
113: {
1.11 mpech 114: off_t size;
115: int llen;
116: char *p;
1.1 deraadt 117: char *start;
118:
119: if (!(size = sbp->st_size))
1.4 millert 120: return (0);
1.1 deraadt 121:
1.4 millert 122: if (size > SIZE_T_MAX)
123: return (1);
1.1 deraadt 124:
1.6 mickey 125: if ((start = mmap(NULL, (size_t)size, PROT_READ, MAP_PRIVATE,
1.8 art 126: fileno(fp), (off_t)0)) == MAP_FAILED)
1.4 millert 127: return (1);
1.1 deraadt 128: p = start + size - 1;
129:
130: if (style == RBYTES && off < size)
131: size = off;
132:
133: /* Last char is special, ignore whether newline or not. */
134: for (llen = 1; --size; ++llen)
135: if (*--p == '\n') {
136: WR(p + 1, llen);
137: llen = 0;
138: if (style == RLINES && !--off) {
139: ++p;
140: break;
141: }
142: }
143: if (llen)
144: WR(p, llen);
145: if (munmap(start, (size_t)sbp->st_size))
1.4 millert 146: ierr();
147:
148: return (0);
1.1 deraadt 149: }
150:
151: typedef 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: */
168: static void
169: r_buf(fp)
170: FILE *fp;
171: {
1.11 mpech 172: BF *mark, *tr, *tl = NULL;
173: int ch, len, llen;
174: char *p;
1.1 deraadt 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)
1.3 millert 187: err(1, NULL);
1.1 deraadt 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;
1.9 pjanzen 195: } else {
196: mark = tl;
197: mark->next = mark->prev = mark;
198: }
1.5 mickey 199:
200: if (!enomem)
201: tl->len = 0;
1.1 deraadt 202:
203: /* Fill the block with input data. */
204: for (p = tl->l, len = 0;
205: len < BSZ && (ch = getc(fp)) != EOF; ++len)
206: *p++ = ch;
207:
208: /*
209: * If no input data for this block and we tossed some data,
210: * recover it.
211: */
212: if (!len) {
213: if (enomem)
214: enomem -= tl->len;
215: tl = tl->prev;
216: break;
217: }
218:
219: tl->len = len;
220: if (ch == EOF)
221: break;
222: }
223:
224: if (enomem) {
225: (void)fprintf(stderr,
1.10 pvalchev 226: "tail: warning: %lld bytes discarded\n", (long long)enomem);
1.1 deraadt 227: rval = 1;
228: }
229:
230: /*
231: * Step through the blocks in the reverse order read. The last char
232: * is special, ignore whether newline or not.
233: */
234: for (mark = tl;;) {
235: for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
236: --p, ++llen)
237: if (*p == '\n') {
238: if (llen) {
239: WR(p + 1, llen);
240: llen = 0;
241: }
242: if (tl == mark)
243: continue;
244: for (tr = tl->next; tr->len; tr = tr->next) {
245: WR(tr->l, tr->len);
246: tr->len = 0;
247: if (tr == mark)
248: break;
249: }
250: }
251: tl->len = llen;
252: if ((tl = tl->prev) == mark)
253: break;
254: }
255: tl = tl->next;
256: if (tl->len) {
257: WR(tl->l, tl->len);
258: tl->len = 0;
259: }
260: while ((tl = tl->next)->len) {
261: WR(tl->l, tl->len);
262: tl->len = 0;
263: }
264: }