From 0e5b10196941e708825db35b7e1e9da37354c4c8 Mon Sep 17 00:00:00 2001 From: narnaud Date: Fri, 6 May 2022 11:08:05 +0200 Subject: [PATCH] handle builtins without forks when required (without redirs) --- caller.c | 19 +++++++++++++++-- minishell.c | 59 +++++++++++++++++++++++++++++------------------------ minishell.h | 7 ++++--- 3 files changed, 53 insertions(+), 32 deletions(-) diff --git a/caller.c b/caller.c index a548b8a..7aa6875 100755 --- a/caller.c +++ b/caller.c @@ -6,13 +6,13 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/05 09:40:42 by narnaud ### ########.fr */ +/* Updated: 2022/05/05 20:52:22 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int command_call(t_datas *datas, t_command *cmd) +int builtin_call(t_datas *datas, t_command *cmd) { if (ft_strncmp(cmd->argv[0], "echo", 5) == 0) return (ft_echo(cmd)); @@ -32,6 +32,21 @@ int command_call(t_datas *datas, t_command *cmd) return (-1); } + +int is_builtin(char *cmd) +{ + if (!ft_strncmp(cmd, "echo", 5) || + !ft_strncmp(cmd, "pwd", 4) || + !ft_strncmp(cmd, "cd", 3) || + !ft_strncmp(cmd, "export", 7) || + !ft_strncmp(cmd, "unset", 6) || + !ft_strncmp(cmd, "env", 4) || + !ft_strncmp(cmd, "exit", 5)) + return (1); + else + return (0); +} + void exe(t_datas *datas, t_command *cmd) { char **path_dirs; diff --git a/minishell.c b/minishell.c index 067bfb3..b7999ff 100755 --- a/minishell.c +++ b/minishell.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ -/* Updated: 2022/05/05 16:28:15 by mea ### ########.fr */ +/* Updated: 2022/05/05 20:53:48 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,21 +24,10 @@ void handle_status(t_datas *datas, int status) datas->exit_code = WEXITSTATUS(status); } -int caller(t_datas *datas, t_command *cmd) +pid_t caller(t_datas *datas, t_command *cmd) { pid_t pid; - int pip[2]; - int status; - if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) - { - pipe(pip); - cmd->fd[1] = pip[1]; - cmd->next->fd[0] = pip[0]; - } - - if (!cmd->prev && !cmd->next && command_call(datas, cmd) != -1)//check_builtin(cmd->argv[0])) - return (1); pid = fork(); if (!pid) { @@ -46,14 +35,30 @@ int caller(t_datas *datas, t_command *cmd) dup2(cmd->fd[0], STDIN_FILENO); if (cmd->fd[1]) dup2(cmd->fd[1], STDOUT_FILENO); - if (command_call(datas, cmd) == -1) + if (builtin_call(datas, cmd) == -1) exe(datas, cmd); exit(EXIT_SUCCESS); } + return pid; +} + +int piper(t_datas *datas, t_command *cmd) +{ + int pip[2]; + int status; + pid_t pid; + + if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) + { + pipe(pip); + cmd->fd[1] = pip[1]; + cmd->next->fd[0] = pip[0]; + } + pid = caller(datas, cmd); if (cmd->fd[1]) close(cmd->fd[1]); if (cmd->next) - caller(datas, cmd->next); + piper(datas, cmd->next); waitpid(pid, &status, 0); handle_status(datas, status); if (cmd->fd[0]) @@ -122,19 +127,15 @@ t_token *lexer(t_datas *datas, char *line) int main(int argc, char **argv, char **envp) { - t_datas datas; - char *line; - char **env_copy; - int i; + t_command *cmd; + t_datas datas; + char *line; + int i; - env_copy = ft_calloc(1024, sizeof(char *)); i = 0; - while (envp[i]) - { - env_copy[i] = ft_strdup(envp[i]); - i++; - } - datas.envp = env_copy; + datas.envp = ft_calloc(ENVP_LIMIT, sizeof(char *)); + while (*envp) + datas.envp[i++] = ft_strdup(*(envp++)); datas.exit_code = 0; if (argc < 2) (void)argv; @@ -147,7 +148,11 @@ int main(int argc, char **argv, char **envp) halt(EXIT_FAILURE); if (is_empty(line)) continue ; - caller(&datas, parser(&datas, lexer(&datas, line), NULL)); + cmd = parser(&datas, lexer(&datas, line), NULL); + if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0])) + builtin_call(&datas, cmd); + else + piper(&datas, cmd); add_history(line); } clear_history(); diff --git a/minishell.h b/minishell.h index 45f03f3..7bab911 100755 --- a/minishell.h +++ b/minishell.h @@ -6,12 +6,13 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/05 14:18:56 by mea ### ########.fr */ +/* Updated: 2022/05/05 20:46:00 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H +# define ENVP_LIMIT 1024 # define PIPE_MAX_SIZE 8192 # define PATHS_MAX_SIZE 126 # define PROMPT_PREFIX 0 @@ -66,8 +67,8 @@ int ft_export(t_datas *datas, t_command *cmd); // ----------------------------------Caller.c void exe(t_datas *datas, t_command *cmd); -int command_call(t_datas *datas, t_command *cmd); - +int builtin_call(t_datas *datas, t_command *cmd); +int is_builtin(char *cmd); // ----------------------------------Parser.c typedef enum e_type