summaryrefslogtreecommitdiffstats
path: root/usr.bin/paste
diff options
context:
space:
mode:
authortjr <tjr@FreeBSD.org>2002-05-24 07:05:10 +0000
committertjr <tjr@FreeBSD.org>2002-05-24 07:05:10 +0000
commitc2f2a6104f8a4bcecaf905424b543d4446ce116a (patch)
treef3c16115537bf5e956249ac8dd94a5a9b6a02d9e /usr.bin/paste
parentab651922a29575f85c6ab6fe89d2f0b57f5080c6 (diff)
downloadFreeBSD-src-c2f2a6104f8a4bcecaf905424b543d4446ce116a.zip
FreeBSD-src-c2f2a6104f8a4bcecaf905424b543d4446ce116a.tar.gz
P1003.2 forbids imposing any limit on line lengths; read character by
character instead of manually buffering each line.
Diffstat (limited to 'usr.bin/paste')
-rw-r--r--usr.bin/paste/paste.c46
1 files changed, 22 insertions, 24 deletions
diff --git a/usr.bin/paste/paste.c b/usr.bin/paste/paste.c
index 730ae67..de05648 100644
--- a/usr.bin/paste/paste.c
+++ b/usr.bin/paste/paste.c
@@ -115,11 +115,10 @@ int
parallel(char **argv)
{
LIST *lp;
- int cnt;
+ int cnt, ich;
char ch, *p;
LIST *head, *tmp;
int opencnt, output;
- char buf[_POSIX2_LINE_MAX + 1];
for (cnt = 0, head = NULL; (p = *argv); ++argv, ++cnt) {
if ((lp = malloc(sizeof(LIST))) == NULL)
@@ -147,7 +146,7 @@ parallel(char **argv)
putchar(ch);
continue;
}
- if (!fgets(buf, sizeof(buf), lp->fp)) {
+ if ((ich = getc(lp->fp)) == EOF) {
if (!--opencnt)
break;
lp->fp = NULL;
@@ -156,9 +155,6 @@ parallel(char **argv)
putchar(ch);
continue;
}
- if (!(p = index(buf, '\n')))
- errx(1, "%s: input line too long", lp->name);
- *p = '\0';
/*
* make sure that we don't print any delimiters
* unless there's a non-empty file.
@@ -170,7 +166,11 @@ parallel(char **argv)
putchar(ch);
} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
putchar(ch);
- (void)printf("%s", buf);
+ if (ich == '\n')
+ continue;
+ do {
+ putchar(ich);
+ } while ((ich = getc(lp->fp)) != EOF && ich != '\n');
}
if (output)
putchar('\n');
@@ -183,9 +183,8 @@ int
sequential(char **argv)
{
FILE *fp;
- int cnt, failed;
- char ch, *p, *dp;
- char buf[_POSIX2_LINE_MAX + 1];
+ int ch, cnt, failed, needdelim;
+ char *p;
failed = 0;
for (; (p = *argv); ++argv) {
@@ -196,23 +195,22 @@ sequential(char **argv)
failed = 1;
continue;
}
- if (fgets(buf, sizeof(buf), fp)) {
- for (cnt = 0, dp = delim;;) {
- if (!(p = index(buf, '\n')))
- errx(1, "%s: input line too long", *argv);
- *p = '\0';
- (void)printf("%s", buf);
- if (!fgets(buf, sizeof(buf), fp))
- break;
- if ((ch = *dp++))
- putchar(ch);
- if (++cnt == delimcnt) {
- dp = delim;
+ cnt = needdelim = 0;
+ while ((ch = getc(fp)) != EOF) {
+ if (needdelim) {
+ needdelim = 0;
+ if (delim[cnt] != '\0')
+ putchar(delim[cnt]);
+ if (++cnt == delimcnt)
cnt = 0;
- }
}
- putchar('\n');
+ if (ch != '\n')
+ putchar(ch);
+ else
+ needdelim = 1;
}
+ if (needdelim)
+ putchar('\n');
if (fp != stdin)
(void)fclose(fp);
}
OpenPOWER on IntegriCloud