/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* b_export_next.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 "d_define.h" #include "b_export_next.h" #include "f_fail.h" #include "s_lvars.h" #include "s_struct.h" #include "u_utils.h" #include "u_vars.h" static char **b_get_var(const char arg[], t_msh *msh) { size_t len; char **var; if ((var = (char**)malloc(3 * sizeof(char*))) == NULL) f_alloc_and_destroy_msh(msh); len = 0; while (arg[len] != '=' && arg[len] != '\0') len++; len += 1; if ((var[B_VAR_NAME] = (char*)malloc((len + 1) * sizeof(char))) == NULL) f_alloc_and_destroy_msh(msh); var[B_VAR_NAME][0] = '$'; ft_strlcpy(var[B_VAR_NAME] + 1, arg, len); if ((var[B_VAR_VAL] = ft_strdup(arg + len)) == NULL) f_alloc_and_destroy_msh(msh); var[B_VAR_NULL] = NULL; return (var); } static long b_is_it_in_env(const char varname[], t_msh *msh) { char **env_dup; size_t i; env_dup = u_get_env_var_names(msh); i = 0; while (env_dup[i] != NULL) { if (ft_strncmp(varname, env_dup[i], ft_strlen(env_dup[i]) + 1) == 0) { ft_delwords(env_dup); return (i); } i++; } ft_delwords(env_dup); return (-1); } static void b_add_to_env(const char arg[], t_msh *msh) { size_t i; char **nenvp; i = 0; while (msh->envp[i] != NULL) i++; if (!(nenvp = (char**)malloc((i + 2) * sizeof(char*)))) f_alloc_and_destroy_msh(msh); i = 0; while (msh->envp[i] != NULL) { if (!(nenvp[i] = ft_strdup(msh->envp[i]))) f_alloc_and_destroy_msh(msh); i++; } if (!(nenvp[i] = ft_strdup(arg))) f_alloc_and_destroy_msh(msh); nenvp[i + 1] = 0; ft_delwords(msh->envp); msh->envp = nenvp; } void b_export_with_equals(const char arg[], t_msh *msh) { char varval[4096]; char **var; long env_i; varval[0] = C_NUL; var = b_get_var(arg, msh); if ((env_i = b_is_it_in_env(var[B_VAR_NAME] + 1, msh)) != -1) { ft_memdel((void*)&msh->envp[env_i]); if ((msh->envp[env_i] = ft_strdup(arg)) == NULL) f_alloc_and_destroy_msh(msh); return ; } u_get_custom_var(varval, var[B_VAR_NAME], 4096, msh); if (varval[0] != C_NUL) { b_add_to_env(arg, msh); lvars_delone(&msh->vars, var[B_VAR_NAME] + 1); } else { b_add_to_env(arg, msh); } ft_delwords(var); }