Re: [hackers] [sbase][PATCH] Make 'w' command print byte count
Thanks for the patch.
On 2020-09-07, Tait Hoyem <code_AT_tait.tech> wrote:
> diff --git a/ed.c b/ed.c
> index cee9687..82b9c2c 100644
> --- a/ed.c
> +++ b/ed.c
> _AT_@ -623,14 +623,16 @@ static void
> dowrite(const char *fname, int trunc)
> {
> FILE *fp;
> - int i, line;
> + int i, line, bytecount;
I think we should use a different type than int here. I'm not sure if
size_t or off_t is more appropriate, but size_t is probably
reasonable.
>
> if (!(fp = fopen(fname, (trunc) ? "w" : "a")))
> error("input/output error");
>
> line = curln;
> - for (i = line1; i <= line2; ++i)
> + for (i = line1; i <= line2; ++i) {
> + bytecount += strlen(gettxt(i));
> fputs(gettxt(i), fp);
I think we should try to avoid recomputing gettxt(i) here. In fact,
looking at the body of gettxt, it actually stores the result in
text.str, along with its length in text.siz.
What do you think about the following?
gettxt(i);
bytecount += text.siz - 1;
fwrite(text.str, 1, text.siz - 1);
> + }
This should be indented with a tab.
>
> curln = line2;
> if (fclose(fp))
> _AT_@ -638,6 +640,7 @@ dowrite(const char *fname, int trunc)
> strcpy(savfname, fname);
> modflag = 0;
> curln = line;
> + printf("%d\n", bytecount);
> }
>
> static void
>
> diff --git a/TODO b/TODO
> index 59c9440..92acfa3 100644
> --- a/TODO
> +++ b/TODO
> _AT_@ -55,7 +55,6 @@ ed
> line
> .
> 1g/^$/p
> -* w command doesn't print byte count.
> * Editing huge files doesn't work well.
>
> printf
>
>
Received on Mon Sep 14 2020 - 00:14:53 CEST
This archive was generated by hypermail 2.3.0
: Mon Sep 14 2020 - 00:24:30 CEST