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
This commit is contained in:
Tanguy MAZE 2019-09-20 15:49:38 +02:00
parent cf62dae53b
commit 05d8424f75
9 changed files with 220 additions and 30 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2019/09/20 11:52:03 by tmaze ### ########.fr #
# Updated: 2019/09/20 14:39:42 by tmaze ### ########.fr #
# #
#******************************************************************************#
@ -36,8 +36,13 @@ OBJDIR = objs
INCDIR = includes libft/includes
# Source files
SRC = main.c \
ms_env.c \
SRC = main.c \
cmd_env.c \
cmd_setenv.c \
cmd_unsetenv.c \
cmd_cd.c \
cmd_echo.c \
ms_env.c
OBJ = $(SRC:.c=.o)

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 12:00:04 by tmaze ### ########.fr */
/* Updated: 2019/09/20 14:36:35 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,20 @@
# include <sys/stat.h>
# include "libft.h"
# define S_BIN 5
typedef struct s_builtin
{
char *cmd;
int (*f)(char **argv, t_list **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);
void lstdelenvelem(void *content, size_t size);
t_list *env2lst(char **env);

20
srcs/cmd_cd.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_cd(char** argv, t_list** env)
{
(void)argv;
(void)env;
return (0);
}

20
srcs/cmd_echo.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_echo(char** argv, t_list** env)
{
(void)argv;
(void)env;
return (0);
}

35
srcs/cmd_env.c Normal file
View File

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void put_env(t_list *env)
{
t_list *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_putchar('\n');
it = it->next;
}
}
int cmd_env(char** argv, t_list** env)
{
if (!argv[1])
put_env(*env);
return (0);
}

20
srcs/cmd_setenv.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_setenv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_setenv(char** argv, t_list** env)
{
(void)argv;
(void)env;
return (0);
}

20
srcs/cmd_unsetenv.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_unsetenv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_unsetenv(char** argv, t_list** env)
{
(void)argv;
(void)env;
return (0);
}

View File

@ -6,32 +6,22 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
/* Updated: 2019/09/20 11:58:04 by tmaze ### ########.fr */
/* Updated: 2019/09/20 14:49:03 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void put_env(t_list *env)
{
t_list *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_putchar('\n');
it = it->next;
}
}
int main(void)
{
extern char **environ;
t_list *env;
char *cmd;
extern char **environ;
static t_builtin built[S_BIN] = {{"env", &cmd_env}, {"cd", &cmd_cd}
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
, {"unsetenv", &cmd_unsetenv}};
char **argv;
t_list *env;
char *cmd;
int i;
env = NULL;
if ((env = env2lst(environ)) == NULL)
@ -40,15 +30,36 @@ int main(void)
{
cmd = NULL;
ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET);
if (ft_getline(&cmd) >= 0)
if (ft_getline(&cmd) >= 0 && cmd != NULL
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
{
ft_printf("%s\n", cmd);
if (ft_strequ(cmd, "env"))
put_env(env);
ft_strdel(&cmd);
if (ft_strequ(argv[0], "exit"))
{
ft_del_words_tables(&argv);
ft_lstdel(&env, &lstdelenvelem);
return (0);
}
i = 0;
while (i < S_BIN)
{
if (ft_strequ(argv[0], built[i].cmd))
{
built[i].f(argv, &env);
break ;
}
i++;
}
if (i == S_BIN)
{
// exec cmd
ft_printf("exec %s...\n", argv[0]);
}
}
ft_strdel(&cmd);
break ;
ft_del_words_tables(&argv);
}
ft_del_words_tables(&argv);
ft_strdel(&cmd);
ft_lstdel(&env, &lstdelenvelem);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 11:43:53 by tmaze #+# #+# */
/* Updated: 2019/09/20 12:00:38 by tmaze ### ########.fr */
/* Updated: 2019/09/20 15:32:46 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -41,3 +41,48 @@ t_list *env2lst(char **env)
}
return (ret);
}
t_list *env_cpy(t_list *env)
{
t_list *it;
t_list *new;
t_list *cpy;
int i;
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);
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);
}
}
}