[hackers] [sbase] libutil/getlines: use known line length || Hiltjo Posthuma
commit 3f017068376baab48e935885c7870e99c6421fcd
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Tue May 5 14:55:09 2015 +0200
libutil/getlines: use known line length
also style: linelen = length of getline(), this was slightly confusing.
diff --git a/libutil/getlines.c b/libutil/getlines.c
index 820c781..3a4d0f4 100644
--- a/libutil/getlines.c
+++ b/libutil/getlines.c
_AT_@ -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';
}
}
Received on Thu May 07 2015 - 19:18:55 CEST
This archive was generated by hypermail 2.3.0
: Thu May 07 2015 - 19:24:14 CEST