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