summaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-10-17 15:51:42 +0200
committerJo-Philipp Wich <jo@mein.io>2022-10-18 09:19:57 +0200
commit00af065057a0e9c10ce6a6475acc47920790c2a9 (patch)
tree8b9888fd3a07a6090ef4edf8e0634281326400d9 /lib
parentf956e92bb5a5fa3268835f472b92c8d84c807db0 (diff)
fs: expose `getdelim()` functionality through `fd.read()`
When `fd.read()` is invoked with a single-character string argument, invoke `getdelim()` internally to read the input until the give character or EOF. This is useful for reading character delimited input data. For example `fd.read('\n')` will read any data up to the first newline (or EOF) while `fd.read('\0x00')` will read until the first null byte. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'lib')
-rw-r--r--lib/fs.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/fs.c b/lib/fs.c
index f5951db..b0b085f 100644
--- a/lib/fs.c
+++ b/lib/fs.c
@@ -73,8 +73,9 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, const char *type)
if (ucv_type(limit) == UC_STRING) {
lstr = ucv_string_get(limit);
+ llen = ucv_string_length(limit);
- if (!strcmp(lstr, "line")) {
+ if (llen == 4 && !strcmp(lstr, "line")) {
llen = getline(&p, &rlen, *fp);
if (llen == -1)
@@ -82,7 +83,7 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, const char *type)
len = (size_t)llen;
}
- else if (!strcmp(lstr, "all")) {
+ else if (llen == 3 && !strcmp(lstr, "all")) {
while (true) {
rlen = fread(buf, 1, sizeof(buf), *fp);
@@ -102,6 +103,14 @@ uc_fs_read_common(uc_vm_t *vm, size_t nargs, const char *type)
break;
}
}
+ else if (llen == 1) {
+ llen = getdelim(&p, &rlen, *lstr, *fp);
+
+ if (llen == -1)
+ err_return(errno);
+
+ len = (size_t)llen;
+ }
else {
return NULL;
}