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,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */ /* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
/* Updated: 2019/09/20 14:36:35 by tmaze ### ########.fr */ /* Updated: 2019/09/26 16:53:31 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,19 +19,28 @@
# define S_BIN 5 # define S_BIN 5
typedef struct s_builtin typedef struct s_env
{ {
char *cmd; char *key;
int (*f)(char **argv, t_list **env); char *val;
} t_builtin; struct s_env *next;
} t_env;
int cmd_env(char** argv, t_list** env); typedef struct s_builtin
int cmd_setenv(char** argv, t_list** env); {
int cmd_unsetenv(char** argv, t_list** env); char *cmd;
int cmd_cd(char** argv, t_list** env); int (*f)(char **argv, t_env **env);
int cmd_echo(char** argv, t_list** env); } t_builtin;
void lstdelenvelem(void *content, size_t size); int cmd_env(char **argv, t_env **env);
t_list *env2lst(char **env); int cmd_setenv(char **argv, t_env **env);
int cmd_unsetenv(char **argv, t_env **env);
int cmd_cd(char **argv, t_env **env);
int cmd_echo(char **argv, t_env **env);
void lstdelelem(t_env **elem);
void lstdel(t_env **env);
t_env *lstnew(char *env);
t_env *lstaddend(t_env **alst, t_env *new);
#endif #endif

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:42:42 by tmaze #+# #+# */ /* Created: 2019/09/20 14:42:42 by tmaze #+# #+# */
/* Updated: 2019/09/20 14:43:57 by tmaze ### ########.fr */ /* Updated: 2019/09/26 15:26:44 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int cmd_cd(char** argv, t_list** env) int cmd_cd(char** argv, t_env** env)
{ {
(void)argv; (void)argv;
(void)env; (void)env;

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:42:30 by tmaze #+# #+# */ /* Created: 2019/09/20 14:42:30 by tmaze #+# #+# */
/* Updated: 2019/09/20 14:43:52 by tmaze ### ########.fr */ /* Updated: 2019/09/26 15:26:53 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int cmd_echo(char** argv, t_list** env) int cmd_echo(char** argv, t_env** env)
{ {
(void)argv; (void)argv;
(void)env; (void)env;

View File

@ -6,28 +6,28 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */ /* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */
/* Updated: 2019/09/20 15:02:22 by tmaze ### ########.fr */ /* Updated: 2019/09/26 15:20:58 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static void put_env(t_list *env) static void put_env(t_env *env)
{ {
t_list *it; t_env *it;
it = env; it = env;
while (it) while (it)
{ {
ft_printf("%s=", ((char**)it->content)[0]); ft_printf("%s=", it->key);
if (((char**)it->content)[1]) if (it->val)
ft_printf("%s", ((char**)it->content)[1]); ft_printf("%s", it->val);
ft_putchar('\n'); ft_putchar('\n');
it = it->next; it = it->next;
} }
} }
int cmd_env(char** argv, t_list** env) int cmd_env(char** argv, t_env** env)
{ {
if (!argv[1]) if (!argv[1])
put_env(*env); put_env(*env);

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:41:08 by tmaze #+# #+# */ /* Created: 2019/09/20 14:41:08 by tmaze #+# #+# */
/* Updated: 2019/09/20 14:43:47 by tmaze ### ########.fr */ /* Updated: 2019/09/26 15:27:06 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int cmd_setenv(char** argv, t_list** env) int cmd_setenv(char** argv, t_env** env)
{ {
(void)argv; (void)argv;
(void)env; (void)env;

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:42:01 by tmaze #+# #+# */ /* Created: 2019/09/20 14:42:01 by tmaze #+# #+# */
/* Updated: 2019/09/20 14:43:41 by tmaze ### ########.fr */ /* Updated: 2019/09/26 15:27:17 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int cmd_unsetenv(char** argv, t_list** env) int cmd_unsetenv(char** argv, t_env** env)
{ {
(void)argv; (void)argv;
(void)env; (void)env;

View File

@ -6,12 +6,35 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */ /* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
/* Updated: 2019/09/20 14:49:03 by tmaze ### ########.fr */ /* Updated: 2019/09/26 16:48:49 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
t_env *env2lst(char **env)
{
t_env *ret;
t_env *new;
int i;
i = 0;
ret = NULL;
while (env[i])
{
if ((new = lstnew(env[i])) != NULL)
lstaddend(&ret, new);
else
{
lstdelelem(&new);
lstdel(&ret);
return (NULL);
}
i++;
}
return (ret);
}
int main(void) int main(void)
{ {
extern char **environ; extern char **environ;
@ -19,7 +42,7 @@ int main(void)
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo} , {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
, {"unsetenv", &cmd_unsetenv}}; , {"unsetenv", &cmd_unsetenv}};
char **argv; char **argv;
t_list *env; t_env *env;
char *cmd; char *cmd;
int i; int i;
@ -37,7 +60,7 @@ int main(void)
if (ft_strequ(argv[0], "exit")) if (ft_strequ(argv[0], "exit"))
{ {
ft_del_words_tables(&argv); ft_del_words_tables(&argv);
ft_lstdel(&env, &lstdelenvelem); lstdel(&env);
return (0); return (0);
} }
i = 0; i = 0;
@ -60,6 +83,6 @@ int main(void)
} }
ft_del_words_tables(&argv); ft_del_words_tables(&argv);
ft_strdel(&cmd); ft_strdel(&cmd);
ft_lstdel(&env, &lstdelenvelem); lstdel(&env);
return (0); return (0);
} }

View File

@ -6,83 +6,65 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 11:43:53 by tmaze #+# #+# */ /* 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" #include "minishell.h"
void lstdelenvelem(void *content, size_t size) void lstdelelem(t_env **elem)
{ {
size = 0; if (elem)
ft_strdel(&(((char**)content)[1])); {
ft_strdel(&(((char**)content)[0])); ft_strdel(&((*elem)->key));
ft_memdel(&content); ft_strdel(&((*elem)->val));
ft_memdel((void**)elem);
}
} }
t_list *env2lst(char **env) void lstdel(t_env **env)
{ {
t_list *ret; t_env *tmp;
t_list *new;
while (*env)
{
tmp = *env;
*env = (*env)->next;
lstdelelem(&tmp);
}
}
t_env *lstnew(char *env)
{
t_env *ret;
int i; int i;
i = 0; i = 0;
ret = NULL; ret = NULL;
while (env[i]) while (env[i] && 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);
}
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); return (ret);
} }
t_list *env_cpy(t_list *env) t_env *lstaddend(t_env **alst, t_env *new)
{ {
t_list *it; t_env *tmp;
t_list *new;
t_list *cpy;
int i;
it = env; if (new == NULL)
cpy = NULL; return (NULL);
while (it) if (*alst == NULL)
*alst = new;
else
{ {
while (((char**)it->content)[i]) tmp = *alst;
i++; while (tmp->next)
i++; tmp = tmp->next;
if ((new = (t_list*)ft_memalloc(sizeof(t_list))) == NULL tmp->next = new;
|| (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);
}
} }
return (*alst);
} }

25
srcs/ms_exec.c Normal file
View File

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
/* Updated: 2019/09/26 16:43:05 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *check_path(char *cmd, char *path)
{
char **splitpath;
int i;
if ((splitpath = ft_strsplit(path, ':')) != NULL)
{
i = 0;
}
return (NULL);
}