summaryrefslogtreecommitdiffhomepage
path: root/lib/fs.c
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-10-08 01:15:26 +0200
committerGitHub <noreply@github.com>2022-10-08 01:15:26 +0200
commitf956e92bb5a5fa3268835f472b92c8d84c807db0 (patch)
tree9ee39228787d1e5a2d11a17c1211b7f406d56df2 /lib/fs.c
parent1712e771b92b08d9f46b23a136cfd2bde877b71b (diff)
parent4ae70721aae5f974538928cadc71fa3269e5ad20 (diff)
Merge pull request #115 from jow-/fs-use-getline
fs: use `getline()` for line wise read operations
Diffstat (limited to 'lib/fs.c')
-rw-r--r--lib/fs.c23
1 files changed, 5 insertions, 18 deletions
diff --git a/lib/fs.c b/lib/fs.c
index e0e5179..f5951db 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -64,6 +64,7 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, const char *type)
size_t rlen, len = 0;
const char *lstr;
int64_t lsize;
+ ssize_t llen;
FILE **fp = uc_fn_this(type);
@@ -74,26 +75,12 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, const char *type)
lstr = ucv_string_get(limit);
if (!strcmp(lstr, "line")) {
- while (true) {
- if (!fgets(buf, sizeof(buf), *fp))
- break;
+ llen = getline(&p, &rlen, *fp);
- rlen = strlen(buf);
- tmp = realloc(p, len + rlen + 1);
-
- if (!tmp) {
- free(p);
- err_return(ENOMEM);
- }
+ if (llen == -1)
+ err_return(errno);
- snprintf(tmp + len, rlen + 1, "%s", buf);
-
- p = tmp;
- len += rlen;
-
- if (rlen > 0 && buf[rlen - 1] == '\n')
- break;
- }
+ len = (size_t)llen;
}
else if (!strcmp(lstr, "all")) {
while (true) {