fixe builtin exit to accept arguments

This commit is contained in:
whaffman 2025-02-20 15:54:48 +01:00
parent eda2954279
commit a93185a45e
3 changed files with 29 additions and 6 deletions

View File

@ -11,6 +11,7 @@ A lot of amazing shell stuff
-[x] Get environment array (export) -[x] Get environment array (export)
-[x] Set environment variable (export) -[x] Set environment variable (export)
-[x] Simple builtin export -[x] Simple builtin export
-[x] builtins
-[x] Preliminary signals -[x] Preliminary signals
-[x] Define struct for commands, something like ( -[x] Define struct for commands, something like (
```c ```c

View File

@ -6,16 +6,37 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:59 by whaffman #+# #+# */
/* Updated: 2025/02/20 12:17:40 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 15:54:11 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_exit(t_minishell *minishell, t_command *cmd) int builtin_exit(t_minishell *minishell, t_command *cmd)
{ {
(void)cmd; int exit_status;
free_minishell(minishell);
exit(EXIT_SUCCESS); exit_status = 0; //TODO EXIT last exit status
return (SUCCESS); if (ft_count_arr(cmd->args) > 2)
{
ft_putstr_fd("exit\n", STDERR_FILENO);
ft_putstr_fd("minishell: exit: too many arguments\n", STDERR_FILENO);
return (FAILURE);
}
if (ft_count_arr(cmd->args) == 2 && cmd->args[1] != NULL)
{
if (ft_isdigit_str(cmd->args[1]) == FALSE)
{
ft_putstr_fd("exit\n", STDERR_FILENO);
ft_putstr_fd("minishell: exit: ", STDERR_FILENO);
ft_putstr_fd(cmd->args[1], STDERR_FILENO);
ft_putstr_fd(": numeric argument required\n", STDERR_FILENO);
return (FAILURE);
}
exit_status = ft_atoi(cmd->args[1]);
}
free_minishell(minishell);
exit(exit_status);
return (FAILURE);
} }

View File

@ -6,12 +6,13 @@
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */ /* +#+ */
/* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */ /* Created: 2025/02/20 11:03:33 by whaffman #+# #+# */
/* Updated: 2025/02/20 13:54:04 by whaffman ######## odam.nl */ /* Updated: 2025/02/20 15:36:16 by whaffman ######## odam.nl */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int is_builtin(char *cmd) int is_builtin(char *cmd)
{ {
const char *builtins[] = {"echo", "cd", "pwd", "export", const char *builtins[] = {"echo", "cd", "pwd", "export",