Builtins fixed
The builtins wasnt protected, now all data received is protected, the hashmap addition is protected and added functionality of env, export and unset (not implemented in this version). Added fixed details documentation in docs/builtins_fixes.md generated by codex and created tests/builtins_edge_cases.sh to test all the builtins to work correctly
This commit is contained in:
@@ -12,23 +12,53 @@
|
||||
|
||||
#include "builtins.h"
|
||||
|
||||
u_int8_t set_builtins(
|
||||
t_minishell *minishell
|
||||
) {
|
||||
minishell->builtins
|
||||
= ft_hashmap_new(4, ft_hashmap_hashstr, ft_hashmap_strcmp);
|
||||
if (minishell->builtins == NULL)
|
||||
static uint8_t register_builtin(
|
||||
t_minishell *minishell,
|
||||
const char *name,
|
||||
t_builtin_func builtin
|
||||
)
|
||||
{
|
||||
char *key;
|
||||
|
||||
key = ft_strdup(name);
|
||||
if (key == NULL)
|
||||
return (0);
|
||||
ft_hashmap_put(minishell->builtins, ft_strdup("cd"), builtin_cd);
|
||||
ft_hashmap_put(minishell->builtins, ft_strdup("echo"), builtin_echo);
|
||||
ft_hashmap_put(minishell->builtins, ft_strdup("exit"), builtin_exit);
|
||||
ft_hashmap_put(minishell->builtins, ft_strdup("pwd"), builtin_pwd);
|
||||
ft_hashmap_put(minishell->builtins, key, builtin);
|
||||
if (!ft_hashmap_contains_key(minishell->builtins, name))
|
||||
{
|
||||
free(key);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
u_int8_t is_builtin(
|
||||
uint8_t set_builtins(
|
||||
t_minishell *minishell
|
||||
) {
|
||||
minishell->builtins
|
||||
= ft_hashmap_new(7, ft_hashmap_hashstr, ft_hashmap_strcmp);
|
||||
if (minishell->builtins == NULL)
|
||||
return (0);
|
||||
if (!register_builtin(minishell, "cd", builtin_cd)
|
||||
|| !register_builtin(minishell, "echo", builtin_echo)
|
||||
|| !register_builtin(minishell, "env", builtin_env)
|
||||
|| !register_builtin(minishell, "exit", builtin_exit)
|
||||
|| !register_builtin(minishell, "export", builtin_export)
|
||||
|| !register_builtin(minishell, "pwd", builtin_pwd)
|
||||
|| !register_builtin(minishell, "unset", builtin_unset))
|
||||
{
|
||||
ft_hashmap_clear_keys(&minishell->builtins);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
uint8_t is_builtin(
|
||||
const char *command_name,
|
||||
t_minishell *minishell
|
||||
) {
|
||||
if (command_name == NULL || minishell == NULL
|
||||
|| minishell->builtins == NULL)
|
||||
return (0);
|
||||
return (ft_hashmap_contains_key(minishell->builtins, command_name));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user