commit 925572f3afedf7a80e13edd1233cc04c1a4a60f0
parent 2361134beb5458adbff043ea611884a090d56d42
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date: Sun, 11 Jun 2017 20:00:21 +0200
scanfile(): reuse line-buffer
Diffstat:
ind.c | | | 20 | ++++++++++++-------- |
1 file changed, 12 insertions(+), 8 deletions(-)
diff --git a/ind.c b/ind.c
@@ -229,27 +229,31 @@ addindexs(Indexs *idx, Elems *el)
Indexs *
scanfile(char *fname)
{
- char *ln;
- int fd;
+ char *ln = NULL;
+ size_t linesiz = 0;
+ ssize_t n;
+ FILE *fp;
Indexs *ret;
Elems *el;
- fd = open(fname, O_RDONLY);
- if(fd < 0)
+ if (!(fp = fopen(fname, "r")))
return nil;
ret = xcalloc(1, sizeof(Indexs));
- while((ln = readln(fd)) != nil) {
+ while ((n = getline(&ln, &linesiz, fp)) > 0) {
+ if (ln[n - 1] == '\n')
+ ln[--n] = '\0';
el = getadv(ln);
- free(ln);
if(el == nil)
continue;
addindexs(ret, el);
- el = nil;
}
- close(fd);
+ if (ferror(fp))
+ perror("getline");
+ free(ln);
+ fclose(fp);
if(ret->n == nil) {
free(ret);