Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.bin / tail / read.c
1 /*-
2  * Copyright (c) 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Edward Sze-Tyan Wang.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by the University of
19  *      California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)read.c      8.1 (Berkeley) 6/6/93";
40 #endif
41 static const char rcsid[] =
42   "$FreeBSD: src/usr.bin/tail/read.c,v 1.6.8.1 2001/01/24 08:41:14 ru Exp $";
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <unistd.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <err.h>
54 #include "extern.h"
55
56 /*
57  * bytes -- read bytes to an offset from the end and display.
58  *
59  * This is the function that reads to a byte offset from the end of the input,
60  * storing the data in a wrap-around buffer which is then displayed.  If the
61  * rflag is set, the data is displayed in lines in reverse order, and this
62  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
63  * it is displayed from the character closest to the beginning of the input to
64  * the end.
65  */
66 int
67 bytes(fp, off)
68         FILE *fp;
69         off_t off;
70 {
71         int ch, len, tlen;
72         char *ep, *p, *t;
73         int wrap;
74         char *sp;
75
76         if ((sp = p = malloc(off)) == NULL)
77                 err(1, "malloc");
78
79         for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
80                 *p = ch;
81                 if (++p == ep) {
82                         wrap = 1;
83                         p = sp;
84                 }
85         }
86         if (ferror(fp)) {
87                 ierr();
88                 return 1;
89         }
90
91         if (rflag) {
92                 for (t = p - 1, len = 0; t >= sp; --t, ++len)
93                         if (*t == '\n' && len) {
94                                 WR(t + 1, len);
95                                 len = 0;
96                 }
97                 if (wrap) {
98                         tlen = len;
99                         for (t = ep - 1, len = 0; t >= p; --t, ++len)
100                                 if (*t == '\n') {
101                                         if (len) {
102                                                 WR(t + 1, len);
103                                                 len = 0;
104                                         }
105                                         if (tlen) {
106                                                 WR(sp, tlen);
107                                                 tlen = 0;
108                                         }
109                                 }
110                         if (len)
111                                 WR(t + 1, len);
112                         if (tlen)
113                                 WR(sp, tlen);
114                 }
115         } else {
116                 if (wrap && (len = ep - p))
117                         WR(p, len);
118                 if (len = p - sp)
119                         WR(sp, len);
120         }
121         return 0;
122 }
123
124 /*
125  * lines -- read lines to an offset from the end and display.
126  *
127  * This is the function that reads to a line offset from the end of the input,
128  * storing the data in an array of buffers which is then displayed.  If the
129  * rflag is set, the data is displayed in lines in reverse order, and this
130  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
131  * it is displayed from the line closest to the beginning of the input to
132  * the end.
133  */
134 int
135 lines(fp, off)
136         FILE *fp;
137         off_t off;
138 {
139         struct {
140                 u_int blen;
141                 u_int len;
142                 char *l;
143         } *lines;
144         int ch;
145         char *p;
146         int blen, cnt, recno, wrap;
147         char *sp;
148
149         if ((lines = malloc(off * sizeof(*lines))) == NULL)
150                 err(1, "malloc");
151         bzero(lines, off * sizeof(*lines));
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, "realloc");
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, "realloc");
168                         }
169                         bcopy(sp, lines[recno].l, 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 }