-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_struct_utils.c
96 lines (86 loc) · 2.21 KB
/
make_struct_utils.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* make_struct_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marlean <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 16:18:54 by rdanyell #+# #+# */
/* Updated: 2022/06/17 12:31:37 by marlean ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_copy_arr(char **arr_in, char **arr_out)
{
int i;
i = 0;
while (arr_out[i])
i++;
arr_in = malloc(sizeof(char *) * i + 1);
arr_in[i] = NULL;
i = -1;
while (arr_out[++i])
arr_in[i] = ft_substr(arr_out[i], 0, ft_strlen(arr_out[i]));
}
int ft_n_words(char **arr, int i)
{
int n;
n = 0;
while (arr[i] && !ft_isdelim(arr[i++]))
n++;
return (n);
}
void ft_empty_arr(char ***tmp, char *str)
{
*tmp = (char **)malloc(sizeof(char *) * 2);
(*tmp)[0] = ft_substr(str, 0, ft_strlen(str));
ft_free(str);
(*tmp)[1] = NULL;
}
void add_first_str_in_arr(char ***arr, char *str)
{
char **tmp;
int i;
i = 0;
if (*arr)
{
while ((*arr)[i])
i++;
tmp = malloc(sizeof(char *) * (i + 2));
tmp[i + 1] = NULL;
tmp[0] = ft_substr(str, 0, ft_strlen(str));
ft_free(str);
i = 0;
while ((*arr)[i])
{
tmp[i + 1] = ft_substr((*arr)[i], 0, ft_strlen((*arr)[i]));
ft_free((*arr)[i]);
i++;
}
}
else
ft_empty_arr(&tmp, str);
free(*arr);
*arr = tmp;
}
void ft_one_name(t_com **com)
{
t_com *tmp;
int first_name;
tmp = *com;
first_name = 1;
while ((*com))
{
if (first_name && (*com)->name != NULL)
first_name = 0;
else
{
add_first_str_in_arr(&(*com)->arg, (*com)->name);
(*com)->name = NULL;
}
if ((*com)->delim == 1)
first_name = 1;
(*com) = (*com)->next;
}
*com = tmp;
}