-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenviron_functions2.c
More file actions
126 lines (107 loc) · 2.56 KB
/
environ_functions2.c
File metadata and controls
126 lines (107 loc) · 2.56 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
#include "shell.h"
/* Environ functions 2 */
/**
* remove_environ - Remove environment variable(s)
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: 0 on success, 1 on failure
*/
int remove_environ(data_t *data_use)
{
int i = 0;
/* Check if there are enough arguments */
if (data_use->argc == 1)
{
_eputs("Too few arguements.\n");
return (1);
}
/* Loop through the arguments */
while (i + 1 <= data_use->argc)
{
/* Remove the corresponding environment variables */
remove_environ_variable(data_use, data_use->argv[i + 1]);
i++;
}
/* Return 0 on success */
return (0);
}
/**
* remove_environ_variable - Remove an environment variable
*
* @data_use: Pointer to the data structure containing information
* about the shell
* @var: The environment variable to be removed (char)
*
* Return: 1 if the variable is removed, 0 otherwise
*/
int remove_environ_variable(data_t *data_use, char *var)
{
node_t *node = data_use->env;
size_t i;
char *ptr;
/* Check if the list is empty or the variable is NULL */
if (node == NULL || var == NULL)
return (0);
/* Loop through the nodes to find */
for (i = 0; node; i++)
{
ptr = check_needle(node->str, var);
/* Check if the variable is found and remove it */
if (ptr && *ptr == '=')
{
if (*ptr == '=')
{
/* Remove the specified variable */
data_use->env_update = delete_node(&(data_use->env), i);
i = 0;
node = data_use->env;
continue;
}
}
node = node->next;
}
/* Return 1 if the variable is removed, 0 otherwise */
return (data_use->env_update);
}
/**
* list_environ - List environment variables and store
* them in the data structure
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: 0 on success
*/
int list_environ(data_t *data_use)
{
node_t *node = NULL;
size_t i = 0;
/* Loop through the global 'environ' variable */
while (environ[i])
{
/* Add each variable to the linked list */
add_node_end(&node, environ[i], 0);
i++;
}
/* Update the 'data_use->env' with the newly created linked list */
data_use->env = node;
/* Return 0 on success */
return (0);
}
/**
* print_environ - Print the list of environment variables
*
* @data_use: Pointer to the data structure containing information
* about the shell
*
* Return: 0 on success
*/
int print_environ(data_t *data_use)
{
/* Use the print_string_list function to print the environment variables */
print_string_list(data_use->env);
/* Return 0 on success */
return (0);
}