From 480c9810c9340075371250acf5cbc8aa0e07029e Mon Sep 17 00:00:00 2001 From: gwojda Date: Sat, 18 Feb 2017 16:52:55 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20builtin=20hash=20+=20ajout=20rehash=20s?= =?UTF-8?q?i=20access=20ne=20fonctionne=20pas=20sur=20path=20donn=C3=A9=20?= =?UTF-8?q?dans=20la=20table?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 42sh/Makefile | 1 + 42sh/includes/builtin.h | 3 +- 42sh/includes/hash.h | 3 +- 42sh/src/builtin/builtin_exit.c | 2 +- 42sh/src/builtin/builtin_hash.c | 56 +++++++++++++++++++++++++++++++++ 42sh/src/builtin/is_builtin.c | 3 +- 42sh/src/hash_table/is_hash.c | 13 ++++++-- 42sh/test | 21 +++++++++++++ 42sh/testmake | 3 ++ 9 files changed, 98 insertions(+), 7 deletions(-) create mode 100644 42sh/src/builtin/builtin_hash.c create mode 100644 42sh/test create mode 100644 42sh/testmake diff --git a/42sh/Makefile b/42sh/Makefile index fea79a42..7c55625d 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -37,6 +37,7 @@ builtin/builtin_echo.c\ builtin/builtin_env.c\ builtin/builtin_exit.c\ builtin/builtin_export.c\ +builtin/builtin_hash.c\ builtin/builtin_history.c\ builtin/builtin_read.c\ builtin/builtin_setenv.c\ diff --git a/42sh/includes/builtin.h b/42sh/includes/builtin.h index bb3c2637..dd7b03a8 100644 --- a/42sh/includes/builtin.h +++ b/42sh/includes/builtin.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ -/* Updated: 2017/02/15 11:45:15 by gwojda ### ########.fr */ +/* Updated: 2017/02/18 16:44:35 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,5 +30,6 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]); int builtin_fg(const char *path, char *const av[], char *const envp[]); int builtin_bg(const char *path, char *const av[], char *const envp[]); int builtin_history(const char *path, char *const av[], char *const envp[]); +int builtin_hash(const char *path, char *const av[], char *const envp[]); #endif diff --git a/42sh/includes/hash.h b/42sh/includes/hash.h index fa43f929..aa4dab8c 100644 --- a/42sh/includes/hash.h +++ b/42sh/includes/hash.h @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:10:14 by gwojda #+# #+# */ -/* Updated: 2017/02/18 14:18:06 by gwojda ### ########.fr */ +/* Updated: 2017/02/18 16:35:34 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,6 +28,7 @@ int ft_hash(t_process *p); int ft_is_hash(t_process *p); int ft_hash_str(char *str); +void ft_hash_free(void *ptr, size_t size); void ft_free_hash_table(void); #endif diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index fd884465..370e60ac 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ -/* Updated: 2017/02/18 14:17:28 by gwojda ### ########.fr */ +/* Updated: 2017/02/18 16:48:42 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_hash.c b/42sh/src/builtin/builtin_hash.c new file mode 100644 index 00000000..f030a63d --- /dev/null +++ b/42sh/src/builtin/builtin_hash.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtin_hash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/18 16:37:43 by gwojda #+# #+# */ +/* Updated: 2017/02/18 16:51:43 by gwojda ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void ft_hash_lst_print(t_list *list) +{ + while (list) + { + ft_putstr(((t_hash *)list->content)->key); + ft_putchar('='); + ft_putstr(((t_hash *)list->content)->path); + ft_putchar('\n'); + list = list->next; + } +} + +static int ft_hash_opt(char *const av[]) +{ + if (av[1] && !ft_strcmp(av[1], "-r")) + ft_free_hash_table(); + else if (av[1]) + { + ft_dprintf(2, "42sh: hash: invalid option\n"); + ft_dprintf(2, "hash: usage: hash [-r]\n"); + return (1); + } + return (0); +} + +int builtin_hash(const char *path, char *const av[], char *const envp[]) +{ + int i; + + (void)path; + (void)envp; + if (ft_hash_opt(av)) + return (0); + i = 0; + while (i < MAX_HASH) + { + if (g_hash[i]) + ft_hash_lst_print(g_hash[i]); + ++i; + } + return (0); +} diff --git a/42sh/src/builtin/is_builtin.c b/42sh/src/builtin/is_builtin.c index 8dd86660..d9ba30bc 100644 --- a/42sh/src/builtin/is_builtin.c +++ b/42sh/src/builtin/is_builtin.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ -/* Updated: 2017/02/15 11:44:31 by gwojda ### ########.fr */ +/* Updated: 2017/02/18 16:44:56 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ t_stof g_builtin[] = {"bg", &builtin_bg}, {"history", &builtin_history}, {"read", &builtin_read}, + {"hash", &builtin_hash}, {NULL, NULL}, }; diff --git a/42sh/src/hash_table/is_hash.c b/42sh/src/hash_table/is_hash.c index 67fad4bd..03fdf717 100644 --- a/42sh/src/hash_table/is_hash.c +++ b/42sh/src/hash_table/is_hash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/18 11:08:40 by gwojda #+# #+# */ -/* Updated: 2017/02/18 13:41:48 by gwojda ### ########.fr */ +/* Updated: 2017/02/18 16:42:12 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,19 +15,26 @@ int ft_is_hash(t_process *p) { t_list *list; + t_list *ref; int id; id = ft_hash_str(p->av[0]); - if (!g_hash[id]) - return (0); list = g_hash[id]; + ref = list; while (list) { if (!ft_strcmp(((t_hash *)list->content)->key, p->av[0])) { + if (access(((t_hash *)list->content)->path, X_OK)) + { + ref->next = list->next; + ft_lstdelone(&list, &ft_hash_free); + return (0); + } p->path = ft_strdup(((t_hash *)list->content)->path); return (1); } + ref = list; list = list->next; } return (0); diff --git a/42sh/test b/42sh/test new file mode 100644 index 00000000..2fc442d0 --- /dev/null +++ b/42sh/test @@ -0,0 +1,21 @@ + shell_init.c 28 interactive shell settings + main.c 88 start of shell JOBC is ON + main.c 64 [hash -r] stack=[0] state=[4] + token_print.c 29 13:[hash] + token_print.c 29 13:[-r] + main.c 64 [hash] stack=[0] state=[4] + token_print.c 29 13:[hash] + main.c 64 [ls] stack=[0] state=[4] + token_print.c 29 13:[ls] + main.c 64 [ls] stack=[0] state=[4] + token_print.c 29 13:[ls] + main.c 64 [grep] stack=[0] state=[4] + token_print.c 29 13:[grep] + main.c 64 [hash] stack=[0] state=[4] + token_print.c 29 13:[hash] + main.c 64 [hash -r] stack=[0] state=[4] + token_print.c 29 13:[hash] + token_print.c 29 13:[-r] + main.c 64 [hash] stack=[0] state=[4] + token_print.c 29 13:[hash] + shell_exit.c 17 shell_exit() diff --git a/42sh/testmake b/42sh/testmake new file mode 100644 index 00000000..e0995542 --- /dev/null +++ b/42sh/testmake @@ -0,0 +1,3 @@ + shell_init.c 28 interactive shell settings + main.c 88 start of shell JOBC is ON + shell_exit.c 17 shell_exit()