exit: work for simple arguments

This does not address non-integer arguments, or extremly high/low arguments.
This commit is contained in:
Khaïs COLIN 2025-04-01 18:31:38 +02:00
parent f4e9955f75
commit 094cce3c8c
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
7 changed files with 56 additions and 5 deletions

View file

@ -31,6 +31,7 @@ srcs = \
src/executing/here_doc/random_filename.c \ src/executing/here_doc/random_filename.c \
src/executing/here_doc/strip_newline.c \ src/executing/here_doc/strip_newline.c \
src/executing/simple_cmd/builtin_cd.c \ src/executing/simple_cmd/builtin_cd.c \
src/executing/simple_cmd/builtin_exit.c \
src/executing/simple_cmd/builtin_export.c \ src/executing/simple_cmd/builtin_export.c \
src/executing/simple_cmd/builtin_invalid.c \ src/executing/simple_cmd/builtin_invalid.c \
src/executing/simple_cmd/builtin_pwd.c \ src/executing/simple_cmd/builtin_pwd.c \

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 18:17:56 by khais #+# #+# */
/* Updated: 2025/04/02 15:06:22 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "builtins.h"
#include <stdlib.h>
#include "libft.h"
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app)
{
int status;
status = 0;
if (cmd->words->next != NULL)
status = ft_atoi(cmd->words->next->word->word);
simple_cmd_destroy(cmd);
env_destroy(app->env);
exit(status);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/01 16:37:21 by khais #+# #+# */ /* Created: 2025/04/01 16:37:21 by khais #+# #+# */
/* Updated: 2025/04/01 16:37:38 by khais ### ########.fr */ /* Updated: 2025/04/01 18:17:25 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,8 @@ t_builtin_type get_builtin(t_simple_cmd *cmd)
return (BUILTIN_CD); return (BUILTIN_CD);
if (ft_strcmp("export", word) == 0) if (ft_strcmp("export", word) == 0)
return (BUILTIN_EXPORT); return (BUILTIN_EXPORT);
if (ft_strcmp("exit", word) == 0)
return (BUILTIN_EXIT);
return (BUILTIN_INVALID); return (BUILTIN_INVALID);
} }
@ -35,6 +37,7 @@ t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app)
[BUILTIN_PWD] = builtin_pwd, [BUILTIN_PWD] = builtin_pwd,
[BUILTIN_CD] = builtin_cd, [BUILTIN_CD] = builtin_cd,
[BUILTIN_EXPORT] = builtin_export, [BUILTIN_EXPORT] = builtin_export,
[BUILTIN_EXIT] = builtin_exit,
}; };
type = get_builtin(cmd); type = get_builtin(cmd);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 14:16:13 by khais #+# #+# */ /* Created: 2025/03/31 14:16:13 by khais #+# #+# */
/* Updated: 2025/04/01 16:37:15 by khais ### ########.fr */ /* Updated: 2025/04/01 18:17:50 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,6 +19,7 @@ t_builtin_type builtin_invalid(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app); t_builtin_type builtin_pwd(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app); t_builtin_type builtin_cd(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app); t_builtin_type builtin_export(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type builtin_exit(t_simple_cmd *cmd, t_minishell *app);
t_builtin_type get_builtin(t_simple_cmd *cmd); t_builtin_type get_builtin(t_simple_cmd *cmd);
t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app); t_builtin_type execute_builtin(t_simple_cmd *cmd, t_minishell *app);

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:21:56 by khais #+# #+# */ /* Created: 2025/03/27 16:21:56 by khais #+# #+# */
/* Updated: 2025/04/02 18:45:47 by khais ### ########.fr */ /* Updated: 2025/04/02 19:15:08 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 16:20:31 by khais #+# #+# */ /* Created: 2025/03/27 16:20:31 by khais #+# #+# */
/* Updated: 2025/04/01 13:54:10 by khais ### ########.fr */ /* Updated: 2025/04/01 18:17:33 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,7 @@ typedef enum e_builtin_type
BUILTIN_PWD, BUILTIN_PWD,
BUILTIN_CD, BUILTIN_CD,
BUILTIN_EXPORT, BUILTIN_EXPORT,
BUILTIN_EXIT,
} t_builtin_type; } t_builtin_type;
#endif // SIMPLE_CMD_EXECUTE_H #endif // SIMPLE_CMD_EXECUTE_H

18
test.sh
View file

@ -357,6 +357,24 @@ hi
0 0
EOF EOF
when_run <<EOF "exit works"
$MINISHELL
exit
echo \$?
EOF
expecting <<EOF
0
EOF
when_run <<EOF "exit works with custom exit code"
$MINISHELL
exit 99
echo \$?
EOF
expecting <<EOF
99
EOF
when_run <<EOF "quoted parentheses are not operators" when_run <<EOF "quoted parentheses are not operators"
echo unclosed '(' echo unclosed '('
EOF EOF