Commit 1f591552 authored by komask's avatar komask
Browse files

normalize cd

parent 51010a74
......@@ -7,12 +7,19 @@ comportement simplifié ^D
masaki:
stat_loc pour
export fd = fd
unset fd = fd
(void)var; to keep?
M_ERROR on peut s'en passer?
a la fin avec rgrep:
erreurs de write et de ft_put* a terminer
gestion d'erreurs des fonctions autorisees une a une
alientest: exit pipe
static functions
......
#include "builtins.h"
int ft_cd(char **args, t_xe *xe)
int cd_to(char *newpath, t_xe *xe)
{
int ret;
char *path;
char *oldpwd;
char *full_newpath;
int ret;
if (args[1] == NULL)
{
ret = get_var_pos(xe->env, "HOME", 4);
if (ret == NOT_FOUND)
return (HOME_NOT_SET);
path = get_var_value(xe->env, "HOME", 4);
}
else if (args[2] != NULL)
return (CD_ARG_ERR);
else
path = ft_strdup(args[1]);
if (path == NULL)
return (MALLOC_ERR);
oldpwd = get_var_value(xe->env, "PWD", 3);
if (oldpwd == NULL)
{
free(path);
return (MALLOC_ERR);
}
if (chdir(path) == ERROR)
if (chdir(newpath) == ERROR)
{
free(path);
free(oldpwd);
xe->stat_loc = 1;
return (INVALID_CD_PATH);
}
free(path);
env_replace_var("OLDPWD", oldpwd, xe);// erreur
ret = env_replace_var("OLDPWD", oldpwd, xe);
free(oldpwd);
path = getcwd(NULL, 0);
if (path == NULL)
if (ret != SUCCESS)
return (MALLOC_ERR);
full_newpath = getcwd(NULL, 0);
if (full_newpath == NULL)
return (INVALID_CD_NEW_PATH);
env_replace_var("PWD", path, xe);// erreur
free(path);
xe->stat_loc = 0;
ret = env_replace_var("PWD", full_newpath, xe);
free(full_newpath);
if (ret != SUCCESS)
return (MALLOC_ERR);
return (SUCCESS);
}
int ft_cd(char **args, t_xe *xe)
{
int ret;
char *newpath;
ret = SUCCESS;
if (args[1] == NULL)
{
if (get_var_pos(xe->env, "HOME", 4) == NOT_FOUND)
ret = HOME_NOT_SET;
else
newpath = get_var_value(xe->env, "HOME", 4);
}
else if (args[2] != NULL)
ret = CD_ARG_ERR;
else
newpath = ft_strdup(args[1]);
if (ret == SUCCESS && newpath == NULL)
ret = MALLOC_ERR;
else if (ret == SUCCESS)
{
ret = cd_to(newpath, xe);
free(newpath);
}
return (ret);
}
......@@ -34,6 +34,5 @@ int ft_echo(char **args, t_xe *xe)
if (!(options & ECHO_N_OPTION))
if (ft_putchar_fd('\n', STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
xe->stat_loc = 0;
return (SUCCESS);
}
......@@ -5,6 +5,5 @@ int ft_env(char **args, t_xe *xe)
(void)args;
if (ft_printarray_fd(xe->env, STDOUT_FILENO) != SUCCESS)
return (WRITE_ERR);
xe->stat_loc = 0;
return (SUCCESS);
}
#include "builtins.h"
char **sort_variables(char **env, char **exported)
static char **sort_variables(char **env, char **exported)
{
char **sorted_array;
size_t env_len;
......@@ -18,7 +18,7 @@ char **sort_variables(char **env, char **exported)
return (sorted_array);
}
int print_export_var_value(char *value)
static int print_export_var_value(char *value)
{
while (*value != '\0')
{
......@@ -32,7 +32,7 @@ int print_export_var_value(char *value)
return (SUCCESS);
}
int print_export_one_var(char *variable, ssize_t equalsign_pos)
static int print_export_one_var(char *variable, ssize_t equalsign_pos)
{
if (write(STDOUT_FILENO, variable, equalsign_pos + 1) != equalsign_pos + 1)
return (WRITE_ERR);
......@@ -45,7 +45,7 @@ int print_export_one_var(char *variable, ssize_t equalsign_pos)
return (SUCCESS);
}
int print_export(char **env, char **exported)
static int print_export(char **env, char **exported)
{
char **variables;
size_t i;
......@@ -213,7 +213,6 @@ static int ft_export_checks(char **args, t_xe *xe)
if (ft_exported_export(*args, xe) != SUCCESS)
return (MALLOC_ERR);
}
xe->stat_loc = 0;
}
else
{
......@@ -232,10 +231,8 @@ int ft_export(char **args, t_xe *xe)
if (ft_arraylen(args) == 1)
{
ret = print_export(xe->env, xe->exported);
if (ret == SUCCESS)
xe->stat_loc = 0;
else
xe->stat_loc = 1;
if (ret != MALLOC_ERR)
ret = WRITE_ERR;
}
else
ret = ft_export_checks(args + 1, xe);
......
......@@ -8,16 +8,12 @@ int ft_pwd(char **args, t_xe *xe)
(void)xe;
buf = getcwd(NULL, 0);
if (buf == NULL)
{
xe->stat_loc = 1;
return (INVALID_PWD_PATH);
}
if (ft_putendl_fd(buf, STDOUT_FILENO) != WRITE_SUCCESS)
{
free(buf);
return (WRITE_ERR);
}
free(buf);
xe->stat_loc = 0;
return (SUCCESS);
}
......@@ -24,7 +24,6 @@ static int unset_one_var(char *var, t_xe *xe)
xe->exported = new_array;
}
}
xe->stat_loc = 0;
return (SUCCESS);
}
......
......@@ -231,9 +231,8 @@ int execute_cmd(char **args, char **redir_paths, enum e_redir_op *redir_types,
int i;
int ret;
enum e_cmd_code cmd_code;
const t_exec command[8] = {ft_echo, ft_cd, ft_pwd,
ft_export, ft_unset, ft_env,
ft_exit, launch_ext};
const t_exec command[8] = {ft_echo, ft_cd, ft_pwd, ft_export, ft_unset,
ft_env, ft_exit, launch_ext};
i = 0;
if (redir_paths != NULL)
......@@ -248,5 +247,12 @@ int execute_cmd(char **args, char **redir_paths, enum e_redir_op *redir_types,
else if (cmd_code == EXIT && xe->pipe & CMD_PIPE)
return (PIPE_EXIT);
ret = command[cmd_code](args, xe);
if (cmd_code < 6)
{
if (ret == SUCCESS)
xe->stat_loc = 0;
else
xe->stat_loc = 1;
}
return (ret);
}
......@@ -34,8 +34,6 @@ int exec_error(int err_code, t_xe *xe)
ft_putstr_fd("command error: ", STDERR_FILENO);
ft_putendl_fd(err_msg[err_code - _EXEC_ERROR_ - 1], STDERR_FILENO);
// have to set stat_loc as well !!!
if (err_code == HOME_NOT_SET)
xe->stat_loc = 1;
if (err_code == NO_SUCH_FILE)
xe->stat_loc = 127;
if (xe->pipe & EXEC_PIPE)
......@@ -68,10 +66,7 @@ int clean_and_exit(int err_code, t_xe *xe)
int ret;
if (err_code == ARG_ERR)
{
if (ft_putstr_fd("Minishell takes no argument\n", STDERR_FILENO) != WRITE_SUCCESS) // takes no argument?
ft_error(WRITE_ERR, xe); // possible?
}
ft_putstr_fd("minishell takes no argument\n", STDERR_FILENO);
if (err_code < FT_EXIT)
ret = EXIT_SUCCESS;
else
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment