summaryrefslogtreecommitdiffstats
path: root/src/b_cd.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/b_cd.c')
-rw-r--r--src/b_cd.c42
1 files changed, 14 insertions, 28 deletions
diff --git a/src/b_cd.c b/src/b_cd.c
index 40fbc1a..9bac514 100644
--- a/src/b_cd.c
+++ b/src/b_cd.c
@@ -16,6 +16,7 @@
#include <linux/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);
}