-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpand_wildcard.c
94 lines (84 loc) · 2.21 KB
/
expand_wildcard.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* expand_wildcard.c :+: :+: */
/* +:+ */
/* By: sde-rijk <[email protected]> +#+ */
/* +#+ */
/* Created: 2021/12/13 15:17:24 by sde-rijk #+# #+# */
/* Updated: 2021/12/24 11:27:51 by daniel ######## odam.nl */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "Libft/libft.h"
#include "stdio.h"
static int expand_wild(char **part, int *wild_quoted);
int count_parts(t_part *s)
{
int i;
i = 0;
while (s[i].part)
i++;
return (i);
}
int replace_parts(t_part **parts, int i)
{
char **split;
int nr_old_parts;
int nr_new_parts;
int j;
char *old;
nr_old_parts = count_parts(*parts);
split = ft_split((*parts)[i].part, ' ');
nr_new_parts = ft_count_strs(split);
old = (*parts)[i].part;
*parts = ft_recalloc(*parts,
(nr_old_parts + nr_new_parts + 1) * sizeof(**parts),
(nr_old_parts + 1) * sizeof(**parts));
ft_memmove((*parts) + i + nr_new_parts - 1, (*parts) + i,
(nr_old_parts - i) * sizeof(**parts));
j = 0;
while (j < nr_new_parts)
{
(*parts)[i + j].part = split[j];
(*parts)[i + j].type = NORMAL;
j++;
}
free(split);
free(old);
return (nr_new_parts);
}
void expand_wildcard(t_part **parts, int **wild_quoted)
{
int i;
int j;
i = 0;
j = 0;
while ((*parts)[i].part)
{
if ((*parts)[i].type == NORMAL)
{
if (expand_wild(&(*parts)[i].part, wild_quoted[j]))
i += replace_parts(parts, i);
}
i++;
j++;
}
}
static int expand_wild(char **part, int *wild_quoted)
{
char *str;
int i;
i = 0;
str = *part;
while (str[i])
{
if (str[i] == '*')
{
*part = ft_wildcard(*part, wild_quoted);
return (1);
}
i++;
}
return (0);
}