-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjoin_args_and_envp.c
More file actions
94 lines (85 loc) · 2.23 KB
/
join_args_and_envp.c
File metadata and controls
94 lines (85 loc) · 2.23 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* join_args_and_envp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aouchaad <aouchaad@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/22 11:40:11 by aouchaad #+# #+# */
/* Updated: 2023/06/23 15:09:54 by aouchaad ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void fill_args(char **args, t_arguments *s_args, char *command)
{
int i;
t_arguments *tmp;
tmp = s_args;
args[0] = command;
i = 1;
while (tmp)
{
if (tmp->arg && tmp->arg[0] != '\0')
args[i++] = tmp->arg;
tmp = tmp->next;
}
args[i] = NULL;
}
char **join_args(char *command, t_arguments *s_args)
{
char **args;
t_arguments *tmp;
int arg_count;
args = NULL;
tmp = s_args;
arg_count = 0;
while (tmp)
{
arg_count++;
tmp = tmp->next;
}
args = malloc (sizeof(char *) * (arg_count + 2));
if (arg_count == 0)
{
args[0] = ft_strdup(command);
args[1] = NULL;
return (args);
}
fill_args(args, s_args, command);
return (args);
}
void node_counter(t_environment_path *s_envp, int *node_count)
{
t_environment_path *tmp;
tmp = s_envp;
while (tmp)
{
(*node_count)++;
tmp = tmp->next;
}
}
char **join_envp(t_environment_path *s_envp)
{
char **envp;
t_environment_path *tmp;
int node_count;
int i;
envp = NULL;
tmp = s_envp;
node_count = 0;
node_counter(s_envp, &node_count);
if (node_count == 0)
return (NULL);
envp = malloc (sizeof(char *) * (node_count + 1));
i = 0;
while (tmp)
{
envp[i] = ft_strdup(tmp->variable);
envp[i] = ft_sssstrjoin(envp[i], ft_strdup("="));
envp[i] = ft_sssstrjoin(envp[i], ft_strdup(tmp->valeur));
tmp = tmp->next;
i++;
}
envp[i] = NULL;
return (envp);
}