aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c41
1 files changed, 27 insertions, 14 deletions
diff --git a/src/env.c b/src/env.c
index 631c2b4..d402a1b 100644
--- a/src/env.c
+++ b/src/env.c
@@ -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);
+}