-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils_3.c
71 lines (64 loc) · 1.71 KB
/
utils_3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: phelebra <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/21 13:14:37 by fvonsovs #+# #+# */
/* Updated: 2023/08/01 14:55:13 by phelebra ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// following mostly for lexer
/*
takes 3d matrix
returns 2d array
*/
char **ft_matrix_replace_in(char ***big, char **small, int n)
{
char **aux;
int i[3];
i[0] = -1;
i[1] = -1;
i[2] = -1;
if (!big || !*big || n < 0 || n >= ft_matrixlen(*big))
return (NULL);
aux = ft_calloc(ft_matrixlen(*big) + ft_matrixlen(small), sizeof(char *));
while (aux && big[0][++i[0]])
{
if (i[0] != n)
aux[++i[2]] = ft_strdup(big[0][i[0]]);
else
{
while (small && small[++i[1]])
aux[++i[2]] = ft_strdup(small[i[1]]);
}
}
ft_free_matrix(big);
*big = aux;
return (*big);
}
int ft_matrixlen(char **m)
{
int i;
i = 0;
while (m && m[i])
i++;
return (i);
}
void ft_free_matrix(char ***m)
{
int i;
i = 0;
while (m && m[0] && m[0][i])
{
free(m[0][i]);
i++;
}
if (m)
{
free(m[0]);
*m = NULL;
}
}