commit 3f017068376baab48e935885c7870e99c6421fcd
parent adf9f475250e56067c4409f8a515b0005f1a2f3b
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Tue, 5 May 2015 14:55:09 +0200
libutil/getlines: use known line length
also style: linelen = length of getline(), this was slightly confusing.
Diffstat:
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/libutil/getlines.c b/libutil/getlines.c
@@ -18,13 +18,13 @@ getlines(FILE *fp, struct linebuf *b)
b->capacity += 512;
b->lines = erealloc(b->lines, b->capacity * sizeof(*b->lines));
}
- linelen = len + 1;
- b->lines[b->nlines - 1] = memcpy(emalloc(linelen), line, linelen);
+ linelen = len;
+ b->lines[b->nlines - 1] = memcpy(emalloc(linelen + 1), line, linelen + 1);
}
free(line);
- if (b->lines && b->nlines && !strchr(b->lines[b->nlines - 1], '\n')) {
- b->lines[b->nlines - 1] = erealloc(b->lines[b->nlines - 1], linelen + 1);
- b->lines[b->nlines - 1][linelen - 1] = '\n';
- b->lines[b->nlines - 1][linelen] = '\0';
+ if (b->lines && b->nlines && b->lines[b->nlines - 1][linelen - 1] != '\n') {
+ b->lines[b->nlines - 1] = erealloc(b->lines[b->nlines - 1], linelen + 2);
+ b->lines[b->nlines - 1][linelen] = '\n';
+ b->lines[b->nlines - 1][linelen + 1] = '\0';
}
}