aboutsummaryrefslogtreecommitdiff
path: root/usr.bin/fmt
diff options
context:
space:
mode:
authorJoerg Wunsch <joerg@FreeBSD.org>1995-11-15 15:01:01 +0000
committerJoerg Wunsch <joerg@FreeBSD.org>1995-11-15 15:01:01 +0000
commite6c267f18cf334713acda85263db0cf855b9d989 (patch)
treef231c4400619580b57e090898ecd2649b87e296b /usr.bin/fmt
parentf3117d66d3367ce2d219a42a265cb0054d9f12ed (diff)
downloadsrc-e6c267f18cf334713acda85263db0cf855b9d989.tar.gz
src-e6c267f18cf334713acda85263db0cf855b9d989.zip
Notes
Diffstat (limited to 'usr.bin/fmt')
-rw-r--r--usr.bin/fmt/fmt.c37
1 files changed, 31 insertions, 6 deletions
diff --git a/usr.bin/fmt/fmt.c b/usr.bin/fmt/fmt.c
index dfbdb9796174..cba6456131bd 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);
}