ajout builtin hash + ajout rehash si access ne fonctionne pas sur path donné dans la table

This commit is contained in:
gwojda 2017-02-18 16:52:55 +01:00
parent 36f45536fe
commit 480c9810c9
9 changed files with 98 additions and 7 deletions

View file

@ -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\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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},
};

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

21
42sh/test Normal file
View file

@ -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()

3
42sh/testmake Normal file
View file

@ -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()