Pádraig Brady <[email protected]> writes: > I don't think getline() is a generic enough interface, > especially for a utility used to introduce new lines. > > Consider for example you want 80x10 lines of "y", > you could do this previously with: > > $ yes | tr -d '\n' | fold -w 80 | head -n 10 > > While now fold will just consume all of memory. > Instead you might fread() to an IO_BUFSIZE buffer?
Good point. Using fread is also faster since it doesn't check for the delimiter upon reading a character, and doesn't need to lock the stream each call (since we use fread_unlocked). Will push the attached in a bit. Collin
>From 06c1bd78b9f29afc2195d9fb1cc2efd3b382cbd8 Mon Sep 17 00:00:00 2001 Message-ID: <06c1bd78b9f29afc2195d9fb1cc2efd3b382cbd8.1756062860.git.collin.fu...@gmail.com> From: Collin Funk <[email protected]> Date: Sun, 24 Aug 2025 12:05:41 -0700 Subject: [PATCH] fold: use fread instead of getline * src/fold.c: Include ioblksize.h. (fold_file): Use a IO_BUFSIZE-sized buffer. Use fread instead of getline. Check for if we reached the end of file. --- src/fold.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/fold.c b/src/fold.c index 153ce15a6..d800c9338 100644 --- a/src/fold.c +++ b/src/fold.c @@ -25,6 +25,7 @@ #include "system.h" #include "fadvise.h" +#include "ioblksize.h" #include "mcel.h" #include "xdectoint.h" @@ -138,9 +139,8 @@ fold_file (char const *filename, size_t width) idx_t offset_out = 0; /* Index in 'line_out' for next char. */ static char *line_out = nullptr; static idx_t allocated_out = 0; - static char *line_in = nullptr; - static size_t allocated_in = 0; - static ssize_t length_in = 0; + static char line_in[IO_BUFSIZE]; + static size_t length_in = 0; int saved_errno; if (STREQ (filename, "-")) @@ -159,7 +159,7 @@ fold_file (char const *filename, size_t width) fadvise (istream, FADVISE_SEQUENTIAL); - while (0 <= (length_in = getline (&line_in, &allocated_in, istream))) + while (0 < (length_in = fread (line_in, 1, sizeof line_in, istream))) { char *p = line_in; char *lim = p + length_in; @@ -243,6 +243,8 @@ fold_file (char const *filename, size_t width) memcpy (line_out + offset_out, p, g.len); offset_out += g.len; } + if (feof (istream)) + break; } saved_errno = errno; -- 2.51.0
