void runCommand(char ** args) { pid_t pid; bool bg; pid = fork(); bg = is_background(args); if (pid < 0) { /* error */ printf("Error in forking. We should check this out."); } else if (pid == 0) { /* Child */ if (execvp(args[0], args) < 0) { printf("Could not find program or it failed in some way\n"); exit(1); } if (bg) { /* should the child do something special when its in bg mode? */ free(args); } } else { /* parent */ if (!bg) { int status; print_pid(pid); while (wait(&status) != pid); /* wait for completion HITTAD DEN RAD ONLINE */ print_pid(pid); free(args); } } }
int execute(char **args) { if(args==NULL || args[0]==NULL) return 1; int i,len; i=0; len=num_bultin(); for(i=0;i<len;i++) { if(!strcmp(args[0],builtin_name[i])) return (*builtin_function[i])(args); } int background=is_background(args); int status=exec_child(args,background); return status; }
void runCommand(char ** args) { pid_t pid; bool bg; pid = fork(); bg = is_background(args); pipe(pipes[0]); if (pid < 0) { /* error */ printf("Error in forking. We should check this out."); } else if (pid == 0) { /* Child */ if (execvp(args[0], args) < 0) { printf("Could not find program or it failed in some way\n"); exit(1); } if (bg) { /* should the child do something special when its in bg mode? */ free(args); } char string[] = "hej"; close(pipes[0][READ]); write(pipes[0][WRITE], string, strlen(string)+1); close(pipes[0][WRITE]); } else { /* parent */ if (!bg) { int status; print_pid(pid); while (wait(&status) != pid); /* wait for completion HITTAD DEN RAD ONLINE */ print_pid(pid); free(args); } char msg[100]; close(pipes[0][WRITE]); read(pipes[0][READ], msg, sizeof(msg)); close(pipes[0][READ]); printf("pipe message: %s\n", msg); } }
void call_execve(char *path) { // create new process to execute to command int child_state; pid_t pid; int background = is_background(); if ((pid = fork()) == 0) { if (background) { args[background] = NULL; } execv(path, args); fprintf(stderr, "%s: command not found\n", args[0]); exit(0); } else if (pid > 0) { if (!background) { waitpid(pid, &child_state, 0); write_to_log(pid); } } else { perror(""); } }
static bool is_valid(scalar_type const &v) { return is_in_range(v) && (is_foreground(v) || is_background(v)); }
static bool is_valid_background(scalar_type const &v) { return is_in_range(v) && is_background(v); }