recursion
This commit is contained in:
parent
b484c81320
commit
f85bb632e2
@ -7,44 +7,36 @@
|
|||||||
|
|
||||||
static char *program_name = "pscat";
|
static char *program_name = "pscat";
|
||||||
|
|
||||||
/* Originally designed to use parentheses but changed to brackets to escape the
|
|
||||||
* hassle of escaping them from the Bourne shell. */
|
|
||||||
#define L_PAREN '['
|
#define L_PAREN '['
|
||||||
#define R_PAREN ']'
|
#define R_PAREN ']'
|
||||||
|
|
||||||
int main(int argc, char *argv[]){
|
int
|
||||||
char **psstart;
|
forking(char **argv){
|
||||||
int child;
|
int child;
|
||||||
int i;
|
char **corr;
|
||||||
unsigned int retval;
|
|
||||||
int terms;
|
|
||||||
|
|
||||||
retval = 0;
|
corr = corresponding_arg(argv, L_PAREN, R_PAREN);
|
||||||
|
*corr = NULL;
|
||||||
|
if(fork() == 0){
|
||||||
|
execvp(argv[1], argv+1);
|
||||||
|
}else{
|
||||||
|
wait(&child);
|
||||||
|
argv = corr;
|
||||||
|
if(*++argv == NULL)
|
||||||
|
return WIFEXITED(child) * WEXITSTATUS(child);
|
||||||
|
else
|
||||||
|
return forking(argv);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if((terms = check_arg(++argv, L_PAREN, R_PAREN)) == 0){
|
int main(int argc, char *argv[]){
|
||||||
|
|
||||||
|
if((check_arg(++argv, L_PAREN, R_PAREN)) == 0){
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"Usage: %s \"[\" [utility [argument...]] \"]\" ...\n",
|
"Usage: %s \"[\" [utility [argument...]] \"]\" ...\n",
|
||||||
argv[-1] == NULL ? program_name : argv[-1]
|
argv[-1] == NULL ? program_name : argv[-1]
|
||||||
);
|
);
|
||||||
return EX_USAGE;
|
return EX_USAGE;
|
||||||
}
|
|
||||||
|
|
||||||
/* loop starts with *argv -> the next L_PAREN */
|
|
||||||
for(i = 0; i < terms; ++i){
|
|
||||||
psstart = argv + 1;
|
|
||||||
argv = corresponding_arg(argv, L_PAREN, R_PAREN);
|
|
||||||
/* *argv -> the corresponding R_PAREN. turn it into NULL to
|
|
||||||
* terminate the argument list to send to execvp(3) */
|
|
||||||
*argv = NULL;
|
|
||||||
if(fork() == 0){
|
|
||||||
execvp(psstart[0], psstart);
|
|
||||||
}else
|
}else
|
||||||
wait(&child);
|
return forking(argv);
|
||||||
/* interpret status information from wait(2) */
|
|
||||||
if(WIFEXITED(child))
|
|
||||||
retval += WEXITSTATUS(child);
|
|
||||||
++argv;
|
|
||||||
}
|
|
||||||
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user