exit error

This commit is contained in:
Loic Deridder
2025-02-03 14:22:33 +01:00
parent 45dc502424
commit dbb7506a31
5 changed files with 15 additions and 13 deletions

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/28 09:01:33 by lderidde #+# #+# */ /* Created: 2025/01/28 09:01:33 by lderidde #+# #+# */
/* Updated: 2025/02/03 13:03:14 by lderidde ### ########.fr */ /* Updated: 2025/02/03 14:05:25 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -26,7 +26,7 @@
# define EXPRT_INV "not a valid identifier" # define EXPRT_INV "not a valid identifier"
// void echo(char *msg, int flag); // void echo(char *msg, int flag);
int builtin_echo(char **arg, char **envp); int builtin_echo(char **arg, char **envp);
int builtin_exit(char **arg, bool depth); int builtin_exit(char **arg, bool depth, t_ast_n *node);
int builtin_pwd(char **arg); int builtin_pwd(char **arg);
int builtin_env(char **arg, char **envp); int builtin_env(char **arg, char **envp);
int builtin_unset(char **arg, t_ast_n *head); int builtin_unset(char **arg, t_ast_n *head);

View File

@@ -6,14 +6,14 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:25:25 by lderidde #+# #+# */ /* Created: 2025/01/27 11:25:25 by lderidde #+# #+# */
/* Updated: 2025/01/27 14:15:38 by lderidde ### ########.fr */ /* Updated: 2025/02/03 13:45:35 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MINISHELL_H #ifndef MINISHELL_H
# define MINISHELL_H # define MINISHELL_H
# define DEBUG 1 # define DEBUG 0
typedef struct s_ast_n t_ast_n; typedef struct s_ast_n t_ast_n;
typedef struct s_node t_node; typedef struct s_node t_node;

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/24 14:32:13 by lderidde #+# #+# */ /* Created: 2025/01/24 14:32:13 by lderidde #+# #+# */
/* Updated: 2025/01/28 10:39:44 by lderidde ### ########.fr */ /* Updated: 2025/02/03 14:17:14 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -49,23 +49,24 @@ int bash_exiterrorcount(void)
return (2); return (2);
} }
int builtin_exit(char **arg, bool depth) int builtin_exit(char **arg, bool depth, t_ast_n *node)
{ {
long res; long res;
if (ft_isnumeric(arg[1])) res = node->msh->ex_code;
if (arg[1] && ft_isnumeric(arg[1]))
res = ft_atol(arg[1]); res = ft_atol(arg[1]);
if (depth == true) if (depth == true)
{ {
if (count_args(arg) > 2 && ft_isnumeric(arg[1])) if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
return (err_msg_cmd("exit", NULL, "too many arguments", 1)); return (err_msg_cmd("exit", NULL, "too many arguments", 1));
else if (!ft_isnumeric(arg[1]) || errno == ERANGE) else if (arg[1] && (!ft_isnumeric(arg[1]) || errno == ERANGE))
return (err_msg_cmd("exit", arg[1], "numeric argument required", 2)); return (err_msg_cmd("exit", arg[1], "numeric argument required", 2));
return (res % 256); return (res % 256);
} }
if (count_args(arg) > 2 && ft_isnumeric(arg[1])) if (count_args(arg) > 2 && ft_isnumeric(arg[1]))
return (bash_exiterrorcount()); return (bash_exiterrorcount());
else if (!ft_isnumeric(arg[1]) || errno == ERANGE) else if (arg[1] && (!ft_isnumeric(arg[1]) || errno == ERANGE))
bash_exit_errornum(arg[1]); bash_exit_errornum(arg[1]);
else else
bash_exit(res % 256); bash_exit(res % 256);

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */ /* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
/* Updated: 2025/02/03 13:15:41 by lderidde ### ########.fr */ /* Updated: 2025/02/03 14:05:14 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -80,7 +80,7 @@ int exec_builtin(t_ast_n *node)
{ {
handle_redir(node); handle_redir(node);
if (ft_strncmp(node->cmd, "exit", 4) == 0) if (ft_strncmp(node->cmd, "exit", 4) == 0)
return (builtin_exit(node->args, true)); return (builtin_exit(node->args, false, node));
else if (ft_strncmp(node->cmd, "pwd", 3) == 0) else if (ft_strncmp(node->cmd, "pwd", 3) == 0)
return (builtin_pwd(node->args)); return (builtin_pwd(node->args));
else if (ft_strncmp(node->cmd, "echo", 4) == 0) else if (ft_strncmp(node->cmd, "echo", 4) == 0)

View File

@@ -6,7 +6,7 @@
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */ /* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */ /* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */
/* Updated: 2025/02/03 13:19:31 by lderidde ### ########.fr */ /* Updated: 2025/02/03 14:19:09 by lderidde ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -57,6 +57,7 @@ t_msh *init_msh(char **envp)
t_msh *msh; t_msh *msh;
msh = malloc(sizeof(t_msh) * 1); msh = malloc(sizeof(t_msh) * 1);
msh->ex_code = 0;
if (!msh) if (!msh)
return (NULL); return (NULL);
if (!envp[0]) if (!envp[0])
@@ -80,7 +81,7 @@ int main(int ac, char **av, char **envp)
{ {
input = powerline(); input = powerline();
msh->head = parser(input, msh); msh->head = parser(input, msh);
execute_command(msh->head); msh->ex_code = execute_command(msh->head);
free(input); free(input);
} }
} }