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,43 +12,104 @@
|
||||
|
||||
#include "builtins.h"
|
||||
|
||||
static u_int8_t handle_error(t_command cmd, t_minishell *msh, char *path);
|
||||
static uint8_t handle_error(void);
|
||||
static void update_pwd_vars(t_minishell *msh, char *oldpwd);
|
||||
static char *get_path_from_env(
|
||||
t_minishell *msh,
|
||||
const char *env_name,
|
||||
const char *error,
|
||||
uint8_t *status
|
||||
);
|
||||
static char *resolve_cd_path(
|
||||
t_command cmd,
|
||||
t_minishell *msh,
|
||||
uint8_t *status
|
||||
);
|
||||
|
||||
u_int8_t builtin_cd(
|
||||
uint8_t builtin_cd(
|
||||
t_command cmd,
|
||||
t_minishell *msh
|
||||
){
|
||||
char *path;
|
||||
char *oldpwd;
|
||||
uint8_t status;
|
||||
bool show_pwd;
|
||||
|
||||
if (cmd.argc > 2)
|
||||
{
|
||||
ft_eputendl("minishell: cd: too many arguments");
|
||||
return (2);
|
||||
}
|
||||
else if (cmd.argc == 1)
|
||||
path = get_env("HOME", msh);
|
||||
else
|
||||
path = cmd.argv[1];
|
||||
path = resolve_cd_path(cmd, msh, &status);
|
||||
if (status != EXIT_SUCCESS)
|
||||
return (status);
|
||||
show_pwd = (cmd.argc == 2 && ft_strcmp(cmd.argv[1], "-") == 0);
|
||||
oldpwd = getcwd(NULL, 0);
|
||||
if (chdir(path) == -1)
|
||||
return (handle_error(cmd, msh, path));
|
||||
{
|
||||
free(oldpwd);
|
||||
return (handle_error());
|
||||
}
|
||||
update_pwd_vars(msh, oldpwd);
|
||||
if (show_pwd && get_env("PWD", msh) != NULL)
|
||||
ft_putendl(get_env("PWD", msh));
|
||||
free(oldpwd);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static u_int8_t handle_error(
|
||||
static uint8_t handle_error(void)
|
||||
{
|
||||
perror("minishell: cd");
|
||||
return (EXIT_FAILURE);
|
||||
}
|
||||
|
||||
static void update_pwd_vars(
|
||||
t_minishell *msh,
|
||||
char *oldpwd
|
||||
){
|
||||
char *newpwd;
|
||||
|
||||
if (oldpwd != NULL)
|
||||
set_env("OLDPWD", oldpwd, msh);
|
||||
newpwd = getcwd(NULL, 0);
|
||||
if (newpwd != NULL)
|
||||
{
|
||||
set_env("PWD", newpwd, msh);
|
||||
free(newpwd);
|
||||
}
|
||||
}
|
||||
|
||||
static char *resolve_cd_path(
|
||||
t_command cmd,
|
||||
t_minishell *msh,
|
||||
char *path
|
||||
uint8_t *status
|
||||
){
|
||||
u_int8_t exit_code;
|
||||
|
||||
(void)msh;
|
||||
exit_code = 0;
|
||||
if (access(path, F_OK) != -1)
|
||||
// No such file or directory
|
||||
exit_code = 1;
|
||||
else if (access(path, X_OK) == -1)
|
||||
// Permission denied
|
||||
exit_code = 2;
|
||||
perror(cmd.argv[0]);
|
||||
return (exit_code);
|
||||
if (cmd.argc > 2)
|
||||
{
|
||||
ft_eputendl("minishell: cd: too many arguments");
|
||||
*status = EXIT_FAILURE;
|
||||
return (NULL);
|
||||
}
|
||||
if (cmd.argc == 2 && ft_strcmp(cmd.argv[1], "-") == 0)
|
||||
return (get_path_from_env(msh, "OLDPWD",
|
||||
"minishell: cd: OLDPWD not set", status));
|
||||
if (cmd.argc == 1)
|
||||
return (get_path_from_env(msh, "HOME",
|
||||
"minishell: cd: HOME not set", status));
|
||||
*status = EXIT_SUCCESS;
|
||||
return (cmd.argv[1]);
|
||||
}
|
||||
|
||||
static char *get_path_from_env(
|
||||
t_minishell *msh,
|
||||
const char *env_name,
|
||||
const char *error,
|
||||
uint8_t *status
|
||||
){
|
||||
char *path;
|
||||
|
||||
path = get_env(env_name, msh);
|
||||
if (path == NULL)
|
||||
{
|
||||
ft_eputendl(error);
|
||||
*status = EXIT_FAILURE;
|
||||
return (NULL);
|
||||
}
|
||||
*status = EXIT_SUCCESS;
|
||||
return (path);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user