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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
typedef struct s_env
{
char *key;
char *val;
struct s_env *next;
} t_env;
typedef struct s_builtin
{
char *cmd;
int (*f)(char **argv, t_list **env);
int (*f)(char **argv, t_env **env);
} t_builtin;
int cmd_env(char** argv, t_list** env);
int cmd_setenv(char** argv, t_list** env);
int cmd_unsetenv(char** argv, t_list** env);
int cmd_cd(char** argv, t_list** env);
int cmd_echo(char** argv, t_list** env);
int cmd_env(char **argv, t_env **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 lstdelenvelem(void *content, size_t size);
t_list *env2lst(char **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

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
int cmd_cd(char** argv, t_list** env)
int cmd_cd(char** argv, t_env** env)
{
(void)argv;
(void)env;

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
int cmd_echo(char** argv, t_list** env)
int cmd_echo(char** argv, t_env** env)
{
(void)argv;
(void)env;

View File

@ -6,28 +6,28 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
static void put_env(t_list *env)
static void put_env(t_env *env)
{
t_list *it;
t_env *it;
it = env;
while (it)
{
ft_printf("%s=", ((char**)it->content)[0]);
if (((char**)it->content)[1])
ft_printf("%s", ((char**)it->content)[1]);
ft_printf("%s=", it->key);
if (it->val)
ft_printf("%s", it->val);
ft_putchar('\n');
it = it->next;
}
}
int cmd_env(char** argv, t_list** env)
int cmd_env(char** argv, t_env** env)
{
if (!argv[1])
put_env(*env);

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
int cmd_setenv(char** argv, t_list** env)
int cmd_setenv(char** argv, t_env** env)
{
(void)argv;
(void)env;

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
int cmd_unsetenv(char** argv, t_list** env)
int cmd_unsetenv(char** argv, t_env** env)
{
(void)argv;
(void)env;

View File

@ -6,12 +6,35 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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)
{
extern char **environ;
@ -19,7 +42,7 @@ int main(void)
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
, {"unsetenv", &cmd_unsetenv}};
char **argv;
t_list *env;
t_env *env;
char *cmd;
int i;
@ -37,7 +60,7 @@ int main(void)
if (ft_strequ(argv[0], "exit"))
{
ft_del_words_tables(&argv);
ft_lstdel(&env, &lstdelenvelem);
lstdel(&env);
return (0);
}
i = 0;
@ -60,6 +83,6 @@ int main(void)
}
ft_del_words_tables(&argv);
ft_strdel(&cmd);
ft_lstdel(&env, &lstdelenvelem);
lstdel(&env);
return (0);
}

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)
{
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);
if (new == NULL)
return (NULL);
}
i = 0;
while (((char**)it->content)[i])
if (*alst == NULL)
*alst = new;
else
{
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);
}

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);
}