feature: switched env from ft_list to specific list

This commit is contained in:
Tanguy MAZE
2019-09-26 16:54:52 +02:00
parent 05d8424f75
commit aa8e971688
9 changed files with 128 additions and 89 deletions

View File

@@ -6,83 +6,65 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 11:43:53 by tmaze #+# #+# */
/* Updated: 2019/09/20 15:32:46 by tmaze ### ########.fr */
/* Updated: 2019/09/26 16:48:37 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void lstdelenvelem(void *content, size_t size)
void lstdelelem(t_env **elem)
{
size = 0;
ft_strdel(&(((char**)content)[1]));
ft_strdel(&(((char**)content)[0]));
ft_memdel(&content);
if (elem)
{
ft_strdel(&((*elem)->key));
ft_strdel(&((*elem)->val));
ft_memdel((void**)elem);
}
}
t_list *env2lst(char **env)
void lstdel(t_env **env)
{
t_list *ret;
t_list *new;
t_env *tmp;
while (*env)
{
tmp = *env;
*env = (*env)->next;
lstdelelem(&tmp);
}
}
t_env *lstnew(char *env)
{
t_env *ret;
int i;
i = 0;
ret = NULL;
while (env[i])
{
if ((new = (t_list*)ft_memalloc(sizeof(t_list))) == NULL
|| (new->content = ft_strsplit(env[i], '=')) == NULL
|| ft_lstaddend(&ret, new) == NULL)
{
ft_lstdel(&ret, &lstdelenvelem);
return (NULL);
}
while (env[i] && env[i] != '=')
i++;
}
if (env[i] == '=' && (ret = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
if ((ret->val = ft_strsub(env, i + 1, ft_strlen(env) - i - 1)) == NULL
|| (ret->key = ft_strsub(env, 0, i)) == NULL)
lstdelelem(&ret);
return (ret);
}
t_list *env_cpy(t_list *env)
t_env *lstaddend(t_env **alst, t_env *new)
{
t_list *it;
t_list *new;
t_list *cpy;
int i;
t_env *tmp;
it = env;
cpy = NULL;
while (it)
if (new == NULL)
return (NULL);
if (*alst == NULL)
*alst = new;
else
{
while (((char**)it->content)[i])
i++;
i++;
if ((new = (t_list*)ft_memalloc(sizeof(t_list))) == NULL
|| (new->content = ft_memalloc(sizeof(char*)) * i) == NULL)
{
ft_lstdelenvelem(new->content, 0);
ft_memdel(&new);
ft_lstdel(&cpy, &lstdelenvelem);
return (NULL);
}
i = 0;
while (((char**)it->content)[i])
{
if ((((char**)new->content)[i]
= ft_strdup(((char**)it->content)[i])) == NULL)
{
ft_lstdelenvelem(new->content, 0);
ft_memdel(&new);
ft_lstdel(&cpy, &lstdelenvelem);
return (NULL);
}
}
((char**)new->content)[i] = NULL;
if (lst_addend(&cpy, new) == NULL)
{
ft_lstdelenvelem(new->content, 0);
ft_lstdel(&cpy, &lstdelenvelem);
return (NULL);
}
tmp = *alst;
while (tmp->next)
tmp = tmp->next;
tmp->next = new;
}
return (*alst);
}