-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_utils3.c
More file actions
executable file
·117 lines (107 loc) · 2.76 KB
/
builtin_utils3.c
File metadata and controls
executable file
·117 lines (107 loc) · 2.76 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_utils3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyeyeom <hyeyeom@42student.gyeongsan.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 20:31:38 by hyeyeom #+# #+# */
/* Updated: 2025/04/14 00:38:35 by hyeyeom ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void f_init_env(char **envp, t_envp **n_envps)
{
int i;
t_envp *tmp;
tmp = new_node(envp, 0);
*n_envps = tmp;
i = 0;
while (envp[++i])
{
tmp->next = new_node(envp, i);
tmp = tmp->next;
}
tmp->next = NULL;
}
void delete_t_envp(t_envp **head, char *key)
{
t_envp *current;
t_envp *prev;
if (!head || !*head)
return ;
current = *head;
prev = NULL;
while (current)
{
if (ft_strncmp(current->key, key, (ft_strlen(current->key) + 1)) == 0)
{
if (prev == NULL)
*head = current->next;
else
prev->next = current->next;
free(current->key);
free(current->value);
free(current);
return ;
}
prev = current;
current = current->next;
}
}
int update_t_envp(t_envp *node, char *key, char *value)
{
while (node != NULL)
{
if (ft_strncmp(node->key, key, ft_strlen(node->key) + 1) == 0)
{
free(node->value);
node->value = ft_strdup(value);
return (1);
}
node = node->next;
}
return (0);
}
t_envp *new_node(char **envp, int idx)
{
t_envp *first;
char **split_envp;
int count;
count = f_count_char(envp);
first = (t_envp *)malloc(sizeof(t_envp));
if (!first)
return (NULL);
split_envp = ft_split(envp[idx], '=');
first->key = ft_strdup(split_envp[0]);
if (split_envp[1] == NULL || split_envp[1][0] == '\0')
first->value = ft_strdup("\0");
else
first->value = ft_strdup(split_envp[1]);
first->count = count;
first->next = NULL;
free_double_char(split_envp);
return (first);
}
int update_envp_node(t_envp **head, char *key, char *value)
{
t_envp *current;
current = *head;
while (current)
{
if ((ft_strlen(key) == ft_strlen(current->key)) && \
ft_strncmp(current->key, key, ft_strlen(current->key)) == 0)
{
if (value != NULL)
{
free(current->value);
current->value = ft_strdup(value);
if (!current->value)
return (-1);
}
return (0);
}
current = current->next;
}
return (1);
}