int execution__simple_command(t_command *c, t_env *e) { pid_t p; if (!check_bultins(c, e) && !(p = fork())) { if (c->need_redir) do_redirections(&c->redirs, STDIN_FILENO, STDOUT_FILENO); start_prgm(e->environ, c->argv); } wait(NULL); return (WEXITSTATUS(p)); }
/* Execute a builtin command. * * @builtin: Pointer to a structure describing which builtin command to * execute. * * @cmd_args: List of strings for the arguments to the builtin, not * including the name of the builtin itself. * * @redirs: List of redirections for the builtin. * * @cmd_nargs: Number of arguments that the builtin was passed, not * including the name of the builtin itself. * * Return value: The return value of the builtin command function, or -1 if * there are problems doing or undoing redirections. */ static int execute_builtin(const struct builtin *builtin, const struct list_head *cmd_args, const struct list_head *redirs, unsigned cmd_nargs) { struct orig_fds orig; const char *argv[cmd_nargs + 1]; struct string *s; unsigned i; int status; int ret; /* Do redirections for the builtin */ status = do_redirections(redirs, &orig); if (status) return status; /* Prepare argv for the builtin */ for (i = 0, s = list_entry(cmd_args->next, struct string, list); i < cmd_nargs; i++, s = list_entry(s->list.next, struct string, list)) { argv[i] = s->chars; } argv[i] = NULL; /* Call the built-in function */ status = builtin->func(cmd_nargs, argv); /* Undo redirections for the builtin, unless this was the 'exec' builtin */ if (builtin->func != builtin_exec) { ret = undo_redirections(&orig); if (ret) { if (status == 0) status = ret; mysh_error_with_errno("Failed to restore redirections"); } } return status; }