scanfile(): reuse line-buffer - geomyidae - A small C-based gopherd. HTML git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/geomyidae/ DIR Log DIR Files DIR Refs DIR Tags DIR README DIR LICENSE --- DIR commit 925572f3afedf7a80e13edd1233cc04c1a4a60f0 DIR parent 2361134beb5458adbff043ea611884a090d56d42 HTML Author: Hiltjo Posthuma <hiltjo@codemadness.org> Date: Sun, 11 Jun 2017 20:00:21 +0200 scanfile(): reuse line-buffer Diffstat: M ind.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) --- DIR 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);