-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvp_handle.c
More file actions
121 lines (110 loc) · 2.73 KB
/
envp_handle.c
File metadata and controls
121 lines (110 loc) · 2.73 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* envp_handle.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: elavrich <elavrich@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/07 17:53:36 by elavrich #+# #+# */
/* Updated: 2025/07/26 21:07:49 by elavrich ### ########.fr */
/* */
/* ************************************************************************** */
#include "Minishell.h"
void print_env(t_shell shell)
{
int i;
if (!shell.env_var)
{
printf("No environment variables.\n");
return ;
}
i = 0;
while (shell.env_var[i])
{
write(STDOUT_FILENO, shell.env_var[i], ft_strlen(shell.env_var[i]));
write(STDOUT_FILENO, "\n", 1);
i++;
}
}
char **copy_envp(char **envp, char *new_var)
{
size_t count;
char **copy;
int extra;
extra = (new_var != NULL);
count = 0;
while (envp[count])
count++;
copy = malloc(sizeof(char *) * (count + 1 + extra));
if (!copy)
return (NULL);
ft_memset(copy, 0, sizeof(char *) * (count + 1 + extra));
if (!copy_env_vars(copy, envp, count))
return (free(copy), NULL);
if (new_var)
{
copy[count] = ft_strdup(new_var);
if (!copy[count])
{
free_array(copy);
return (NULL);
}
}
copy[count + extra] = NULL;
return (copy);
}
void add_env(t_shell *shell, char *var)
{
char **new_envp;
int i;
new_envp = copy_envp(shell->env_var, var);
if (!new_envp)
return ;
i = 0;
while (shell->env_var[i])
free(shell->env_var[i++]);
free(shell->env_var);
shell->env_var = new_envp;
}
int search_env(t_shell *shell, char *var)
{
int i;
size_t len;
if (!var || !shell || !shell->env_var)
return (-1);
len = 0;
i = 0;
if (!var)
return (0);
if (var[0] == '?')
return (i);
while (var[len] && (ft_isalnum(var[len]) || var[len] == '_'))
len++;
if (len <= 0)
return (-1);
shell->var_len = len;
while (shell->env_var[i])
{
if (ft_strncmp(shell->env_var[i], var, len) == 0
&& shell->env_var[i][len] == '=')
return (i);
i++;
}
return (-1);
}
void update_env(t_shell *shell, char *var, char *name)
{
int pos;
char *new_val;
if (!shell || !shell->env_var || !var || !name)
return ;
pos = search_env(shell, name);
if (pos >= 0)
{
new_val = ft_strdup(var);
if (!new_val)
return ;
free(shell->env_var[pos]);
shell->env_var[pos] = new_val;
}
}