Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/builtin/export/export_utils.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* export_utils.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/07/27 17:56:21 by yabtaour #+# #+# */
9
/* Updated: 2022/09/19 16:30:06 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../../minishell.h"
14
15
int ft_change_env_value(t_data *data, char *name, char *value)
16
{
17
t_env *env_clone;
18
19
env_clone = data->lst_env;
20
while (env_clone)
21
{
22
if (ft_strcmp(name, env_clone->name) == 0)
23
{
24
if (value)
25
{
26
free(env_clone->value);
27
if (value[0] == '\0')
28
{
29
env_clone->value = malloc (1);
30
env_clone->value[0] = '\0';
31
}
32
else
33
env_clone->value = ft_substr(value, 0, ft_strlen(value));
34
}
35
}
36
env_clone = env_clone->next;
37
}
38
free(name);
39
if (value)
40
free(value);
41
return (0);
42
}
43
44
int ft_name_exists(t_data *data, char *name)
45
{
46
t_env *env_clone;
47
48
if (!name)
49
return (0);
50
env_clone = data->lst_env;
51
while (env_clone)
52
{
53
if (ft_strcmp(name, env_clone->name) == 0)
54
return (1);
55
env_clone = env_clone->next;
56
}
57
return (0);
58
}
59
60
char *ft_get_name_exp(char *name)
61
{
62
int i;
63
char *new_name;
64
65
new_name = NULL;
66
i = 0;
67
if (!name)
68
return (NULL);
69
while (name[i] && name[i] != '=')
70
i++;
71
new_name = ft_substr(name, 0, i);
72
return (new_name);
73
}
74
75
char *export_no_value(char *value, int idx)
76
{
77
char *new;
78
79
if (value[idx] == '\0')
80
{
81
new = malloc(sizeof(char) * 1);
82
new[0] = '\0';
83
return (new);
84
}
85
return (NULL);
86
}
87
88
char *ft_get_value_exp(char *value)
89
{
90
int i;
91
int j;
92
int origin;
93
char *new_value;
94
95
i = 0;
96
j = 0;
97
new_value = NULL;
98
if (!value)
99
return (NULL);
100
while (value[i] && value[i] != '=')
101
i++;
102
if (value[i])
103
{
104
origin = i + 1;
105
new_value = export_no_value(value, origin);
106
if (new_value)
107
return (new_value);
108
while (value[++i])
109
j++;
110
new_value = ft_substr(value, origin, j);
111
}
112
return (new_value);
113
}
114
115