shorten a little
This commit is contained in:
parent
e1334ef022
commit
6221ba9b8c
@ -1,7 +1,5 @@
|
|||||||
#include <errno.h> /* errno */
|
|
||||||
#include <fcntl.h> /* open(2), O_RDONLY */
|
#include <fcntl.h> /* open(2), O_RDONLY */
|
||||||
#include <stdio.h> /* fprintf(3) */
|
#include <stdio.h> /* fprintf(3), perror(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
|
||||||
@ -10,23 +8,6 @@
|
|||||||
#include <sys/wait.h> /* wait(2), WIFEXITED, WEXITSTATUS */
|
#include <sys/wait.h> /* wait(2), WIFEXITED, WEXITSTATUS */
|
||||||
#include "libpsargs.h"
|
#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 main(int argc, char *argv[]){
|
||||||
int child;
|
int child;
|
||||||
char **corr;
|
char **corr;
|
||||||
@ -35,13 +16,21 @@ int main(int argc, char *argv[]){
|
|||||||
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]);
|
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 */
|
/* substitute standard input for /dev/null */
|
||||||
if((fd = dup(STDIN_FILENO)) < 0
|
if((fd = dup(STDIN_FILENO)) < 0
|
||||||
|| dup2(open("/dev/null", O_RDONLY), STDIN_FILENO) < 0)
|
|| dup2(open("/dev/null", O_RDONLY), STDIN_FILENO)
|
||||||
return oserr(argv[0], strerror(errno));
|
< 0){
|
||||||
|
perror(argv[0]);
|
||||||
|
return EX_OSERR;
|
||||||
|
}
|
||||||
|
|
||||||
for(i = 0; ; ++i){
|
for(i = 0; ; ++i){
|
||||||
*(corr = corresponding_arg(curr++)) = NULL;
|
*(corr = corresponding_arg(curr++)) = NULL;
|
||||||
@ -51,8 +40,10 @@ int main(int argc, char *argv[]){
|
|||||||
|
|
||||||
if( r == -1 /* fork(2) failed */
|
if( r == -1 /* fork(2) failed */
|
||||||
|| i == 1 || r == 0 /* exec(3) failed */
|
|| i == 1 || r == 0 /* exec(3) failed */
|
||||||
|| dup2(fd, STDIN_FILENO) != STDIN_FILENO)
|
|| dup2(fd, STDIN_FILENO) != STDIN_FILENO){
|
||||||
return oserr(argv[0], strerror(errno));
|
perror(argv[0]);
|
||||||
|
return EX_OSERR;
|
||||||
|
}
|
||||||
|
|
||||||
wait(&child);
|
wait(&child);
|
||||||
if(!WIFEXITED(child) || WEXITSTATUS(child) != 0)
|
if(!WIFEXITED(child) || WEXITSTATUS(child) != 0)
|
||||||
|
Loading…
Reference in New Issue
Block a user