diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-04-03 15:13:08 +0200 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-04-03 15:13:08 +0200 |
| commit | c0e2ee28eedc1a9a886f9729a994d77738e2eb58 (patch) | |
| tree | f099ec9b9effe0db078a521c9405c5765b68fd8f /src/env.c | |
| parent | e5393671a265e1c301c6c303f21f938c4cf9ca75 (diff) | |
| download | minishell-c0e2ee28eedc1a9a886f9729a994d77738e2eb58.tar.gz minishell-c0e2ee28eedc1a9a886f9729a994d77738e2eb58.tar.bz2 minishell-c0e2ee28eedc1a9a886f9729a994d77738e2eb58.zip | |
Refactoring env, connecting pipes, preprocess draft
Diffstat (limited to 'src/env.c')
| -rw-r--r-- | src/env.c | 41 |
1 files changed, 27 insertions, 14 deletions
@@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */ -/* Updated: 2020/04/02 10:33:12 by charles ### ########.fr */ +/* Updated: 2020/04/03 14:25:31 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ #include "minishell.h" +#define ENV_VEC_DEFAULT_SIZE 64 + /* ** \brief Convert array of string to environment hash table ** \param envp array of string (each in the format `name=value`) @@ -25,28 +27,22 @@ t_env env_from_array(char **envp) { + char *tmp; t_env env; - size_t i; - i = 0; - while (envp[i] != NULL) - if (ft_strchr(envp[i++], '=') == NULL) - return (NULL); - if ((env = ft_vecnew(i + 1)) == NULL) + if ((env = ft_vecnew(ENV_VEC_DEFAULT_SIZE)) == NULL) return (NULL); - env->size = i + 1; - i = 0; - while (envp[i] != NULL) + while (*envp != NULL) { - if ((env->data[i] = ft_strdup(envp[i])) == NULL) + if ((tmp = ft_strdup(*envp)) == NULL || + ft_vecpush(env, tmp) == NULL) { ft_vecdestroy(env, free); return (NULL); } - i++; + envp++; } - env->data[i] = NULL; - return (env); + return (ft_vecpush(env, NULL)); } /** @@ -69,3 +65,20 @@ char *env_search(t_env env, char *key) } return (NULL); } + +char *env_match_first(t_env env, const char *haystack) +{ + int len; + size_t i; + + len = -1; + while (ft_isalnum(haystack[len]) || haystack[len] == '_') + len++; + while (i < env->size) + { + if (ft_strncmp((char*)env->data[i], haystack, len) == 0) + return (ft_strchr((char*)env->data[i], '=') + 1); + i++; + } + return (NULL); +} |
