correction segv edition de ligne + ajout table de hash

This commit is contained in:
gwojda 2017-02-18 14:42:43 +01:00
parent e10d64c05b
commit ea073dceaf
14 changed files with 449 additions and 9 deletions

View file

@ -110,6 +110,11 @@ glob/lib_perso/ft_strsubf.c\
glob/lib_perso/ft_tabdel.c\ glob/lib_perso/ft_tabdel.c\
glob/lib_perso/ft_tablen.c\ glob/lib_perso/ft_tablen.c\
glob/match_pattern.c\ glob/match_pattern.c\
hash_table/ft_add_hash.c\
hash_table/hash.c\
hash_table/hash_free.c\
hash_table/hash_str.c\
hash_table/is_hash.c\
history/add_str_in_history.c\ history/add_str_in_history.c\
history/history.c\ history/history.c\
history/history_parsing.c\ history/history_parsing.c\

33
42sh/includes/hash.h Normal file
View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hash.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#ifndef HASH_H
# define HASH_H
# define MAX_HASH 200
extern t_list *g_hash[MAX_HASH];
typedef struct s_hash
{
char *key;
char *path;
} t_hash;
int ft_add_hash(t_process *p);
int ft_hash(t_process *p);
int ft_is_hash(t_process *p);
int ft_hash_str(char *str);
void ft_free_hash_table(void);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/02/15 14:20:59 by gwojda ### ########.fr */ /* Updated: 2017/02/18 11:10:50 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,7 @@
# include "job_control.h" # include "job_control.h"
# include "glob.h" # include "glob.h"
# include "completion.h" # include "completion.h"
# include "hash.h"
# include <dirent.h> # include <dirent.h>
# include <sys/stat.h> # include <sys/stat.h>

View file

@ -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/16 12:44:44 by gwojda ### ########.fr */ /* Updated: 2017/02/18 14:17:28 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,6 +37,7 @@ int builtin_exit(const char *path, char *const av[], char *const envp[])
status = 0; status = 0;
} }
ft_save_termios(-1); ft_save_termios(-1);
ft_free_hash_table();
exit(status); exit(status);
return (0); return (0);
} }

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* launch_process.c :+: :+: :+: */ /* launch_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */
/* Updated: 2017/02/07 17:54:12 by jhalford ### ########.fr */ /* Updated: 2017/02/18 13:27:16 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* process_setexec.c :+: :+: :+: */ /* process_setexec.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */ /* Created: 2016/12/13 17:07:10 by jhalford #+# #+# */
/* Updated: 2017/02/07 12:07:43 by jhalford ### ########.fr */ /* Updated: 2017/02/18 13:24:16 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,8 +28,7 @@ int process_setexec(t_type type, t_process *p)
p->attributes |= PROCESS_SCRIPT; p->attributes |= PROCESS_SCRIPT;
p->path = ft_strdup(p->av[0]); p->path = ft_strdup(p->av[0]);
} }
else if ((p->path = ft_findexec(ft_getenv( else if (ft_hash(p))
data_singleton()->env, "PATH"), p->av[0])))
{ {
p->execf = &execve; p->execf = &execve;
p->attributes |= PROCESS_BINARY; p->attributes |= PROCESS_BINARY;

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_add_hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:20:11 by gwojda #+# #+# */
/* Updated: 2017/02/18 14:38:21 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
# include "minishell.h"
int ft_add_hash(t_process *p)
{
int id;
t_hash hash;
if (!(hash.path = ft_findexec(
ft_getenv(data_singleton()->env, "PATH"), p->av[0])))
return (0);
hash.key = ft_strdup(p->av[0]);
id = ft_hash_str(p->av[0]);
ft_lsteadd(&(g_hash[id]), ft_lstnew(&hash, sizeof(t_hash)));
p->path = ft_strdup(hash.path);
return (1);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:06:19 by gwojda #+# #+# */
/* Updated: 2017/02/18 14:16:35 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_list *g_hash[MAX_HASH] = {NULL};
int ft_hash(t_process *p)
{
if (!ft_is_hash(p))
if (!ft_add_hash(p))
return (0);
return (1);
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hash_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 14:12:22 by gwojda #+# #+# */
/* Updated: 2017/02/18 14:20:20 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_hash_free(void *ptr, size_t size)
{
(void)size;
free(((t_hash *)ptr)->key);
free(((t_hash *)ptr)->path);
free(ptr);
}
void ft_free_hash_table(void)
{
int i;
i = 0;
while (i < MAX_HASH)
{
if (g_hash[i])
ft_lstdel(&g_hash[i], &ft_hash_free);
++i;
}
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hash_str.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:32:18 by gwojda #+# #+# */
/* Updated: 2017/02/18 14:38:05 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_hash_str(char *str)
{
int i;
int id;
i = 0;
id = 0;
while (str[i])
{
id += str[i] * ft_pow(10, i);
id = id % MAX_HASH;
++i;
}
return (id);
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_is_hash(t_process *p)
{
t_list *list;
int id;
id = ft_hash_str(p->av[0]);
if (!g_hash[id])
return (0);
list = g_hash[id];
while (list)
{
if (!ft_strcmp(((t_hash *)list->content)->key, p->av[0]))
{
p->path = ft_strdup(((t_hash *)list->content)->path);
return (1);
}
list = list->next;
}
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 16:52:57 by gwojda #+# #+# */ /* Created: 2016/12/19 16:52:57 by gwojda #+# #+# */
/* Updated: 2017/02/14 11:18:22 by gwojda ### ########.fr */ /* Updated: 2017/02/18 13:58:29 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -63,6 +63,8 @@ void ft_read_it(int input, size_t *pos, char **str)
return ; return ;
ft_read_it_2(input, t); ft_read_it_2(input, t);
ft_read_it_3(str, t, pos, &j); ft_read_it_3(str, t, pos, &j);
if (!*str)
return ;
*pos = pos_tmp; *pos = pos_tmp;
ft_current_str((*str), *pos); ft_current_str((*str), *pos);
ft_get_next_str((*str), pos); ft_get_next_str((*str), pos);

236
42sh/test Normal file
View file

@ -0,0 +1,236 @@
 shell_init.c 28 interactive shell settings
 main.c 88 start of shell JOBC is ON
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 main.c 64 [] stack=[0] state=[0]
 shell_exit.c 17 shell_exit()

15
42sh/testmake Normal file
View file

@ -0,0 +1,15 @@
 shell_init.c 28 interactive shell settings
 main.c 88 start of shell JOBC is ON
 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 [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 [ls] stack=[0] state=[4]
 token_print.c 29 13:[ls]
 main.c 64 [env] stack=[0] state=[4]
 token_print.c 29 13:[env]
 shell_exit.c 17 shell_exit()