diff options
Diffstat (limited to '')
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | src/ft_b_cd.c | 27 | ||||
-rw-r--r-- | src/ft_e_builtins.c | 9 | ||||
-rw-r--r-- | src/ft_e_lcom.c | 1 | ||||
-rw-r--r-- | src/ft_f_errno.c | 26 | ||||
-rw-r--r-- | src/ft_f_errno.h | 21 | ||||
-rw-r--r-- | src/ft_f_fail.h | 2 | ||||
-rw-r--r-- | src/ft_m_redirs.c | 2 |
8 files changed, 82 insertions, 7 deletions
@@ -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_errno.c SRCS_NAME += ft_f_fail.c SRCS_NAME += ft_f_redir.c SRCS_NAME += ft_m_argv.c diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c index 7d4ef1a..e36d60c 100644 --- a/src/ft_b_cd.c +++ b/src/ft_b_cd.c @@ -14,6 +14,8 @@ #include <stdint.h> #include <unistd.h> +#include "ft_f_fail.h" +#include "ft_s_destroy.h" #include "ft_s_struct.h" #include "ft_u_utils.h" #include "ft_u_vars.h" @@ -41,22 +43,37 @@ uint8_t ft_b_cd(char *args[], t_msh *msh) { + /* TODO: norme */ const uint64_t argc = ft_get_argc((const char**)args); char *path; - if (argc == 0) + if (argc >= 2) { - path = ft_subst_var_value("$HOME", msh); - ft_printf("%s\n", path); + ft_fail_too_many_args("cd", msh); + return (1); + } + else if (argc == 0) + { + if (!(path = ft_subst_var_value("$HOME", msh))) + return (1); + } + else + { + if (!(path = ft_strdup(*args))) + { + ft_s_destroy(msh); + ft_fail_alloc(msh); + } } if (chdir(path) != 0) { - /* TODO: handle cd failed */ + ft_f_dump_errno("cd", 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); - /* TODO: finish cd */ return (0); } diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c index bc3eece..1045856 100644 --- a/src/ft_e_builtins.c +++ b/src/ft_e_builtins.c @@ -16,6 +16,7 @@ #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" @@ -37,6 +38,8 @@ static void static void ft_e_builtin_parent(pid_t pid, + const t_lcom *ptr, + uint8_t bu_id, t_msh *msh) { int32_t status; @@ -44,6 +47,10 @@ static void while (wait(&status) != pid) ; msh->ret = WEXITSTATUS(status); + if (bu_id == 1 && msh->ret == 0) + { + ft_b_cd(ptr->argv + 1, msh); + } } void @@ -64,6 +71,6 @@ void } else { - ft_e_builtin_parent(pid, msh); + ft_e_builtin_parent(pid, ptr, bu_id, msh); } } diff --git a/src/ft_e_lcom.c b/src/ft_e_lcom.c index 3288112..4a57f7b 100644 --- a/src/ft_e_lcom.c +++ b/src/ft_e_lcom.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include <libft.h> + #include "ft_e_builtins.h" #include "ft_e_externs.h" #include "ft_s_struct.h" diff --git a/src/ft_f_errno.c b/src/ft_f_errno.c new file mode 100644 index 0000000..22ee7a9 --- /dev/null +++ b/src/ft_f_errno.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_errno.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_f_dump_errno(const char concern[], + t_msh *msh) +{ + ft_dprintf(STDERR_FILENO, "%s: %s: %s\n", + msh->shname, concern, strerror(errno)); +} diff --git a/src/ft_f_errno.h b/src/ft_f_errno.h new file mode 100644 index 0000000..86fce83 --- /dev/null +++ b/src/ft_f_errno.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_f_errno.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_ERRNO_H +#define FT_F_ERRNO_H + +#include "ft_s_struct.h" + +void ft_f_dump_errno(const char concern[], + t_msh *msh); + +#endif diff --git a/src/ft_f_fail.h b/src/ft_f_fail.h index 5091810..5a28b5e 100644 --- a/src/ft_f_fail.h +++ b/src/ft_f_fail.h @@ -13,6 +13,8 @@ #ifndef FT_F_FAIL_H #define FT_F_FAIL_H +#include "ft_f_errno.h" +#include "ft_f_redir.h" #include "ft_s_struct.h" void ft_fail_no_options(const char concern[], diff --git a/src/ft_m_redirs.c b/src/ft_m_redirs.c index bdb5b21..701a2a4 100644 --- a/src/ft_m_redirs.c +++ b/src/ft_m_redirs.c @@ -14,7 +14,7 @@ #include <unistd.h> #include <errno.h> -#include "ft_f_redir.h" +#include "ft_f_fail.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" #include "ft_s_struct.h" |