diff options
author | joerg <joerg@FreeBSD.org> | 1995-11-15 15:01:01 +0000 |
---|---|---|
committer | joerg <joerg@FreeBSD.org> | 1995-11-15 15:01:01 +0000 |
commit | ed683028f0348dc9d1760818143b203126b94a2f (patch) | |
tree | f231c4400619580b57e090898ecd2649b87e296b /usr.bin | |
parent | 96e41a571b9c764e2a83813228ceb2531a7cd301 (diff) | |
download | FreeBSD-src-ed683028f0348dc9d1760818143b203126b94a2f.zip FreeBSD-src-ed683028f0348dc9d1760818143b203126b94a2f.tar.gz |
Dynamically allocate line buffers, instead of assuming that all the
world would fit into BUFSIZ.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/fmt/fmt.c | 37 |
1 files changed, 31 insertions, 6 deletions
diff --git a/usr.bin/fmt/fmt.c b/usr.bin/fmt/fmt.c index dfbdb97..cba6456 100644 --- a/usr.bin/fmt/fmt.c +++ b/usr.bin/fmt/fmt.c @@ -44,6 +44,7 @@ static char sccsid[] = "@(#)fmt.c 8.1 (Berkeley) 7/20/93"; #include <stdio.h> #include <ctype.h> #include <locale.h> +#include <stdlib.h> /* * fmt -- format the concatenation of input files or standard input @@ -136,9 +137,11 @@ main(argc, argv) fmt(fi) FILE *fi; { - char linebuf[BUFSIZ], canonb[BUFSIZ]; + static char *linebuf = 0, *canonb = 0; register char *cp, *cp2, cc; register int c, col; +#define CHUNKSIZE 1024 + static int lbufsize = 0, cbufsize = 0; c = getc(fi); while (c != EOF) { @@ -147,7 +150,15 @@ fmt(fi) * Leave tabs for now. */ cp = linebuf; - while (c != '\n' && c != EOF && cp-linebuf < BUFSIZ-1) { + while (c != '\n' && c != EOF) { + if (cp - linebuf >= lbufsize) { + int offset = cp - linebuf; + lbufsize += CHUNKSIZE; + linebuf = realloc(linebuf, lbufsize); + if(linebuf == 0) + abort(); + cp = linebuf + offset; + } if (c == '\b') { if (cp > linebuf) cp--; @@ -178,13 +189,27 @@ fmt(fi) while (cc = *cp++) { if (cc != '\t') { col++; - if (cp2-canonb < BUFSIZ-1) - *cp2++ = cc; + if (cp2 - canonb >= cbufsize) { + int offset = cp2 - canonb; + cbufsize += CHUNKSIZE; + canonb = realloc(canonb, cbufsize); + if(canonb == 0) + abort(); + cp2 = canonb + offset; + } + *cp2++ = cc; continue; } do { - if (cp2-canonb < BUFSIZ-1) - *cp2++ = ' '; + if (cp2 - canonb >= cbufsize) { + int offset = cp2 - canonb; + cbufsize += CHUNKSIZE; + canonb = realloc(canonb, cbufsize); + if(canonb == 0) + abort(); + cp2 = canonb + offset; + } + *cp2++ = ' '; col++; } while ((col & 07) != 0); } |