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