fixed builtin edge cases
This commit is contained in:
31
srcs/exit.c
31
srcs/exit.c
@ -6,43 +6,42 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
||||
/* Updated: 2024/02/21 17:43:52 by marde-vr ### ########.fr */
|
||||
/* Updated: 2024/03/06 09:10:11 by marde-vr ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void print_numeric_arg_err(char *arg)
|
||||
void numeric_arg_err(char *arg, int *exit_code)
|
||||
{
|
||||
ft_putstr_fd("minishell: exit: ", 2);
|
||||
ft_putstr_fd(arg, 2);
|
||||
ft_putstr_fd(": numeric argument required\n", 2);
|
||||
*exit_code = 2;
|
||||
}
|
||||
|
||||
void exit_bt(t_msh *msh)
|
||||
{
|
||||
t_cmd *start;
|
||||
t_cmd *cur_cmd;
|
||||
int exit_code;
|
||||
|
||||
start = msh->cmds;
|
||||
msh->cmds = msh->cmds->next;
|
||||
cur_cmd = msh->cmds->next;
|
||||
ft_printf("exit\n");
|
||||
if (msh->cmds && msh->cmds->next && msh->cmds->next->type == ARG
|
||||
&& ft_strisnbr(msh->cmds->token))
|
||||
if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG
|
||||
&& ft_strisnbr(cur_cmd->token))
|
||||
{
|
||||
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
|
||||
g_return_code = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (msh->cmds && msh->cmds->type == ARG
|
||||
&& !ft_strisnbr(msh->cmds->token))
|
||||
print_numeric_arg_err(msh->cmds->token);
|
||||
if (msh->cmds && msh->cmds->type == ARG)
|
||||
exit_code = (unsigned char)ft_atoi(msh->cmds->token);
|
||||
else if (msh->cmds && msh->cmds->type == ARG
|
||||
&& !ft_strisnbr(msh->cmds->token))
|
||||
exit_code = 2;
|
||||
if (cur_cmd && cur_cmd->type == ARG
|
||||
&& !ft_strisnbr(cur_cmd->token))
|
||||
numeric_arg_err(cur_cmd->token, &exit_code);
|
||||
else if (cur_cmd && cur_cmd->type == ARG)
|
||||
exit_code = (unsigned char)ft_atoi(cur_cmd->token);
|
||||
else
|
||||
exit_code = g_return_code;
|
||||
free_cmd(start);
|
||||
free_msh(msh);
|
||||
exit(exit_code);
|
||||
}
|
||||
|
Reference in New Issue
Block a user