From 2650cb81631b11b8b3d1c73b12d12ed3436fbf76 Mon Sep 17 00:00:00 2001 From: Lilia-42 Date: Sat, 31 Aug 2024 14:47:38 +0200 Subject: [PATCH] Make vars.c comply with the 42 Norm It was split to vars.c and vars_helpers.c --- inc/minishell_structs.h | 6 +- src/env.c | 25 ++++++- src/vars.c | 147 +++++----------------------------------- src/vars_helpers.c | 107 +++++++++++++++++++++++++++++ 4 files changed, 149 insertions(+), 136 deletions(-) create mode 100644 src/vars_helpers.c diff --git a/inc/minishell_structs.h b/inc/minishell_structs.h index 3c04174..d38c1cc 100644 --- a/inc/minishell_structs.h +++ b/inc/minishell_structs.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* minishell_structs.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ljiriste +#+ +:+ +#+ */ +/* By: lnikolov +#+ +:+ +#+ */ +/* By: lnikolov child_pids, NULL); return ; } + +int set_env_var_value(t_execution_env *env, const char *name, const char *value) +{ + if (get_var_value(&env->vars->other, name)) + return (set_var_value(&env->vars->other, name, value)); + else if (get_var_value(&env->vars->exported, name)) + return (set_var_value(&env->vars->exported, name, value)); + else + return (set_var_value(&env->vars->other, name, value)); +} + +const char *get_env_var_value(const t_execution_env *env, const char *var_name) +{ + const char *res; + + res = get_var_value(&env->vars->other, var_name); + if (res) + return (res); + res = get_var_value(&env->vars->exported, var_name); + return (res); +} diff --git a/src/vars.c b/src/vars.c index 089d31c..dc88fb8 100644 --- a/src/vars.c +++ b/src/vars.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: lnikolov -// This function returns 0 when str does not contain valid variable name -// in front of = else it returns the length of the valid name -int has_valid_name(const char *str) -{ - size_t i; - - if (!str) - return (0); - if (ft_isdigit(str[0])) - return (0); - i = 0; - while (str[i]) - { - if (str[i] == '=') - return (i); - if (!ft_isalnum(str[i]) && str[i] != '_') - return (0); - ++i; - } - return (i); -} - -int is_assignment_word(const char *str) -{ - size_t len; - - len = has_valid_name(str); - return (len > 0 && str[len] == '='); -} - -int add_var_line(t_vec *vec, const char *line) -{ - char *tmp; - - tmp = ft_strdup(line); - if (!tmp) - return (1); - if (ft_vec_insert(vec, &tmp, vec->size - 1) != success) - { - free(tmp); - return (1); - } - return (0); -} - -// The keyval_pair is freed only in case of error -// When allocation fails in ft_strcal_alloc, it is freed automatically -// When allocation the insert fails, it needs to be freed here -// When everything goes smoothly, the memory is freed when the vec -// erases this entry -int add_var(t_vec *vec, const char *key, const char *value) -{ - char *keyval_pair; - - keyval_pair = ft_strdup(key); - if (!keyval_pair) - return (1); - if (!ft_strcat_alloc(&keyval_pair, "=")) - return (1); - if (!ft_strcat_alloc(&keyval_pair, value)) - return (1); - if (ft_vec_insert(vec, &keyval_pair, vec->size - 1) != success) - { - free(keyval_pair); - return (1); - } - return (0); -} - -int init_vars(t_vars *vars, char **envp) -{ - char *tmp; - - ft_vec_init(&vars->exported, sizeof(char *)); - ft_vec_init(&vars->other, sizeof(char *)); - tmp = NULL; - ft_vec_append(&vars->exported, &tmp); - ft_vec_append(&vars->other, &tmp); - while (*envp) - { - if (add_var_line(&vars->exported, *envp)) - return (1); - ++envp; - } - return (0); -} - -void clean_vars(t_vars *vars) -{ - ft_vec_free(&vars->exported, free_str); - ft_vec_free(&vars->other, free_str); - return ; -} - -int cmp_var_name(const char *a, const char *b) -{ - int namelen; - char *eq_ptr; - - eq_ptr = ft_strchr(a, '='); - namelen = eq_ptr - a; - eq_ptr = ft_strchr(b, '='); - if (namelen != eq_ptr - b) - return (1); - return (ft_strncmp(a, b, namelen)); -} - int set_var_value(t_vec *vars, const char *name, const char *value) { ssize_t i; @@ -144,16 +37,6 @@ int set_var_value(t_vec *vars, const char *name, const char *value) return (0); } -int set_env_var_value(t_execution_env *env, const char *name, const char *value) -{ - if (get_var_value(&env->vars->other, name)) - return (set_var_value(&env->vars->other, name, value)); - else if (get_var_value(&env->vars->exported, name)) - return (set_var_value(&env->vars->exported, name, value)); - else - return (set_var_value(&env->vars->other, name, value)); -} - ssize_t get_var_index(const t_vec *vars, const char *var_name) { size_t len; @@ -190,17 +73,6 @@ const char *get_var_value(const t_vec *vars, const char *var_name) return (NULL); } -const char *get_env_var_value(const t_execution_env *env, const char *var_name) -{ - const char *res; - - res = get_var_value(&env->vars->other, var_name); - if (res) - return (res); - res = get_var_value(&env->vars->exported, var_name); - return (res); -} - char *get_var_name(const char *line) { size_t i; @@ -210,3 +82,18 @@ char *get_var_name(const char *line) ++i; return (ft_strndup(line, i)); } + +static int add_var_line(t_vec *vec, const char *line) +{ + char *tmp; + + tmp = ft_strdup(line); + if (!tmp) + return (1); + if (ft_vec_insert(vec, &tmp, vec->size - 1) != success) + { + free(tmp); + return (1); + } + return (0); +} diff --git a/src/vars_helpers.c b/src/vars_helpers.c new file mode 100644 index 0000000..df1e3bf --- /dev/null +++ b/src/vars_helpers.c @@ -0,0 +1,107 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* vars_helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lnikolov + +// The keyval_pair is freed only in case of error +// When allocation fails in ft_strcal_alloc, it is freed automatically +// When allocation the insert fails, it needs to be freed here +// When everything goes smoothly, the memory is freed when the vec +// erases this entry +// static int add_var(t_vec *vec, const char *key, const char *value) +// { +// char *keyval_pair; + +// keyval_pair = ft_strdup(key); +// if (!keyval_pair) +// return (1); +// if (!ft_strcat_alloc(&keyval_pair, "=")) +// return (1); +// if (!ft_strcat_alloc(&keyval_pair, value)) +// return (1); +// if (ft_vec_insert(vec, &keyval_pair, vec->size - 1) != success) +// { +// free(keyval_pair); +// return (1); +// } +// return (0); +// } + +int init_vars(t_vars *vars, char **envp) +{ + char *tmp; + + ft_vec_init(&vars->exported, sizeof(char *)); + ft_vec_init(&vars->other, sizeof(char *)); + tmp = NULL; + ft_vec_append(&vars->exported, &tmp); + ft_vec_append(&vars->other, &tmp); + while (*envp) + { + if (add_var_line(&vars->exported, *envp)) + return (1); + ++envp; + } + return (0); +} + +void clean_vars(t_vars *vars) +{ + ft_vec_free(&vars->exported, free_str); + ft_vec_free(&vars->other, free_str); + return ; +} + +int cmp_var_name(const char *a, const char *b) +{ + int namelen; + char *eq_ptr; + + eq_ptr = ft_strchr(a, '='); + namelen = eq_ptr - a; + eq_ptr = ft_strchr(b, '='); + if (namelen != eq_ptr - b) + return (1); + return (ft_strncmp(a, b, namelen)); +} + +// This function returns 0 when str does not contain valid variable name +// in front of = else it returns the length of the valid name +int has_valid_name(const char *str) +{ + size_t i; + + if (!str) + return (0); + if (ft_isdigit(str[0])) + return (0); + i = 0; + while (str[i]) + { + if (str[i] == '=') + return (i); + if (!ft_isalnum(str[i]) && str[i] != '_') + return (0); + ++i; + } + return (i); +} + +int is_assignment_word(const char *str) +{ + size_t len; + + len = has_valid_name(str); + return (len > 0 && str[len] == '='); +} -- 2.30.2