static int use_execve(t_tree *tree, t_env **env, char *cmd) { char **tab_env; char **tab_lex; int pid; int exec_val; exec_val = 0; if ((tab_env = env_to_tab(*env)) == NULL || ((tab_lex = lex_to_tab(tree->lexer)) == NULL) || ((pid = fork()) == -1)) return (my_perror(QUIT_ERR, QUIT_RET)); if (pid == 0) { if (execve(cmd, tab_lex, tab_env) == -1) return (my_perror(QUIT_ERR, QUIT_RET)); } else { if (wait(&exec_val) == -1) return (my_perror(QUIT_ERR, QUIT_RET)); exec_val = WEXITSTATUS(exec_val); } free_tab(tab_env); free_tab(tab_lex); free(cmd); return (exec_val); }
int ft_apply_cmds(t_op *op) { char **envp; int cmd; cmd = 0; envp = env_to_tab(); while (op) { if (op->type == 0 || (cmd && op->type == 1) || (!cmd && op->type == 2)) { term_reset(); if (!test_alone(op->pipe, envp, &cmd)) test_pipe(op->pipe, envp, &cmd); term_initiate(); } op = op->next; } ft_delfulltab((void ***)&envp); return (1); }
void execute(t_app *app) { int rt; while (app->cur_cmd && !app->bad_cmd) { if (app->cur_cmd->next && app->cur_cmd->next->piped) pipe(app->cur_cmd->next->fildes); set_cmd(app); if (!app->cur_cmd->cmd) return ; app->cur_cmd->child_pid = fork(); if (app->cur_cmd->child_pid == 0) { if (app->cur_cmd->next && app->cur_cmd->next->piped) { close(app->cur_cmd->next->fildes[0]); dup2(app->cur_cmd->next->fildes[1], 1); } if (app->cur_cmd->piped) { dup2(app->cur_cmd->fildes[0], 0); } link_out_file(&app->cur_cmd->files); link_in_file(&app->cur_cmd->files); //print_files_lst(&app->cur_cmd->files); execve(app->cur_cmd->cmd, cmd_to_tab(app) , env_to_tab(app)); } if (app->cur_cmd->next && app->cur_cmd->next->piped) close(app->cur_cmd->next->fildes[1]); if (app->cur_cmd->piped) close(app->cur_cmd->fildes[0]); wait(&rt); app->cur_cmd = app->cur_cmd->next; } }