/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* execution.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: cchauvet fd_in == -1 || cmd->fd_out == -1) return (1); pid = fork(); if (pid == -1) return (1); if (pid == 0) { tab = env_to_strs(env); if (tab == NULL) return (1); tab = NULL; dup2(cmd->fd_out, 1); dup2(cmd->fd_in, 0); execve(cmd->executable, cmd->args, tab); } else waitpid(pid, &return_value, 0); return (return_value); } static int ft_own_cmd(t_list **env, t_cmd *cmd) { int return_code; return_code = -1; if (ft_strcmp(cmd->executable, "pwd") == 0) return_code = pwd(cmd->fd_out); else if (ft_strcmp(cmd->executable, "env") == 0) return_code = print_env(env, cmd->fd_out); else if (ft_strcmp(cmd->executable, "export") == 0) return_code = (print_export(env, cmd->fd_out)); else if (ft_strcmp(cmd->executable, "cd") == 0) return_code = (move_folder(cmd->args[0], cmd->fd_out)); /* if (ft_strcmp(cmd->executable, "unset") == 0) */ /* return_code = (unset(env, cmd->args, cmd->fd_out)); */ else if (ft_strcmp(cmd->executable, "echo") == 0) return_code = (echo(cmd->fd_out, cmd->args + 1)); else if (ft_strcmp(cmd->executable, "exit") == 0) return_code = -2; cmd->executable = NULL; return (return_code); } int ft_cmds_executor(t_list **cmds, t_list **env) { t_cmd *content; t_list *current; char *return_value; int cmd_return; int fds[2]; current = *cmds; while (current != NULL) { content = current->content; if (current->next != NULL) { if (pipe(fds) == -1) { ft_eprintf("minishell: pipe failed\n"); return (1); } content->fd_out = fds[1]; ((t_cmd *) current->next->content)->fd_in = fds[0]; } cmd_return = ft_own_cmd(env, content); if (cmd_return == -1) { content->executable = ft_get_executable_path( content->executable, env); if (content->executable != NULL) cmd_return = ft_excutor(content, env); } if (content->fd_in != 0) close(content->fd_in); if (content->fd_out != 1 && content->fd_out != 2) close(content->fd_out); return_value = ft_itoa(cmd_return); if (return_value == NULL) { ft_eprintf("minishell: malloc failed\n"); return (1); } set_value_by_key("?", return_value, env); current = current->next; } return (0); }