diff options
| author | Dima Dorfman <dd@FreeBSD.org> | 2001-07-02 01:10:31 +0000 |
|---|---|---|
| committer | Dima Dorfman <dd@FreeBSD.org> | 2001-07-02 01:10:31 +0000 |
| commit | f2608e98c4fee765edb9ec740fd33b2103e31f91 (patch) | |
| tree | ffbaafd03469afa8b2ff5120d7c0f1aa1feaeea0 /usr.bin/cut | |
| parent | 5d92611110401598da3c09923fd9a004b2d694e2 (diff) | |
Notes
Diffstat (limited to 'usr.bin/cut')
| -rw-r--r-- | usr.bin/cut/cut.c | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/usr.bin/cut/cut.c b/usr.bin/cut/cut.c index 19b9e078817d..373ff4aa2e93 100644 --- a/usr.bin/cut/cut.c +++ b/usr.bin/cut/cut.c @@ -228,19 +228,29 @@ f_cut(fp, fname) int ch, field, isdelim; char *pos, *p, sep; int output; - char lbuf[_POSIX2_LINE_MAX + 1]; + char *lbuf, *mlbuf = NULL; + size_t lbuflen; - for (sep = dchar; fgets(lbuf, sizeof(lbuf), fp);) { + for (sep = dchar; (lbuf = fgetln(fp, &lbuflen)) != NULL;) { + /* Assert EOL has a newline. */ + if (*(lbuf + lbuflen - 1) != '\n') { + /* Can't have > 1 line with no trailing newline. */ + mlbuf = malloc(lbuflen + 1); + if (mlbuf == NULL) + err(1, "malloc"); + memcpy(mlbuf, lbuf, lbuflen); + *(mlbuf + lbuflen) = '\n'; + lbuf = mlbuf; + } output = 0; for (isdelim = 0, p = lbuf;; ++p) { - if (!(ch = *p)) - errx(1, "%s: line too long.", fname); + ch = *p; /* this should work if newline is delimiter */ if (ch == sep) isdelim = 1; if (ch == '\n') { if (!isdelim && !sflag) - (void)printf("%s", lbuf); + (void)fwrite(lbuf, lbuflen, 1, stdout); break; } } @@ -272,6 +282,8 @@ f_cut(fp, fname) } (void)putchar('\n'); } + if (mlbuf != NULL) + free(mlbuf); } static void |
