diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-12 20:00:38 +0200 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-09-12 20:00:38 +0200 |
commit | 690752d22b5aac9a836e8ba09c8c2544f8d026bd (patch) | |
tree | 2d10d1de5ceed59665a26aaf420e766dc42f732d | |
parent | Sorted alias (diff) | |
download | 42-minishell-690752d22b5aac9a836e8ba09c8c2544f8d026bd.tar.gz 42-minishell-690752d22b5aac9a836e8ba09c8c2544f8d026bd.tar.bz2 42-minishell-690752d22b5aac9a836e8ba09c8c2544f8d026bd.tar.xz 42-minishell-690752d22b5aac9a836e8ba09c8c2544f8d026bd.tar.zst 42-minishell-690752d22b5aac9a836e8ba09c8c2544f8d026bd.zip |
Norme in progress
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | src/b_alias.c | 100 | ||||
-rw-r--r-- | src/b_alias_print.c | 116 | ||||
-rw-r--r-- | src/b_alias_print.h | 20 |
4 files changed, 138 insertions, 99 deletions
@@ -16,6 +16,7 @@ LFT_SRCS_DIR = ${LFT_DIR}src/ #--------------------------------- FILES --------------------------------------# #==============================================================================# SRCS_NAME = b_alias +SRCS_NAME += b_alias_print SRCS_NAME += b_cd SRCS_NAME += b_echo SRCS_NAME += b_env diff --git a/src/b_alias.c b/src/b_alias.c index c00a616..badd281 100644 --- a/src/b_alias.c +++ b/src/b_alias.c @@ -16,110 +16,12 @@ #include <unistd.h> #include <limits.h> +#include "b_alias_print.h" #include "d_define.h" #include "s_struct.h" #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; - - 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); -} - static uint8_t b_print_arg(const char arg[], t_msh *msh) { diff --git a/src/b_alias_print.c b/src/b_alias_print.c new file mode 100644 index 0000000..ebe2708 --- /dev/null +++ b/src/b_alias_print.c @@ -0,0 +1,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); +} diff --git a/src/b_alias_print.h b/src/b_alias_print.h new file mode 100644 index 0000000..a3d2ee3 --- /dev/null +++ b/src/b_alias_print.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* b_alias_print.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#ifndef B_ALIAS_PRINT_H +#define B_ALIAS_PRINT_H + +#include "s_struct.h" + +void b_print_alias_list(t_msh *msh); + +#endif |