-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess2.c
More file actions
136 lines (124 loc) · 2.49 KB
/
Copy pathprocess2.c
File metadata and controls
136 lines (124 loc) · 2.49 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
#include "main.h"
static list_t *head_list;
/**
* add_node - function that add new node at the beginning of a list
* @str: string to be added to the list
* @add: address of the string variable.
* Return: address of the new element
*/
void *add_node(const char *str, char *add)
{
list_t *new_node;
new_node = malloc(sizeof(list_t));
if (new_node == NULL)
{
return (NULL);
}
new_node->str = _strdup(str);
new_node->add = add;
new_node->next = head_list;
head_list = new_node;
return (new_node);
}
/**
* clear_env - atexit function for clearing mem-leaks
*/
void clear_env(void)
{
if (head_list != NULL)
free_list(head_list);
}
/**
* free_node - function that deletes a node.
* @name: node index to be deleted.
* Return: 1 if it succeeded, -1 if it failed.
*/
int free_node(const char *name)
{
list_t *current, *prev;
if (head_list != NULL)
{
prev = head_list;
while (prev != NULL)
{
current = prev->next;
if (_strcmp(name, current->str) == 0)
{
prev->next = current->next;
free(current->add);
free(current->str);
free(current);
current = NULL;
return (0);
}
prev = prev->next;
}
}
return (-1);
}
/**
* free_list - A function that frees all elements in a linked list
* @head: pointer to head element of list
* Return: Nothing
*/
void free_list(list_t *head)
{
if (head != NULL)
{
list_t *temp;
temp = head;
while (head)
{
temp = head;
head = head->next;
free(temp->add);
free(temp->str);
free(temp);
}
free(head);
}
}
/**
* _setenv - Function that sets an enviroment variable
* @name: name of enviroment variable
* @value: value of the enviroment variable
* Return: 0 on success, -1 on failure
*/
int _setenv(const char *name, const char *value)
{
char *env, *token, *tmp;
int count = 0, test = -1, size = _strlen(name) + _strlen(value) + 2;
tmp = malloc(sizeof(char) * size);
if (!tmp)
return (-1);
_strcpy(tmp, name);
_strcat(tmp, "=");
_strcat(tmp, value);
while (environ[count])
{
env = _strdup(environ[count]);
token = _strtok(env, "=");
if (_strcmp(token, name) == 0)
{
environ[count] = _strdup(tmp);
add_node(name, environ[count]);
free(env);
break;
}
if (_strcmp(token, name) != 0 && environ[count + 1] == NULL)
{
environ[count + 1] = _strdup(tmp);
add_node(name, environ[count + 1]);
environ[count + 2] = NULL;
free(env);
break;
}
count++;
test = 1;
free(env);
}
free(tmp);
if (test == -1)
write(STDERR_FILENO, "Set env failed\n", 16);
return (count);
}