ajout builtin hash + ajout rehash si access ne fonctionne pas sur path donné dans la table
This commit is contained in:
parent
36f45536fe
commit
480c9810c9
9 changed files with 98 additions and 7 deletions
|
|
@ -37,6 +37,7 @@ builtin/builtin_echo.c\
|
||||||
builtin/builtin_env.c\
|
builtin/builtin_env.c\
|
||||||
builtin/builtin_exit.c\
|
builtin/builtin_exit.c\
|
||||||
builtin/builtin_export.c\
|
builtin/builtin_export.c\
|
||||||
|
builtin/builtin_hash.c\
|
||||||
builtin/builtin_history.c\
|
builtin/builtin_history.c\
|
||||||
builtin/builtin_read.c\
|
builtin/builtin_read.c\
|
||||||
builtin/builtin_setenv.c\
|
builtin/builtin_setenv.c\
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/13 17:21:56 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_fg(const char *path, char *const av[], char *const envp[]);
|
||||||
int builtin_bg(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_history(const char *path, char *const av[], char *const envp[]);
|
||||||
|
int builtin_hash(const char *path, char *const av[], char *const envp[]);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/02/18 11:10:14 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_is_hash(t_process *p);
|
||||||
int ft_hash_str(char *str);
|
int ft_hash_str(char *str);
|
||||||
|
|
||||||
|
void ft_hash_free(void *ptr, size_t size);
|
||||||
void ft_free_hash_table(void);
|
void ft_free_hash_table(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/28 14:28:41 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
56
42sh/src/builtin/builtin_hash.c
Normal file
56
42sh/src/builtin/builtin_hash.c
Normal 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);
|
||||||
|
}
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/12/13 13:09:57 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},
|
{"bg", &builtin_bg},
|
||||||
{"history", &builtin_history},
|
{"history", &builtin_history},
|
||||||
{"read", &builtin_read},
|
{"read", &builtin_read},
|
||||||
|
{"hash", &builtin_hash},
|
||||||
{NULL, NULL},
|
{NULL, NULL},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/02/18 11:08:40 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)
|
int ft_is_hash(t_process *p)
|
||||||
{
|
{
|
||||||
t_list *list;
|
t_list *list;
|
||||||
|
t_list *ref;
|
||||||
int id;
|
int id;
|
||||||
|
|
||||||
id = ft_hash_str(p->av[0]);
|
id = ft_hash_str(p->av[0]);
|
||||||
if (!g_hash[id])
|
|
||||||
return (0);
|
|
||||||
list = g_hash[id];
|
list = g_hash[id];
|
||||||
|
ref = list;
|
||||||
while (list)
|
while (list)
|
||||||
{
|
{
|
||||||
if (!ft_strcmp(((t_hash *)list->content)->key, p->av[0]))
|
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);
|
p->path = ft_strdup(((t_hash *)list->content)->path);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
ref = list;
|
||||||
list = list->next;
|
list = list->next;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
21
42sh/test
Normal file
21
42sh/test
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
[7m[33m shell_init.c [1m[34m28 [0minteractive shell settings[0m
|
||||||
|
[7m[33m main.c [1m[34m88 [0m[7m[1m[32mstart of shell[0m JOBC is ON[0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mhash -r[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[hash][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[-r][0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mhash[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[hash][0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mls[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[ls][0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mls[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[ls][0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mgrep[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[grep][0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mhash[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[hash][0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mhash -r[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[hash][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[-r][0m
|
||||||
|
[7m[33m main.c [1m[34m64 [0m[[35mhash[0m] stack=[0] state=[4][0m
|
||||||
|
[7m[33m token_print.c [1m[34m29 [0m13:[hash][0m
|
||||||
|
[7m[33m shell_exit.c [1m[34m17 [0mshell_exit()[0m
|
||||||
3
42sh/testmake
Normal file
3
42sh/testmake
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
[7m[33m shell_init.c [1m[34m28 [0minteractive shell settings[0m
|
||||||
|
[7m[33m main.c [1m[34m88 [0m[7m[1m[32mstart of shell[0m JOBC is ON[0m
|
||||||
|
[7m[33m shell_exit.c [1m[34m17 [0mshell_exit()[0m
|
||||||
Loading…
Reference in a new issue