Annotation of src/usr.bin/tail/reverse.c, Revision 1.20
1.20 ! tobias 1: /* $OpenBSD: reverse.c,v 1.19 2009/10/27 23:59:44 deraadt 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: #include <sys/stat.h>
37:
1.3 millert 38: #include <err.h>
1.1 deraadt 39: #include <stdio.h>
40: #include <stdlib.h>
1.3 millert 41: #include <unistd.h>
42:
1.1 deraadt 43: #include "extern.h"
44:
1.12 millert 45: static void r_buf(FILE *);
1.15 otto 46: static int r_reg(FILE *, enum STYLE, off_t, struct stat *);
1.1 deraadt 47:
1.14 henning 48: #define COPYCHAR(fp, ch) \
49: do { \
50: if ((ch = getc(fp)) == EOF) { \
51: ierr(); \
52: return (0); \
53: } \
54: if (putchar(ch) == EOF) { \
55: oerr(); \
56: return (0); \
57: } \
58: } while (0)
59:
1.1 deraadt 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
1.14 henning 67: * REG reverse scan and display the lines
1.1 deraadt 68: * NOREG cyclically read characters into a wrap-around buffer
69: *
70: * LINES display N lines
1.14 henning 71: * REG reverse scan and display the lines
1.1 deraadt 72: * NOREG cyclically read lines into a wrap-around array of buffers
73: *
74: * FILE display the entire file
1.14 henning 75: * REG reverse scan and display the lines
1.1 deraadt 76: * NOREG cyclically read input into a linked list of buffers
77: */
78: void
1.17 kjell 79: reverse(FILE *fp, enum STYLE style, off_t off, struct stat *sbp)
1.1 deraadt 80: {
81: if (style != REVERSE && off == 0)
82: return;
83:
1.4 millert 84: if (!S_ISREG(sbp->st_mode) || r_reg(fp, style, off, sbp) != 0)
1.1 deraadt 85: switch(style) {
86: case FBYTES:
87: case RBYTES:
1.7 ericj 88: (void)bytes(fp, off);
1.1 deraadt 89: break;
90: case FLINES:
91: case RLINES:
1.7 ericj 92: (void)lines(fp, off);
1.1 deraadt 93: break;
94: case REVERSE:
95: r_buf(fp);
96: break;
97: }
98: }
99:
100: /*
101: * r_reg -- display a regular file in reverse order by line.
102: */
1.4 millert 103: static int
1.15 otto 104: r_reg(FILE *fp, enum STYLE style, off_t off, struct stat *sbp)
1.1 deraadt 105: {
1.14 henning 106: off_t start, pos, end;
107: int ch;
1.1 deraadt 108:
1.14 henning 109: end = sbp->st_size;
110: if (end == 0)
1.4 millert 111: return (0);
1.1 deraadt 112:
1.14 henning 113: /* Position before char, ignore last char whether newline or not */
114: pos = end-2;
115: ch = EOF;
116: start = 0;
117:
118: if (style == RBYTES && off < end)
119: start = end - off;
120:
121: for (; pos >= start; pos--) {
122: /* A seek per char isn't a problem with a smart stdio */
123: if (fseeko(fp, pos, SEEK_SET) != 0) {
124: ierr();
125: return (0);
126: }
127: if ((ch = getc(fp)) == '\n') {
128: while (--end > pos)
129: COPYCHAR(fp, ch);
130: end++;
131: if (style == RLINES && --off == 0)
1.1 deraadt 132: break;
133: }
1.14 henning 134: else if (ch == EOF) {
135: ierr();
136: return (0);
137: }
138: }
139: if (pos < start) {
140: if (ch != EOF && ungetc(ch, fp) == EOF) {
141: ierr();
142: return (0);
143: }
144: while (--end >= start)
145: COPYCHAR(fp, ch);
146: }
1.4 millert 147: return (0);
1.1 deraadt 148: }
149:
1.20 ! tobias 150: #define BSZ (128 * 1024)
! 151: struct bf {
1.1 deraadt 152: struct bf *next;
153: struct bf *prev;
1.16 kjell 154: size_t len;
1.20 ! tobias 155: char l[BSZ];
! 156: };
1.1 deraadt 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
1.17 kjell 169: r_buf(FILE *fp)
1.1 deraadt 170: {
1.20 ! tobias 171: struct bf *mark, *tr, *tl = NULL;
1.16 kjell 172: int ch;
173: size_t len, llen;
1.11 mpech 174: char *p;
1.1 deraadt 175: off_t enomem;
176:
177: for (mark = NULL, enomem = 0;;) {
178: /*
179: * Allocate a new block and link it into place in a doubly
180: * linked list. If out of memory, toss the LRU block and
181: * keep going.
182: */
1.20 ! tobias 183: if (enomem || (tl = malloc(sizeof(*tl))) == NULL) {
1.1 deraadt 184: if (!mark)
1.3 millert 185: err(1, NULL);
1.1 deraadt 186: tl = enomem ? tl->next : mark;
187: enomem += tl->len;
188: } else if (mark) {
189: tl->next = mark;
190: tl->prev = mark->prev;
191: mark->prev->next = tl;
192: mark->prev = tl;
1.9 pjanzen 193: } else {
194: mark = tl;
195: mark->next = mark->prev = mark;
196: }
1.5 mickey 197:
198: if (!enomem)
199: tl->len = 0;
1.1 deraadt 200:
201: /* Fill the block with input data. */
202: for (p = tl->l, len = 0;
203: len < BSZ && (ch = getc(fp)) != EOF; ++len)
204: *p++ = ch;
205:
206: /*
207: * If no input data for this block and we tossed some data,
208: * recover it.
209: */
210: if (!len) {
211: if (enomem)
212: enomem -= tl->len;
213: tl = tl->prev;
214: break;
215: }
216:
217: tl->len = len;
218: if (ch == EOF)
219: break;
220: }
221:
222: if (enomem) {
223: (void)fprintf(stderr,
1.10 pvalchev 224: "tail: warning: %lld bytes discarded\n", (long long)enomem);
1.1 deraadt 225: rval = 1;
226: }
227:
228: /*
229: * Step through the blocks in the reverse order read. The last char
230: * is special, ignore whether newline or not.
231: */
232: for (mark = tl;;) {
233: for (p = tl->l + (len = tl->len) - 1, llen = 0; len--;
234: --p, ++llen)
235: if (*p == '\n') {
236: if (llen) {
237: WR(p + 1, llen);
238: llen = 0;
239: }
240: if (tl == mark)
241: continue;
242: for (tr = tl->next; tr->len; tr = tr->next) {
243: WR(tr->l, tr->len);
244: tr->len = 0;
245: if (tr == mark)
246: break;
247: }
248: }
249: tl->len = llen;
250: if ((tl = tl->prev) == mark)
251: break;
252: }
253: tl = tl->next;
254: if (tl->len) {
255: WR(tl->l, tl->len);
256: tl->len = 0;
257: }
258: while ((tl = tl->next)->len) {
259: WR(tl->l, tl->len);
260: tl->len = 0;
1.20 ! tobias 261: }
! 262:
! 263: tl->prev->next = NULL;
! 264: while (tl != NULL) {
! 265: tr = tl->next;
! 266: free(tl);
! 267: tl = tr;
1.1 deraadt 268: }
269: }