-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunsetenv_command.c
109 lines (94 loc) · 2.48 KB
/
unsetenv_command.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unsetenv_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kblack <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/15 20:34:02 by kblack #+# #+# */
/* Updated: 2019/03/27 00:34:14 by kblack ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
** Removes the first node of the linked list
*/
void delete_head_node(t_env *head)
{
t_env *tmp;
tmp = NULL;
if (head->next == NULL)
return ;
head->key = head->next->key;
head->value = head->next->value;
tmp = head->next;
head->next = head->next->next;
return ;
}
/*
** Keeps track of the number of enviroment variables that have been removed
*/
int number_of_args(char **args)
{
int i;
int count;
i = 1;
count = 0;
while (args[i++])
count++;
return (count);
}
/*
** Removes enviroment variable from linked list
*/
void remove_link(t_env *prev)
{
t_env *tmp;
tmp = prev->next;
prev->next = prev->next->next;
free_file(tmp);
}
/*
** Locates and removes the chosen environment variable from the linked list
*/
void remove_env_var(char **args, t_shell *sh)
{
int i;
int count;
t_env *head;
t_env *prev;
i = 0;
head = sh->env_info;
while (args[++i])
{
if (ft_strcmp(head->key, args[i]) == 0)
delete_head_node(head);
prev = head;
while (prev->next != NULL && ft_strcmp(prev->next->key, args[i]) != 0)
prev = prev->next;
if (prev->next == NULL || ft_strcmp(prev->next->key, "_") == 0)
{
if ((count = number_of_args(args)) == i)
return ;
prev = head;
}
else
remove_link(prev);
}
}
/*
** Unset removes environment variables from the list of variables that it tracks
*/
int handle_unsetenv(char **args, t_shell *sh)
{
if (!args[1])
ft_printf("unset: not enough arguments\n");
else
{
if (ft_strchr(args[1], '=') == NULL)
remove_env_var(args, sh);
else
ft_printf("unset: %s: invalid parameter name\n", args[1]);
}
return (1);
}