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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* b_alias_print.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdlib.h>
#include "d_define.h"
#include "s_struct.h"
static char
**b_alloc_buff(t_lalias *ptr, size_t i)
{
char **buff;
if ((buff = (char**)malloc((i + 1) * sizeof(char*))) == NULL)
return (NULL);
i = 0;
while (ptr != NULL)
{
buff[i + 1] = NULL;
if ((buff[i] = (char*)malloc((6 + ft_strlen(ptr->name) + 2
+ ft_strlen(ptr->val) + 3) * sizeof(char))) == NULL)
{
ft_delwords(buff);
return (NULL);
}
ptr = ptr->next;
i++;
}
return (buff);
}
static void
b_fill_buff(t_lalias *ptr, char *buff[])
{
size_t i;
i = 0;
while (ptr != NULL)
{
ft_sprintf(buff[i], "alias %s='%s'\n", ptr->name, ptr->val);
ptr = ptr->next;
i++;
}
}
static void
b_sort_buff(char *buff[])
{
size_t i;
int cmp;
char tmp[ARG_MAX];
i = 1;
while (buff[i] != NULL)
{
if ((cmp = ft_strncmp(buff[i - 1], buff[i], ARG_MAX)) > 0)
{
ft_strlcpy(tmp, buff[i - 1], ARG_MAX);
ft_memdel((void*)&buff[i - 1]);
if ((buff[i - 1] = ft_strdup(buff[i])) == NULL)
return ;
ft_memdel((void*)&buff[i]);
if ((buff[i] = ft_strdup(tmp)) == NULL)
return ;
i = 0;
}
i++;
}
}
static void
b_print_buff(char *buff[])
{
char **ptr;
ptr = buff;
while (*ptr != NULL)
{
ft_printf("%s", *ptr);
ptr++;
}
}
void
b_print_alias_list(t_msh *msh)
{
t_lalias *ptr;
char **buff;
size_t i;
ptr = msh->alias;
i = 0;
while (ptr != NULL)
{
ptr = ptr->next;
i++;
}
ptr = msh->alias;
if ((buff = b_alloc_buff(ptr, i)) == NULL)
return ;
ptr = msh->alias;
b_fill_buff(ptr, buff);
b_sort_buff(buff);
b_print_buff(buff);
ft_delwords(buff);
}
|