P1003.2 forbids imposing any limit on line lengths; read character by
character instead of manually buffering each line.
This commit is contained in:
parent
55be04ab11
commit
ce78cbf9e0
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=97230
@ -115,11 +115,10 @@ int
|
|||||||
parallel(char **argv)
|
parallel(char **argv)
|
||||||
{
|
{
|
||||||
LIST *lp;
|
LIST *lp;
|
||||||
int cnt;
|
int cnt, ich;
|
||||||
char ch, *p;
|
char ch, *p;
|
||||||
LIST *head, *tmp;
|
LIST *head, *tmp;
|
||||||
int opencnt, output;
|
int opencnt, output;
|
||||||
char buf[_POSIX2_LINE_MAX + 1];
|
|
||||||
|
|
||||||
for (cnt = 0, head = NULL; (p = *argv); ++argv, ++cnt) {
|
for (cnt = 0, head = NULL; (p = *argv); ++argv, ++cnt) {
|
||||||
if ((lp = malloc(sizeof(LIST))) == NULL)
|
if ((lp = malloc(sizeof(LIST))) == NULL)
|
||||||
@ -147,7 +146,7 @@ parallel(char **argv)
|
|||||||
putchar(ch);
|
putchar(ch);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (!fgets(buf, sizeof(buf), lp->fp)) {
|
if ((ich = getc(lp->fp)) == EOF) {
|
||||||
if (!--opencnt)
|
if (!--opencnt)
|
||||||
break;
|
break;
|
||||||
lp->fp = NULL;
|
lp->fp = NULL;
|
||||||
@ -156,9 +155,6 @@ parallel(char **argv)
|
|||||||
putchar(ch);
|
putchar(ch);
|
||||||
continue;
|
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
|
* make sure that we don't print any delimiters
|
||||||
* unless there's a non-empty file.
|
* unless there's a non-empty file.
|
||||||
@ -170,7 +166,11 @@ parallel(char **argv)
|
|||||||
putchar(ch);
|
putchar(ch);
|
||||||
} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
|
} else if ((ch = delim[(lp->cnt - 1) % delimcnt]))
|
||||||
putchar(ch);
|
putchar(ch);
|
||||||
(void)printf("%s", buf);
|
if (ich == '\n')
|
||||||
|
continue;
|
||||||
|
do {
|
||||||
|
putchar(ich);
|
||||||
|
} while ((ich = getc(lp->fp)) != EOF && ich != '\n');
|
||||||
}
|
}
|
||||||
if (output)
|
if (output)
|
||||||
putchar('\n');
|
putchar('\n');
|
||||||
@ -183,9 +183,8 @@ int
|
|||||||
sequential(char **argv)
|
sequential(char **argv)
|
||||||
{
|
{
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
int cnt, failed;
|
int ch, cnt, failed, needdelim;
|
||||||
char ch, *p, *dp;
|
char *p;
|
||||||
char buf[_POSIX2_LINE_MAX + 1];
|
|
||||||
|
|
||||||
failed = 0;
|
failed = 0;
|
||||||
for (; (p = *argv); ++argv) {
|
for (; (p = *argv); ++argv) {
|
||||||
@ -196,23 +195,22 @@ sequential(char **argv)
|
|||||||
failed = 1;
|
failed = 1;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (fgets(buf, sizeof(buf), fp)) {
|
cnt = needdelim = 0;
|
||||||
for (cnt = 0, dp = delim;;) {
|
while ((ch = getc(fp)) != EOF) {
|
||||||
if (!(p = index(buf, '\n')))
|
if (needdelim) {
|
||||||
errx(1, "%s: input line too long", *argv);
|
needdelim = 0;
|
||||||
*p = '\0';
|
if (delim[cnt] != '\0')
|
||||||
(void)printf("%s", buf);
|
putchar(delim[cnt]);
|
||||||
if (!fgets(buf, sizeof(buf), fp))
|
if (++cnt == delimcnt)
|
||||||
break;
|
|
||||||
if ((ch = *dp++))
|
|
||||||
putchar(ch);
|
|
||||||
if (++cnt == delimcnt) {
|
|
||||||
dp = delim;
|
|
||||||
cnt = 0;
|
cnt = 0;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
putchar('\n');
|
if (ch != '\n')
|
||||||
|
putchar(ch);
|
||||||
|
else
|
||||||
|
needdelim = 1;
|
||||||
}
|
}
|
||||||
|
if (needdelim)
|
||||||
|
putchar('\n');
|
||||||
if (fp != stdin)
|
if (fp != stdin)
|
||||||
(void)fclose(fp);
|
(void)fclose(fp);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user