[BACK]Return to read.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tail

Annotation of src/usr.bin/tail/read.c, Revision 1.3

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