diff options
| author | Christopher Uhlig | 2025-01-13 11:06:54 +0100 |
|---|---|---|
| committer | Christopher Uhlig | 2025-01-13 11:06:54 +0100 |
| commit | 78dc50a2bce3c6e31405437189e2990d8fc720ac (patch) | |
| tree | d61d9f0d279e191c1bfb34929908f412cf58c02d /src/builtins_part_two.c | |
| parent | ae5512ea0d6d8be833ca3a9b39f93239109f45b4 (diff) | |
| download | minishell-78dc50a2bce3c6e31405437189e2990d8fc720ac.tar.gz minishell-78dc50a2bce3c6e31405437189e2990d8fc720ac.zip | |
here
Diffstat (limited to 'src/builtins_part_two.c')
| -rw-r--r-- | src/builtins_part_two.c | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c new file mode 100644 index 0000000..f54e04f --- /dev/null +++ b/src/builtins_part_two.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins_part_two.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */ +/* Updated: 2024/12/20 18:53:03 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void update_oldpwd(t_env **env) +{ + t_env *current; + t_env *prev; + char cwd[1028]; + char *tmp; + + prev = NULL; + current = *env; + while (current) + { + if (ft_strncmp(current->name, "OLDPWD", 6) == 0) + break ; + prev = current; + current = current->next; + } + getcwd(cwd, sizeof(cwd)); + tmp = ft_strdup(cwd); + free(current->value); + current->value = tmp; +} + +void update_pwd(t_env **env) +{ + t_env *current; + t_env *prev; + char cwd[1028]; + char *tmp; + + prev = NULL; + current = *env; + while (current) + { + if (ft_strncmp(current->name, "PWD", 3) == 0) + break ; + prev = current; + current = current->next; + } + getcwd(cwd, sizeof(cwd)); + tmp = ft_strdup(cwd); + free(current->value); + current->value = tmp; +} + +int cd(t_env **env, char **av) +{ + t_env *current; + + current = *env; + if (av[1] == NULL) + { + update_oldpwd(env); + while (current) + { + if (ft_strncmp(current->name, "HOME", 4) == 0) + break ; + current = current->next; + } + if (chdir(current->value) == -1) + return (1); + } + else + { + update_oldpwd(env); + if (chdir(av[1]) == -1) + return (1); + update_pwd(env); + } + return (0); +} |
