diff options
author | dd <dd@FreeBSD.org> | 2001-04-25 05:42:53 +0000 |
---|---|---|
committer | dd <dd@FreeBSD.org> | 2001-04-25 05:42:53 +0000 |
commit | 3b8fad2c5973912ad3f2cbc7ac26dc242ab95134 (patch) | |
tree | 494d67c8e885a20f29299c03f08422a641e9baa5 /usr.bin/cut | |
parent | a8735d68201d58c79fb8453329d6325f70d846bb (diff) | |
download | FreeBSD-src-3b8fad2c5973912ad3f2cbc7ac26dc242ab95134.zip FreeBSD-src-3b8fad2c5973912ad3f2cbc7ac26dc242ab95134.tar.gz |
Teach cut(1) how to handle long lines: convert from fgets(3) to fgetln(3).
PR: 26810
Reviewed by: dwmalone
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 19b9e07..373ff4a 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 |