/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_u_utils.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 "ft_f_fail.h" #include "ft_s_destroy.h" #include "ft_s_lcom.h" #include "ft_s_struct.h" uint64_t ft_get_argc(const char *args[]) { uint64_t argc; argc = 0; while (args[argc]) { argc++; } return (argc); } char *ft_get_home_dir(char **envp) { char **ptr; char *path; ptr = envp; while (*ptr) { if (ft_strncmp("HOME", *ptr, 4) == 0) { path = ft_substr(*ptr, 5, ft_strlen(*ptr + 5)); return (path); } ptr++; } return (NULL); } /* ** char * ** ft_subst_var_value(const char varname[], const t_msh *msh); ** ** DESCRIPTION ** The ft_subst_var_value() function returns ** a heap-allocated, null-terminated string ** that may later be free'd containing the ** value of the variable varname[] including ** the '$' prefix. NULL is returned if varname[] ** wasn't found. */ char *ft_subst_var_value(const char varname[], t_msh *msh) { char **p_env; char *varval; p_env = msh->envp; varval = NULL; while (*p_env) { if (!ft_strncmp(*p_env, varname + 1, ft_strlen(varname + 1) + 1)) { ft_printf("(%s)\n", *p_env); while (**p_env != '\0' && **p_env != '=') **p_env += 1; if (**p_env == '=') **p_env += 1; if (!(varval = ft_strdup(*p_env))) { ft_lcom_clear(&msh->curr); ft_s_destroy(msh); ft_fail_alloc(msh); } return (varval); } p_env += 1; } return (NULL); }