Skip to content

Commit

Permalink
Merge pull request #7 from jackoske/fixing_exit_codes
Browse files Browse the repository at this point in the history
Fixing exit codes
  • Loading branch information
jackoske authored Aug 5, 2024
2 parents 7a93faa + 281cdf9 commit 85dd9a7
Show file tree
Hide file tree
Showing 20 changed files with 265 additions and 89 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
.cache/
.cache
compile_commands.json
/minishell_tester
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ #
# By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/05/14 15:15:48 by iverniho #+# #+# #
# Updated: 2024/08/02 16:19:19 by Jskehan ### ########.fr #
# Updated: 2024/08/02 18:04:41 by iverniho ### ########.fr #
# #
# **************************************************************************** #

NAME = minishell

CC = cc
CFLAGS = -Wall -Wextra -Werror -fsanitize=address -g
CFLAGS = -Wall -Wextra -Werror #-fsanitize=address -g

LIBFT = ./lib/libft/

Expand Down
41 changes: 25 additions & 16 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ */
/* By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/14 15:11:41 by iverniho #+# #+# */
/* Updated: 2024/08/02 17:16:17 by Jskehan ### ########.fr */
/* Updated: 2024/08/05 12:54:54 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,8 +31,10 @@
# define NEWLINE_ERR "minishell> syntax error near unexpected token `newline'"
# define SYNTAX_ERR "minishell> syntax error near unexpected token "
# define NO_FILE_ERR "minishell> No such file or directory: "
# define CMD_NOT_FOUND "minishell> command not found: "
# define CMD_NOT_FOUND ": command not found"
# define PERM_ERR "minishell> permission denied: "
# define NUM_REQ "numeric argument required"
# define NOT_VALID_ID " not a valid identifier"

# define READ_END 0
# define WRITE_END 1
Expand Down Expand Up @@ -65,20 +67,20 @@ extern volatile sig_atomic_t g_is_executing_command;

typedef struct s_signals
{
volatile sig_atomic_t sigint_received;
volatile sig_atomic_t is_executing_command;
} t_signals;
volatile sig_atomic_t sigint_received;
volatile sig_atomic_t is_executing_command;
} t_signals;

typedef struct s_mini
{
char **envp;
t_list *node;
char *current_dir;
int exit_status;
t_signals signals;
} t_mini;
char **envp;
t_list *node;
char *current_dir;
int exit_status;
t_signals signals;
} t_mini;

extern t_mini *g_mini;
extern t_mini *g_mini;

typedef struct s_cmd
{
Expand All @@ -102,7 +104,6 @@ typedef struct s_cmd
t_cmd *init_cmd(t_mini *mini);
void init_mini(t_mini *mini);
void free_mini(t_mini *mini);
void free_cmd(void *cmd_ptr);
void setup_signal_handlers(void);
void setup_child_signals(void);
void handle_sigint(int sig);
Expand Down Expand Up @@ -142,9 +143,12 @@ void execute_builtin(t_mini *mini, t_cmd *cmd);
int check_after_equal(char *str);
int add_env_key(char *str, t_mini *mini, int len);
int is_special_char_in_env(char *str);
void replace_value(char *key, char *value,
t_mini *mini);
void replace_value(char *key, char *value, t_mini *mini);
int is_already_exist(char *key, t_mini *mini);
void show_last_command_status(t_mini *mini, char **str);
int is_special_char_export(char c);
int is_all_num(char *str);


/* Built-in Commands */
int mini_cd(char **args, t_mini *mini);
Expand All @@ -171,4 +175,9 @@ char *ft_getenv(const char *name, char **envp,
char **ft_setenv(const char *name, const char *value,
char **envp, int overwrite);
char **copy_env(char **envp);

///temporary
void ft_error1(int error, char *arg, int exit_code, char *message);
int check_special_in_key(char *str);

#endif // MINISHELL_H
16 changes: 15 additions & 1 deletion lib/libft/src_extra/ft_is_special_symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 17:35:34 by iverniho #+# #+# */
/* Updated: 2024/06/07 17:35:47 by iverniho ### ########.fr */
/* Updated: 2024/08/04 15:12:53 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,3 +16,17 @@ int ft_is_special_symbol(char c)
{
return (c == '|' || c == '<' || c == '>');
}

int is_special_char_export(char c)
{
char *prohibited_chars;

prohibited_chars = "!@#$%^&*()-+=|\\{}[]:;\"'<>,.?/";
while (*prohibited_chars) {
if (c == *prohibited_chars) {
return 1;
}
prohibited_chars++;
}
return 0;
}
1 change: 1 addition & 0 deletions minishell_tester
Submodule minishell_tester added at 67d136
4 changes: 2 additions & 2 deletions src/builtin/builtin_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* builtin_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ */
/* By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/18 18:06:33 by Jskehan #+# #+# */
/* Updated: 2024/07/19 16:40:12 by Jskehan ### ########.fr */
/* Updated: 2024/08/02 15:43:45 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
66 changes: 58 additions & 8 deletions src/builtin/builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* builtin_functions.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: Jskehan <jskehan@student.42Berlin.de> +#+ +:+ +#+ */
/* By: iverniho <iverniho@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 10:40:36 by Jskehan #+# #+# */
/* Updated: 2024/07/18 18:07:01 by Jskehan ### ########.fr */
/* Updated: 2024/08/05 11:46:14 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,10 +27,18 @@ void mini_echo(t_cmd *cmd)
}
while (cmd->full_command[i])
{
ft_putstr_fd(cmd->full_command[i], 1);
if (cmd->full_command[i + 1])
ft_putstr_fd(" ", 1);
i++;
if (ft_strcmp(cmd->full_command[i], "$?") == 0)
{
show_last_command_status(cmd->mini, cmd->full_command);
return ;
}
else
{
ft_putstr_fd(cmd->full_command[i], 1);
if (cmd->full_command[i + 1])
ft_putstr_fd(" ", 1);
i++;
}
}
if (newline)
ft_putstr_fd("\n", 1);
Expand All @@ -53,15 +61,57 @@ void mini_pwd(void)
perror("getcwd");
}
}
// void mini_exit(char **args, t_mini *mini)
// {
// int exit_status;

// if (args[1])
// exit_status = ft_atoi(args[1]);
// else
// exit_status = mini->exit_status;
// // For example: free allocated memory, close file descriptors, etc.
// exit(exit_status);
// }
int ft_isdigit_str(char *str)
{
int i;

i = 0;
if (str[i] == '-' || str[i] == '+')
i++;
while (str[i])
{
if (!ft_isdigit(str[i]))
return (0);
i++;
}
return (1);
}

// Handle the `exit` built-in command
void mini_exit(char **args, t_mini *mini)
{
int exit_status;

if (args[1])
(void)mini;
exit_status = g_mini->exit_status;
if (ft_2d_array_len(args) > 2)
{
// ft_putstr_fd("minishell: exit: too many arguments\n", STDERR_FILENO);
// mini_perror("too many arguments", "exit", 2);
ft_error1(8, NULL, 1, "too many arguments");
exit(1);
}
else if (!ft_isdigit_str(args[1]))
{
// mini_perror("numeric argument required", args[1], 2);
ft_error1(7, args[1], 2, "numeric argument required");
exit(2);
}
else if (args[1])
exit_status = ft_atoi(args[1]);
else
exit_status = mini->exit_status;
exit_status = g_mini->exit_status;
// For example: free allocated memory, close file descriptors, etc.
exit(exit_status);
}
19 changes: 13 additions & 6 deletions src/builtin/builtin_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/16 10:43:33 by Jskehan #+# #+# */
/* Updated: 2024/07/28 16:04:01 by iverniho ### ########.fr */
/* Updated: 2024/08/04 16:29:27 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -33,9 +33,16 @@ int is_builtin(t_cmd *cmd)
return (0);
}

void show_last_command_status(t_mini *mini)
void show_last_command_status(t_mini *mini, char **str)
{
ft_putnbr_fd(mini->exit_status, STDOUT_FILENO);
(void)mini;
if (ft_2d_array_len(str) == 1)
{
ft_putstr_fd(ft_itoa(g_mini->exit_status), STDERR_FILENO);
ft_putstr_fd(": command not found\n", STDERR_FILENO);
return ;
}
ft_putnbr_fd(g_mini->exit_status, STDOUT_FILENO);
ft_putchar_fd('\n', STDOUT_FILENO);
}

Expand All @@ -51,12 +58,12 @@ void execute_builtin(t_mini *mini, t_cmd *cmd)
else if (ft_strcmp(cmd->full_command[0], "exit") == 0)
mini_exit(cmd->full_command, mini);
else if (ft_strcmp(cmd->full_command[0], "$?") == 0)
show_last_command_status(mini);
show_last_command_status(mini, cmd->full_command);
else if (ft_strcmp(cmd->full_command[0], "export") == 0)
mini_export(cmd->full_command, mini);
return (mini_export(cmd->full_command, mini));
else if (ft_strcmp(cmd->full_command[0], "unset") == 0)
mini_unset(cmd->full_command, mini);
else if (ft_strcmp(cmd->full_command[0], "env") == 0)
mini_env(mini);
mini->exit_status = 0;
g_mini->exit_status = 0;
}
9 changes: 8 additions & 1 deletion src/env/additional_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: iverniho <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/27 17:12:47 by iverniho #+# #+# */
/* Updated: 2024/08/02 15:37:51 by iverniho ### ########.fr */
/* Updated: 2024/08/05 13:00:26 by iverniho ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -89,6 +89,13 @@ void mini_export(char **args, t_mini *mini)

if (ft_2d_array_len(args) < 2)
return (export_with_no_args(mini));
if (is_special_char_in_env(*(args + 1)))
{
g_mini->exit_status = 1;
return ;
}
if (ft_strlen(args[1]) == 1 && args[1][0] == '=')
return (ft_error1(9, args[1], 1, NOT_VALID_ID));
if ((!ft_strchr(*(args + 1), '=')) || (!check_after_equal(*(args + 1))))
if (add_env_key(*(args + 1), mini, ft_2d_array_len(mini->envp) + 1))
return ;
Expand Down
Loading

0 comments on commit 85dd9a7

Please sign in to comment.