#include "minishell.h" void close_fd(t_cmd *cmd, pid_t pid) { if (cmd->fd_in != 0) close(cmd->fd_in); if (pid == 0) { if (cmd->pipe_out != 1) { close(cmd->pipe_in); close(cmd->pipe_out); } if (cmd->fd_out != 1) close(cmd->fd_out); } else { if (cmd->fd_out != 1 && cmd->fd_redirect == 1) close(cmd->fd_out); } } void exec_cmd(char **envp, t_list *cmd_list) { t_cmd *cmd; pid_t pid; pid_t wpid; int status; while(cmd_list) { cmd = cmd_list->content; pid = fork(); if (pid == 0) { if (cmd->fd_in != 0) dup2(cmd->fd_in, STDIN_FILENO); if (cmd->fd_out != 1) dup2(cmd->fd_out, STDOUT_FILENO); close_fd(cmd, pid); execve(cmd->argv[0], cmd->argv, envp); } else { close_fd(cmd, pid); // if (cmd->fd_in != 0) // close(cmd->fd_in); // if (cmd->fd_out != 1 && cmd->fd_redirect == 1) // close(cmd->fd_out); } while ((wpid = wait(&status)) > 0); cmd_list = cmd_list->next; } } void shell_loop(t_all *c) { char *line_input; t_list *cmd; line_input = NULL; while (1) { if (line_input) free(line_input); line_input = readline(c->prompt); if (line_input && *line_input) { cmd = parser(line_input, c->envp); exec_cmd(c->envp, cmd); } } } void wip_test() { char term_desc[2048]; char *term_type; int term_width; int term_height; int ret; term_type = getenv("TERM"); if (term_type == 0) ft_putstr_fd("Specify a terminal type with `setenv TERM '.\n", 2); ret = tgetent(term_desc, term_type); if (ret < 0) ft_putstr_fd("Could not access the termcap data base.\n", 2); if (ret == 0) ft_putstr_fd("Terminal type `%s' is not defined.\n", 2); term_height = tgetnum ("li"); term_width = tgetnum ("co"); /* Extract information that termcap functions use. */ /* temp = tgetstr ("pc", BUFFADDR); PC = temp ? *temp : 0; BC = tgetstr ("le", BUFFADDR); UP = tgetstr ("up", BUFFADDR); */ } int main(int argc, char *argv[], char *envp[]) { t_all c; (void)argc; (void)argv; if (!init(&c, envp)) exit(EXIT_FAILURE); shell_loop(&c); return (0); }