forked from glyif/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironB.c
125 lines (103 loc) · 2.06 KB
/
environB.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
#include "header.h"
/**
* add_node_env - adds new node to end of linked list
* @head: beginning of linked list
* @var: variable to be added
* @val: value to be added
*
* Return: pointer to new node
*/
env_t *add_node_env(env_t **head, char *var, char *val)
{
env_t *new_node, *temp;
new_node = safe_malloc(sizeof(env_t));
new_node->var = _strdup(var);
new_node->val = _strdup(val);
new_node->next = NULL;
if (!*head)
{
*head = new_node;
}
else
{
temp = *head;
while (temp->next)
temp = temp->next;
temp->next = new_node;
}
return (new_node);
}
/**
* modify_node_env - checks to see if node exists, and modifies it if so.
* @head: beginning of linked list
* @new_var: variable to modify
* @new_val: new string to be modified to found node
*
* Return: pointer to new node or NULL if fail
*/
int modify_node_env(env_t **head, char *new_var, char *new_val)
{
env_t *temp;
temp = *head;
while (temp)
{
if (_strcmp(temp->var, new_var) == 0)
{
free(temp->val);
temp->val = _strdup(new_val);
return (EXT_SUCCESS);
}
temp = temp->next;
}
return (EXT_FAILURE);
}
/**
* remove_node_env - removes node from linked list
* @head: beginning of linked list
* @var: var of node to be removed from linked list
*
* Return: pointer to new node or NULL
*/
int remove_node_env(env_t **head, char *var)
{
env_t *copy_head = *head, *temp = *head;
if (head == NULL)
return (EXT_FAILURE);
copy_head = NULL;
while (temp)
{
if (_strcmp(temp->var, var) == 0)
{
if (copy_head)
copy_head->next = temp->next;
else
*head = temp->next;
free(temp->var);
free(temp->val);
free(temp);
return (EXT_SUCCESS);
}
copy_head = temp;
temp = temp->next;
}
return (EXT_FAILURE);
}
/**
* fetch_node - fetches a node of a given var
* @head: head of list
* @var: value to match of the node to fetch
*
* Return: fetched node or head
*/
env_t *fetch_node(env_t *head, char *var)
{
env_t *tmp;
tmp = head;
while (tmp != NULL)
{
if (_strcmp(tmp->var, var) == 0)
return (tmp);
tmp = tmp->next;
}
return (NULL);
}