Skip to content

Commit 4df96de

Browse files
committed
fixed builtin edge cases
1 parent 87f8f9a commit 4df96de

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

srcs/cd.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
9-
/* Updated: 2024/02/21 17:30:36 by marde-vr ### ########.fr */
9+
/* Updated: 2024/03/06 08:43:53 by marde-vr ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -19,13 +19,17 @@ int cd(t_cmd *args)
1919
if (args->next && args->next->next && args->next->next->type == ARG)
2020
{
2121
ft_printf_fd(2, "minishell: cd: too many arguments\n");
22+
g_return_code = 1;
2223
return (1);
2324
}
2425
if (!args->next || args->next->type != ARG)
2526
new_wd = getenv("HOME");
2627
else
2728
new_wd = args->next->token;
2829
if (chdir(new_wd) == -1)
30+
{
2931
perror("minishell: cd");
32+
g_return_code = 1;
33+
}
3034
return (0);
3135
}

srcs/exit.c

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,42 @@
66
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
9-
/* Updated: 2024/02/21 17:43:52 by marde-vr ### ########.fr */
9+
/* Updated: 2024/03/06 09:10:11 by marde-vr ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "minishell.h"
1414

15-
void print_numeric_arg_err(char *arg)
15+
void numeric_arg_err(char *arg, int *exit_code)
1616
{
1717
ft_putstr_fd("minishell: exit: ", 2);
1818
ft_putstr_fd(arg, 2);
1919
ft_putstr_fd(": numeric argument required\n", 2);
20+
*exit_code = 2;
2021
}
2122

2223
void exit_bt(t_msh *msh)
2324
{
24-
t_cmd *start;
25+
t_cmd *cur_cmd;
2526
int exit_code;
2627

27-
start = msh->cmds;
28-
msh->cmds = msh->cmds->next;
28+
cur_cmd = msh->cmds->next;
2929
ft_printf("exit\n");
30-
if (msh->cmds && msh->cmds->next && msh->cmds->next->type == ARG
31-
&& ft_strisnbr(msh->cmds->token))
30+
if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG
31+
&& ft_strisnbr(cur_cmd->token))
32+
{
3233
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
34+
g_return_code = 1;
35+
}
3336
else
3437
{
35-
if (msh->cmds && msh->cmds->type == ARG
36-
&& !ft_strisnbr(msh->cmds->token))
37-
print_numeric_arg_err(msh->cmds->token);
38-
if (msh->cmds && msh->cmds->type == ARG)
39-
exit_code = (unsigned char)ft_atoi(msh->cmds->token);
40-
else if (msh->cmds && msh->cmds->type == ARG
41-
&& !ft_strisnbr(msh->cmds->token))
42-
exit_code = 2;
38+
if (cur_cmd && cur_cmd->type == ARG
39+
&& !ft_strisnbr(cur_cmd->token))
40+
numeric_arg_err(cur_cmd->token, &exit_code);
41+
else if (cur_cmd && cur_cmd->type == ARG)
42+
exit_code = (unsigned char)ft_atoi(cur_cmd->token);
4343
else
4444
exit_code = g_return_code;
45-
free_cmd(start);
4645
free_msh(msh);
4746
exit(exit_code);
4847
}

srcs/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
9-
/* Updated: 2024/03/05 18:30:57 by marde-vr ### ########.fr */
9+
/* Updated: 2024/03/06 08:32:18 by marde-vr ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -88,7 +88,7 @@ int main(int argc, char **argv, char **envp)
8888

8989
commands = (char *)1;
9090
init_minishell(&msh, argc, argv, envp);
91-
handle_minishellrc(msh);
91+
//handle_minishellrc(msh);
9292
while (msh->env && commands)
9393
{
9494
prompt = get_prompt(msh->env);

srcs/path.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
9-
/* Updated: 2024/03/05 17:30:27 by marde-vr ### ########.fr */
9+
/* Updated: 2024/03/06 08:42:49 by marde-vr ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -103,5 +103,6 @@ void get_cmd_path(t_msh *msh)
103103
ft_printf_fd(2, "%s: command not found\n", msh->cmds->token);
104104
free(msh->cmds->token);
105105
msh->cmds->token = 0;
106+
g_return_code = 127;
106107
}
107108
}

0 commit comments

Comments
 (0)