-
Notifications
You must be signed in to change notification settings - Fork 0
/
expansions.c
executable file
·96 lines (86 loc) · 1.84 KB
/
expansions.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
#include "minishell.h"
char *change_var(char *cmd, char *var, char *value, int pos_s)
{
char *new_cmd;
char *temp;
temp = ft_substr(cmd, 0, pos_s);
new_cmd = ft_strjoin(temp, value);
temp = cmd;
new_cmd = ft_strjoin(new_cmd, cmd + pos_s + ft_strlen(var) + 1);
ft_free(&temp);
return (new_cmd);
}
char *get_sub(char *str)
{
int i;
char *new_str;
i = 0;
while (str[i] && !ft_ismetachar(str[i]) && !ft_isspace(str[i]))
i++;
new_str = ft_substr(str, 0, i);
return (new_str);
}
char *join_three(char *str, int j, char *str2, t_env *new_envp)
{
char *return_str;
char *str1;
char *str3;
return_str = NULL;
str1 = ft_substr(str, 0, j);
str3 = ft_get_env(new_envp->env, str2);
if (!str3)
return (NULL);
return_str = ft_strjoin(str1, str3);
freetwo_ptrs(&str1, &str3);
str3 = ft_substr(str, j + ft_strlen(str2) + 1, ft_strlen(str) - j
+ ft_strlen(str2));
str1 = return_str;
return_str = ft_strjoin(return_str, str3);
free(str);
freethree_ptrs(&str2, &str1, &str3);
return (return_str);
}
char *join_status(char *str, int j, char *str2)
{
char *return_str;
char *str1;
char *str3;
return_str = NULL;
str1 = ft_substr(str, 0, j);
free(str);
return_str = ft_strjoin(str1, str2);
str = return_str;
str3 = ft_substr(str, j + ft_strlen(str2) + 1, ft_strlen(str) - j
- ft_strlen(str2) - 1);
return_str = ft_strjoin(return_str, str3);
free(str);
freethree_ptrs(&str2, &str1, &str3);
return (return_str);
}
int expand(char ***cmds, t_env *new_envp)
{
int i;
int j;
char **tmp;
i = -1;
tmp = *cmds;
while (tmp[++i])
{
if (is_quote(tmp[i]) == 1)
tmp[i] = ft_new_trim(tmp[i]);
else
{
tmp[i] = ft_new_trim(tmp[i]);
j = 0;
while (tmp[i][j])
{
get_flag(&tmp, &i, &j, new_envp);
if (j < 0)
return (-1);
if (!tmp[i])
return (return_swap(&cmds, tmp));
}
}
}
return (return_swap(&cmds, tmp));
}