diff options
Diffstat (limited to 'src/usr.bin/rs/rs.c')
-rw-r--r-- | src/usr.bin/rs/rs.c | 61 |
1 files changed, 29 insertions, 32 deletions
diff --git a/src/usr.bin/rs/rs.c b/src/usr.bin/rs/rs.c index f29f580..5d702cf 100644 --- a/src/usr.bin/rs/rs.c +++ b/src/usr.bin/rs/rs.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rs.c,v 1.24 2014/10/08 04:07:24 doug Exp $ */ +/* $OpenBSD: rs.c,v 1.28 2015/11/10 14:42:41 schwarze Exp $ */ /*- * Copyright (c) 1993 @@ -67,10 +67,10 @@ char **elem; char **endelem; char *curline; int allocsize = BUFSIZ; -int curlen; +ssize_t curlen; int irows, icols; int orows, ocols; -int maxlen; +ssize_t maxlen; int skip; int propgutter; char isep = ' ', osep = ' '; @@ -93,6 +93,9 @@ void putfile(void); int main(int argc, char *argv[]) { + if (pledge("stdio", NULL) == -1) + err(1, "pledge"); + getargs(argc, argv); getfile(); if (flags & SHAPEONLY) { @@ -115,11 +118,13 @@ getfile(void) char **padto; while (skip--) { - get_line(); + if (get_line() == EOF) + return; if (flags & SKIPPRINT) puts(curline); } - get_line(); + if (get_line() == EOF) + return; if (flags & NOARGS && curlen < owidth) flags |= ONEPERLINE; if (flags & ONEPERLINE) @@ -268,7 +273,7 @@ prepfile(void) *ep = *(ep - nelem); nelem = lp - elem; } - if (!(colwidths = (short *) calloc(ocols, sizeof(short)))) + if (!(colwidths = calloc(ocols, sizeof(short)))) errx(1, "malloc: No gutter space"); if (flags & SQUEEZE) { if (flags & TRANSPOSE) @@ -300,37 +305,29 @@ prepfile(void) nelem = n; } -#define BSIZE 2048 -char ibuf[BSIZE]; /* two screenfuls should do */ - int get_line(void) /* get line; maintain curline, curlen; manage storage */ { - static int putlength; - static char *endblock = ibuf + BSIZE; - char *p; - int c, i; + static char *ibuf = NULL; + static size_t ibufsz = 0; - if (!irows) { - curline = ibuf; - putlength = flags & DETAILSHAPE; - } - else if (skip <= 0) { /* don't waste storage */ - curline += curlen + 1; - if (putlength) /* print length, recycle storage */ - printf(" %d line %d\n", curlen, irows); - } - if (!putlength && endblock - curline < BUFSIZ) { /* need storage */ - if (!(curline = (char *) malloc(BSIZE))) - errx(1, "File too large"); - endblock = curline + BSIZE; + if (irows > 0 && flags & DETAILSHAPE) + printf(" %zd line %d\n", curlen, irows); + + if ((curlen = getline(&ibuf, &ibufsz, stdin)) == EOF) { + if (ferror(stdin)) + err(1, NULL); + return EOF; } - for (p = curline, i = 1; i < BUFSIZ; *p++ = c, i++) - if ((c = getchar()) == EOF || c == '\n') - break; - *p = '\0'; - curlen = i - 1; - return(c); + if (curlen > 0 && ibuf[curlen - 1] == '\n') + ibuf[--curlen] = '\0'; + + if (skip >= 0 || flags & SHAPEONLY) + curline = ibuf; + else if ((curline = strdup(ibuf)) == NULL) + err(1, NULL); + + return 0; } char ** |