xref: /illumos-gate/usr/src/cmd/tail/read.c (revision 209e49b2)
1*209e49b2SChris Love /*
2*209e49b2SChris Love  * Copyright (c) 1991, 1993
3*209e49b2SChris Love  *	The Regents of the University of California.  All rights reserved.
4*209e49b2SChris Love  *
5*209e49b2SChris Love  * This code is derived from software contributed to Berkeley by
6*209e49b2SChris Love  * Edward Sze-Tyan Wang.
7*209e49b2SChris Love  *
8*209e49b2SChris Love  * Redistribution and use in source and binary forms, with or without
9*209e49b2SChris Love  * modification, are permitted provided that the following conditions
10*209e49b2SChris Love  * are met:
11*209e49b2SChris Love  * 1. Redistributions of source code must retain the above copyright
12*209e49b2SChris Love  *    notice, this list of conditions and the following disclaimer.
13*209e49b2SChris Love  * 2. Redistributions in binary form must reproduce the above copyright
14*209e49b2SChris Love  *    notice, this list of conditions and the following disclaimer in the
15*209e49b2SChris Love  *    documentation and/or other materials provided with the distribution.
16*209e49b2SChris Love  * 4. Neither the name of the University nor the names of its contributors
17*209e49b2SChris Love  *    may be used to endorse or promote products derived from this software
18*209e49b2SChris Love  *    without specific prior written permission.
19*209e49b2SChris Love  *
20*209e49b2SChris Love  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21*209e49b2SChris Love  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*209e49b2SChris Love  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*209e49b2SChris Love  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24*209e49b2SChris Love  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*209e49b2SChris Love  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*209e49b2SChris Love  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*209e49b2SChris Love  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*209e49b2SChris Love  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*209e49b2SChris Love  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*209e49b2SChris Love  * SUCH DAMAGE.
31*209e49b2SChris Love  */
32*209e49b2SChris Love 
33*209e49b2SChris Love #include <sys/types.h>
34*209e49b2SChris Love #include <sys/stat.h>
35*209e49b2SChris Love 
36*209e49b2SChris Love #include <err.h>
37*209e49b2SChris Love #include <errno.h>
38*209e49b2SChris Love #include <fcntl.h>
39*209e49b2SChris Love #include <stdio.h>
40*209e49b2SChris Love #include <stdlib.h>
41*209e49b2SChris Love #include <string.h>
42*209e49b2SChris Love #include <strings.h>
43*209e49b2SChris Love #include <unistd.h>
44*209e49b2SChris Love 
45*209e49b2SChris Love #include "extern.h"
46*209e49b2SChris Love 
47*209e49b2SChris Love /*
48*209e49b2SChris Love  * bytes -- read bytes to an offset from the end and display.
49*209e49b2SChris Love  *
50*209e49b2SChris Love  * This is the function that reads to a byte offset from the end of the input,
51*209e49b2SChris Love  * storing the data in a wrap-around buffer which is then displayed.  If the
52*209e49b2SChris Love  * rflag is set, the data is displayed in lines in reverse order, and this
53*209e49b2SChris Love  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
54*209e49b2SChris Love  * it is displayed from the character closest to the beginning of the input to
55*209e49b2SChris Love  * the end.
56*209e49b2SChris Love  */
57*209e49b2SChris Love int
bytes(FILE * fp,const char * fn,off_t off)58*209e49b2SChris Love bytes(FILE *fp, const char *fn, off_t off)
59*209e49b2SChris Love {
60*209e49b2SChris Love 	int ch, len, tlen;
61*209e49b2SChris Love 	char *ep, *p, *t;
62*209e49b2SChris Love 	int wrap;
63*209e49b2SChris Love 	char *sp;
64*209e49b2SChris Love 
65*209e49b2SChris Love 	if ((sp = p = malloc(off)) == NULL)
66*209e49b2SChris Love 		err(1, "malloc");
67*209e49b2SChris Love 
68*209e49b2SChris Love 	for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF; ) {
69*209e49b2SChris Love 		*p = ch;
70*209e49b2SChris Love 		if (++p == ep) {
71*209e49b2SChris Love 			wrap = 1;
72*209e49b2SChris Love 			p = sp;
73*209e49b2SChris Love 		}
74*209e49b2SChris Love 	}
75*209e49b2SChris Love 	if (ferror(fp)) {
76*209e49b2SChris Love 		ierr(fn);
77*209e49b2SChris Love 		free(sp);
78*209e49b2SChris Love 		return (1);
79*209e49b2SChris Love 	}
80*209e49b2SChris Love 
81*209e49b2SChris Love 	if (rflag) {
82*209e49b2SChris Love 		for (t = p - 1, len = 0; t >= sp; --t, ++len)
83*209e49b2SChris Love 			if (*t == '\n' && len) {
84*209e49b2SChris Love 				WR(t + 1, len);
85*209e49b2SChris Love 				len = 0;
86*209e49b2SChris Love 		}
87*209e49b2SChris Love 		if (wrap) {
88*209e49b2SChris Love 			tlen = len;
89*209e49b2SChris Love 			for (t = ep - 1, len = 0; t >= p; --t, ++len)
90*209e49b2SChris Love 				if (*t == '\n') {
91*209e49b2SChris Love 					if (len) {
92*209e49b2SChris Love 						WR(t + 1, len);
93*209e49b2SChris Love 						len = 0;
94*209e49b2SChris Love 					}
95*209e49b2SChris Love 					if (tlen) {
96*209e49b2SChris Love 						WR(sp, tlen);
97*209e49b2SChris Love 						tlen = 0;
98*209e49b2SChris Love 					}
99*209e49b2SChris Love 				}
100*209e49b2SChris Love 			if (len)
101*209e49b2SChris Love 				WR(t + 1, len);
102*209e49b2SChris Love 			if (tlen)
103*209e49b2SChris Love 				WR(sp, tlen);
104*209e49b2SChris Love 		}
105*209e49b2SChris Love 	} else {
106*209e49b2SChris Love 		if (wrap && (len = ep - p))
107*209e49b2SChris Love 			WR(p, len);
108*209e49b2SChris Love 		len = p - sp;
109*209e49b2SChris Love 		if (len)
110*209e49b2SChris Love 			WR(sp, len);
111*209e49b2SChris Love 	}
112*209e49b2SChris Love 
113*209e49b2SChris Love 	free(sp);
114*209e49b2SChris Love 	return (0);
115*209e49b2SChris Love }
116*209e49b2SChris Love 
117*209e49b2SChris Love /*
118*209e49b2SChris Love  * lines -- read lines to an offset from the end and display.
119*209e49b2SChris Love  *
120*209e49b2SChris Love  * This is the function that reads to a line offset from the end of the input,
121*209e49b2SChris Love  * storing the data in an array of buffers which is then displayed.  If the
122*209e49b2SChris Love  * rflag is set, the data is displayed in lines in reverse order, and this
123*209e49b2SChris Love  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
124*209e49b2SChris Love  * it is displayed from the line closest to the beginning of the input to
125*209e49b2SChris Love  * the end.
126*209e49b2SChris Love  */
127*209e49b2SChris Love int
lines(FILE * fp,const char * fn,off_t off)128*209e49b2SChris Love lines(FILE *fp, const char *fn, off_t off)
129*209e49b2SChris Love {
130*209e49b2SChris Love 	struct {
131*209e49b2SChris Love 		int blen;
132*209e49b2SChris Love 		uint_t len;
133*209e49b2SChris Love 		char *l;
134*209e49b2SChris Love 	} *llines;
135*209e49b2SChris Love 	int ch, rc;
136*209e49b2SChris Love 	char *p, *sp;
137*209e49b2SChris Love 	int blen, cnt, recno, wrap;
138*209e49b2SChris Love 
139*209e49b2SChris Love 	if ((llines = malloc(off * sizeof (*llines))) == NULL)
140*209e49b2SChris Love 		err(1, "malloc");
141*209e49b2SChris Love 	bzero(llines, off * sizeof (*llines));
142*209e49b2SChris Love 	p = sp = NULL;
143*209e49b2SChris Love 	blen = cnt = recno = wrap = 0;
144*209e49b2SChris Love 	rc = 0;
145*209e49b2SChris Love 
146*209e49b2SChris Love 	while ((ch = getc(fp)) != EOF) {
147*209e49b2SChris Love 		if (++cnt > blen) {
148*209e49b2SChris Love 			if ((sp = realloc(sp, blen += 1024)) == NULL)
149*209e49b2SChris Love 				err(1, "realloc");
150*209e49b2SChris Love 			p = sp + cnt - 1;
151*209e49b2SChris Love 		}
152*209e49b2SChris Love 		*p++ = ch;
153*209e49b2SChris Love 		if (ch == '\n') {
154*209e49b2SChris Love 			if ((int)llines[recno].blen < cnt) {
155*209e49b2SChris Love 				llines[recno].blen = cnt + 256;
156*209e49b2SChris Love 				if ((llines[recno].l = realloc(llines[recno].l,
157*209e49b2SChris Love 				    llines[recno].blen)) == NULL)
158*209e49b2SChris Love 					err(1, "realloc");
159*209e49b2SChris Love 			}
160*209e49b2SChris Love 			bcopy(sp, llines[recno].l, llines[recno].len = cnt);
161*209e49b2SChris Love 			cnt = 0;
162*209e49b2SChris Love 			p = sp;
163*209e49b2SChris Love 			if (++recno == off) {
164*209e49b2SChris Love 				wrap = 1;
165*209e49b2SChris Love 				recno = 0;
166*209e49b2SChris Love 			}
167*209e49b2SChris Love 		}
168*209e49b2SChris Love 	}
169*209e49b2SChris Love 	if (ferror(fp)) {
170*209e49b2SChris Love 		ierr(fn);
171*209e49b2SChris Love 		rc = 1;
172*209e49b2SChris Love 		goto done;
173*209e49b2SChris Love 	}
174*209e49b2SChris Love 	if (cnt) {
175*209e49b2SChris Love 		llines[recno].l = sp;
176*209e49b2SChris Love 		sp = NULL;
177*209e49b2SChris Love 		llines[recno].len = cnt;
178*209e49b2SChris Love 		if (++recno == off) {
179*209e49b2SChris Love 			wrap = 1;
180*209e49b2SChris Love 			recno = 0;
181*209e49b2SChris Love 		}
182*209e49b2SChris Love 	}
183*209e49b2SChris Love 
184*209e49b2SChris Love 	if (rflag) {
185*209e49b2SChris Love 		for (cnt = recno - 1; cnt >= 0; --cnt)
186*209e49b2SChris Love 			WR(llines[cnt].l, llines[cnt].len);
187*209e49b2SChris Love 		if (wrap)
188*209e49b2SChris Love 			for (cnt = off - 1; cnt >= recno; --cnt)
189*209e49b2SChris Love 				WR(llines[cnt].l, llines[cnt].len);
190*209e49b2SChris Love 	} else {
191*209e49b2SChris Love 		if (wrap)
192*209e49b2SChris Love 			for (cnt = recno; cnt < off; ++cnt)
193*209e49b2SChris Love 				WR(llines[cnt].l, llines[cnt].len);
194*209e49b2SChris Love 		for (cnt = 0; cnt < recno; ++cnt)
195*209e49b2SChris Love 			WR(llines[cnt].l, llines[cnt].len);
196*209e49b2SChris Love 	}
197*209e49b2SChris Love done:
198*209e49b2SChris Love 	for (cnt = 0; cnt < off; cnt++)
199*209e49b2SChris Love 		free(llines[cnt].l);
200*209e49b2SChris Love 	free(sp);
201*209e49b2SChris Love 	free(llines);
202*209e49b2SChris Love 	return (rc);
203*209e49b2SChris Love }
204