Home | History | Annotate | Line # | Download | only in tail
forward.c revision 1.3
      1  1.1    glass /*-
      2  1.1    glass  * Copyright (c) 1991 The Regents of the University of California.
      3  1.1    glass  * 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.2  mycroft /*static char sccsid[] = "from: @(#)forward.c	5.4 (Berkeley) 2/12/92";*/
     39  1.3      cgd static char rcsid[] = "$Id: forward.c,v 1.3 1994/03/28 02:22:29 cgd Exp $";
     40  1.1    glass #endif /* not lint */
     41  1.1    glass 
     42  1.1    glass #include <sys/types.h>
     43  1.1    glass #include <sys/stat.h>
     44  1.1    glass #include <sys/time.h>
     45  1.1    glass #include <sys/mman.h>
     46  1.1    glass #include <fcntl.h>
     47  1.1    glass #include <errno.h>
     48  1.1    glass #include <unistd.h>
     49  1.1    glass #include <stdio.h>
     50  1.1    glass #include <stdlib.h>
     51  1.1    glass #include <string.h>
     52  1.1    glass #include "extern.h"
     53  1.1    glass 
     54  1.1    glass static void rlines __P((FILE *, long, struct stat *));
     55  1.1    glass 
     56  1.1    glass /*
     57  1.1    glass  * forward -- display the file, from an offset, forward.
     58  1.1    glass  *
     59  1.1    glass  * There are eight separate cases for this -- regular and non-regular
     60  1.1    glass  * files, by bytes or lines and from the beginning or end of the file.
     61  1.1    glass  *
     62  1.1    glass  * FBYTES	byte offset from the beginning of the file
     63  1.1    glass  *	REG	seek
     64  1.1    glass  *	NOREG	read, counting bytes
     65  1.1    glass  *
     66  1.1    glass  * FLINES	line offset from the beginning of the file
     67  1.1    glass  *	REG	read, counting lines
     68  1.1    glass  *	NOREG	read, counting lines
     69  1.1    glass  *
     70  1.1    glass  * RBYTES	byte offset from the end of the file
     71  1.1    glass  *	REG	seek
     72  1.1    glass  *	NOREG	cyclically read characters into a wrap-around buffer
     73  1.1    glass  *
     74  1.1    glass  * RLINES
     75  1.1    glass  *	REG	mmap the file and step back until reach the correct offset.
     76  1.1    glass  *	NOREG	cyclically read lines into a wrap-around array of buffers
     77  1.1    glass  */
     78  1.1    glass void
     79  1.1    glass forward(fp, style, off, sbp)
     80  1.1    glass 	FILE *fp;
     81  1.1    glass 	enum STYLE style;
     82  1.1    glass 	long off;
     83  1.1    glass 	struct stat *sbp;
     84  1.1    glass {
     85  1.1    glass 	register int ch;
     86  1.1    glass 	struct timeval second;
     87  1.1    glass 	fd_set zero;
     88  1.1    glass 
     89  1.1    glass 	switch(style) {
     90  1.1    glass 	case FBYTES:
     91  1.1    glass 		if (off == 0)
     92  1.1    glass 			break;
     93  1.1    glass 		if (S_ISREG(sbp->st_mode)) {
     94  1.1    glass 			if (sbp->st_size < off)
     95  1.1    glass 				off = sbp->st_size;
     96  1.1    glass 			if (fseek(fp, off, SEEK_SET) == -1)
     97  1.1    glass 				ierr();
     98  1.1    glass 		} else while (off--)
     99  1.1    glass 			if ((ch = getc(fp)) == EOF) {
    100  1.1    glass 				if (ferror(fp))
    101  1.1    glass 					ierr();
    102  1.1    glass 					break;
    103  1.1    glass 				}
    104  1.1    glass 		break;
    105  1.1    glass 	case FLINES:
    106  1.1    glass 		if (off == 0)
    107  1.1    glass 			break;
    108  1.1    glass 		for (;;) {
    109  1.1    glass 			if ((ch = getc(fp)) == EOF) {
    110  1.1    glass 				if (ferror(fp))
    111  1.1    glass 					ierr();
    112  1.1    glass 				break;
    113  1.1    glass 			}
    114  1.1    glass 			if (ch == '\n' && !--off)
    115  1.1    glass 				break;
    116  1.1    glass 		}
    117  1.1    glass 		break;
    118  1.1    glass 	case RBYTES:
    119  1.1    glass 		if (S_ISREG(sbp->st_mode)) {
    120  1.1    glass 			if (sbp->st_size >= off &&
    121  1.1    glass 			    fseek(fp, -off, SEEK_END) == -1)
    122  1.1    glass 				ierr();
    123  1.1    glass 		} else if (off == 0) {
    124  1.1    glass 			while (getc(fp) != EOF);
    125  1.1    glass 			if (ferror(fp))
    126  1.1    glass 				ierr();
    127  1.1    glass 		} else
    128  1.1    glass 			bytes(fp, off);
    129  1.1    glass 		break;
    130  1.1    glass 	case RLINES:
    131  1.1    glass 		if (S_ISREG(sbp->st_mode))
    132  1.1    glass 			if (!off) {
    133  1.1    glass 				if (fseek(fp, 0L, SEEK_END) == -1)
    134  1.1    glass 					ierr();
    135  1.1    glass 			} else
    136  1.1    glass 				rlines(fp, off, sbp);
    137  1.1    glass 		else if (off == 0) {
    138  1.1    glass 			while (getc(fp) != EOF);
    139  1.1    glass 			if (ferror(fp))
    140  1.1    glass 				ierr();
    141  1.1    glass 		} else
    142  1.1    glass 			lines(fp, off);
    143  1.1    glass 		break;
    144  1.1    glass 	}
    145  1.1    glass 
    146  1.1    glass 	/*
    147  1.1    glass 	 * We pause for one second after displaying any data that has
    148  1.1    glass 	 * accumulated since we read the file.
    149  1.1    glass 	 */
    150  1.1    glass 	if (fflag) {
    151  1.1    glass 		FD_ZERO(&zero);
    152  1.1    glass 		second.tv_sec = 1;
    153  1.1    glass 		second.tv_usec = 0;
    154  1.1    glass 	}
    155  1.1    glass 
    156  1.1    glass 	for (;;) {
    157  1.1    glass 		while ((ch = getc(fp)) != EOF)
    158  1.1    glass 			if (putchar(ch) == EOF)
    159  1.1    glass 				oerr();
    160  1.1    glass 		if (ferror(fp))
    161  1.1    glass 			ierr();
    162  1.1    glass 		(void)fflush(stdout);
    163  1.1    glass 		if (!fflag)
    164  1.1    glass 			break;
    165  1.1    glass 		/* Sleep is eight system calls.  Do it fast. */
    166  1.1    glass 		if (select(0, &zero, &zero, &zero, &second) == -1)
    167  1.1    glass 			err("select: %s", strerror(errno));
    168  1.1    glass 		clearerr(fp);
    169  1.1    glass 	}
    170  1.1    glass }
    171  1.1    glass 
    172  1.1    glass /*
    173  1.1    glass  * rlines -- display the last offset lines of the file.
    174  1.1    glass  */
    175  1.1    glass static void
    176  1.1    glass rlines(fp, off, sbp)
    177  1.1    glass 	FILE *fp;
    178  1.1    glass 	long off;
    179  1.1    glass 	struct stat *sbp;
    180  1.1    glass {
    181  1.1    glass 	register off_t size;
    182  1.1    glass 	register char *p;
    183  1.1    glass 
    184  1.1    glass 	if (!(size = sbp->st_size))
    185  1.1    glass 		return;
    186  1.1    glass 
    187  1.1    glass 	if ((p = mmap(NULL,
    188  1.3      cgd 	    size, PROT_READ, 0, fileno(fp), (off_t)0)) == (caddr_t)-1)
    189  1.1    glass 		err("%s", strerror(errno));
    190  1.1    glass 
    191  1.1    glass 	/* Last char is special, ignore whether newline or not. */
    192  1.1    glass 	for (p += size - 1; --size;)
    193  1.1    glass 		if (*--p == '\n' && !--off) {
    194  1.1    glass 			++p;
    195  1.1    glass 			break;
    196  1.1    glass 		}
    197  1.1    glass 
    198  1.1    glass 	/* Set the file pointer to reflect the length displayed. */
    199  1.1    glass 	size = sbp->st_size - size;
    200  1.1    glass 	WR(p, size);
    201  1.1    glass 	if (fseek(fp, sbp->st_size, SEEK_SET) == -1)
    202  1.1    glass 		ierr();
    203  1.1    glass }
    204