aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/file.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2022-08-26 05:07:32 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2022-08-26 05:07:32 +0200
commita593789a46ceb27d26186db7b3215b2a046ff865 (patch)
tree978ac4dbf25e1b9fb7da8299d1abb9514017420a /src/file.c
parentadd some more code (diff)
downloadcimple-a593789a46ceb27d26186db7b3215b2a046ff865.tar.gz
cimple-a593789a46ceb27d26186db7b3215b2a046ff865.zip
worker: capture process output
Diffstat (limited to 'src/file.c')
-rw-r--r--src/file.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/src/file.c b/src/file.c
index ede640b..0273e71 100644
--- a/src/file.c
+++ b/src/file.c
@@ -59,3 +59,41 @@ int file_exists(const char *path)
int ret = lstat(path, &stat);
return !ret && S_ISREG(stat.st_mode);
}
+
+int file_read(int fd, char **output, size_t *len)
+{
+ char buf[128];
+ size_t buf_len = sizeof(buf) / sizeof(buf[0]);
+ int ret = 0;
+
+ *output = NULL;
+ *len = 0;
+
+ while (1) {
+ ssize_t read_now = read(fd, buf, buf_len);
+
+ if (read_now < 0) {
+ print_errno("read");
+ ret = read_now;
+ goto free_output;
+ }
+
+ if (!read_now)
+ goto exit;
+
+ *output = realloc(*output, *len + read_now + 1);
+ if (!*output) {
+ print_errno("realloc");
+ return -1;
+ }
+ memcpy(*output + *len, buf, read_now);
+ *len += read_now;
+ *(*output + *len) = '\0';
+ }
+
+free_output:
+ free(*output);
+
+exit:
+ return ret;
+}