diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-12 19:16:21 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-12 19:16:21 +0200 |
commit | b41111c07a40ee405fb8e8b6c178a315272db7ae (patch) | |
tree | b743a4c153b17369ae27c7554f61b3f11e15df6f | |
parent | Fix (diff) | |
download | 42-minishell-b41111c07a40ee405fb8e8b6c178a315272db7ae.tar.gz 42-minishell-b41111c07a40ee405fb8e8b6c178a315272db7ae.tar.bz2 42-minishell-b41111c07a40ee405fb8e8b6c178a315272db7ae.tar.xz 42-minishell-b41111c07a40ee405fb8e8b6c178a315272db7ae.tar.zst 42-minishell-b41111c07a40ee405fb8e8b6c178a315272db7ae.zip |
Sorted alias
-rw-r--r-- | libft/src/ft_strlcpy.c | 4 | ||||
-rw-r--r-- | src/b_alias.c | 87 | ||||
-rw-r--r-- | src/s_lalias.c | 1 |
3 files changed, 88 insertions, 4 deletions
diff --git a/libft/src/ft_strlcpy.c b/libft/src/ft_strlcpy.c index ed4d924..5076a69 100644 --- a/libft/src/ft_strlcpy.c +++ b/libft/src/ft_strlcpy.c @@ -16,9 +16,9 @@ size_t ft_strlcpy(char *dst, const char *src, size_t size) { - size_t src_len; + size_t src_len; - if (!dst || !src) + if (dst == NULL || src == NULL) return (0); src_len = ft_strlen(src); if (src_len + 1 < size) diff --git a/src/b_alias.c b/src/b_alias.c index 5ca86da..c00a616 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -12,6 +12,7 @@ #include <libft.h> #include <stdint.h> +#include <stdlib.h> #include <unistd.h> #include <limits.h> @@ -20,12 +21,87 @@ #include "u_alias.h" #include "u_utils.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++; + } +} + static void b_print_alias_list(t_msh *msh) { t_lalias *ptr; + char **buff; size_t i; - /* char buff[255][255][ARG_MAX]; */ ptr = msh->alias; i = 0; @@ -34,7 +110,14 @@ static void ptr = ptr->next; i++; } - ft_printf("alias %s='%s'\n", ptr->name, ptr->val); + 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); } static uint8_t diff --git a/src/s_lalias.c b/src/s_lalias.c index 56b3985..a31909b 100644 --- a/src/s_lalias.c +++ b/src/s_lalias.c @@ -65,6 +65,7 @@ void ft_memdel((void*)&tmp->name); ft_memdel((void*)&tmp->val); ft_memdel((void*)&tmp); + tmp = renext; } *lalias = NULL; } |