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