diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-06 21:56:20 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-06 21:56:20 +0200 |
commit | 4543c3ba3222d47780ad3e091cfe6f3098cc2bca (patch) | |
tree | fb8a70bf05464a6fa2c820cc43ea34421340c903 /src/b_cd.c | |
parent | Commit from minishell (diff) | |
download | 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.gz 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.bz2 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.xz 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.tar.zst 42-minishell-4543c3ba3222d47780ad3e091cfe6f3098cc2bca.zip |
Stacked
Diffstat (limited to '')
-rw-r--r-- | src/b_cd.c | 42 |
1 files changed, 14 insertions, 28 deletions
@@ -16,6 +16,7 @@ #include <limits.h> #include "b_export_next.h" +#include "d_define.h" #include "f_fail.h" #include "s_destroy.h" #include "s_struct.h" @@ -24,39 +25,25 @@ #include "u_vars_next.h" static void - set_path(char **path, - char *args[], - t_msh *msh) -{ - if ((*path = ft_strdup(*args)) == NULL) - { - f_alloc_and_destroy_msh(msh); - } -} - -static void b_set_oldpwd(t_msh *msh) { - char *pwd; - char *tmp; - char fmt[PATH_MAX]; + char pwd[PATH_MAX]; + char tmp[PATH_MAX]; - if ((pwd = u_get_var_value("$PWD", msh)) == NULL) + u_get_var_value(pwd, "$PWD", PATH_MAX, msh); + if (pwd[0] == C_NUL) { - if ((pwd = ft_strdup(msh->cwd)) == NULL) - f_alloc_and_destroy_msh(msh); + ft_strlcpy(pwd, msh->cwd, PATH_MAX); } - if ((tmp = u_get_var_value("$OLDPWD", msh)) == NULL) + u_get_var_value(tmp, "$OLDPWD", PATH_MAX, msh); + if (tmp[0] == C_NUL) { - ft_sprintf(fmt, "%s=%s", "OLDPWD", pwd); - b_export_with_equals(fmt, msh); - ft_memdel((void*)pwd); + ft_sprintf(tmp, "%s=%s", "OLDPWD", pwd); + b_export_with_equals(tmp, msh); } else { u_subst_var_value("$OLDPWD", pwd, msh); - ft_memdel((void*)&pwd); - ft_memdel((void*)&tmp); } } @@ -125,7 +112,7 @@ uint8_t t_msh *msh) { const uint64_t argc = u_builtins_get_argc((const char**)args); - char *path; + char path[PATH_MAX]; if (argc >= 2) { @@ -134,7 +121,8 @@ uint8_t } else if (argc == 0) { - if ((path = u_get_var_value("$HOME", msh)) == NULL) + u_get_var_value(path, "$HOME", PATH_MAX, msh); + if (path[0] == C_NUL) { ft_dprintf(STDERR_FILENO, "minishell: cd: %s\n", FT_FAIL_HOME_NOT_SET); @@ -142,14 +130,12 @@ uint8_t } } else - set_path(&path, args, msh); + ft_strlcpy(path, *args, PATH_MAX); if (chdir(path) != 0) { f_fail_chd("cd", path, msh); - ft_memdel((void*)&path); return (1); } b_upgrade_pwd(path, msh); - ft_memdel((void*)&path); return (0); } |