=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/paste/paste.c,v retrieving revision 1.23 retrieving revision 1.24 diff -c -r1.23 -r1.24 *** src/usr.bin/paste/paste.c 2018/01/02 06:56:41 1.23 --- src/usr.bin/paste/paste.c 2018/08/04 16:14:03 1.24 *************** *** 1,4 **** ! /* $OpenBSD: paste.c,v 1.23 2018/01/02 06:56:41 guenther Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. --- 1,4 ---- ! /* $OpenBSD: paste.c,v 1.24 2018/08/04 16:14:03 schwarze Exp $ */ /* * Copyright (c) 1989 The Regents of the University of California. *************** *** 104,114 **** { SIMPLEQ_HEAD(, list) head = SIMPLEQ_HEAD_INITIALIZER(head); struct list *lp; int cnt; - char ch, *p; int opencnt, output; ! char *buf, *lbuf; ! size_t len; for (cnt = 0; (p = *argv); ++argv, ++cnt) { if (!(lp = malloc(sizeof(struct list)))) --- 104,114 ---- { SIMPLEQ_HEAD(, list) head = SIMPLEQ_HEAD_INITIALIZER(head); struct list *lp; + char *line, *p; + size_t len, linesize; int cnt; int opencnt, output; ! char ch; for (cnt = 0; (p = *argv); ++argv, ++cnt) { if (!(lp = malloc(sizeof(struct list)))) *************** *** 123,139 **** SIMPLEQ_INSERT_TAIL(&head, lp, entries); } for (opencnt = cnt; opencnt;) { output = 0; SIMPLEQ_FOREACH(lp, &head, entries) { - lbuf = NULL; if (!lp->fp) { if (output && lp->cnt && (ch = delim[(lp->cnt - 1) % delimcnt])) putchar(ch); continue; } ! if (!(buf = fgetln(lp->fp, &len))) { if (!--opencnt) break; if (lp->fp != stdin) --- 123,144 ---- SIMPLEQ_INSERT_TAIL(&head, lp, entries); } + line = NULL; + linesize = 0; + for (opencnt = cnt; opencnt;) { output = 0; SIMPLEQ_FOREACH(lp, &head, entries) { if (!lp->fp) { if (output && lp->cnt && (ch = delim[(lp->cnt - 1) % delimcnt])) putchar(ch); continue; } ! if ((len = getline(&line, &linesize, lp->fp)) == -1) { ! if (ferror(lp->fp)) ! err(1, "%s", lp->fp == stdin ? ! "getline" : lp->name); if (!--opencnt) break; if (lp->fp != stdin) *************** *** 144,158 **** putchar(ch); continue; } ! if (buf[len - 1] == '\n') ! buf[len - 1] = '\0'; ! else { ! if ((lbuf = malloc(len + 1)) == NULL) ! err(1, "malloc"); ! memcpy(lbuf, buf, len); ! lbuf[len] = '\0'; ! buf = lbuf; ! } /* * make sure that we don't print any delimiters * unless there's a non-empty file. --- 149,156 ---- putchar(ch); continue; } ! if (line[len - 1] == '\n') ! line[len - 1] = '\0'; /* * make sure that we don't print any delimiters * unless there's a non-empty file. *************** *** 164,222 **** putchar(ch); } else if ((ch = delim[(lp->cnt - 1) % delimcnt])) putchar(ch); ! (void)printf("%s", buf); ! if (lbuf) ! free(lbuf); } if (output) putchar('\n'); } } void sequential(char **argv) { FILE *fp; int cnt; - char ch, *p, *dp; - char *buf, *lbuf; - size_t len; for (; (p = *argv); ++argv) { - lbuf = NULL; if (p[0] == '-' && !p[1]) fp = stdin; else if (!(fp = fopen(p, "r"))) { warn("%s", p); continue; } ! if ((buf = fgetln(fp, &len))) { ! for (cnt = 0, dp = delim;;) { ! if (buf[len - 1] == '\n') ! buf[len - 1] = '\0'; ! else { ! if ((lbuf = malloc(len + 1)) == NULL) ! err(1, "malloc"); ! memcpy(lbuf, buf, len); ! lbuf[len] = '\0'; ! buf = lbuf; ! } ! (void)printf("%s", buf); ! if (!(buf = fgetln(fp, &len))) ! break; ! if ((ch = *dp++)) ! putchar(ch); ! if (++cnt == delimcnt) { ! dp = delim; ! cnt = 0; ! } ! } ! putchar('\n'); } if (fp != stdin) (void)fclose(fp); - free(lbuf); } } int --- 162,210 ---- putchar(ch); } else if ((ch = delim[(lp->cnt - 1) % delimcnt])) putchar(ch); ! fputs(line, stdout); } if (output) putchar('\n'); } + free(line); } void sequential(char **argv) { FILE *fp; + char *line, *p; + size_t len, linesize; int cnt; + line = NULL; + linesize = 0; for (; (p = *argv); ++argv) { if (p[0] == '-' && !p[1]) fp = stdin; else if (!(fp = fopen(p, "r"))) { warn("%s", p); continue; } ! cnt = -1; ! while ((len = getline(&line, &linesize, fp)) != -1) { ! if (line[len - 1] == '\n') ! line[len - 1] = '\0'; ! if (cnt >= 0) ! putchar(delim[cnt]); ! if (++cnt == delimcnt) ! cnt = 0; ! fputs(line, stdout); } + if (ferror(fp)) + err(1, "%s", fp == stdin ? "getline" : p); + if (cnt >= 0) + putchar('\n'); if (fp != stdin) (void)fclose(fp); } + free(line); } int