summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--src/ft_b_cd.c30
-rw-r--r--src/ft_e_builtins.c6
-rw-r--r--src/ft_f_chdir.c27
-rw-r--r--src/ft_f_chdir.h22
-rw-r--r--src/ft_f_fail.h1
-rw-r--r--src/ft_s_init.c1
7 files changed, 65 insertions, 23 deletions
diff --git a/Makefile b/Makefile
index 0eda608..1f3a4c4 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,7 @@ SRCS_NAME += ft_e_builtins.c
SRCS_NAME += ft_e_externs.c
SRCS_NAME += ft_e_externs_next.c
SRCS_NAME += ft_e_lcom.c
+SRCS_NAME += ft_f_chdir.c
SRCS_NAME += ft_f_errno.c
SRCS_NAME += ft_f_fail.c
SRCS_NAME += ft_f_redir.c
diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c
index e36d60c..54dc92e 100644
--- a/src/ft_b_cd.c
+++ b/src/ft_b_cd.c
@@ -20,25 +20,6 @@
#include "ft_u_utils.h"
#include "ft_u_vars.h"
-/* static void */
-/* ft_switch_env_var(char **envp) */
-/* { */
-/* char **ptr; */
-/* char *path; */
-
-/* ptr = envp; */
-/* while (*ptr) */
-/* { */
-/* if (ft_strncmp("HOME", *ptr, 4) == 0) */
-/* { */
-/* path = ft_substr(*ptr, 5, ft_strlen(*ptr + 5)); */
-/* return (path); */
-/* } */
-/* ptr++; */
-/* } */
-/* return (NULL); */
-/* } */
-
uint8_t
ft_b_cd(char *args[],
t_msh *msh)
@@ -64,14 +45,21 @@ uint8_t
ft_s_destroy(msh);
ft_fail_alloc(msh);
}
+ if (!ft_strncmp("~/", path, 2) || !ft_strncmp("~", path, 2))
+ {
+ if (!(path = ft_strsubst(path, "~", ft_subst_var_value("$HOME", msh))))
+ {
+ ft_s_destroy(msh);
+ ft_fail_alloc(msh);
+ }
+ }
}
if (chdir(path) != 0)
{
- ft_f_dump_errno("cd", msh);
+ ft_fail_chd("cd", path, msh);
ft_memdel((void*)&path);
return (1);
}
- /* TODO: ft_switch_env_var() */
ft_memdel((void*)&msh->cwd);
msh->cwd = getcwd(NULL, 0);
ft_memdel((void*)&path);
diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c
index 1045856..c78a80c 100644
--- a/src/ft_e_builtins.c
+++ b/src/ft_e_builtins.c
@@ -16,7 +16,6 @@
#include <stdint.h>
#include <unistd.h>
-#include "ft_b_builtins.h"
#include "ft_m_redirs.h"
#include "ft_s_destroy.h"
#include "ft_s_lcom.h"
@@ -49,8 +48,11 @@ static void
msh->ret = WEXITSTATUS(status);
if (bu_id == 1 && msh->ret == 0)
{
- ft_b_cd(ptr->argv + 1, msh);
+ msh->bu_ptr[bu_id](ptr->argv + 1, msh);
+ /* TODO: export $PWD */
}
+ else if (bu_id == 6)
+ msh->bu_ptr[bu_id](ptr->argv + 1, msh);
}
void
diff --git a/src/ft_f_chdir.c b/src/ft_f_chdir.c
new file mode 100644
index 0000000..1386626
--- /dev/null
+++ b/src/ft_f_chdir.c
@@ -0,0 +1,27 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_f_chdir.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include "ft_s_struct.h"
+
+void
+ ft_fail_chd(const char concern[],
+ const char path[],
+ t_msh *msh)
+{
+ ft_dprintf(STDERR_FILENO, "%s: %s: %s: %s\n",
+ msh->shname, concern, path, strerror(errno));
+}
diff --git a/src/ft_f_chdir.h b/src/ft_f_chdir.h
new file mode 100644
index 0000000..fae82d7
--- /dev/null
+++ b/src/ft_f_chdir.h
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_f_chdir.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef FT_F_CHDIR_H
+#define FT_F_CHDIR_H
+
+#include "ft_s_struct.h"
+
+void ft_fail_chd(const char concern[],
+ const char pathp[],
+ t_msh *msh);
+
+#endif
diff --git a/src/ft_f_fail.h b/src/ft_f_fail.h
index 5a28b5e..b176cbb 100644
--- a/src/ft_f_fail.h
+++ b/src/ft_f_fail.h
@@ -13,6 +13,7 @@
#ifndef FT_F_FAIL_H
#define FT_F_FAIL_H
+#include "ft_f_chdir.h"
#include "ft_f_errno.h"
#include "ft_f_redir.h"
#include "ft_s_struct.h"
diff --git a/src/ft_s_init.c b/src/ft_s_init.c
index 46dac90..76bf72d 100644
--- a/src/ft_s_init.c
+++ b/src/ft_s_init.c
@@ -30,6 +30,7 @@ t_msh
return (NULL);
if (!(msh->shname = ft_strdup(argv[0])))
return (NULL);
+ /* TODO: shname: care about "./", try with symlinks */
msh->cwd = NULL;
msh->cwd = getcwd(NULL, 0);
/* TODO: handle getcwd failed */