-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathenv.c
138 lines (128 loc) · 2.67 KB
/
env.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdlib.h>
#include "libft/libft.h"
#include "env.h"
#include "minishell.h"
char *get_env_val(const char *env_key)
{
t_var *var;
if (!ft_strcmp(env_key, "?"))
return (ft_itoa(get_status()));
var = get_env(env_key);
if (!var || !var->value)
return (NULL);
return (ft_strdup(var->value));
}
/*
* Get environment variable with key.
*
* env_key: key name of environment variable.
*
* return: found t_var
*/
t_var *get_env(const char *env_key)
{
t_var *current;
current = g_shell.vars;
while (current)
{
if (!ft_strcmp(env_key, current->key))
return (current);
current = current->next;
}
return (NULL);
}
/*
* Split a string at the first appearance of a character.
*
* str: zero-terminated string.
* c: split text with this character.
*
* return: NULL-terminated char's pointer array.
*/
char **split_first_c(const char *str, char c)
{
size_t idx;
char **result;
result = ft_calloc(3, sizeof(char *));
if (!result)
return (NULL);
idx = 0;
while (true)
{
if (str[idx] == c || str[idx] == '\0')
{
result[0] = ft_substr(str, 0, idx);
if (!result[0])
return (free_ptrarr_and_rtn_null((void **)result));
break ;
}
idx++;
}
if (str[idx])
{
result[1] = ft_strdup(str + idx + 1);
if (!result[1])
return (free_ptrarr_and_rtn_null((void **)result));
}
return (result);
}
/* Split string by colon(':').
*
* str: Colon delimited string.
* def_str: Default string.
*
* Return: An array of strings created as a result of string delimitation.
*
* ex:
* - args:
* - str: "::/"
* - default_str: "default"
* - return: ["default", "default", "/"]
*/
char **get_colon_units(const char *str, const char *default_str)
{
void **result;
char *next;
void **tmparr;
result = NULL;
next = ft_strchr(str, ':');
while (next)
{
tmparr = result;
if (next - str == 0)
result = ptrarr_add_ptr(result, ft_strdup(default_str));
else
result = ptrarr_add_ptr(result, ft_substr(str, 0, next - str));
free(tmparr);
str = next + 1;
next = ft_strchr(str, ':');
}
tmparr = result;
if (*str == '\0')
result = ptrarr_add_ptr(result, ft_strdup(default_str));
else
result = ptrarr_add_ptr(result, ft_strdup(str));
free(tmparr);
return ((char **)result);
}
/*
* Get value from key-value string.
* key-value string form like "key=value", "key:value", or something like that.
*
* kvstr: key-value string.
* delimiter: delimiter of key-value string.
*
* return: value of key-value string.
*/
char *get_val_from_kvstr(const char *kvstr, char delimiter)
{
char **kvarr;
char *valstr;
kvarr = split_first_c(kvstr, delimiter);
if (!kvarr)
return (NULL);
valstr = kvarr[1];
free(kvarr[0]);
free(kvarr);
return (valstr);
}