-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpand.c
72 lines (65 loc) · 1.89 KB
/
expand.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bpisak-l <bpisak-l@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/17 10:25:04 by bpisak-l #+# #+# */
/* Updated: 2024/07/26 13:41:01 by bpisak-l ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int escape_dollar(char *str, int i, char *map)
{
int res;
if (!str[i])
return (0);
res = map[i] != '\'' && str[i] == '\\' && str[i + 1] == '$';
return (res);
}
char *expand_variables(char *s, char *skip)
{
char *map;
char *res;
int i;
int j;
char *buff;
map = operation_map(s, NULL, skip);
i = -1;
j = 0;
res = NULL;
buff = m_ft_calloc(1, ft_strlen(s) + 1);
while (map[++i])
{
if (s[i] && map[i] != '\'' && s[i] == '\\' && s[i + 1] == '\\')
buff[j++] = s[i++];
else if (!escape_dollar(s, i, map) && start_variable(s, map, i))
{
j = 0;
append_variable_value(&res, &buff, s, &i);
}
else if (!escape_dollar(s, i, map))
buff[j++] = s[i];
}
free(map);
return (ft_str_append(res, buff));
}
char *remove_chars(char *str, char *skip)
{
char *map;
int i;
int j;
char *res;
map = operation_map(str, NULL, skip);
res = m_ft_calloc(ft_strlen(map) + 1, 1);
i = -1;
j = 0;
while (map[++i])
{
if (map[i] != SKIP)
res[j++] = str[i];
}
free(map);
return (res);
}