1
0

shorten a little

This commit is contained in:
dtb 2024-01-13 11:23:01 -07:00
parent e1334ef022
commit 6221ba9b8c

View File

@ -1,7 +1,5 @@
#include <errno.h> /* errno */
#include <fcntl.h> /* open(2), O_RDONLY */
#include <stdio.h> /* fprintf(3) */
#include <string.h> /* strerror(3) */
#include <stdio.h> /* fprintf(3), perror(3) */
#if !defined EX_OK || !defined EX_OSERR || !defined EX_USAGE
# include <sysexits.h>
#endif
@ -10,23 +8,6 @@
#include <sys/wait.h> /* wait(2), WIFEXITED, WEXITSTATUS */
#include "libpsargs.h"
int oserr(char *argv0, char *s){
fprintf(stderr, "%s: %s\n", argv0, s);
return EX_OSERR;
}
int usage(char *s){
fprintf(stderr,
"Usage: %s \"[\" [command (arguments...)] \"]\""
" \"[\" [command (arguments...)] \"]\"\n",
s);
return EX_USAGE;
}
int main(int argc, char *argv[]){
int child;
char **corr;
@ -35,13 +16,21 @@ int main(int argc, char *argv[]){
int i;
int r;
if(argc < 2 || check_arg(curr = &argv[1]) != 2)
return usage(argc == 0 ? "<no argv[0]>" : argv[0]);
if(argc < 2 || check_arg(curr = &argv[1]) != 2){
fprintf(stderr,
"Usage: %s \"[\" [command (arguments...)] \"]\""
" \"[\" [command (arguments...)] \"]\"\n",
argc == 0 ? "<no argv[0]>" : argv[0]);
return EX_USAGE;
}
/* 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));
|| dup2(open("/dev/null", O_RDONLY), STDIN_FILENO)
< 0){
perror(argv[0]);
return EX_OSERR;
}
for(i = 0; ; ++i){
*(corr = corresponding_arg(curr++)) = NULL;
@ -51,8 +40,10 @@ int main(int argc, char *argv[]){
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));
|| dup2(fd, STDIN_FILENO) != STDIN_FILENO){
perror(argv[0]);
return EX_OSERR;
}
wait(&child);
if(!WIFEXITED(child) || WEXITSTATUS(child) != 0)