-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_helpers.c
75 lines (59 loc) · 1.17 KB
/
env_helpers.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
#include "main.h"
/**
* is_valid_env - checks if variable is valid
*
* @name: name of variable
*
* Return: 1 if valid else 0
*/
bool is_valid_env(const char *name)
{
size_t i;
if (name == NULL || isdigit(name[0]) || strlen(name) == 0 ||
strchr(name, '=') != NULL)
return (false);
for (i = 0; name[i] != '\0'; i++)
if (!isalnum(name[i]) && name[i] != '_')
return (false);
return (true);
}
/**
* get_env_index - gets the index of variable
*
* @name: name of variable
*
* Return: index of variable or -1
*/
int get_env_index(const char *name)
{
char **env = get_state()->env;
int name_len = strlen(name);
int i;
if (env == NULL)
return (-1);
for (i = 0; env[i] != NULL; i++)
{
int env_len = (strchr(env[i], '=')) - env[i];
if (env_len != name_len)
continue;
if (strncmp(env[i], name, name_len) == 0)
return (i);
}
return (-1);
}
/**
* assemble_env - assembles env variable
*
* @name: name of variable
* @value: value of variable
*
* Return: assembled variable
*/
char *assemble_env(const char *name, const char *value)
{
char *env = NULL;
string_cat(&env, name);
string_cat(&env, "=");
string_cat(&env, value);
return (env);
}