-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.c
105 lines (89 loc) · 1.7 KB
/
env.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
#include "shell.h"
/**
* create_node - creats a new node to the list
* @data: the variable to set
* @number: keeps track of the varible
* Return: a pointer to the node
*/
Node_env *create_node(char *data, int number)
{
Node_env *new_node;
new_node = (Node_env *)malloc(sizeof(Node_env));
if (new_node == NULL)
{
perror("Error: malloc failed");
exit(EXIT_FAILURE);
}
new_node->data = data;
new_node->number = number;
new_node->next = NULL;
return (new_node);
}
/**
* add_to_list - adds a node to the list
* @head: head of the linked list
* @new_node: the node to be added
*/
void add_to_list(Node_env **head, Node_env *new_node)
{
Node_env *current;
if (*head != NULL)
{
current = *head;
while (current->next != NULL)
current = current->next;
current->next = new_node;
}
else
*head = new_node;
}
/**
* print_env - prints the node to the stderr
* @head: head of the linked list
* Return: nothing
*/
void print_env(Node_env *head)
{
Node_env *current;
current = head;
while (current != NULL)
{
write(STDOUT_FILENO, current->data, _strlen(current->data));
write(STDOUT_FILENO, "\n", 1);
current = current->next;
}
}
/**
* handle_env - handles the env built-in
* Return: 0 on success
*/
int handle_env(void)
{
char **env = environ;
Node_env *head = NULL;
while (*env != NULL)
{
add_to_list(&head, create_node(*env, 0));
env++;
}
print_env(head);
free_env(head);
return (0);
}
/**
* free_env - frees the list
* @head: head to the linked list
*/
void free_env(Node_env *head)
{
Node_env *current = head;
Node_env *temp;
while (current != NULL)
{
temp = current;
current = current->next;
if (temp->number == 1)
free(temp->data);
free(temp);
}
}