-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.c
executable file
·128 lines (119 loc) · 3.13 KB
/
export.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mazaroua <mazaroua@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/17 02:20:21 by mazaroua #+# #+# */
/* Updated: 2023/05/16 15:28:22 by mazaroua ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void if_equal_only(char *str, char **name_value, t_export **export,
t_env_list **env_list)
{
if (eqaul_last_only(str) && check_if_exist(name_value[0], *export))
custom_edit_value(name_value[0], name_value[1], export, env_list);
else if (check_if_exist(name_value[0], *export))
{
free_rows(name_value);
return ;
}
else if (eqaul_last_only(str))
{
addback_env(env_list, addnew_env(name_value[0], "\0"));
addback_export(export, addnew_export(name_value[0], "eq"));
}
else
{
addback_export(export, addnew_export(name_value[0], name_value[1]));
}
free_rows(name_value);
}
void add_var_in_list(t_export **export, t_env_list **env_list, char *str)
{
char **name_value;
name_value = fill_name_value(str);
if (!name_value)
return ;
if (check_if_equal_is(str) && eqaul_last_only(str) == 0)
{
if (check_if_exist(name_value[0], *export))
{
if (check_append(str))
{
append_free(name_value, export, env_list);
return ;
}
edit_free(name_value, export, env_list);
return ;
}
addback_free(export, env_list, name_value);
}
else
{
if_equal_only(str, name_value, export, env_list);
free(name_value);
}
}
void print_export(t_export *head)
{
while (head)
{
if (head->flag == 1)
printf("declare -x %s=\"\"\n", head->var);
else if (*(head->value) == '\0')
{
printf("declare -x %s\n", head->var);
}
else
printf("declare -x %s=\"%s\"\n", head->var, head->value);
head = head->next;
}
}
void edit_env(t_env_list **env_list, char *name, char *value)
{
t_env_list *lst;
int no_created;
no_created = 0;
lst = (*env_list);
while (lst)
{
if (ft_strcmp(lst->name, name) == 0)
{
no_created = 1;
free(lst->value);
lst->value = ft_strdup_export(value);
lst->flag = 0;
return ;
}
lst = lst->next;
}
if (!no_created)
addback_env(env_list, addnew_env(name, value));
}
void do_export(char *str[], t_export **export,
t_env_list **env_list, int flag)
{
int i;
int j;
i = 1;
j = 0;
g_var.exit_state = 0;
if (str[1] == NULL)
print_export(*export);
while (str[i])
{
if (correct_name(get_name(str[i])) && !ft_isdigit(str[i][0]))
{
add_var_in_list(export, env_list, str[i]);
}
else
error_od_export(str[i]);
i++;
}
sort(export);
if (flag)
exit(g_var.exit_state);
}