Dev (#2)
* Restart from scratch branched out on dev removed all previous source files started test on new env2lst function * norm: splitted functions in main into appropriate file Moved lstdelenvelem & env2lst to ms_env.c Normed out files * Feat(WIP): Added possibility to cmd_env to execute commands with altered env. Added dummy function for cd, echo, setenv & unsetenv Started work on env copy feature * feature: switched env from ft_list to specific list * feature: added execution of commands from path * feature: added env, setenv & unsetenv builtins added -i option & command execution to env added setenv & unsetenv builtins * Clean-up: normed out files * Changed comportment on error for check_path functions * feature: added completion for ~ and ${} WIP leaks detected on ${} completion * fix leak on ext need test * feature: added echo cmd changed printf ref to ft_printf * feature: add cd built-in * Simple norm-out * moved env functions to libft * fixed out-of-memory access in extension * Resolved infinite loop in extension revolving WIP norming in cd * Normedout cmd_cd.c * Normed out functions in cmd_cd * Normed out funtions sorting out needed * removed -fsanitize from Makefile * corrected env -i crash & SHLVL * Delete norm.txt * added put_error_cd2 in put_error_cd.c * added inline environment
This commit is contained in:
167
srcs/cmd_env.c
167
srcs/cmd_env.c
@@ -5,144 +5,91 @@
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */
|
||||
/* Updated: 2020/01/30 15:41:08 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
void put_error(char *exec, char *file, char *msg)
|
||||
static void put_env(t_env *env)
|
||||
{
|
||||
ft_putstr(exec);
|
||||
ft_putstr(": ");
|
||||
ft_putstr(file);
|
||||
ft_putstr(": ");
|
||||
ft_putendl(msg);
|
||||
}
|
||||
t_env *it;
|
||||
|
||||
void put_env(t_list *env)
|
||||
{
|
||||
t_list *tmp;
|
||||
|
||||
tmp = env;
|
||||
while (tmp)
|
||||
it = env;
|
||||
while (it)
|
||||
{
|
||||
ft_putstr(((t_envelem*)(tmp->content))->key);
|
||||
ft_putchar('=');
|
||||
ft_putendl(((t_envelem*)(tmp->content))->val);
|
||||
tmp = tmp->next;
|
||||
ft_printf("%s=", it->key);
|
||||
if (it->val)
|
||||
ft_printf("%s", it->val);
|
||||
ft_putchar('\n');
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
|
||||
t_envelem *envelemdup(t_envelem *elem)
|
||||
t_env *envcpy(t_env *env)
|
||||
{
|
||||
t_envelem *ret;
|
||||
t_env *cpy;
|
||||
|
||||
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);
|
||||
cpy = NULL;
|
||||
if (env != NULL && (cpy = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
|
||||
if ((cpy->key = ft_strdup(env->key)) == NULL
|
||||
|| (cpy->val = ft_strdup(env->val)) == NULL)
|
||||
ft_envdelelem(&cpy);
|
||||
return (cpy);
|
||||
}
|
||||
|
||||
t_list *env_cpy(t_list *env)
|
||||
int env_cleanup(t_env *env_cp, int code)
|
||||
{
|
||||
t_list *ret;
|
||||
t_list *ind;
|
||||
t_list *tmp;
|
||||
t_envelem *envelem;
|
||||
ft_envdel(&env_cp);
|
||||
if (code == 2)
|
||||
ft_putstr_fd("minishell: env: Env copy error\n", 2);
|
||||
return (code);
|
||||
}
|
||||
|
||||
ret = NULL;
|
||||
ind = env;
|
||||
while (ind)
|
||||
t_env *inline_env(char **argv, int *i, t_env **env_cp)
|
||||
{
|
||||
t_env *new;
|
||||
|
||||
while (ft_strchr(argv[*i], '='))
|
||||
{
|
||||
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)
|
||||
if ((new = ft_envnew(argv[*i])) == NULL)
|
||||
{
|
||||
ft_envelemdel(&envelem);
|
||||
ft_lstdel(&ret, &ft_lstdelenvelem);
|
||||
ft_printf("env: memory error\n");
|
||||
ft_envdel(env_cp);
|
||||
break ;
|
||||
}
|
||||
ft_envelemdel(&envelem);
|
||||
ind = ind->next;
|
||||
ft_envaddend(env_cp, new);
|
||||
(*i)++;
|
||||
}
|
||||
return (ret);
|
||||
return (*env_cp);
|
||||
}
|
||||
|
||||
int exec_env(char **argv, t_list **env)
|
||||
int cmd_env(char **argv, t_env **env)
|
||||
{
|
||||
int ret;
|
||||
char **env_tab;
|
||||
char *path;
|
||||
int i;
|
||||
t_env *it;
|
||||
t_env *new;
|
||||
t_env *env_cp;
|
||||
|
||||
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)
|
||||
i = 1;
|
||||
if (!(env_cp = NULL) && !ft_strequ(argv[i], "-i") && (env != NULL))
|
||||
{
|
||||
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)
|
||||
it = *env;
|
||||
while (it)
|
||||
{
|
||||
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);
|
||||
if ((new = envcpy(it)) == NULL)
|
||||
return (env_cleanup(env_cp, 2));
|
||||
ft_envaddend(&env_cp, new);
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
ft_lstdel(&env_cp, &ft_lstdelenvelem);
|
||||
return (0);
|
||||
else if (ft_strequ(argv[i], "-i"))
|
||||
i++;
|
||||
inline_env(argv, &i, &env_cp);
|
||||
if (argv[i])
|
||||
exec_cmd(argv + i, &env_cp);
|
||||
else
|
||||
put_env(env_cp);
|
||||
return (env_cleanup(env_cp, 0));
|
||||
}
|
||||
|
Reference in New Issue
Block a user