-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_export.c
113 lines (102 loc) · 2.31 KB
/
print_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* print_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wonyang <wonyang@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/09 20:04:00 by wonyang #+# #+# */
/* Updated: 2023/01/14 17:06:19 by wonyang ### ########seoul.kr */
/* */
/* ************************************************************************** */
#include <unistd.h>
#include <stdlib.h>
#include "return.h"
#include "ds_envp.h"
static void sort_arr(char **arr, int len)
{
int i;
int j;
char *tmp;
i = 0;
while (i < len - 1)
{
j = 0;
while (j < len - 1 - i)
{
if (ft_strcmp(arr[j], arr[j + 1]) > 0)
{
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
j++;
}
i++;
}
}
static int count_key(t_envp *envp)
{
int i;
t_enode *node;
i = 0;
node = envp->head;
while (node)
{
i++;
node = node->next;
}
return (i);
}
static void print_line(t_envp *envp, char *key)
{
char *value;
value = search_key_value(envp, key);
ft_putstr_fd("declare -x ", STDOUT_FILENO);
ft_putstr_fd(key, STDOUT_FILENO);
if (value)
{
ft_putstr_fd("=\"", STDOUT_FILENO);
ft_putstr_fd(value, STDOUT_FILENO);
ft_putstr_fd("\"", STDOUT_FILENO);
}
ft_putchar_fd('\n', STDOUT_FILENO);
}
static char **make_arr(t_envp *envp)
{
int i;
t_enode *node;
char **key_arr;
int len;
len = count_key(envp);
key_arr = (char **)malloc(sizeof(char *) * (len + 1));
if (!key_arr)
return (NULL);
key_arr[len] = NULL;
node = envp->head;
i = 0;
while (node)
{
key_arr[i] = node->key;
i++;
node = node->next;
}
sort_arr(key_arr, len);
return (key_arr);
}
int print_envp(t_envp *envp)
{
int i;
char **key_arr;
key_arr = make_arr(envp);
if (!key_arr)
return (1);
i = 0;
while (key_arr[i])
{
print_line(envp, key_arr[i]);
i++;
}
free(key_arr);
return (0);
}