diff --git a/builtins/builtins.h b/builtins/builtins.h index 25ab11a..8b7679a 100644 --- a/builtins/builtins.h +++ b/builtins/builtins.h @@ -6,7 +6,7 @@ /* By: cchauvet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/20 14:27:36 by erey-bet #+# #+# */ -/* Updated: 2023/03/31 15:02:43 by erey-bet ### ########.fr */ +/* Updated: 2023/04/18 13:22:22 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "./builtins_private.h" -int make_move(char *path, int fd) +int make_move(char *path, t_list **env) { char *join; + char *old; join = ft_strjoin("/", path); - join = ft_strfjoin(get_pwd(fd), join); + join = ft_strfjoin(get_pwd(), join); + old = get_pwd(); if (chdir(join) == 0) { + set_value_by_key("OLDPWD", old, env); + set_value_by_key("PWD", get_pwd(), env); free(join); return (0); } + free(old); free(join); write(2, "cd: No such file or directory\n", 30); return (1); } -int move_folder(char **args, int fd) +int move_folder(char **args, t_list **env) { char *path; + char *old; if (args[0] == NULL || args[1] != NULL) { @@ -40,11 +46,17 @@ int move_folder(char **args, int fd) path = args[0]; if (path[0] == '/' || ft_strncmp(path, "..", ft_strlen(path)) == 0) { + old = get_pwd(); if (chdir(path) == 0) + { + set_value_by_key("OLDPWD", old, env); + set_value_by_key("PWD", get_pwd(), env); return (0); + } + free(old); write(2, "cd: No such file or directory\n", 30); return (1); } else - return (make_move(path, fd)); + return (make_move(path, env)); } diff --git a/builtins/pwd.c b/builtins/pwd.c index b803d70..ae096bd 100644 --- a/builtins/pwd.c +++ b/builtins/pwd.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/17 16:09:11 by erey-bet #+# #+# */ -/* Updated: 2023/03/09 20:00:19 by erey-bet ### ########.fr */ +/* Updated: 2023/04/18 12:59:25 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,13 +20,13 @@ int pwd(int fd) ft_putendl_fd(path, fd); else { - ft_putendl_fd("Error getcwd", fd); + ft_putendl_fd("Error getcwd", 2); return (1); } return (0); } -char *get_pwd(int fd) +char *get_pwd(void) { char *str; @@ -35,7 +35,7 @@ char *get_pwd(int fd) return (str); else { - ft_putendl_fd("Error getcwd", fd); + ft_putendl_fd("Error getcwd", 2); return (NULL); } } diff --git a/env/env1.c b/env/env1.c index d6001c9..d096ed6 100644 --- a/env/env1.c +++ b/env/env1.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/02/02 14:39:56 by erey-bet #+# #+# */ -/* Updated: 2023/03/30 13:07:15 by erey-bet ### ########.fr */ +/* Updated: 2023/04/18 12:47:08 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,7 +34,11 @@ void add_sort(t_list **head, t_env *var) } } if (current->next == NULL) + { current->next = ft_calloc(1, sizeof(t_list)); + if (!current->next) + return ; + } } char *get_value_by_key(char *key, t_list **head) @@ -61,9 +65,7 @@ int set_value_by_key(char *key, char *value, t_list **head) if (ft_strcmp(((t_env *)current->content)->key, key) == 0) { free(((t_env *)current->content)->value); - free(((t_env *)current->content)->key); ((t_env *)current->content)->value = value; - ((t_env *)current->content)->key = key; return (0); } current = current->next; @@ -97,7 +99,10 @@ t_list **init_env(char **env) return (NULL); *head = ft_calloc(1, sizeof(t_list)); if (*head == NULL) + { + free(head); return (NULL); + } i = -1; while (env[++i]) { diff --git a/env/env2.c b/env/env2.c index 2a3e5e9..4b425c2 100644 --- a/env/env2.c +++ b/env/env2.c @@ -6,7 +6,7 @@ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/03/09 19:59:03 by erey-bet #+# #+# */ -/* Updated: 2023/03/09 19:59:10 by erey-bet ### ########.fr */ +/* Updated: 2023/04/18 13:26:31 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/execution/execution.c b/execution/execution.c index 35ac93f..23e0c1c 100644 --- a/execution/execution.c +++ b/execution/execution.c @@ -6,7 +6,7 @@ /* By: cchauvet executable, "export") == 0) return_code = ft_export(data->env, cmd->args + 1, cmd->fd_out[0]); else if (ft_strcmp(cmd->executable, "cd") == 0) - return_code = (move_folder(cmd->args + 1, cmd->fd_out[0])); + return_code = (move_folder(cmd->args + 1, data->env, cmd->fd_out[0])); else if (ft_strcmp(cmd->executable, "unset") == 0) return_code = (unset(data->env, cmd->args, cmd->fd_out[0])); else if (ft_strcmp(cmd->executable, "echo") == 0) diff --git a/main.c b/main.c index 50b2db6..c604530 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: cchauvet