diff --git a/inc/builtin/env_utils.h b/inc/builtin/env_utils.h index 44dc97e..1714a5d 100644 --- a/inc/builtin/env_utils.h +++ b/inc/builtin/env_utils.h @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 10:25:47 by dayano #+# #+# */ -/* Updated: 2025/05/17 12:40:38 by ttsubo ### ########.fr */ +/* Updated: 2025/05/22 12:30:11 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,5 +24,7 @@ t_env *get_env_node(t_env *current, const char *key); char *get_env_value(t_env *current, const char *key); int set_env_value(t_env *current, const char *key, char *value); bool has_env_key(t_env *current, const char *key); +void remove_env_value(t_env *env, char *key); +int add_env_value(t_env *env, char *key, char *value); #endif diff --git a/src/builtin/cd.c b/src/builtin/cd.c index 3f240c1..5db3786 100644 --- a/src/builtin/cd.c +++ b/src/builtin/cd.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/14 11:12:37 by ttsubo #+# #+# */ -/* Updated: 2025/05/17 13:36:48 by ttsubo ### ########.fr */ +/* Updated: 2025/05/22 12:50:37 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,17 +16,28 @@ static int _cd_common(t_minish *minish, char *path) { char *pwd; char *newpwd; + char *oldpwd; - pwd = get_env_value(minish->env, "PWD"); if (chdir(path) < 0) return (perror(path), 1); newpwd = getcwd(NULL, 0); if (!newpwd) return (perror("getcwd"), 1); - set_env_value(minish->env, "OLDPWD", pwd); - set_env_value(minish->env, "PWD", newpwd); - free_str(&newpwd); - return (0); + pwd = ft_strdup(get_env_value(minish->env, "PWD")); + oldpwd = get_env_value(minish->env, "OLDPWD"); + if (pwd && oldpwd) + { + set_env_value(minish->env, "PWD", newpwd); + set_env_value(minish->env, "OLDPWD", pwd); + } + if (!pwd && oldpwd) + remove_env_value(minish->env, "OLDPWD"); + if (pwd && !oldpwd) + { + set_env_value(minish->env, "PWD", newpwd); + add_env_value(minish->env, "OLDPWD", pwd); + } + return (free_str(&pwd), free_str(&newpwd), 0); } static int _cd_home(t_minish *minish) @@ -44,14 +55,14 @@ static int _cd_oldpwd(t_minish *minish) char *oldpwd; int status; - oldpwd = get_env_value(minish->env, "OLDPWD"); + oldpwd = ft_strdup(get_env_value(minish->env, "OLDPWD")); if (!oldpwd) return (error_mes("bash: cd", "OLDPWD not set"), EXIT_FAILURE); status = _cd_common(minish, oldpwd); if (status == 0) - return (printf("%s\n", oldpwd), status); - else - return (status); + printf("%s\n", oldpwd); + free_str(&oldpwd); + return (status); } static int _cd_normal(char *path, t_minish *minish) diff --git a/src/builtin/env_utils.c b/src/builtin/env_utils.c index dd3a485..8c99dfb 100644 --- a/src/builtin/env_utils.c +++ b/src/builtin/env_utils.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/17 10:26:59 by dayano #+# #+# */ -/* Updated: 2025/04/28 16:32:43 by ttsubo ### ########.fr */ +/* Updated: 2025/05/22 12:29:48 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,3 +52,48 @@ void remove_env_node(t_env **env_lst, t_env *target) current = current->next; } } + +void remove_env_value(t_env *env, char *key) +{ + t_env *current; + t_env *prev; + + current = env; + prev = NULL; + while (current) + { + if (ft_strcmp(current->key, key) == 0) + { + if (prev) + prev->next = current->next; + else + env = current->next; + free(current->key); + free(current->value); + free(current); + return ; + } + prev = current; + current = current->next; + } +} + +int add_env_value(t_env *env, char *key, char *value) +{ + t_env *node; + + if (!is_valid_key(key)) + return (export_err_invalid("minish", key, value)); + node = ft_calloc(1, sizeof(t_env)); + if (!node) + return (EXIT_FAILURE); + node->key = ft_strdup(key); + if (!node->key) + return (free(node), EXIT_FAILURE); + node->value = ft_strdup(value); + if (!node->value) + return (free(node->key), free(node), EXIT_FAILURE); + node->is_exported = 1; + add_env_back(&env, node); + return (0); +} diff --git a/src/invoke_cmd/create_envp.c b/src/invoke_cmd/create_envp.c index 85d4cc4..67eabe3 100644 --- a/src/invoke_cmd/create_envp.c +++ b/src/invoke_cmd/create_envp.c @@ -6,7 +6,7 @@ /* By: ttsubo +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/24 13:56:59 by dayano #+# #+# */ -/* Updated: 2025/05/04 22:09:48 by ttsubo ### ########.fr */ +/* Updated: 2025/05/22 12:36:37 by ttsubo ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,9 +73,9 @@ char **create_envp(t_minish *minish) envp[i] = _get_envp_key_value(env); if (!envp[i]) return (free_strs(&envp), NULL); + i++; } env = env->next; - i++; } envp[i] = NULL; return (envp);