diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | src/m_minishell.c | 2 | ||||
-rw-r--r-- | src/s_init.c | 204 | ||||
-rw-r--r-- | src/s_init.h | 12 | ||||
-rw-r--r-- | src/s_init_next.c | 109 | ||||
-rw-r--r-- | src/s_init_next.h | 23 |
6 files changed, 212 insertions, 139 deletions
@@ -64,6 +64,7 @@ SRCS_NAME += m_prompt_seq SRCS_NAME += s_destroy SRCS_NAME += s_com SRCS_NAME += s_init +SRCS_NAME += s_init_next SRCS_NAME += s_lalias SRCS_NAME += s_line SRCS_NAME += s_lvars diff --git a/src/m_minishell.c b/src/m_minishell.c index 938779c..e9475b3 100644 --- a/src/m_minishell.c +++ b/src/m_minishell.c @@ -28,7 +28,7 @@ int main(int argc, char *const argv[], char *const envp[]) int32_t ret; t_msh *msh; - if ((msh = init_msh(argc, argv, envp)) == NULL) + if ((msh = s_init_msh(argc, argv, envp)) == NULL) { ft_dprintf(STDERR_FILENO, "%s\n", strerror(errno)); return (M_RET_ALLOC); diff --git a/src/s_init.c b/src/s_init.c index 1ce73dd..0842f7c 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -15,173 +15,113 @@ #include <stdlib.h> #include <unistd.h> #include <limits.h> -#include <dirent.h> -#include <errno.h> #include "b_export_next.h" #include "d_define.h" #include "f_fail.h" #include "m_funptr.h" -#include "s_init.h" +#include "s_init_next.h" #include "u_vars.h" #include "u_vars_next.h" -static void - s_init_sqb_ref_one(t_msh *msh) +static void s_init_sqb_ref_one(t_msh *msh) { - ft_memcpy(msh->sqb_ref[B_ID_SQB_B], B_SQB_B, ft_strlen(B_SQB_B) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_C], B_SQB_C, ft_strlen(B_SQB_C) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_D], B_SQB_D, ft_strlen(B_SQB_D) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_E], B_SQB_E, ft_strlen(B_SQB_E) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_F], B_SQB_F, ft_strlen(B_SQB_F) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_H], B_SQB_H, ft_strlen(B_SQB_H) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_N], B_SQB_N, ft_strlen(B_SQB_N) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_P], B_SQB_P, ft_strlen(B_SQB_P) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_R], B_SQB_R, ft_strlen(B_SQB_R) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_S], B_SQB_S, ft_strlen(B_SQB_S) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_W], B_SQB_W, ft_strlen(B_SQB_W) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_X], B_SQB_X, ft_strlen(B_SQB_X) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_Z], B_SQB_Z, ft_strlen(B_SQB_Z) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_L_MAJ], - B_SQB_L_MAJ, ft_strlen(B_SQB_L_MAJ) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_S_MAJ], - B_SQB_S_MAJ, ft_strlen(B_SQB_S_MAJ) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_NT], B_SQB_NT, ft_strlen(B_SQB_NT) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_OT], B_SQB_OT, ft_strlen(B_SQB_OT) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_EQUA], - B_SQB_EQUA, ft_strlen(B_SQB_EQUA) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_DIFF], - B_SQB_DIFF, ft_strlen(B_SQB_DIFF) + 1); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_B], B_SQB_B, + (ft_strlen(B_SQB_B) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_C], B_SQB_C, + (ft_strlen(B_SQB_C) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_D], B_SQB_D, + (ft_strlen(B_SQB_D) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_E], B_SQB_E, + (ft_strlen(B_SQB_E) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_F], B_SQB_F, + (ft_strlen(B_SQB_F) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_H], B_SQB_H, + (ft_strlen(B_SQB_H) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_N], B_SQB_N, + (ft_strlen(B_SQB_N) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_P], B_SQB_P, + (ft_strlen(B_SQB_P) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_R], B_SQB_R, + (ft_strlen(B_SQB_R) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_S], B_SQB_S, + (ft_strlen(B_SQB_S) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_W], B_SQB_W, + (ft_strlen(B_SQB_W) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_X], B_SQB_X, + (ft_strlen(B_SQB_X) + 1) * sizeof(char)); } -static void - s_init_sqb_ref_two(t_msh *msh) +static void s_init_sqb_ref_two(t_msh *msh) { - ft_memcpy(msh->sqb_ref[B_ID_SQB_EQ], B_SQB_EQ, ft_strlen(B_SQB_EQ) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_NE], B_SQB_NE, ft_strlen(B_SQB_NE) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_GT], B_SQB_GT, ft_strlen(B_SQB_GT) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_GE], B_SQB_GE, ft_strlen(B_SQB_GE) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_LT], B_SQB_LT, ft_strlen(B_SQB_LT) + 1); - ft_memcpy(msh->sqb_ref[B_ID_SQB_LE], B_SQB_LE, ft_strlen(B_SQB_LE) + 1); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_Z], B_SQB_Z, + (ft_strlen(B_SQB_Z) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_L_MAJ], B_SQB_L_MAJ, + (ft_strlen(B_SQB_L_MAJ) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_S_MAJ], B_SQB_S_MAJ, + (ft_strlen(B_SQB_S_MAJ) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_NT], B_SQB_NT, + (ft_strlen(B_SQB_NT) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_OT], B_SQB_OT, + (ft_strlen(B_SQB_OT) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_EQUA], B_SQB_EQUA, + (ft_strlen(B_SQB_EQUA) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_DIFF], B_SQB_DIFF, + (ft_strlen(B_SQB_DIFF) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_EQ], B_SQB_EQ, + (ft_strlen(B_SQB_EQ) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_NE], B_SQB_NE, + (ft_strlen(B_SQB_NE) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_GT], B_SQB_GT, + (ft_strlen(B_SQB_GT) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_GE], B_SQB_GE, + (ft_strlen(B_SQB_GE) + 1) * sizeof(char)); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_LT], B_SQB_LT, + (ft_strlen(B_SQB_LT) + 1) * sizeof(char)); } -static char - **dupenv_del(char **nenvp, - uint64_t i) +static void s_init_sqb_ref_thr(t_msh *msh) { - while (i > 0) - { - ft_memdel((void*)&nenvp[i]); - i--; - } - ft_memdel((void*)&nenvp); - return (NULL); -} - -static char - **dupenv(char *const envp[]) -{ - uint64_t i; - char **nenvp; - - i = 0; - while (envp[i]) - { - i++; - } - if (!(nenvp = (char**)malloc((i + 1) * sizeof(char*)))) - { - return (NULL); - } - i = 0; - while (envp[i]) - { - if (!(nenvp[i] = ft_strdup(envp[i]))) - return (dupenv_del(nenvp, i)); - i++; - } - nenvp[i] = NULL; - return (nenvp); + (void)ft_memcpy(msh->sqb_ref[B_ID_SQB_LE], B_SQB_LE, + (ft_strlen(B_SQB_LE) + 1) * sizeof(char)); } -static void - inc_shlvl(t_msh *msh) +static void s_null_some(t_msh *msh) { - char fmt[8]; - char tmp[255]; - char str[255]; - int32_t shlvl; - - u_get_var_value(tmp, "$SHLVL", 255, msh); - if (tmp[0] == C_NUL) - { - ft_memcpy(fmt, "SHLVL=1", 8); - b_export_with_equals(fmt, msh); - } - else - { - shlvl = ft_atoi(tmp); - if (shlvl >= 999) - f_shlvl_too_high(shlvl); - shlvl = (shlvl >= 999) ? 0 : shlvl; - shlvl = (shlvl < 0) ? 0 : shlvl + 1; - ft_itoa_s(str, shlvl); - u_subst_var_value("$SHLVL", str, msh); - } -} - -static void - set_cwd(char cwd[], t_msh *msh) -{ - char fmt[PATH_MAX]; - DIR *dir; - - u_get_var_value(cwd, "$PWD", PATH_MAX, msh); - if (cwd[0] == C_NUL) - { - getcwd(cwd, PATH_MAX); - ft_sprintf(fmt, "%s=%s", "PWD", cwd); - b_export_with_equals(fmt, msh); - return ; - } - if ((dir = opendir(cwd)) != NULL) - closedir(dir); - else if (errno == ENOENT) - { - getcwd(cwd, PATH_MAX); - ft_sprintf(fmt, "%s=%s", "PWD", cwd); - b_export_with_equals(fmt, msh); - return ; - } + msh->fd = 0; + msh->curr = NULL; + msh->com = NULL; + msh->pipes = NULL; + msh->vars = NULL; + msh->alias = NULL; + msh->prev_hist = NULL; } -t_msh *init_msh(int32_t argc, char *const argv[], char *const envp[]) +t_msh *s_init_msh(int32_t argc, char *const argv[], char *const envp[]) { - char cwd[PATH_MAX]; t_msh *msh; + char cwd[PATH_MAX]; if ((msh = (t_msh*)malloc(sizeof(t_msh))) == NULL) return (NULL); msh->envp = NULL; - if ((msh->envp = dupenv(envp)) == NULL) + if ((msh->envp = s_dupenv(envp)) == NULL) return (NULL); msh->argc = argc - 1; msh->argv = (char**)argv; msh->ret = 0; init_buptr(msh); - msh->fd = 0; - msh->curr = NULL; - msh->com = NULL; - msh->pipes = NULL; - msh->vars = NULL; - msh->alias = NULL; - msh->prev_hist = NULL; - set_cwd(cwd, msh); + s_null_some(msh); + s_set_cwd(cwd, msh); if ((msh->cwd = ft_strdup(cwd)) == NULL) + { + ft_memdel((void*)&msh); return (NULL); + } msh->env_fork_tmp[0][0] = '\0'; - inc_shlvl(msh); + s_inc_shlvl(msh); s_init_sqb_ref_one(msh); s_init_sqb_ref_two(msh); + s_init_sqb_ref_thr(msh); return (msh); } diff --git a/src/s_init.h b/src/s_init.h index a598043..f95918b 100644 --- a/src/s_init.h +++ b/src/s_init.h @@ -10,14 +10,14 @@ /* */ /* ************************************************************************** */ -#ifndef S_INIT_H -#define S_INIT_H +#ifndef FT_S_INIT_H +# define FT_S_INIT_H -#include <stdlib.h> -#include <stdint.h> +# include <stdlib.h> +# include <stdint.h> -#include "s_struct.h" +# include "s_struct.h" -t_msh *init_msh(int32_t argc, char *const argv[], char *const envp[]); +t_msh *s_init_msh(int32_t argc, char *const argv[], char *const envp[]); #endif diff --git a/src/s_init_next.c b/src/s_init_next.c new file mode 100644 index 0000000..e8e0006 --- /dev/null +++ b/src/s_init_next.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_init_next.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 <stdlib.h> +#include <unistd.h> +#include <dirent.h> +#include <errno.h> +#include <limits.h> + +#include "b_export_next.h" +#include "f_fail.h" +#include "s_struct.h" +#include "u_vars.h" +#include "u_vars_next.h" + +void s_inc_shlvl(t_msh *msh) +{ + char fmt[8]; + char tmp[255]; + char str[255]; + int32_t shlvl; + + u_get_var_value(tmp, "$SHLVL", 255, msh); + if (tmp[0] == C_NUL) + { + ft_memcpy(fmt, "SHLVL=1", 8); + b_export_with_equals(fmt, msh); + } + else + { + shlvl = ft_atoi(tmp); + if (shlvl >= 999) + f_shlvl_too_high(shlvl); + shlvl = (shlvl >= 999) ? 0 : shlvl; + shlvl = (shlvl < 0) ? 0 : shlvl + 1; + ft_itoa_s(str, shlvl); + u_subst_var_value("$SHLVL", str, msh); + } +} + +char **s_dupenv_del(char **nenvp, uint64_t i) +{ + while (i > 0) + { + ft_memdel((void*)&nenvp[i]); + i--; + } + ft_memdel((void*)&nenvp); + return (NULL); +} + +char **s_dupenv(char *const envp[]) +{ + uint64_t i; + char **nenvp; + + i = 0; + while (envp[i] != NULL) + { + i++; + } + if ((nenvp = (char**)malloc((i + 1) * sizeof(char*))) == NULL) + { + return (NULL); + } + i = 0; + while (envp[i] != NULL) + { + if ((nenvp[i] = ft_strdup(envp[i])) == NULL) + return (s_dupenv_del(nenvp, i)); + i++; + } + nenvp[i] = NULL; + return (nenvp); +} + +void s_set_cwd(char cwd[], t_msh *msh) +{ + char fmt[PATH_MAX]; + DIR *dir; + + u_get_var_value(cwd, "$PWD", PATH_MAX, msh); + if (cwd[0] == C_NUL) + { + getcwd(cwd, PATH_MAX); + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + return ; + } + if ((dir = opendir(cwd)) != NULL) + closedir(dir); + else if (errno == ENOENT) + { + getcwd(cwd, PATH_MAX); + ft_sprintf(fmt, "%s=%s", "PWD", cwd); + b_export_with_equals(fmt, msh); + return ; + } +} diff --git a/src/s_init_next.h b/src/s_init_next.h new file mode 100644 index 0000000..9ea51ee --- /dev/null +++ b/src/s_init_next.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* s_init_next.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_S_INIT_NEXT_H +# define FT_S_INIT_NEXT_H + +# include "s_struct.h" + +void s_inc_shlvl(t_msh *msh); +char **s_dupenv_del(char **nenvp, uint64_t i); +char **s_dupenv(char *const envp[]); +void s_set_cwd(char cwd[], t_msh *msh); + +#endif |