From 9e5151366f57f8dfe6406da29a606eb52d794b73 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 17 Sep 2016 00:21:04 -0400 Subject: Add initial pbd implementation Error handling in C is tedious. --- .bin/pbd.c | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 .bin/pbd.c (limited to '.bin/pbd.c') diff --git a/.bin/pbd.c b/.bin/pbd.c new file mode 100755 index 00000000..713800d4 --- /dev/null +++ b/.bin/pbd.c @@ -0,0 +1,71 @@ +#if 0 +exec clang -Weverything $@ -o $(dirname $0)/pbd $0 +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main() { + int server = socket(PF_INET, SOCK_STREAM, 0); + if (server < 0) err(EX_OSERR, "socket"); + + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_port = htons(7062), + .sin_addr = { .s_addr = htonl(0x7f000001) }, + }; + + if (bind(server, (struct sockaddr *) &addr, sizeof(addr)) < 0) + err(EX_OSERR, "bind"); + + if (listen(server, 1) < 0) + err(EX_OSERR, "listen"); + + for (;;) { + int client = accept(server, NULL, NULL); + if (client < 0) err(EX_OSERR, "accept"); + + pid_t pid_paste = fork(); + if (pid_paste < 0) err(EX_OSERR, "fork"); + + if (pid_paste) { + if (waitpid(pid_paste, NULL, 0) < 0) + warn("waitpid"); + // TODO: Check child status. + } else { + if (dup2(client, STDOUT_FILENO) < 0) + err(EX_OSERR, "dup2"); + if (execlp("pbpaste", "pbpaste") < 0) + err(EX_OSERR, "execlp"); + } + + uint8_t x; + ssize_t peek = recv(client, &x, 1, MSG_PEEK); + if (peek < 0) err(EX_IOERR, "recv"); + + if (peek) { + pid_t pid_copy = fork(); + if (pid_copy < 0) err(EX_OSERR, "fork"); + + if (pid_copy) { + if (waitpid(pid_copy, NULL, 0) < 0) + warn("waitpid"); + // TODO: Check child status. + } else { + if (dup2(client, STDIN_FILENO) < 0) + err(EX_OSERR, "dup2"); + if (execlp("pbcopy", "pbcopy") < 0) + err(EX_OSERR, "execlp"); + } + } + + if (close(client) < 0) warn("close"); + } +} -- cgit 1.4.1