From 58f7f3e9d2a61d46e57ffe324ae6ae7f21e875a3 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Thu, 23 Apr 2020 16:29:20 +0200 Subject: Prepared builtins templates --- Makefile | 5 ++- src/ft_b_builtins.h | 3 ++ src/ft_b_cd.c | 24 ++++++++++++ src/ft_b_cd.h | 22 +++++++++++ src/ft_b_pwd.c | 24 ++++++++++++ src/ft_b_pwd.h | 22 +++++++++++ src/ft_b_unset.c | 24 ++++++++++++ src/ft_b_unset.h | 22 +++++++++++ src/ft_e_builtin.c | 109 ---------------------------------------------------- src/ft_e_builtin.h | 23 ----------- src/ft_e_builtins.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/ft_e_builtins.h | 23 +++++++++++ src/ft_e_lcom.c | 2 +- src/ft_m_funptr.c | 6 +-- 14 files changed, 281 insertions(+), 137 deletions(-) create mode 100644 src/ft_b_cd.c create mode 100644 src/ft_b_cd.h create mode 100644 src/ft_b_pwd.c create mode 100644 src/ft_b_pwd.h create mode 100644 src/ft_b_unset.c create mode 100644 src/ft_b_unset.h delete mode 100644 src/ft_e_builtin.c delete mode 100644 src/ft_e_builtin.h create mode 100644 src/ft_e_builtins.c create mode 100644 src/ft_e_builtins.h diff --git a/Makefile b/Makefile index 2970572..82e9194 100644 --- a/Makefile +++ b/Makefile @@ -15,11 +15,14 @@ LFT_SRCS_DIR = ${LFT_DIR}src/ #--------------------------------- FILES --------------------------------------# #==============================================================================# SRCS_NAME = minishell.c +SRCS_NAME += ft_b_cd.c SRCS_NAME += ft_b_echo.c SRCS_NAME += ft_b_env.c SRCS_NAME += ft_b_exit.c SRCS_NAME += ft_b_export.c -SRCS_NAME += ft_e_builtin.c +SRCS_NAME += ft_b_pwd.c +SRCS_NAME += ft_b_unset.c +SRCS_NAME += ft_e_builtins.c SRCS_NAME += ft_e_lcom.c SRCS_NAME += ft_f_fail.c SRCS_NAME += ft_f_file.c diff --git a/src/ft_b_builtins.h b/src/ft_b_builtins.h index 21ffe0b..36e6498 100644 --- a/src/ft_b_builtins.h +++ b/src/ft_b_builtins.h @@ -13,9 +13,12 @@ #ifndef FT_B_BUILTINS_H #define FT_B_BUILTINS_H +#include "ft_b_cd.h" #include "ft_b_echo.h" #include "ft_b_env.h" #include "ft_b_exit.h" #include "ft_b_export.h" +#include "ft_b_pwd.h" +#include "ft_b_unset.h" #endif diff --git a/src/ft_b_cd.c b/src/ft_b_cd.c new file mode 100644 index 0000000..c0bf04b --- /dev/null +++ b/src/ft_b_cd.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_b_cd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_s_struct.h" + +uint8_t + ft_b_cd(char *args[], + t_msh *msh) +{ + (void)args; + (void)msh; + /* TODO: do cd */ + return (0); +} diff --git a/src/ft_b_cd.h b/src/ft_b_cd.h new file mode 100644 index 0000000..e789cc0 --- /dev/null +++ b/src/ft_b_cd.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_b_cd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_B_CD_H +#define FT_B_CD_H + +#include +#include "ft_s_struct.h" + +uint8_t ft_b_cd(char *args[], + t_msh *msh); + +#endif diff --git a/src/ft_b_pwd.c b/src/ft_b_pwd.c new file mode 100644 index 0000000..f06a320 --- /dev/null +++ b/src/ft_b_pwd.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_b_pwd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_s_struct.h" + +uint8_t + ft_b_pwd(char *args[], + t_msh *msh) +{ + (void)args; + (void)msh; + /* TODO: do pwd */ + return (0); +} diff --git a/src/ft_b_pwd.h b/src/ft_b_pwd.h new file mode 100644 index 0000000..653c9e4 --- /dev/null +++ b/src/ft_b_pwd.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_b_pwd.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_B_PWD_H +#define FT_B_PWD_H + +#include +#include "ft_s_struct.h" + +uint8_t ft_b_pwd(char *args[], + t_msh *msh); + +#endif diff --git a/src/ft_b_unset.c b/src/ft_b_unset.c new file mode 100644 index 0000000..a382aca --- /dev/null +++ b/src/ft_b_unset.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_b_unset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include "ft_s_struct.h" + +uint8_t + ft_b_unset(char *args[], + t_msh *msh) +{ + (void)args; + (void)msh; + /* TODO: do unset */ + return (0); +} diff --git a/src/ft_b_unset.h b/src/ft_b_unset.h new file mode 100644 index 0000000..5ab1b2b --- /dev/null +++ b/src/ft_b_unset.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_b_unset.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_B_UNSET_H +#define FT_B_UNSET_H + +#include +#include "ft_s_struct.h" + +uint8_t ft_b_unset(char *args[], + t_msh *msh); + +#endif diff --git a/src/ft_e_builtin.c b/src/ft_e_builtin.c deleted file mode 100644 index 4f58f44..0000000 --- a/src/ft_e_builtin.c +++ /dev/null @@ -1,109 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_builtin.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#include -#include -#include -#include -#include -#include -#include -#include "ft_f_file.h" -#include "ft_s_destroy.h" -#include "ft_s_lcom.h" -#include "ft_s_struct.h" - -static void - ft_dup_redirs(const t_lcom *ptr, - t_msh *msh) -{ - int32_t fd; - - if (ptr->redir == -1) - { - if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) - ft_f_file(ptr->rdrpath, msh); - /* TODO: handle < redir */ - } - if (ptr->redir == 1) - { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) - ft_f_file(ptr->rdrpath, msh); - dup2(fd, STDOUT_FILENO); - close(fd); - } - if (ptr->redir == 2) - { - if ((fd = open(ptr->rdrpath, - O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) - ft_f_file(ptr->rdrpath, msh); - dup2(fd, STDOUT_FILENO); - close(fd); - } -} - -static void - ft_e_builtin_child(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh) -{ - int32_t ret; - - ft_dup_redirs(ptr, msh); - ret = msh->bu_ptr[bu_id](ptr->args, msh); - ft_lcom_clear(&msh->curr); - ft_s_destroy(msh); - exit(ret); -} - -static void - ft_e_builtin_parent(pid_t pid, - t_msh *msh) -{ - int32_t status; - - while (wait(&status) != pid) - { - /* TODO: fix this unnormed syntax */ - } - msh->ret = WEXITSTATUS(status); -} - -void - ft_e_builtin(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh) -{ - pid_t pid; - - /* TODO: handle exit | bu_id = 6 */ - if (bu_id != 6) - { - if ((pid = fork()) == 0) - { - ft_e_builtin_child(ptr, bu_id, msh); - } - else if (pid < 0) - { - /* TODO: handle fork failed */ - } - else - { - ft_e_builtin_parent(pid, msh); - } - } - else - { - msh->ret = msh->bu_ptr[bu_id](ptr->args, msh); - } -} diff --git a/src/ft_e_builtin.h b/src/ft_e_builtin.h deleted file mode 100644 index 45dad81..0000000 --- a/src/ft_e_builtin.h +++ /dev/null @@ -1,23 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_e_builtin.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rbousset +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ -/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef FT_E_BUILTIN_H -#define FT_E_BUILTIN_H - -#include -#include "ft_s_struct.h" - -void ft_e_builtin(const t_lcom *ptr, - uint8_t bu_id, - t_msh *msh); - -#endif diff --git a/src/ft_e_builtins.c b/src/ft_e_builtins.c new file mode 100644 index 0000000..34a8534 --- /dev/null +++ b/src/ft_e_builtins.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_e_builtins.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include +#include +#include +#include +#include +#include +#include "ft_f_file.h" +#include "ft_s_destroy.h" +#include "ft_s_lcom.h" +#include "ft_s_struct.h" + +static void + ft_dup_redirs(const t_lcom *ptr, + t_msh *msh) +{ + int32_t fd; + + if (ptr->redir == -1) + { + if ((fd = open(ptr->rdrpath, O_RDONLY)) == -1) + ft_f_file(ptr->rdrpath, msh); + /* TODO: handle < redir */ + } + if (ptr->redir == 1) + { + if ((fd = open(ptr->rdrpath, + O_CREAT | O_TRUNC | O_WRONLY, 0644)) == -1) + ft_f_file(ptr->rdrpath, msh); + dup2(fd, STDOUT_FILENO); + close(fd); + } + if (ptr->redir == 2) + { + if ((fd = open(ptr->rdrpath, + O_CREAT | O_APPEND | O_WRONLY, 0644)) == -1) + ft_f_file(ptr->rdrpath, msh); + dup2(fd, STDOUT_FILENO); + close(fd); + } +} + +static void + ft_e_builtin_child(const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh) +{ + int32_t ret; + + ft_dup_redirs(ptr, msh); + ret = msh->bu_ptr[bu_id](ptr->args, msh); + ft_lcom_clear(&msh->curr); + ft_s_destroy(msh); + exit(ret); +} + +static void + ft_e_builtin_parent(pid_t pid, + t_msh *msh) +{ + int32_t status; + + while (wait(&status) != pid) + { + /* TODO: fix this unnormed syntax */ + } + msh->ret = WEXITSTATUS(status); +} + +void + ft_e_builtin(const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh) +{ + pid_t pid; + + /* TODO: handle exit | bu_id = 6 */ + if (bu_id != 6) + { + if ((pid = fork()) == 0) + { + ft_e_builtin_child(ptr, bu_id, msh); + } + else if (pid < 0) + { + /* TODO: handle fork failed */ + } + else + { + ft_e_builtin_parent(pid, msh); + } + } + else + { + msh->ret = msh->bu_ptr[bu_id](ptr->args, msh); + } +} diff --git a/src/ft_e_builtins.h b/src/ft_e_builtins.h new file mode 100644 index 0000000..21908b8 --- /dev/null +++ b/src/ft_e_builtins.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_e_builtins.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef FT_E_BUILTINS_H +#define FT_E_BUILTINS_H + +#include +#include "ft_s_struct.h" + +void ft_e_builtin(const t_lcom *ptr, + uint8_t bu_id, + t_msh *msh); + +#endif diff --git a/src/ft_e_lcom.c b/src/ft_e_lcom.c index 7922997..c0a137a 100644 --- a/src/ft_e_lcom.c +++ b/src/ft_e_lcom.c @@ -11,7 +11,7 @@ /* ************************************************************************** */ #include -#include "ft_e_builtin.h" +#include "ft_e_builtins.h" #include "ft_s_struct.h" static uint8_t diff --git a/src/ft_m_funptr.c b/src/ft_m_funptr.c index 22b390f..0682a20 100644 --- a/src/ft_m_funptr.c +++ b/src/ft_m_funptr.c @@ -22,10 +22,10 @@ void ft_init_buptr(t_msh *msh) { msh->bu_ptr[0] = ft_b_echo; - /* msh->bu_ptr[1] = ft_b_cd; */ - /* msh->bu_ptr[2] = ft_b_pwd; */ + msh->bu_ptr[1] = ft_b_cd; + msh->bu_ptr[2] = ft_b_pwd; msh->bu_ptr[3] = ft_b_export; - /* msh->bu_ptr[4] = ft_b_unset; */ + msh->bu_ptr[4] = ft_b_unset; msh->bu_ptr[5] = ft_b_env; msh->bu_ptr[6] = ft_b_exit; /* TODO: them builtins */ -- cgit v1.2.3