diff options
author | Moritz Warning <moritzwarning@web.de> | 2015-01-18 02:01:14 +0100 |
---|---|---|
committer | Moritz Warning <moritzwarning@web.de> | 2015-01-20 13:43:59 +0100 |
commit | 04a518498c6bdcbe24c9e9f92ed525f3551e0431 (patch) | |
tree | cad387c4ce1ed909d8187313637673b155fbbc6f /utils/sockread/src/main.c | |
parent | e5c39cc9997baccb4a66792e245326c9aadd92be (diff) |
sockread: add support for reading data from a pipe
Diffstat (limited to 'utils/sockread/src/main.c')
-rw-r--r-- | utils/sockread/src/main.c | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/utils/sockread/src/main.c b/utils/sockread/src/main.c index 3343f2a45..06c21def4 100644 --- a/utils/sockread/src/main.c +++ b/utils/sockread/src/main.c @@ -3,15 +3,16 @@ #include <stdlib.h> #include <errno.h> #include <stddef.h> -#include <stdio.h> -#include <string.h> +#include <unistd.h> #include <sys/socket.h> #include <sys/un.h> - int main(int argc, char *argv[]) { + char buf[1024]; + ssize_t r; + if (argc != 2) { - fprintf(stderr, "Usage: %s <socket>\n", argv[0]); + fprintf(stderr, "Write to and read from a Unix domain socket.\n\nUsage: %s <socket>\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) { |