42-archive/42sh/src/hash_table/is_hash.c
Gautier Wojda c24e0fdb27 #221
2017-03-28 18:51:26 +02:00

49 lines
1.5 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* is_hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 11:08:40 by gwojda #+# #+# */
/* Updated: 2017/03/21 17:40:20 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *h_free_one(t_list **head, t_list *list, t_list *ref)
{
if (!((*head)->next))
ft_lstdelone(head, &ft_hash_free);
else
{
ref->next = list->next;
ft_lstdelone(&list, &ft_hash_free);
}
return (NULL);
}
char *ft_is_hash(char *cmd)
{
t_list *list;
t_list *ref;
int id;
id = ft_hash_str(cmd);
list = g_hash[id];
ref = list;
while (list)
{
if (!ft_strcmp(((t_hash *)list->content)->key, cmd))
{
if (access(((t_hash *)list->content)->path,
X_OK | F_OK) < 0)
return (h_free_one(&g_hash[id], list, ref));
return (ft_strdup(((t_hash *)list->content)->path));
}
ref = list;
list = list->next;
}
return (NULL);
}