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

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