diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-10-07 23:18:06 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-10-07 23:19:39 +0200 |
commit | 4ae70721aae5f974538928cadc71fa3269e5ad20 (patch) | |
tree | a74c75aebd2d43bdae2ae3bb323981d943274e9f | |
parent | f8e00b4b83dad76e183b8293870cfe3110f1fa94 (diff) |
fs: use `getline()` for line wise read operations
Use `getline()` instead of a custom `fgets()` wrapper logic to perform
line wise reads from open file handles. This is required to properly
deal with lines containing embedded null bytes.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-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) { |