Michael Forney wrote:
 
> This works, but I'm not sure why `s` is static here, since it is
> cleared at the start of the function, and freed at the end of the
> function.
> 
> Maybe I'm missing something, but I think it would be better to replace
> the first `free(s.str)` with `s.str = NULL`, and make `s` have
> automatic storage.

Like this or I misunderstood?  Because this works as well.
---
static void
join(void)
{
        int i;
        char *t, c;
        String s;

        s.str = NULL;
        s.siz = s.cap = 0;
        for (i = line1;; i = nextln(i)) {
                for (t = gettxt(i); (c = *t) != '\n'; ++t)
                        addchar(*t, &s);
                if (i == line2)
                        break;
        }

        addchar('\n', &s);
        addchar('\0', &s);
        delete(line1, line2);
        inject(s.str, BEFORE);
        free(s.str);
}
---

-- 
caóc

Reply via email to