-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment_variable_operations.c
More file actions
145 lines (132 loc) · 3.48 KB
/
Copy pathenvironment_variable_operations.c
File metadata and controls
145 lines (132 loc) · 3.48 KB
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
139
140
141
142
143
144
145
#include "shell.h"
/**
* _getenv - Retrieves the value of an environment variable.
* @name: The name of the environment variable.
* @env: The environment variables array
* Return: A pointer to the value of the environment variable
* or NULL if not found.
**/
char *_getenv(const char *name, char **env)
{
size_t nameLength = _strlen(name);
int i;
for (i = 0; env[i] != NULL; i++)
{
if (_strncmp(name, env[i], nameLength) == 0 &&
env[i][nameLength] == '=')
{
return (env[i] + nameLength + 1);
}
}
return (NULL);
}
/**
* print_env - Prints the environment variables.
* @env: Null-terminated array of strings representing the environment.
*
* Description: This function iterates over each string in the 'env' array,
* which represents an environment variable, and writes it to stdout. Each
* environment variable is printed on a new line.
*/
void print_env(char **env)
{
int i;
for (i = 0; env[i] != NULL; i++)
{
/* Write the environment variable to stdout */
write(STDOUT_FILENO, env[i], _strlen(env[i]));
write(STDOUT_FILENO, "\n", 1);
}
}
/**
* set_env_var - Sets an environment variable
* @name: The name of the environment variable
* @value: The value to be set for the environment variable
* @env: A pointer to the environment variables array
*
* Return: Nothing
*/
void set_env_var(char *name, char *value, char ***env)
{
int i;
char *new_env_var;
/* Check if environment variable already exists */
for (i = 0; (*env)[i] != NULL; i++)
{
if (_strncmp(name, (*env)[i], _strlen(name)) == 0
&& (*env)[i][_strlen(name)] == '=')
{
/* If it exists, modify the value */
new_env_var = malloc(_strlen(name) + _strlen(value) + 2);
if (new_env_var == NULL)
{
write(STDERR_FILENO, "Failed to allocate memory\n", 26);
return;
}
_strcpy(new_env_var, name);
_strcat(new_env_var, "=");
_strcat(new_env_var, value);
free((*env)[i]);
(*env)[i] = new_env_var;
return;
}
}
/* If it doesn't exist, create a new environment variable */
new_env_var = malloc(_strlen(name) + _strlen(value) + 2);
if (new_env_var == NULL)
{
write(STDERR_FILENO, "Failed to allocate memory\n", 26);
return;
}
_strcpy(new_env_var, name);
_strcat(new_env_var, "=");
_strcat(new_env_var, value);
/* Add the new environment variable to the env array */
(*env)[i] = new_env_var;
(*env)[i + 1] = NULL;
}
/**
* unset_env_var - Unsets an environment variable
* @name: The name of the environment variable to be unset
* @env: A pointer to the environment variables array
*
* Return: Nothing
*/
void unset_env_var(char *name, char ***env)
{
int i;
int found = 0; /* Add a flag to check if the variable was found */
/* Find the environment variable */
for (i = 0; (*env)[i] != NULL; i++)
{
if (_strncmp(name, (*env)[i], _strlen(name)) == 0
&& (*env)[i][_strlen(name)] == '=')
{
free((*env)[i]);
/* Shift all elements down one spot */
while ((*env)[i] != NULL)
{
(*env)[i] = (*env)[i + 1];
i++;
}
(*env)[i] = NULL;
found = 1; /* Set the flag to true */
break;
}
}
/* If the variable was not found, print an error message */
if (!found)
{
char *error_message = malloc(_strlen(name) + 42);
if (error_message == NULL)
{
write(STDERR_FILENO, "Failed to allocate memory\n", 26);
return;
}
_strcpy(error_message, "Error: Environment variable '");
_strcat(error_message, name);
_strcat(error_message, "' not found\n");
write(STDERR_FILENO, error_message, _strlen(error_message));
free(error_message);
}
}