From e1334ef0226e74d6e6cf5d3e02ad896d1a9a30d5 Mon Sep 17 00:00:00 2001 From: DTB Date: Sat, 13 Jan 2024 11:16:21 -0700 Subject: [PATCH] make program match ideal --- psrelay/psrelay.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/psrelay/psrelay.c b/psrelay/psrelay.c index fffb911..f1ef05b 100644 --- a/psrelay/psrelay.c +++ b/psrelay/psrelay.c @@ -1,10 +1,11 @@ #include /* errno */ +#include /* open(2), O_RDONLY */ #include /* fprintf(3) */ #include /* strerror(3) */ #if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE # include #endif -#include /* fork(2) */ +#include /* dup(2), dup2(2), fork(2), STDIN_FILENO */ #include /* */ #include /* wait(2), WIFEXITED, WEXITSTATUS */ #include "libpsargs.h" @@ -30,18 +31,27 @@ int main(int argc, char *argv[]){ int child; char **corr; char **curr; + int fd; int i; int r; if(argc < 2 || check_arg(curr = &argv[1]) != 2) return usage(argc == 0 ? "" : argv[0]); - do{ + /* substitute standard input for /dev/null */ + if((fd = dup(STDIN_FILENO)) < 0 + || dup2(open("/dev/null", O_RDONLY), STDIN_FILENO) < 0) + return oserr(argv[0], strerror(errno)); + + for(i = 0; ; ++i){ *(corr = corresponding_arg(curr++)) = NULL; if(i == 1 || (r = fork()) == 0) execvp(*curr, curr); - if(i == 1 || r == -1 || r == 0) + + if( r == -1 /* fork(2) failed */ + || i == 1 || r == 0 /* exec(3) failed */ + || dup2(fd, STDIN_FILENO) != STDIN_FILENO) return oserr(argv[0], strerror(errno)); wait(&child); @@ -49,7 +59,7 @@ int main(int argc, char *argv[]){ return 1; curr = corr + 1; - }while(++i < 2); - /* UNREACHABLE */ + } + /* UNREACHABLE */ }