summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <programble@gmail.com>2016-09-18 13:15:05 -0400
committerJune McEnroe <programble@gmail.com>2016-09-18 13:15:05 -0400
commit1f1e44096daa4928649808997b3d9bf5efee6cc8 (patch)
treed0a203fd7eb2cdf4d76344728d98d92b42eb3bb3
parentForward pbd through *.local SSH (diff)
downloadsrc-1f1e44096daa4928649808997b3d9bf5efee6cc8.tar.gz
src-1f1e44096daa4928649808997b3d9bf5efee6cc8.zip
Clean up error handling in pbd
-rwxr-xr-x.bin/pbd.c29
1 files changed, 17 insertions, 12 deletions
diff --git a/.bin/pbd.c b/.bin/pbd.c
index 47fdedc4..9e716d6d 100755
--- a/.bin/pbd.c
+++ b/.bin/pbd.c
@@ -8,6 +8,7 @@ exec clang -Weverything $@ -o $(dirname $0)/pbd $0
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/socket.h>
+#include <sys/wait.h>
 #include <sysexits.h>
 #include <unistd.h>
 
@@ -17,19 +18,23 @@ static void spawn(const char *cmd, int child_fd, int parent_fd) {
 
     if (pid) {
         int status;
-        if (waitpid(pid, &status, 0) < 0)
-            err(EX_OSERR, "waitpid");
+        pid_t wait = waitpid(pid, &status, 0);
+        if (wait < 0) err(EX_OSERR, "waitpid");
+
         if (status)
             warnx("child %s status %d", cmd, status);
     } else {
-        if (dup2(parent_fd, child_fd) < 0)
-            err(EX_OSERR, "dup2");
-        if (execlp(cmd, cmd) < 0)
-            err(EX_OSERR, "execlp");
+        int fd = dup2(parent_fd, child_fd);
+        if (fd < 0) err(EX_OSERR, "dup2");
+
+        int error = execlp(cmd, cmd);
+        if (error) err(EX_OSERR, "execlp");
     }
 }
 
 int main() {
+    int error;
+
     int server = socket(PF_INET, SOCK_STREAM, 0);
     if (server < 0) err(EX_OSERR, "socket");
 
@@ -39,11 +44,11 @@ int main() {
         .sin_addr = { .s_addr = htonl(0x7f000001) },
     };
 
-    if (bind(server, (struct sockaddr *) &addr, sizeof(addr)) < 0)
-        err(EX_OSERR, "bind");
+    error = bind(server, (struct sockaddr *) &addr, sizeof(addr));
+    if (error) err(EX_OSERR, "bind");
 
-    if (listen(server, 1) < 0)
-        err(EX_OSERR, "listen");
+    error = listen(server, 1);
+    if (error) err(EX_OSERR, "listen");
 
     for (;;) {
         int client = accept(server, NULL, NULL);
@@ -57,7 +62,7 @@ int main() {
 
         if (peek) spawn("pbcopy", STDIN_FILENO, client);
 
-        if (close(client) < 0)
-            err(EX_IOERR, "close");
+        error = close(client);
+        if (error) err(EX_IOERR, "close");
     }
 }