-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
97 lines (87 loc) · 2.19 KB
/
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
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: darodrig <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/26 12:42:32 by asegovia #+# #+# */
/* Updated: 2020/06/30 13:11:58 by darodrig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char
*get_var(char *var, char **env)
{
int i;
i = 0;
while (env[i])
{
if (ft_strncmp(var, env[i], ft_strlen(var)) == 0 &&
env[i][ft_strlen(var)] == '=')
return (&env[i][ft_strlen(var) + 1]);
i++;
}
return (NULL);
}
void
envp_dup(t_sh *sh, char **envp)
{
int i;
int size;
char **new;
i = 0;
size = array_counter(envp);
new = malloc(sizeof(char*) * size + 1);
while (i < size)
{
new[i] = ft_strdup(envp[i]);
i++;
}
new[i] = NULL;
sh->envp = new;
}
int
array_counter(char **arr)
{
int i;
i = 0;
while (arr && arr[i] != NULL)
i++;
return (i);
}
int
set_special_chars(t_sh *sh, char c)
{
if (c == '\'' && sh->bar == 0)
{
sh->squotes = !sh->squotes;
}
else if (c == '\"' && sh->bar == 0 && sh->squotes == 0)
sh->dquotes = !sh->dquotes;
else if (c == '\\')
sh->bar = !sh->bar;
if (c != '\\' && sh->bar == 1 && c != '$')
sh->bar = 0;
if (sh->dquotes == 0 && sh->squotes == 0 && sh->bar == 0)
return (1);
if (sh->dquotes == 1 && sh->squotes == 0 && sh->bar == 0)
return (1);
if (sh->dquotes == 0 && sh->squotes == 1 && sh->bar == 0)
return (1);
return (0);
}
int
search_str_in_arr(char *str, char **arr)
{
int i;
int var_len;
var_len = ft_strlen(str);
i = -1;
while (arr[++i])
{
if (ft_strncmp(str, arr[i], var_len) == 0)
return (i);
}
return (0);
}