ed

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

commit b9eaf38210bc9b98df0f36eeef7c15b9d4cf1dfd
parent 1435ae52c39e108c0764872377ed478e610cb4b5
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Tue,  1 Dec 2015 18:44:42 +0100

ed: doread: simplify getline

Diffstat:
Med.c | 18+++++++-----------
1 file changed, 7 insertions(+), 11 deletions(-)

diff --git a/ed.c b/ed.c @@ -447,7 +447,8 @@ static void doread(char *fname) { FILE *fp; - size_t len = 0, cnt, n; + size_t len = 0, cnt; + ssize_t n; char *s = NULL, *p; if (!savfname) @@ -455,17 +456,12 @@ doread(char *fname) if (!(fp = fopen(fname, "r"))) error("input/output error"); curln = line2; - for (cnt = 0; getline(&s, &len, fp) > 0; ++cnt) { - if (!(n = strlen(s))) - break; + for (cnt = 0; (n = getline(&s, &len, fp)) > 0; ++cnt) { if (s[n-1] != '\n') { - if (n == len) { - if (len == SIZE_MAX || - !(p = realloc(s, len + 1))) - error("out of memory"); - ++len; - s = p; - } + if (len == SIZE_MAX || + !(p = realloc(s, ++len))) + error("out of memory"); + s = p; s[n-1] = '\n'; s[n] = '\0'; }