ed

simple ed
git clone git://git.2f30.org/ed
Log | Files | Refs | LICENSE

commit 513fd592d1efb2b23572a894fe2eb02d9b31bcfb
parent 80c2cda614620d10054464447dc8c4b4399a27d8
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  3 Dec 2015 09:50:44 +0100

Convert addchar into a generic function

 gettxt
but it can be useful to use it in another places with different
buffers.

Diffstat:
Med.c | 26+++++++++++++-------------
1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/ed.c b/ed.c @@ -69,19 +69,19 @@ prevln(int line) return (line < 0) ? lastln : line; } -static void -addchar(char c) +static char * +addchar(char c, char *t, size_t *capacity, size_t *size) { - char *p; + size_t cap = *capacity, siz = *size; - if (sizetxt >= memtxt) { - if (memtxt > SIZE_MAX - LINESIZE || - !(p = realloc(text, memtxt + LINESIZE))) + if (siz >= cap && + (cap > SIZE_MAX - LINESIZE || + (t = realloc(t, cap += LINESIZE)) == NULL)) error("out of memory"); - memtxt += LINESIZE; - text = p; - } - text[sizetxt++] = c; + t[siz++] = c; + *size = siz; + *capacity = cap; + return t; } static int @@ -157,13 +157,13 @@ repeat: } for (p = buf + off - lasto; p < buf + csize && *p != '\n'; ++p) { ++off; - addchar(*p); + text = addchar(*p, text, &memtxt, &sizetxt); } if (csize && p == buf + csize) goto repeat; - addchar('\n'); - addchar('\0'); + text = addchar('\n', text, &memtxt, &sizetxt); + text = addchar('\0', text, &memtxt, &sizetxt); return text; }