[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.5

1.5     ! mpech       1: /*     $OpenBSD: read.c,v 1.4 2000/06/23 17:04:46 ericj 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.5     ! mpech      44: static char rcsid[] = "$OpenBSD: read.c,v 1.4 2000/06/23 17:04:46 ericj 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.
1.4       ericj      69:  *
1.5     ! mpech      70:  * A non-zero return means an (non-fatal) error occurred.
1.4       ericj      71:  *
1.1       deraadt    72:  */
1.4       ericj      73: int
1.1       deraadt    74: bytes(fp, off)
                     75:        register FILE *fp;
                     76:        off_t off;
                     77: {
                     78:        register int ch, len, tlen;
                     79:        register char *ep, *p, *t;
                     80:        int wrap;
                     81:        char *sp;
                     82:
                     83:        if ((sp = p = malloc(off)) == NULL)
1.3       millert    84:                err(1, NULL);
1.1       deraadt    85:
                     86:        for (wrap = 0, ep = p + off; (ch = getc(fp)) != EOF;) {
                     87:                *p = ch;
                     88:                if (++p == ep) {
                     89:                        wrap = 1;
                     90:                        p = sp;
                     91:                }
                     92:        }
                     93:        if (ferror(fp)) {
                     94:                ierr();
1.4       ericj      95:                return(1);
1.1       deraadt    96:        }
                     97:
                     98:        if (rflag) {
                     99:                for (t = p - 1, len = 0; t >= sp; --t, ++len)
                    100:                        if (*t == '\n' && len) {
                    101:                                WR(t + 1, len);
                    102:                                len = 0;
                    103:                }
                    104:                if (wrap) {
                    105:                        tlen = len;
                    106:                        for (t = ep - 1, len = 0; t >= p; --t, ++len)
                    107:                                if (*t == '\n') {
                    108:                                        if (len) {
                    109:                                                WR(t + 1, len);
                    110:                                                len = 0;
                    111:                                        }
                    112:                                        if (tlen) {
                    113:                                                WR(sp, tlen);
                    114:                                                tlen = 0;
                    115:                                        }
                    116:                                }
                    117:                        if (len)
                    118:                                WR(t + 1, len);
                    119:                        if (tlen)
                    120:                                WR(sp, tlen);
                    121:                }
                    122:        } else {
                    123:                if (wrap && (len = ep - p))
                    124:                        WR(p, len);
1.3       millert   125:                if ((len = p - sp))
1.1       deraadt   126:                        WR(sp, len);
                    127:        }
1.4       ericj     128:        return(0);
1.1       deraadt   129: }
                    130:
                    131: /*
                    132:  * lines -- read lines to an offset from the end and display.
                    133:  *
                    134:  * This is the function that reads to a line offset from the end of the input,
                    135:  * storing the data in an array of buffers which is then displayed.  If the
                    136:  * rflag is set, the data is displayed in lines in reverse order, and this
                    137:  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
                    138:  * it is displayed from the line closest to the beginning of the input to
                    139:  * the end.
1.4       ericj     140:  *
1.5     ! mpech     141:  * A non-zero return means an (non-fatal) error occurred.
1.4       ericj     142:  *
1.1       deraadt   143:  */
1.4       ericj     144: int
1.1       deraadt   145: lines(fp, off)
                    146:        register FILE *fp;
                    147:        off_t off;
                    148: {
                    149:        struct {
                    150:                u_int blen;
                    151:                u_int len;
                    152:                char *l;
                    153:        } *lines;
                    154:        register int ch;
1.3       millert   155:        register char *p = NULL;
1.1       deraadt   156:        int blen, cnt, recno, wrap;
1.3       millert   157:        char *sp = NULL;
1.1       deraadt   158:
1.3       millert   159:        if ((lines = calloc(off, sizeof(*lines))) == NULL)
                    160:                err(1, NULL);
1.1       deraadt   161:
                    162:        blen = cnt = recno = wrap = 0;
                    163:
                    164:        while ((ch = getc(fp)) != EOF) {
                    165:                if (++cnt > blen) {
                    166:                        if ((sp = realloc(sp, blen += 1024)) == NULL)
1.3       millert   167:                                err(1, NULL);
1.1       deraadt   168:                        p = sp + cnt - 1;
                    169:                }
                    170:                *p++ = ch;
                    171:                if (ch == '\n') {
                    172:                        if (lines[recno].blen < cnt) {
                    173:                                lines[recno].blen = cnt + 256;
                    174:                                if ((lines[recno].l = realloc(lines[recno].l,
                    175:                                    lines[recno].blen)) == NULL)
1.3       millert   176:                                        err(1, NULL);
1.1       deraadt   177:                        }
1.3       millert   178:                        memcpy(lines[recno].l, sp, (lines[recno].len = cnt));
1.1       deraadt   179:                        cnt = 0;
                    180:                        p = sp;
                    181:                        if (++recno == off) {
                    182:                                wrap = 1;
                    183:                                recno = 0;
                    184:                        }
                    185:                }
                    186:        }
                    187:        if (ferror(fp)) {
                    188:                ierr();
1.4       ericj     189:                return(1);
1.1       deraadt   190:        }
                    191:        if (cnt) {
                    192:                lines[recno].l = sp;
                    193:                lines[recno].len = cnt;
                    194:                if (++recno == off) {
                    195:                        wrap = 1;
                    196:                        recno = 0;
                    197:                }
                    198:        }
                    199:
                    200:        if (rflag) {
                    201:                for (cnt = recno - 1; cnt >= 0; --cnt)
                    202:                        WR(lines[cnt].l, lines[cnt].len);
                    203:                if (wrap)
                    204:                        for (cnt = off - 1; cnt >= recno; --cnt)
                    205:                                WR(lines[cnt].l, lines[cnt].len);
                    206:        } else {
                    207:                if (wrap)
                    208:                        for (cnt = recno; cnt < off; ++cnt)
                    209:                                WR(lines[cnt].l, lines[cnt].len);
                    210:                for (cnt = 0; cnt < recno; ++cnt)
                    211:                        WR(lines[cnt].l, lines[cnt].len);
                    212:        }
1.4       ericj     213:        return(0);
1.1       deraadt   214: }