From 9f0b0f5422b6b4344c2cfa896106dc2cffc7d432 Mon Sep 17 00:00:00 2001 From: Camille Chauvet Date: Fri, 24 Feb 2023 13:13:11 +0100 Subject: [PATCH] add: exit cmd --- Makefile | 2 +- builtins/exit.c | 14 +++++++------- builtins/unset.c | 2 ++ execution.c | 22 +++++++++++++++------- 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 2456888..c97bdba 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ UTILS_SRC = utils/ft_is_in_quote.c utils/ft_strncpy.c utils/ft_strreplace.c utils/ft_strnchr.c utils/ft_split_quoted.c utils/ft_strshift.c utils/ft_quote_remover.c utils/ft_str_is_empty.c utils/ft_atoi_check.c -SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c builtins/cd.c builtins/exit.c +SRCS = ${UTILS_SRC} main.c file.c infile.c outfile.c heredoc.c syntatics.c cmd.c cmds.c env.c env2.c env3.c execution.c spacer.c env_fill.c builtins/echo.c builtins/pwd.c builtins/export.c builtins/env.c builtins/cd.c builtins/exit.c builtins/unset.c OBJS = ${SRCS:.c=.o} diff --git a/builtins/exit.c b/builtins/exit.c index 688f3a6..d2cbfea 100644 --- a/builtins/exit.c +++ b/builtins/exit.c @@ -14,7 +14,7 @@ static int error(int err, char *reason, char *problem, int fd) { - write(fd, "bash: exit: ", 12); + ft_putstr_fd("minishell: exit ", fd); if (problem != NULL) { ft_putstr_fd(problem, fd); @@ -36,11 +36,11 @@ int ft_exit(char **args, int fd) return (error(2, "numeric argument required", args[0], fd)); if (args[1] != NULL) return (error(-1, "too many arguments", NULL, fd)); - return (0); + return (ft_atoi(args[0])); } -int main(int argc, char *argv[]) -{ - (void)argc; - return(ft_exit(argv + 1, 1)); -} +/* int main(int argc, char *argv[]) */ +/* { */ +/* (void)argc; */ +/* return(ft_exit(argv + 1, 1)); */ +/* } */ diff --git a/builtins/unset.c b/builtins/unset.c index 8522d16..8504b61 100644 --- a/builtins/unset.c +++ b/builtins/unset.c @@ -10,6 +10,8 @@ /* */ /* ************************************************************************** */ +#include "../minishell.h" + int error(char *str, int fd) { write(fd, "bash: unset: `", 14); diff --git a/execution.c b/execution.c index 3a2576c..8507b0e 100644 --- a/execution.c +++ b/execution.c @@ -95,25 +95,33 @@ static int ft_excutor(t_cmd *cmd, t_list **env) return (return_value); } -static int ft_own_cmd(t_list **env, t_cmd *cmd) +static int ft_own_cmd(t_data *data, t_cmd *cmd) { int return_code; + int exit_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); + return_code = print_env(data->env, cmd->fd_out); else if (ft_strcmp(cmd->executable, "export") == 0) - return_code = (export(env,cmd->args + 1, cmd->fd_out)); + return_code = (export(data->env,cmd->args + 1, cmd->fd_out)); else if (ft_strcmp(cmd->executable, "cd") == 0) return_code = (move_folder(cmd->args[1], cmd->fd_out)); - /* if (ft_strcmp(cmd->executable, "unset") == 0) */ - /* return_code = (unset(env, cmd->args, cmd->fd_out)); */ + if (ft_strcmp(cmd->executable, "unset") == 0) + return_code = (unset(data->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; + { + exit_code = ft_exit(cmd->args + 1, cmd->fd_out); + if (exit_code != -1) + { + data->exit_code = exit_code; + return_code = -2; + } + } if (return_code != -1) cmd->executable = NULL; return (return_code); @@ -141,7 +149,7 @@ int ft_cmds_executor(t_data *data, t_list **cmds) content->fd_out = fds[1]; ((t_cmd *) current->next->content)->fd_in = fds[0]; } - cmd_return = ft_own_cmd(data->env, content); + cmd_return = ft_own_cmd(data, content); if (cmd_return == -1) { content->executable = ft_get_executable_path(