blob: 631c2b4dc236e18ed5fe1b946a45099ee1be6fb8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/28 09:21:24 by cacharle #+# #+# */
/* Updated: 2020/04/02 10:33:12 by charles ### ########.fr */
/* */
/* ************************************************************************** */
/*
** \file env.c
** \brief Environment manipulation
*/
#include "minishell.h"
/*
** \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;
size_t i;
i = 0;
while (envp[i] != NULL)
if (ft_strchr(envp[i++], '=') == NULL)
return (NULL);
if ((env = ft_vecnew(i + 1)) == NULL)
return (NULL);
env->size = i + 1;
i = 0;
while (envp[i] != NULL)
{
if ((env->data[i] = ft_strdup(envp[i])) == NULL)
{
ft_vecdestroy(env, free);
return (NULL);
}
i++;
}
env->data[i] = NULL;
return (env);
}
/**
** \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_search(t_env env, char *key)
{
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);
}
|