Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ SRCS += env.c
SRCS += export.c
SRCS += display_sorted_exportables_variables.c
SRCS += pwd.c
SRCS += exit.c

# srcs/builtins/echo

Expand Down
6 changes: 4 additions & 2 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: linux <linux@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/09 16:11:56 by hucherea #+# #+# */
/* Updated: 2025/02/15 04:50:18 by linux ### ########.fr */
/* Updated: 2025/02/15 17:24:42 by linux ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -78,7 +78,8 @@ typedef enum e_builtin_type
EXPORT,
PWD,
CD,
ECHO
ECHO,
EXIT
} t_builtin_type;

typedef enum e_new_line_status
Expand Down Expand Up @@ -146,6 +147,7 @@ char *add_last_arg(char *line, char *arg);
int cd(t_command *command);
char *get_target_from_args(char **args);
int update_env_variables(char *old_pwd);
int exit_builtin(t_command *command);

// EXIT STATUS

Expand Down
67 changes: 67 additions & 0 deletions srcs/builtins/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hucherea <hucherea@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/15 16:45:34 by linux #+# #+# */
/* Updated: 2025/02/16 17:15:40 by hucherea ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

bool ft_isnumber(char *args)
{
int i;

i = 0;
if (args[i] == '-' || args[i] == '+')
i++;
while (args[i] != '\0')
{
if (!ft_isdigit(args[i]))
return (false);
i++;
}
return (true);
}

int error_exit(char *arg)
{
ft_putstr_fd("exit\n", 2);
ft_putstr_fd("minishell: exit: ", 2);
ft_putstr_fd(arg, 2);
ft_putstr_fd(": numeric argument required\n", 2);
return (EXIT_FAILURE);
}

int exit_builtin(t_command *command)
{
int exit_code;
char **args;

args = list_to_strs_array(command->command_args, args_list_to_args_array);
exit_code = 0;
if (ft_lstsize(command->command_args) > 2)
{
ft_putstr_fd("exit\n", 2);
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
return (EXIT_FAILURE);
}
if (ft_lstsize(command->command_args) > 1)
{
if (ft_isnumber(args[1]))
{
exit_code = ft_atoi(args[1]);
}
else
{
return ((unsigned char)error_exit(args[1]));
}
}
ft_putstr_fd("exit\n", 2);
exit((unsigned char)exit_code);
return ((unsigned char)exit_code);
}
6 changes: 4 additions & 2 deletions srcs/command_interpretation/launch_builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: linux <linux@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/01 18:21:50 by tchobert #+# #+# */
/* Updated: 2025/02/15 05:08:32 by linux ### ########.fr */
/* Updated: 2025/02/15 17:24:07 by linux ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -20,6 +20,7 @@ static t_builtin_type get_builtin_type(const char *command_name)
"pwd",
"cd",
"echo",
"exit",
NULL,
};
t_builtin_type type;
Expand Down Expand Up @@ -65,7 +66,8 @@ int execute_builtin(t_minishell_context *minishell_context,
export,
pwd,
cd,
echo
echo,
exit_builtin,
};
int builtin_return;

Expand Down