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

1.12    ! otto        1: /*     $OpenBSD: read.c,v 1.11 2007/09/16 18:07:40 otto 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.
1.7       millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
                     37: #if 0
                     38: static char sccsid[] = "@(#)read.c     8.1 (Berkeley) 6/6/93";
                     39: #endif
1.12    ! otto       40: static char rcsid[] = "$OpenBSD: read.c,v 1.11 2007/09/16 18:07:40 otto Exp $";
1.1       deraadt    41: #endif /* not lint */
                     42:
                     43: #include <sys/types.h>
                     44: #include <sys/stat.h>
1.8       kjell      45: #include <sys/limits.h>
1.3       millert    46:
                     47: #include <err.h>
1.1       deraadt    48: #include <stdio.h>
                     49: #include <stdlib.h>
                     50: #include <string.h>
1.3       millert    51: #include <unistd.h>
                     52:
1.1       deraadt    53: #include "extern.h"
                     54:
                     55: /*
                     56:  * bytes -- read bytes to an offset from the end and display.
                     57:  *
                     58:  * This is the function that reads to a byte offset from the end of the input,
                     59:  * storing the data in a wrap-around buffer which is then displayed.  If the
                     60:  * rflag is set, the data is displayed in lines in reverse order, and this
                     61:  * routine has the usual nastiness of trying to find the newlines.  Otherwise,
                     62:  * it is displayed from the character closest to the beginning of the input to
                     63:  * the end.
1.4       ericj      64:  *
1.5       mpech      65:  * A non-zero return means an (non-fatal) error occurred.
1.4       ericj      66:  *
1.1       deraadt    67:  */
1.4       ericj      68: int
1.9       kjell      69: bytes(FILE *fp, off_t off)
1.1       deraadt    70: {
1.8       kjell      71:        int ch;
                     72:        size_t len, tlen;
1.6       mpech      73:        char *ep, *p, *t;
1.1       deraadt    74:        int wrap;
                     75:        char *sp;
                     76:
1.8       kjell      77:        if (off > SIZE_T_MAX)
                     78:                errx(1, "offset too large");
                     79:
1.1       deraadt    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();
1.10      otto       92:                free(sp);
1.4       ericj      93:                return(1);
1.1       deraadt    94:        }
                     95:
                     96:        if (rflag) {
                     97:                for (t = p - 1, len = 0; t >= sp; --t, ++len)
                     98:                        if (*t == '\n' && len) {
                     99:                                WR(t + 1, len);
                    100:                                len = 0;
                    101:                }
                    102:                if (wrap) {
                    103:                        tlen = len;
                    104:                        for (t = ep - 1, len = 0; t >= p; --t, ++len)
                    105:                                if (*t == '\n') {
                    106:                                        if (len) {
                    107:                                                WR(t + 1, len);
                    108:                                                len = 0;
                    109:                                        }
                    110:                                        if (tlen) {
                    111:                                                WR(sp, tlen);
                    112:                                                tlen = 0;
                    113:                                        }
                    114:                                }
                    115:                        if (len)
                    116:                                WR(t + 1, len);
                    117:                        if (tlen)
                    118:                                WR(sp, tlen);
                    119:                }
                    120:        } else {
                    121:                if (wrap && (len = ep - p))
                    122:                        WR(p, len);
1.3       millert   123:                if ((len = p - sp))
1.1       deraadt   124:                        WR(sp, len);
                    125:        }
1.10      otto      126:
                    127:        free(sp);
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.9       kjell     145: lines(FILE *fp, off_t off)
1.1       deraadt   146: {
                    147:        struct {
1.8       kjell     148:                size_t blen;
                    149:                size_t len;
1.1       deraadt   150:                char *l;
                    151:        } *lines;
1.11      otto      152:        int ch, rc = 0;
1.6       mpech     153:        char *p = NULL;
1.8       kjell     154:        int wrap;
                    155:        size_t cnt, recno, blen, newsize;
                    156:        char *sp = NULL, *newp = NULL;
                    157:
                    158:        if (off > SIZE_T_MAX)
                    159:                errx(1, "offset too large");
1.1       deraadt   160:
1.3       millert   161:        if ((lines = calloc(off, sizeof(*lines))) == NULL)
                    162:                err(1, NULL);
1.1       deraadt   163:
                    164:        blen = cnt = recno = wrap = 0;
                    165:
                    166:        while ((ch = getc(fp)) != EOF) {
                    167:                if (++cnt > blen) {
1.8       kjell     168:                        newsize = blen + 1024;
                    169:                        if ((newp = realloc(sp, newsize)) == NULL)
1.3       millert   170:                                err(1, NULL);
1.8       kjell     171:                        sp = newp;
                    172:                        blen = newsize;
1.1       deraadt   173:                        p = sp + cnt - 1;
                    174:                }
                    175:                *p++ = ch;
                    176:                if (ch == '\n') {
                    177:                        if (lines[recno].blen < cnt) {
1.8       kjell     178:                                newsize = cnt + 256;
                    179:                                if ((newp = realloc(lines[recno].l,
                    180:                                    newsize)) == NULL)
1.3       millert   181:                                        err(1, NULL);
1.8       kjell     182:                                lines[recno].l = newp;
                    183:                                lines[recno].blen = newsize;
1.1       deraadt   184:                        }
1.3       millert   185:                        memcpy(lines[recno].l, sp, (lines[recno].len = cnt));
1.1       deraadt   186:                        cnt = 0;
                    187:                        p = sp;
                    188:                        if (++recno == off) {
                    189:                                wrap = 1;
                    190:                                recno = 0;
                    191:                        }
                    192:                }
                    193:        }
                    194:        if (ferror(fp)) {
                    195:                ierr();
1.11      otto      196:                rc = 1;
                    197:                goto done;
1.1       deraadt   198:        }
                    199:        if (cnt) {
                    200:                lines[recno].l = sp;
                    201:                lines[recno].len = cnt;
                    202:                if (++recno == off) {
                    203:                        wrap = 1;
                    204:                        recno = 0;
                    205:                }
                    206:        }
                    207:
                    208:        if (rflag) {
1.8       kjell     209:                for (cnt = recno; cnt > 0; --cnt)
                    210:                        WR(lines[cnt - 1].l, lines[cnt - 1].len);
1.1       deraadt   211:                if (wrap)
1.8       kjell     212:                        for (cnt = off; cnt > recno; --cnt)
                    213:                                WR(lines[cnt - 1].l, lines[cnt - 1].len);
1.1       deraadt   214:        } else {
                    215:                if (wrap)
                    216:                        for (cnt = recno; cnt < off; ++cnt)
                    217:                                WR(lines[cnt].l, lines[cnt].len);
                    218:                for (cnt = 0; cnt < recno; ++cnt)
                    219:                        WR(lines[cnt].l, lines[cnt].len);
                    220:        }
1.11      otto      221: done:
                    222:        for (cnt = 0; cnt < off; cnt++)
                    223:                free(lines[cnt].l);
                    224:        free(sp);
                    225:        free(lines);
                    226:        return(rc);
1.1       deraadt   227: }