aboutsummaryrefslogtreecommitdiff
path: root/src/builtins_part_two.c
diff options
context:
space:
mode:
authorChristopher Uhlig2024-10-25 20:53:44 +0200
committerChristopher Uhlig2024-10-25 20:53:44 +0200
commit00ad7429f223c85e99da6ffa8f7dade0c73c97b5 (patch)
tree001c5556aa20dfcdecb61fb0623772e6b54c7e90 /src/builtins_part_two.c
parentf4663487bec69c37ed6a010a342ce2132f400eb5 (diff)
downloadminishell-00ad7429f223c85e99da6ffa8f7dade0c73c97b5.tar.gz
minishell-00ad7429f223c85e99da6ffa8f7dade0c73c97b5.zip
update for builtins and extra functions
Diffstat (limited to 'src/builtins_part_two.c')
-rw-r--r--src/builtins_part_two.c82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c
new file mode 100644
index 0000000..94ef258
--- /dev/null
+++ b/src/builtins_part_two.c
@@ -0,0 +1,82 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* builtins_part_two.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */
+/* Updated: 2024/10/25 20:52:46 by chuhlig ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "env.h"
+
+void update_oldpwd(t_env **env)
+{
+ t_env *current;
+ t_env *prev;
+ char cwd[1028];
+ char *tmp;
+
+ 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;
+
+ 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;
+ t_env *prev;
+ t_env *pwd;
+
+ current = env;
+ if (av[1] == NULL)
+ {
+ update_oldpwd(&env);
+ while (current)
+ {
+ if (ft_strncmp(current->name, "HOME", 4) == 0)
+ break ;
+ prev = current;
+ current = current->next;
+ }
+ if (chdir(current->value) == -1)
+ return ;
+ }
+ else
+ {
+ update_oldpwd(&env);
+ if (chdir(av[1]) == -1)
+ return ;
+ update_pwd(&env);
+ }
+} \ No newline at end of file