Add read_pipe_all()

Add a convenience function to read from a pipe until all requested data
has been read.
This commit is contained in:
Romain Vimont 2021-10-17 16:20:46 +02:00
parent 96b18dabaa
commit 0681480809
2 changed files with 18 additions and 0 deletions

View file

@ -61,3 +61,18 @@ get_local_file_path(const char *name) {
return file_path;
}
ssize_t
read_pipe_all(pipe_t pipe, char *data, size_t len) {
size_t copied = 0;
while (len > 0) {
ssize_t r = read_pipe(pipe, data, len);
if (r <= 0) {
return copied ? (ssize_t) copied : r;
}
len -= r;
data += r;
copied += r;
}
return copied;
}

View file

@ -96,6 +96,9 @@ is_regular_file(const char *path);
ssize_t
read_pipe(pipe_t pipe, char *data, size_t len);
ssize_t
read_pipe_all(pipe_t pipe, char *data, size_t len);
void
close_pipe(pipe_t pipe);