From 250886a9e1dce0526e1d1dc20743ecd15907f63f Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 12 Jun 2017 05:21:34 +0100 Subject: [PATCH] Use read_fd to get pipe command from stdin instead of ad-hoc code read_fd will also now throw on read error instead of just returning the data read so far as if nothing failed. --- src/file.cc | 10 ++++++---- src/main.cc | 13 +------------ 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/file.cc b/src/file.cc index 420c957b..d7ff2544 100644 --- a/src/file.cc +++ b/src/file.cc @@ -41,6 +41,9 @@ public: file_access_error(StringView filename, StringView error_desc) : runtime_error(format("{}: {}", filename, error_desc)) {} + + file_access_error(int fd, StringView error_desc) + : runtime_error(format("fd {}: {}", fd, error_desc)) {} }; String parse_filename(StringView filename) @@ -168,11 +171,10 @@ String read_fd(int fd, bool text) String content; constexpr size_t bufsize = 256; char buf[bufsize]; - while (true) + while (ssize_t size = read(fd, buf, bufsize)) { - ssize_t size = read(fd, buf, bufsize); - if (size == -1 or size == 0) - break; + if (size == -1) + throw file_access_error{fd, strerror(errno)}; if (text) { diff --git a/src/main.cc b/src/main.cc index cec1186c..8c5c4fde 100644 --- a/src/main.cc +++ b/src/main.cc @@ -744,20 +744,9 @@ int run_filter(StringView keystr, StringView commands, ConstArrayView