1
0

make program match ideal

This commit is contained in:
dtb 2024-01-13 11:16:21 -07:00
parent 928fef0786
commit e1334ef022

View File

@ -1,10 +1,11 @@
#include <errno.h> /* errno */ #include <errno.h> /* errno */
#include <fcntl.h> /* open(2), O_RDONLY */
#include <stdio.h> /* fprintf(3) */ #include <stdio.h> /* fprintf(3) */
#include <string.h> /* strerror(3) */ #include <string.h> /* strerror(3) */
#if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE #if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE
# include <sysexits.h> # include <sysexits.h>
#endif #endif
#include <unistd.h> /* fork(2) */ #include <unistd.h> /* dup(2), dup2(2), fork(2), STDIN_FILENO */
#include <sys/types.h> /* <sys/wait.h> */ #include <sys/types.h> /* <sys/wait.h> */
#include <sys/wait.h> /* wait(2), WIFEXITED, WEXITSTATUS */ #include <sys/wait.h> /* wait(2), WIFEXITED, WEXITSTATUS */
#include "libpsargs.h" #include "libpsargs.h"
@ -30,18 +31,27 @@ int main(int argc, char *argv[]){
int child; int child;
char **corr; char **corr;
char **curr; char **curr;
int fd;
int i; int i;
int r; int r;
if(argc < 2 || check_arg(curr = &argv[1]) != 2) if(argc < 2 || check_arg(curr = &argv[1]) != 2)
return usage(argc == 0 ? "<no argv[0]>" : argv[0]); return usage(argc == 0 ? "<no argv[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; *(corr = corresponding_arg(curr++)) = NULL;
if(i == 1 || (r = fork()) == 0) if(i == 1 || (r = fork()) == 0)
execvp(*curr, curr); 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)); return oserr(argv[0], strerror(errno));
wait(&child); wait(&child);
@ -49,7 +59,7 @@ int main(int argc, char *argv[]){
return 1; return 1;
curr = corr + 1; curr = corr + 1;
}while(++i < 2); }
/* UNREACHABLE */
/* UNREACHABLE */
} }