Home | History | Annotate | Line # | Download | only in tail
read.c revision 1.9
      1 /*	$NetBSD: read.c,v 1.9 2003/08/07 11:16:02 agc 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[] = "@(#)read.c	8.1 (Berkeley) 6/6/93";
     39 #endif
     40 __RCSID("$NetBSD: read.c,v 1.9 2003/08/07 11:16:02 agc Exp $");
     41 #endif /* not lint */
     42 
     43 #include <sys/types.h>
     44 #include <sys/stat.h>
     45 #include <fcntl.h>
     46 #include <errno.h>
     47 #include <unistd.h>
     48 #include <stdio.h>
     49 #include <stdlib.h>
     50 #include <string.h>
     51 #include "extern.h"
     52 
     53 /*
     54  * bytes -- read bytes to an offset from the end and display.
     55  *
     56  * This is the function that reads to a byte offset from the end of the input,
     57  * storing the data in a wrap-around buffer which is then displayed.  If the
     58  * rflag is set, the data is displayed in lines in reverse order, and this
     59  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
     60  * it is displayed from the character closest to the beginning of the input to
     61  * the end.
     62  *
     63  * Non-zero return means than a (non-fatal) error occurred.
     64  */
     65 int
     66 bytes(FILE *fp, __off_t off)
     67 {
     68 	int ch, len, tlen;
     69 	char *ep, *p, *t;
     70 	int wrap;
     71 	char *sp;
     72 
     73 	if ((sp = p = malloc(off)) == NULL)
     74 		err(1, "%s", strerror(errno));
     75 
     76 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
     77 		*p = ch;
     78 		if (++p == ep) {
     79 			wrap = 1;
     80 			p = sp;
     81 		}
     82 	}
     83 	if (ferror(fp)) {
     84 		ierr();
     85 		return (1);
     86 	}
     87 
     88 	if (rflag) {
     89 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
     90 			if (*t == '\n' && len) {
     91 				WR(t + 1, len);
     92 				len = 0;
     93 		}
     94 		if (wrap) {
     95 			tlen = len;
     96 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
     97 				if (*t == '\n') {
     98 					if (len) {
     99 						WR(t + 1, len);
    100 						len = 0;
    101 					}
    102 					if (tlen) {
    103 						WR(sp, tlen);
    104 						tlen = 0;
    105 					}
    106 				}
    107 			if (len)
    108 				WR(t + 1, len);
    109 			if (tlen)
    110 				WR(sp, tlen);
    111 		}
    112 	} else {
    113 		if (wrap && (len = ep - p))
    114 			WR(p, len);
    115 		if ((len = p - sp) == 0)
    116 			WR(sp, len);
    117 	}
    118 	return (0);
    119 }
    120 
    121 /*
    122  * lines -- read lines to an offset from the end and display.
    123  *
    124  * This is the function that reads to a line offset from the end of the input,
    125  * storing the data in an array of buffers which is then displayed.  If the
    126  * rflag is set, the data is displayed in lines in reverse order, and this
    127  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
    128  * it is displayed from the line closest to the beginning of the input to
    129  * the end.
    130  *
    131  * Non-zero return means than a (non-fatal) error occurred.
    132  */
    133 int
    134 lines(FILE *fp, __off_t off)
    135 {
    136 	struct {
    137 		u_int blen;
    138 		u_int len;
    139 		char *l;
    140 	} *lines;
    141 	int ch;
    142 	char *p;
    143 	int blen, cnt, recno, wrap;
    144 	char *sp;
    145 
    146 	p = NULL;
    147 	if ((lines = malloc(off * sizeof(*lines))) == NULL)
    148 		err(1, "%s", strerror(errno));
    149 
    150 	memset(lines, 0, sizeof(*lines) * off);
    151 
    152 	sp = NULL;
    153 	blen = cnt = recno = wrap = 0;
    154 
    155 	while ((ch = getc(fp)) != EOF) {
    156 		if (++cnt > blen) {
    157 			if ((sp = realloc(sp, blen += 1024)) == NULL)
    158 				err(1, "%s", strerror(errno));
    159 			p = sp + cnt - 1;
    160 		}
    161 		*p++ = ch;
    162 		if (ch == '\n') {
    163 			if (lines[recno].blen < cnt) {
    164 				lines[recno].blen = cnt + 256;
    165 				if ((lines[recno].l = realloc(lines[recno].l,
    166 				    lines[recno].blen)) == NULL)
    167 					err(1, "%s", strerror(errno));
    168 			}
    169 			memmove(lines[recno].l, sp, lines[recno].len = cnt);
    170 			cnt = 0;
    171 			p = sp;
    172 			if (++recno == off) {
    173 				wrap = 1;
    174 				recno = 0;
    175 			}
    176 		}
    177 	}
    178 	if (ferror(fp)) {
    179 		ierr();
    180 		return (1);
    181 	}
    182 	if (cnt) {
    183 		lines[recno].l = sp;
    184 		lines[recno].len = cnt;
    185 		if (++recno == off) {
    186 			wrap = 1;
    187 			recno = 0;
    188 		}
    189 	}
    190 
    191 	if (rflag) {
    192 		for (cnt = recno - 1; cnt >= 0; --cnt)
    193 			WR(lines[cnt].l, lines[cnt].len);
    194 		if (wrap)
    195 			for (cnt = off - 1; cnt >= recno; --cnt)
    196 				WR(lines[cnt].l, lines[cnt].len);
    197 	} else {
    198 		if (wrap)
    199 			for (cnt = recno; cnt < off; ++cnt)
    200 				WR(lines[cnt].l, lines[cnt].len);
    201 		for (cnt = 0; cnt < recno; ++cnt)
    202 			WR(lines[cnt].l, lines[cnt].len);
    203 	}
    204 	return (0);
    205 }
    206