Restart from scratch
branched out on dev removed all previous source files started test on new env2lst function
This commit is contained in:
148
srcs/cmd_env.c
148
srcs/cmd_env.c
@@ -1,148 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cmd_env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2018/12/14 15:54:45 by tmaze #+# #+# */
|
||||
/* Updated: 2019/02/01 16:26:42 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void put_error(char *exec, char *file, char *msg)
|
||||
{
|
||||
ft_putstr(exec);
|
||||
ft_putstr(": ");
|
||||
ft_putstr(file);
|
||||
ft_putstr(": ");
|
||||
ft_putendl(msg);
|
||||
}
|
||||
|
||||
void put_env(t_list *env)
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
tmp = env;
|
||||
while (tmp)
|
||||
{
|
||||
ft_putstr(((t_envelem*)(tmp->content))->key);
|
||||
ft_putchar('=');
|
||||
ft_putendl(((t_envelem*)(tmp->content))->val);
|
||||
tmp = tmp->next;
|
||||
}
|
||||
}
|
||||
|
||||
t_envelem *envelemdup(t_envelem *elem)
|
||||
{
|
||||
t_envelem *ret;
|
||||
|
||||
if ((ret = (t_envelem*)ft_memalloc(sizeof(t_envelem))) == NULL)
|
||||
return (NULL);
|
||||
if ((ret->key = ft_strdup(elem->key)) == NULL)
|
||||
ft_envelemdel(&ret);
|
||||
if (ret == NULL)
|
||||
return (0);
|
||||
if ((ret->val = ft_strdup(elem->val)) == NULL)
|
||||
{
|
||||
ft_strdel(&(ret->key));
|
||||
ft_envelemdel(&ret);
|
||||
return (NULL);
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_list *env_cpy(t_list *env)
|
||||
{
|
||||
t_list *ret;
|
||||
t_list *ind;
|
||||
t_list *tmp;
|
||||
t_envelem *envelem;
|
||||
|
||||
ret = NULL;
|
||||
ind = env;
|
||||
while (ind)
|
||||
{
|
||||
if ((envelem = envelemdup((t_envelem*)(ind->content))) == NULL)
|
||||
ft_lstdel(&ret, &ft_lstdelenvelem);
|
||||
if (envelem == NULL)
|
||||
break ;
|
||||
if ((tmp = ft_lstnew((void*)envelem, sizeof(t_envelem))) == NULL
|
||||
|| ft_lstaddend(&ret, tmp) == NULL)
|
||||
{
|
||||
ft_envelemdel(&envelem);
|
||||
ft_lstdel(&ret, &ft_lstdelenvelem);
|
||||
break ;
|
||||
}
|
||||
ft_envelemdel(&envelem);
|
||||
ind = ind->next;
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
int exec_env(char **argv, t_list **env)
|
||||
{
|
||||
int ret;
|
||||
char **env_tab;
|
||||
char *path;
|
||||
|
||||
if ((env_tab = envlsttotab(*env)) == NULL)
|
||||
return (1);
|
||||
if ((path = check_path(argv[0], *env)) == NULL)
|
||||
ft_del_words_tables(&env_tab);
|
||||
if (path == NULL)
|
||||
return (1);
|
||||
if ((ret = fork()) == 0)
|
||||
{
|
||||
execve(path, argv, env_tab);
|
||||
if (ft_strcmp(path, argv[0]) != 0)
|
||||
ft_strdel(&path);
|
||||
ft_del_words_tables(&env_tab);
|
||||
exit(1);
|
||||
}
|
||||
else if (ret == -1)
|
||||
return (1);
|
||||
waitpid(ret, NULL, 0);
|
||||
ft_del_words_tables(&env_tab);
|
||||
if (ft_strcmp(path, argv[0]) != 0)
|
||||
ft_strdel(&path);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int cmd_env(char **argv, t_list **env)
|
||||
{
|
||||
t_envelem *elem;
|
||||
t_list *env_cp;
|
||||
t_list *new;
|
||||
char *tmp;
|
||||
size_t i;
|
||||
|
||||
if (argv[1] == NULL)
|
||||
put_env(*env);
|
||||
i = 0;
|
||||
if ((env_cp = env_cpy(*env)) == NULL)
|
||||
return (2);
|
||||
while (argv[++i] && (tmp = ft_strchr(argv[i], '=')))
|
||||
{
|
||||
if ((elem = ft_envtoenvelem(argv[i])) == NULL)
|
||||
ft_lstdel(&env_cp, &ft_lstdelenvelem);
|
||||
if (elem == NULL)
|
||||
return (1);
|
||||
if ((new = ft_lstnew((void*)elem, sizeof(t_envelem))) == NULL)
|
||||
{
|
||||
ft_envelemdel(&elem);
|
||||
ft_lstdel(&env_cp, &ft_lstdelenvelem);
|
||||
return (1);
|
||||
}
|
||||
ft_memdel((void**)&elem);
|
||||
if (ft_lstaddend(&env_cp, new) == NULL)
|
||||
{
|
||||
ft_lstdel(&env_cp, &ft_lstdelenvelem);
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
ft_lstdel(&env_cp, &ft_lstdelenvelem);
|
||||
return (0);
|
||||
}
|
Reference in New Issue
Block a user