-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetenv.c
124 lines (114 loc) · 2.51 KB
/
getenv.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
#include "shell.h"
/**
* get_env_var_value - Gets the value of an environment variable.
* @var_name: The name of the environment variable.
* Return: A pointer to the value of the environment variable.
*/
char *get_env_var_value(const char *var_name)
{
char *var_value = NULL;
if (var_name[0] == '?')
{
var_value = _itoa(0);
}
else if (var_name[0] == '$')
{
var_value = _itoa(getpid());
}
else
{
var_value = _getenv(var_name);
}
return (var_value);
}
/**
* replace_env_var - Replaces an environment variable in a string.
* @start: Pointer to the start of the string.
* @var_value: Value of the environment variable to be replaced.
* @end: Pointer to the end of the replacement.
*/
void replace_env_var(char **start, const char *var_value, char *end)
{
size_t var_value_length = _strlen(var_value);
size_t remaining_length = _strlen(end);
_memmove(*start + var_value_length, end, remaining_length + 1);
_memcpy(*start, var_value, var_value_length);
*start += var_value_length;
}
/**
* replace_env_vars - Replaces environment variables in a string.
* @input: The input string containing environment variables.
*/
void replace_env_vars(char *input)
{
char *start = input;
char *end = input;
char *dollar_sign, *var_name, *var_value;
while ((dollar_sign = _strchr(start, '$')) != NULL)
{
end = dollar_sign;
while (*end != '\0' && (*end == '$'
|| my_isalnum(*end)
|| *end == '_'))
{
end++;
}
if (end > dollar_sign + 1)
{
var_name = dollar_sign + 1;
var_value = get_env_var_value(var_name);
if (var_value != NULL)
{
replace_env_var(&start, var_value, end);
}
else
{
start = end;
}
}
else
{
start = end;
}
}
}
/**
* handle_special_env - Handles special environment variables.
* @name: The name of the environment variable.
* @progname: The name of the program.
*/
void handle_special_env(const char *name, const char *progname)
{
char *value = _getenv(name);
if (value != NULL)
{
_puts(value);
}
else
{
print_err(progname, "not found", name);
}
}
/**
* _getenv - Gets the value of an environment variable.
* @name: The name of the environment variable.
* Return: A pointer to the value of the environment variable.
*/
char *_getenv(const char *name)
{
size_t name_len = _strlen(name);
char **env;
if (name == NULL || *name == '\0')
{
return (NULL);
}
for (env = environ; *env != NULL; env++)
{
if (_strncmp(name, *env, name_len) == 0
&& (*env)[name_len] == '=')
{
return (*env + name_len + 1);
}
}
return (NULL);
}