int sh_execute(char **args, int argc) { int i; char ***cmd; int cmdn; if(argc > 2){ // printf("%d\n", argc); cmd = split_multiple(argc, args, &cmdn); return fork_pipes(cmdn, cmd); } if (args[0] == NULL) { // An empty command was entered. return 1; } for (i = 0; i < sh_num_builtins(); i++) { if (strcmp(args[0], builtin_str[i]) == 0) { return (*builtin_func[i])(args); } } return sh_launch(args); }
int main(int argc, char **argv) { /*TODO Define isSignal, should be set by a macro at compilation time*/ int i; if (argc == 1) /* There were no arguments */ { char *printenv[] = { "printenv", 0}; char *sort[] = { "sort", 0 }; char *less[] = { "less", 0 }; struct command cmd[] = { {printenv}, {sort}, {less} }; fork_pipes(3, cmd); } else { char *tmp; int len = 1; for (i = 1; i < argc; i++) { len += strlen(argv[i]) + 2; } tmp = (char *) malloc(len); tmp[0] = '\0'; int pos = 0; for (i = 1; i < argc; i++) { pos += sprintf(tmp + pos, "%s%s", (i == 1 ? "" : "|"), argv[i]); } char *printenv[] = { "printenv", 0}; char *grep[] = { "grep", "-E", tmp, NULL}; char *sort[] = { "sort", 0 }; char *less[] = { "less", 0 }; struct command cmd[] = { {printenv}, {grep}, {sort}, {less} }; fork_pipes(4, cmd); free(tmp); } return(0); }
int main () { const char *ls[] = { "ls", "-l", 0 }; const char *awk[] = { "awk", "{print $1}", 0 }; const char *sort[] = { "sort", 0 }; const char *uniq[] = { "uniq", 0 }; struct command cmd [] = { {ls}, {awk}, {sort}, {uniq} }; return fork_pipes (4, cmd); }
int main () { const char *ls[] = { "ls", "-l", 0 }; const char *awk[] = { "awk", "{print $1}", 0 }; const char *sort[] = { "sort", 0 }; const char *uniq[] = { "uniq", 0 }; struct command cmd [] = { {ls}, {awk}, {sort}, {uniq} }; printf("!!!!!IN SPAWN:%s\n", cmd[1].argv[0]); /*I added to understand the cmd struct*/ return fork_pipes (4, cmd); }
void execute(int cmdc,command *cmd) { pid_t pid; int status; if ((pid = fork()) < 0) { printf("*** ERROR: forking child process failed\n"); exit(1); } else if (pid == 0) { fork_pipes(cmdc,cmd); } else { while (wait(&status) != pid) ; } }