From: Moritz Warning Date: Sun, 18 Jan 2015 01:01:14 +0000 (+0100) Subject: sockread: add support for reading data from a pipe X-Git-Url: http://git.lede-project.org./?a=commitdiff_plain;h=04a518498c6bdcbe24c9e9f92ed525f3551e0431;p=feed%2Fpackages.git sockread: add support for reading data from a pipe --- diff --git a/utils/sockread/Makefile b/utils/sockread/Makefile index 78beee2483..d3c15d468a 100644 --- a/utils/sockread/Makefile +++ b/utils/sockread/Makefile @@ -18,7 +18,7 @@ define Package/sockread endef define Package/sockread/description - sockread reads data from a Unix domain socket + sockread writes and reads data from a Unix domain socket represented as a special file on the file system. endef diff --git a/utils/sockread/src/main.c b/utils/sockread/src/main.c index 3343f2a456..06c21def4d 100644 --- a/utils/sockread/src/main.c +++ b/utils/sockread/src/main.c @@ -3,15 +3,16 @@ #include #include #include -#include -#include +#include #include #include - int main(int argc, char *argv[]) { + char buf[1024]; + ssize_t r; + if (argc != 2) { - fprintf(stderr, "Usage: %s \n", argv[0]); + fprintf(stderr, "Write to and read from a Unix domain socket.\n\nUsage: %s \n", argv[0]); return 1; } @@ -36,8 +37,15 @@ int main(int argc, char *argv[]) { return 1; } - char buf[1024]; - ssize_t r; + /* Check if stdin refers to a terminal */ + if (!isatty(fileno(stdin))) { + /* Read from stdin and write to socket */ + while (0 < (r = fread(buf, 1, sizeof(buf), stdin))) { + send(fd, buf, r, 0); + } + } + + /* Read from socket and write to stdout */ while (1) { r = recv(fd, buf, sizeof(buf), 0); if (r < 0) {