-
Notifications
You must be signed in to change notification settings - Fork 0
/
env_to_dict.c
49 lines (38 loc) · 854 Bytes
/
env_to_dict.c
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
#include "env.h"
/**
* env_to_dict - creates a list from environment
* @env: environment passed
* Return: head
*/
env_t *env_to_dict(char **env)
{
env_t *head = NULL;
if (!_env_to_dict(&head, env))
free_dict(&head);
return (head);
}
/**
* _env_to_dict - turn the environment into a linked list (helper)
* @tailptr: pointer to the tail of the list
* @env: environment
*
* Return: pointer to the tail of the list
*/
env_t *_env_to_dict(env_t **tailptr, char **env)
{
env_t *tail;
char *env_str;
ssize_t key_len;
if (!*env)
return (*tailptr);
env_str = _strdup(*env);
if (!env_str)
return (NULL);
key_len = _strchr(*env, '=');
if (key_len == -1)
return (NULL);
env_str[key_len] = '\0';
tail = add_dict_node_end(tailptr, env_str, env_str + key_len + 1);
free(env_str);
return (_env_to_dict(&tail, env + 1));
}