commit dce88200ff80053a5b68740a5d43eab48a33cbe2
parent 74206e384134e4c0e2db7882ba804651beccdbd4
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sun, 20 Dec 2020 14:19:12 -0800
Make helper function for setting up pipes
Diffstat:
M | main.c | | | 21 | ++++++++++++++------- |
1 file changed, 14 insertions(+), 7 deletions(-)
diff --git a/main.c b/main.c
@@ -278,6 +278,17 @@ gphcache(const char *cache, const char *host, const char *path, const char *port
}
int
+pipedup(int old, int new, int fildes[2])
+{
+ if (dup2(old, new) == -1 ||
+ close(fildes[0]) || close(fildes[1])) {
+ warn("unable to setup pipe");
+ return 1;
+ }
+ return 0;
+}
+
+int
gawkat(const char *path)
{
int fds[2];
@@ -293,13 +304,11 @@ gawkat(const char *path)
warn("unable to fork");
return 1;
case 0:
- if (dup2(fds[0], STDIN_FILENO) == -1 || close(fds[0]) || close(fds[1])) {
- warn("unable to setup child pipe");
+ if (pipedup(fds[0], STDIN_FILENO, fds) != 0)
_exit(EXIT_FAILURE);
- }
execvp(*gphfmt, gphfmt);
warn("execvp '%s'", *gphfmt);
- _exit(EXIT_FAILURE);
+ _exit(1);
}
switch (fork()) {
@@ -307,10 +316,8 @@ gawkat(const char *path)
warn("unable to fork");
return 1;
case 0:
- if (dup2(fds[1], STDOUT_FILENO) == -1 || close(fds[0]) || close(fds[1])) {
- warn("unable to setup parent pipe");
+ if (pipedup(fds[1], STDOUT_FILENO, fds) != 0)
_exit(1);
- }
if (putfile(path) != 0)
_exit(1);
_exit(0);