Home | History | Annotate | Line # | Download | only in tail
forward.c revision 1.7
      1  1.7  ghudson /*	$NetBSD: forward.c,v 1.7 1996/02/13 16:49:10 ghudson 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.1    glass  * 3. All advertising materials mentioning features or use of this software
     19  1.1    glass  *    must display the following acknowledgement:
     20  1.1    glass  *	This product includes software developed by the University of
     21  1.1    glass  *	California, Berkeley and its contributors.
     22  1.1    glass  * 4. Neither the name of the University nor the names of its contributors
     23  1.1    glass  *    may be used to endorse or promote products derived from this software
     24  1.1    glass  *    without specific prior written permission.
     25  1.1    glass  *
     26  1.1    glass  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1    glass  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1    glass  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1    glass  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1    glass  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1    glass  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1    glass  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1    glass  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1    glass  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1    glass  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1    glass  * SUCH DAMAGE.
     37  1.1    glass  */
     38  1.1    glass 
     39  1.1    glass #ifndef lint
     40  1.6      jtc #if 0
     41  1.6      jtc static char sccsid[] = "@(#)forward.c	8.1 (Berkeley) 6/6/93";
     42  1.6      jtc #endif
     43  1.7  ghudson static char rcsid[] = "$NetBSD: forward.c,v 1.7 1996/02/13 16:49:10 ghudson Exp $";
     44  1.1    glass #endif /* not lint */
     45  1.1    glass 
     46  1.1    glass #include <sys/types.h>
     47  1.1    glass #include <sys/stat.h>
     48  1.1    glass #include <sys/time.h>
     49  1.1    glass #include <sys/mman.h>
     50  1.6      jtc 
     51  1.6      jtc #include <limits.h>
     52  1.1    glass #include <fcntl.h>
     53  1.1    glass #include <errno.h>
     54  1.1    glass #include <unistd.h>
     55  1.1    glass #include <stdio.h>
     56  1.1    glass #include <stdlib.h>
     57  1.1    glass #include <string.h>
     58  1.1    glass #include "extern.h"
     59  1.1    glass 
     60  1.1    glass static void rlines __P((FILE *, long, struct stat *));
     61  1.1    glass 
     62  1.1    glass /*
     63  1.1    glass  * forward -- display the file, from an offset, forward.
     64  1.1    glass  *
     65  1.1    glass  * There are eight separate cases for this -- regular and non-regular
     66  1.1    glass  * files, by bytes or lines and from the beginning or end of the file.
     67  1.1    glass  *
     68  1.1    glass  * FBYTES	byte offset from the beginning of the file
     69  1.1    glass  *	REG	seek
     70  1.1    glass  *	NOREG	read, counting bytes
     71  1.1    glass  *
     72  1.1    glass  * FLINES	line offset from the beginning of the file
     73  1.1    glass  *	REG	read, counting lines
     74  1.1    glass  *	NOREG	read, counting lines
     75  1.1    glass  *
     76  1.1    glass  * RBYTES	byte offset from the end of the file
     77  1.1    glass  *	REG	seek
     78  1.1    glass  *	NOREG	cyclically read characters into a wrap-around buffer
     79  1.1    glass  *
     80  1.1    glass  * RLINES
     81  1.1    glass  *	REG	mmap the file and step back until reach the correct offset.
     82  1.1    glass  *	NOREG	cyclically read lines into a wrap-around array of buffers
     83  1.1    glass  */
     84  1.1    glass void
     85  1.1    glass forward(fp, style, off, sbp)
     86  1.1    glass 	FILE *fp;
     87  1.1    glass 	enum STYLE style;
     88  1.1    glass 	long off;
     89  1.1    glass 	struct stat *sbp;
     90  1.1    glass {
     91  1.1    glass 	register int ch;
     92  1.1    glass 	struct timeval second;
     93  1.1    glass 
     94  1.1    glass 	switch(style) {
     95  1.1    glass 	case FBYTES:
     96  1.1    glass 		if (off == 0)
     97  1.1    glass 			break;
     98  1.1    glass 		if (S_ISREG(sbp->st_mode)) {
     99  1.1    glass 			if (sbp->st_size < off)
    100  1.1    glass 				off = sbp->st_size;
    101  1.6      jtc 			if (fseek(fp, off, SEEK_SET) == -1) {
    102  1.1    glass 				ierr();
    103  1.6      jtc 				return;
    104  1.6      jtc 			}
    105  1.1    glass 		} else while (off--)
    106  1.1    glass 			if ((ch = getc(fp)) == EOF) {
    107  1.6      jtc 				if (ferror(fp)) {
    108  1.1    glass 					ierr();
    109  1.6      jtc 					return;
    110  1.1    glass 				}
    111  1.6      jtc 				break;
    112  1.6      jtc 			}
    113  1.1    glass 		break;
    114  1.1    glass 	case FLINES:
    115  1.1    glass 		if (off == 0)
    116  1.1    glass 			break;
    117  1.1    glass 		for (;;) {
    118  1.1    glass 			if ((ch = getc(fp)) == EOF) {
    119  1.6      jtc 				if (ferror(fp)) {
    120  1.1    glass 					ierr();
    121  1.6      jtc 					return;
    122  1.6      jtc 				}
    123  1.1    glass 				break;
    124  1.1    glass 			}
    125  1.1    glass 			if (ch == '\n' && !--off)
    126  1.1    glass 				break;
    127  1.1    glass 		}
    128  1.1    glass 		break;
    129  1.1    glass 	case RBYTES:
    130  1.1    glass 		if (S_ISREG(sbp->st_mode)) {
    131  1.1    glass 			if (sbp->st_size >= off &&
    132  1.6      jtc 			    fseek(fp, -off, SEEK_END) == -1) {
    133  1.1    glass 				ierr();
    134  1.6      jtc 				return;
    135  1.6      jtc 			}
    136  1.1    glass 		} else if (off == 0) {
    137  1.1    glass 			while (getc(fp) != EOF);
    138  1.6      jtc 			if (ferror(fp)) {
    139  1.1    glass 				ierr();
    140  1.6      jtc 				return;
    141  1.6      jtc 			}
    142  1.1    glass 		} else
    143  1.1    glass 			bytes(fp, off);
    144  1.1    glass 		break;
    145  1.1    glass 	case RLINES:
    146  1.1    glass 		if (S_ISREG(sbp->st_mode))
    147  1.1    glass 			if (!off) {
    148  1.6      jtc 				if (fseek(fp, 0L, SEEK_END) == -1) {
    149  1.1    glass 					ierr();
    150  1.6      jtc 					return;
    151  1.6      jtc 				}
    152  1.1    glass 			} else
    153  1.1    glass 				rlines(fp, off, sbp);
    154  1.1    glass 		else if (off == 0) {
    155  1.1    glass 			while (getc(fp) != EOF);
    156  1.6      jtc 			if (ferror(fp)) {
    157  1.1    glass 				ierr();
    158  1.6      jtc 				return;
    159  1.6      jtc 			}
    160  1.1    glass 		} else
    161  1.1    glass 			lines(fp, off);
    162  1.1    glass 		break;
    163  1.1    glass 	}
    164  1.1    glass 
    165  1.1    glass 	for (;;) {
    166  1.1    glass 		while ((ch = getc(fp)) != EOF)
    167  1.1    glass 			if (putchar(ch) == EOF)
    168  1.1    glass 				oerr();
    169  1.6      jtc 		if (ferror(fp)) {
    170  1.1    glass 			ierr();
    171  1.6      jtc 			return;
    172  1.6      jtc 		}
    173  1.1    glass 		(void)fflush(stdout);
    174  1.1    glass 		if (!fflag)
    175  1.1    glass 			break;
    176  1.7  ghudson 		/*
    177  1.7  ghudson 		 * We pause for one second after displaying any data that has
    178  1.7  ghudson 		 * accumulated since we read the file.  Since sleep(3) takes
    179  1.7  ghudson 		 * eight system calls, use select() instead.
    180  1.7  ghudson 		 */
    181  1.7  ghudson 		second.tv_sec = 1;
    182  1.7  ghudson 		second.tv_usec = 0;
    183  1.7  ghudson 		if (select(0, NULL, NULL, NULL, &second) == -1)
    184  1.6      jtc 			err(1, "select: %s", strerror(errno));
    185  1.1    glass 		clearerr(fp);
    186  1.1    glass 	}
    187  1.1    glass }
    188  1.1    glass 
    189  1.1    glass /*
    190  1.1    glass  * rlines -- display the last offset lines of the file.
    191  1.1    glass  */
    192  1.1    glass static void
    193  1.1    glass rlines(fp, off, sbp)
    194  1.1    glass 	FILE *fp;
    195  1.1    glass 	long off;
    196  1.1    glass 	struct stat *sbp;
    197  1.1    glass {
    198  1.6      jtc 	register off_t size;
    199  1.1    glass 	register char *p;
    200  1.5      jtc 	char *start;
    201  1.1    glass 
    202  1.1    glass 	if (!(size = sbp->st_size))
    203  1.1    glass 		return;
    204  1.1    glass 
    205  1.6      jtc 	if (size > SIZE_T_MAX) {
    206  1.6      jtc 		err(0, "%s: %s", fname, strerror(EFBIG));
    207  1.6      jtc 		return;
    208  1.6      jtc 	}
    209  1.6      jtc 
    210  1.6      jtc 	if ((start = mmap(NULL, (size_t)size,
    211  1.6      jtc 	    PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1) {
    212  1.6      jtc 		err(0, "%s: %s", fname, strerror(EFBIG));
    213  1.6      jtc 		return;
    214  1.6      jtc 	}
    215  1.1    glass 
    216  1.1    glass 	/* Last char is special, ignore whether newline or not. */
    217  1.5      jtc 	for (p = start + size - 1; --size;)
    218  1.1    glass 		if (*--p == '\n' && !--off) {
    219  1.1    glass 			++p;
    220  1.1    glass 			break;
    221  1.1    glass 		}
    222  1.1    glass 
    223  1.1    glass 	/* Set the file pointer to reflect the length displayed. */
    224  1.1    glass 	size = sbp->st_size - size;
    225  1.1    glass 	WR(p, size);
    226  1.6      jtc 	if (fseek(fp, (long)sbp->st_size, SEEK_SET) == -1) {
    227  1.1    glass 		ierr();
    228  1.6      jtc 		return;
    229  1.6      jtc 	}
    230  1.5      jtc 	if (munmap(start, (size_t)sbp->st_size)) {
    231  1.6      jtc 		err(0, "%s: %s", fname, strerror(errno));
    232  1.6      jtc 		return;
    233  1.5      jtc 	}
    234  1.1    glass }
    235