aboutsummaryrefslogtreecommitdiff
path: root/src/env.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/env.c')
-rw-r--r--src/env.c64
1 files changed, 32 insertions, 32 deletions
diff --git a/src/env.c b/src/env.c
index fc26876..66b4994 100644
--- a/src/env.c
+++ b/src/env.c
@@ -6,65 +6,65 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */
-/* Updated: 2020/04/01 17:03:50 by charles ### ########.fr */
+/* Updated: 2020/04/01 23:09:33 by charles ### ########.fr */
/* */
/* ************************************************************************** */
/*
** \file env.c
-** \brief Environment hash table manipulation
+** \brief Environment manipulation
*/
#include "minishell.h"
/*
-** \brief Number of buckets of an environment hash table
-*/
-
-#define MS_ENV_HT_SIZE 2048
-
-/*
** \brief Convert array of string to environment hash table
** \param envp array of string (each in the format `name=value`)
** \return Environment hash table or NULL on error
*/
-t_env env_from_array(char **envp)
+t_env env_from_array(char **envp)
{
t_env env;
- int i;
- char *key;
- char *value;
+ size_t i;
- if (envp == NULL)
- return (NULL);
- if ((env = ft_htnew(MS_ENV_HT_SIZE)) == NULL)
+ i = 0;
+ while (envp[i] != NULL)
+ if (ft_strchr(envp[i++], '=') == NULL)
+ return (NULL);
+ if ((env = ft_vecnew(i)) == NULL)
return (NULL);
- i = -1;
- while (envp[++i] != NULL)
+ env->size = i;
+ i = 0;
+ while (envp[i] != NULL)
{
- // free stuff on error
- if ((value = ft_strchr(envp[i], '=')) == NULL)
- return (NULL);
- if ((key = ft_strndup(envp[i], ft_strcspn(envp[i], "="))) == NULL)
+ if ((env->data[i] = ft_strdup(envp[i])) == NULL)
+ {
+ ft_vecdestroy(env, free);
return (NULL);
- if ((value = ft_strdup(value + 1)) == NULL)
- return (NULL);
- if (ft_htset(env, key, value, ht_del_str_entry) == NULL)
- return (NULL);
- free(key);
+ }
+ i++;
}
return (env);
}
-/*
-** \brief Convert environment to array of string
-** \param env Environment hash table
-** \return Array of string on NULL on error
+/**
+** \brief Search a key in environment
+** \param env Search environment
+** \param key Searched key
+** \return Value after '=' in environment variable array or NULL if not found
*/
-char **env_to_array(t_env env)
+char *env_search(t_env env, char *key)
{
- (void)env;
+ size_t i;
+
+ i = 0;
+ while (i < env->size)
+ {
+ if (ft_strncmp((char*)env->data[i], key, ft_strlen(key)) == 0)
+ return (ft_strchr((char*)env->data[i], '=') + 1);
+ i++;
+ }
return (NULL);
}