-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprint_in_sort.c
More file actions
112 lines (102 loc) · 2.58 KB
/
print_in_sort.c
File metadata and controls
112 lines (102 loc) · 2.58 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_in_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mamoussa <mamoussa@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/24 16:15:07 by mamoussa #+# #+# */
/* Updated: 2020/12/10 17:59:41 by mamoussa ### ########.fr */
/* */
/* ************************************************************************** */
#include "shell.h"
void node_swap(t_env *head, t_env *current)
{
char *tmp;
tmp = ft_strdup(head->key);
g_tmp = head->key;
head->key = ft_strdup(current->key);
simple_pointer_free(g_tmp);
g_tmp = current->key;
current->key = ft_strdup(tmp);
simple_pointer_free(g_tmp);
simple_pointer_free(tmp);
tmp = ft_strdup(head->value);
g_tmp = head->value;
head->value = ft_strdup(current->value);
simple_pointer_free(g_tmp);
g_tmp = current->value;
current->value = ft_strdup(tmp);
simple_pointer_free(g_tmp);
simple_pointer_free(tmp);
}
int key_compariason(char *head_key, char *cur_key)
{
int i;
int j;
i = 0;
j = 0;
while (head_key[i] && cur_key[i])
{
if (head_key[i] == cur_key[j])
{
i++;
j++;
continue ;
}
else
return (head_key[i] - cur_key[j]);
}
return (0);
}
void node_sort(t_env *head_cpy)
{
t_env *head;
t_env *current;
head = head_cpy;
while (head->next)
{
current = head->next;
while (current)
{
if (key_compariason(head->key, current->key) > 0)
node_swap(head, current);
current = current->next;
}
head = head->next;
}
}
void print_in_sort_helper(t_env *current)
{
if (g_is_out)
{
dup2(g_fd_out, 1);
close(g_fd_out);
}
imp_pipes();
while (current)
{
write(1, "declare -x ", ft_strlen("declare -x "));
write(1, current->key, ft_strlen(current->key));
write(1, "=", 1);
write(1, current->value, ft_strlen(current->value));
write(1, "\n", 1);
current = current->next;
}
exit(0);
}
void print_in_sort(void)
{
t_env *current;
t_env *head_cpy;
pid_t pid;
head_cpy = node_cpy();
current = head_cpy;
node_sort(head_cpy);
if ((pid = fork()) < 0)
return ;
if (pid == 0)
print_in_sort_helper(current);
else
ft_lstclearenv(&head_cpy);
}