diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-10-08 01:15:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-08 01:15:26 +0200 |
commit | f956e92bb5a5fa3268835f472b92c8d84c807db0 (patch) | |
tree | 9ee39228787d1e5a2d11a17c1211b7f406d56df2 /lib/fs.c | |
parent | 1712e771b92b08d9f46b23a136cfd2bde877b71b (diff) | |
parent | 4ae70721aae5f974538928cadc71fa3269e5ad20 (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.c | 23 |
1 files changed, 5 insertions, 18 deletions
@@ -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) { |