Re: [hackers] [sbase][PATCH] Add implementation of tac(1)
On Sun, Mar 03, 2024 at 12:34:16AM +0100, Elie Le Vaillant wrote:
> +static void
> +tac(FILE *fp)
> +{
> + struct linebuf buf = EMPTY_LINEBUF;
> + struct line line;
> + getlines(fp, &buf);
> +
> + if (buf.nolf) {
> + /* If the last line is not LF-terminated, the
> + * first output line should not be either */
> + buf.nolf--;
> + buf.lines[buf.nlines - 1].len--;
> + }
> +
> + while (buf.nlines--) {
> + line = buf.lines[buf.nlines];
> + fwrite(line.data, 1, line.len, stdout);
> + free(line.data);
> + }
> + free(buf.lines);
> +}
I'm not sure what level of optimization the maintainers expect, but
personally, I think there should be separate implementations for
seekable vs non-seekable files to avoid buffering the entire contents of
the file in memory unnecessarily.
Eric
Received on Mon Mar 04 2024 - 22:39:17 CET
This archive was generated by hypermail 2.3.0
: Tue Mar 05 2024 - 20:36:36 CET