Introduction
Introduction Statistics Contact Development Disclaimer Help
scanfile(): reuse line-buffer - geomyidae - A small C-based gopherd.
git clone git://bitreich.org/geomyidae/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfri…
Log
Files
Refs
Tags
README
LICENSE
---
commit 925572f3afedf7a80e13edd1233cc04c1a4a60f0
parent 2361134beb5458adbff043ea611884a090d56d42
Author: Hiltjo Posthuma <[email protected]>
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(-)
---
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);
You are viewing proxied material from bitreich.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.