added basic env & setenv commands

This commit is contained in:
Tanguy MAZE 2019-01-13 19:03:08 +01:00
parent b60be696b0
commit 36d7fc342d
10 changed files with 228 additions and 24 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ # # By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2018/11/22 14:43:33 by tmaze #+# #+# # # Created: 2018/11/22 14:43:33 by tmaze #+# #+# #
# Updated: 2019/01/12 15:37:35 by tmaze ### ########.fr # # Updated: 2019/01/13 17:03:41 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#
@ -16,7 +16,7 @@ CCSTD :=
NAME := minishell NAME := minishell
SRCS := main.c exec.c cmd_echo.c cmd_cd.c SRCS := main.c exec.c cmd_echo.c cmd_cd.c cmd_env.c cmd_setenv.c ms_env.c
OBJS_DIR := objs OBJS_DIR := objs
OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:.c=.o)) OBJS := $(addprefix $(OBJS_DIR)/, $(SRCS:.c=.o))
INCLS := -Iincludes -Ilibft INCLS := -Iincludes -Ilibft

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/01/10 13:52:54 by tmaze ### ########.fr */ /* Updated: 2019/01/13 17:03:08 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@
typedef struct s_builtin typedef struct s_builtin
{ {
char *cmd; char *cmd;
int (*f)(char **argv, t_list *env); int (*f)(char **argv, t_list **env);
} t_builtin; } t_builtin;
typedef struct s_envelem typedef struct s_envelem
@ -29,8 +29,17 @@ typedef struct s_envelem
char *val; char *val;
} t_envelem; } t_envelem;
int exec_cmd(char **argv, t_list *env); t_envelem *elem_new(char *key, char *val);
int cmd_echo(char **argv, t_list *env); t_envelem *env_getelemfromkey(char *key, t_list *env);
int cmd_cd(char **argv, t_list *env); t_envelem *env_addupdate(char *key, char *val, t_list **env);
void env_delelem(char *key, t_list **env);
void ft_lstdelenvelem(void *content, size_t size);
int exec_cmd(char **argv, t_list **env);
int cmd_echo(char **argv, t_list **env);
int cmd_cd(char **argv, t_list **env);
int cmd_env(char **argv, t_list **env);
int cmd_setenv(char **argv, t_list **env);
#endif #endif

2
libft

@ -1 +1 @@
Subproject commit 2e3500751ebc7b13fe478fd1d16ba3bde1000a08 Subproject commit 19d3e23b743dc95d0301579d1c5a0b97b36695aa

45
srcs/cmd_cd.c Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
/* Updated: 2019/01/13 17:00:19 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_cd(char **argv, t_list **env)
{
int i;
int ret;
char *path;
t_list *tmp;
ret = 0;
i = 0;
tmp = *env;
path = NULL;
if (!argv[1])
{
while (tmp )
{
if (ft_strcmp(((t_envelem*)(tmp->content))->key, "HOME") == 0)
{
path = ((t_envelem*)(tmp->content))->val;
break ;
}
tmp = tmp->next;
}
if (path == NULL)
return (1);
}
else
path = argv[1];
ft_putendl(path);
ret = chdir(path);
return (ret);
}

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/27 16:14:07 by tmaze #+# #+# */ /* Created: 2018/11/27 16:14:07 by tmaze #+# #+# */
/* Updated: 2019/01/09 15:13:21 by tmaze ### ########.fr */ /* Updated: 2019/01/13 16:58:14 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int cmd_echo(char **argv, t_list *env) int cmd_echo(char **argv, t_list **env)
{ {
size_t i; size_t i;

30
srcs/cmd_env.c Normal file
View File

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/14 15:54:45 by tmaze #+# #+# */
/* Updated: 2019/01/13 17:48:51 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_env(char **argv, t_list **env)
{
t_list *tmp;
if (argv[1] != NULL)
return (0);
tmp = *env;
while (tmp)
{
ft_putstr(((t_envelem*)(tmp->content))->key);
ft_putchar('=');
ft_putendl(((t_envelem*)(tmp->content))->val);
tmp = tmp->next;
}
return (0);
}

22
srcs/cmd_setenv.c Normal file
View File

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd_setenv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/13 16:20:20 by tmaze #+# #+# */
/* Updated: 2019/01/13 17:05:05 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int cmd_setenv(char **argv, t_list **env)
{
if (argv[1] == NULL)
return (-1);
if (env_addupdate(argv[1], argv[2], env) == NULL)
return (-1);
return (0);
}

View File

@ -6,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/27 15:32:29 by tmaze #+# #+# */ /* Created: 2018/11/27 15:32:29 by tmaze #+# #+# */
/* Updated: 2019/01/12 17:54:54 by tmaze ### ########.fr */ /* Updated: 2019/01/13 17:02:33 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#define S_BIN 2 #define S_BIN 4
char **envlsttotab(t_list *env) char **envlsttotab(t_list *env)
{ {
@ -47,22 +47,22 @@ char **envlsttotab(t_list *env)
** , {"setenv", &cmd_senv}, {"unsetenv", &cmd_senv}, {"env", &cmd_env}}; ** , {"setenv", &cmd_senv}, {"unsetenv", &cmd_senv}, {"env", &cmd_env}};
*/ */
int exec_cmd(char **argv, t_list *env) int exec_cmd(char **argv, t_list **env)
{ {
int ret; int ret;
char **env_tab; char **env_tab;
size_t i; size_t i;
static t_builtin builtins[S_BIN] = {{"echo", &cmd_echo}, static t_builtin builtins[S_BIN] = {{"echo", &cmd_echo},
{"cd", &cmd_cd}}; {"cd", &cmd_cd}, {"env", &cmd_env},
{"setenv", &cmd_setenv}};
i = 0; i = 0;
while (i < S_BIN) while (i < S_BIN)
{ if (ft_strcmp(argv[0], builtins[i++].cmd) == 0)
if (ft_strcmp(argv[0], builtins[i].cmd) == 0) return ((*(builtins[i - 1].f))(argv, env));
return ((*(builtins[i].f))(argv, env)); if ((ret = access(argv[0], X_OK)) == -1)
i++; ft_putendl("minishell: file not found or not executable");
} if (ret == -1 || (env_tab = envlsttotab(*env)) == NULL)
if ((env_tab = envlsttotab(env)) == NULL)
return (-1); return (-1);
if ((ret = fork()) == 0) if ((ret = fork()) == 0)
{ {
@ -72,10 +72,9 @@ int exec_cmd(char **argv, t_list *env)
exit(-1); exit(-1);
} }
else if (ret == -1) else if (ret == -1)
{
ft_putendl_fd("minishell: error", 2); ft_putendl_fd("minishell: error", 2);
if (ret == -1)
return (-1); return (-1);
}
waitpid(ret, NULL, 0); waitpid(ret, NULL, 0);
ft_del_words_tables(&env_tab); ft_del_words_tables(&env_tab);
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/18 13:09:55 by tmaze #+# #+# */ /* Created: 2018/11/18 13:09:55 by tmaze #+# #+# */
/* Updated: 2019/01/12 17:26:33 by tmaze ### ########.fr */ /* Updated: 2019/01/13 16:59:24 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -126,7 +126,7 @@ int main(void)
return (2); return (2);
} }
ft_strdel(&cmd); ft_strdel(&cmd);
exec_cmd(tab_cmd, lst_env); exec_cmd(tab_cmd, &lst_env);
ft_del_words_tables(&tab_cmd); ft_del_words_tables(&tab_cmd);
} }
ft_lstdel(&lst_env, &ft_lstdelenvelem); ft_lstdel(&lst_env, &ft_lstdelenvelem);

99
srcs/ms_env.c Normal file
View File

@ -0,0 +1,99 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/01/13 14:44:20 by tmaze #+# #+# */
/* Updated: 2019/01/13 19:02:04 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_envelem *elem_new(char *key, char *val)
{
t_envelem *elem;
if ((elem = (t_envelem*)ft_memalloc(sizeof(t_envelem))) == NULL)
return (NULL);
if ((elem->key = ft_strdup(key)) == NULL)
{
ft_memdel((void**)&elem);
return (NULL);
}
if (val != NULL && (elem->val = ft_strdup(val)) == NULL)
{
ft_strdel(&(elem->key));
ft_memdel((void**)&elem);
return (NULL);
}
elem->val = (val == NULL) ? NULL : elem->val;
return (elem);
}
t_envelem *env_getelemfromkey(char *key, t_list *env)
{
t_list *tmp;
tmp = env;
while (tmp)
{
if (ft_strcmp(((t_envelem*)(tmp->content))->key, key) == 0)
return ((t_envelem*)(tmp->content));
tmp = tmp->next;
}
return (NULL);
}
t_envelem *env_addupdate(char *key, char *val, t_list **env)
{
t_list *new;
t_envelem *elem;
char *tmp;
if ((elem = env_getelemfromkey(key, *env)) != NULL)
{
if (val == NULL)
tmp = val;
else if ((tmp = ft_strdup(val)) == NULL)
return (NULL);
ft_strdel(&(elem->val));
elem->val = tmp;
return (elem);
}
if ((elem = elem_new(key, val)) == NULL
|| (new = ft_lstnew(elem, sizeof(t_envelem))) == NULL)
return (NULL);
ft_lstaddend(env, new);
ft_memdel((void**)&elem);
return ((t_envelem*)(new->content));
}
void env_delelem(char *key, t_list **env)
{
t_list *tmp;
t_list *prec;
if (ft_strcmp(((t_envelem*)((*env)->content))->key, key) == 0)
{
tmp = *env;
*env = (*env)->next;
ft_lstdelone(&tmp, &ft_lstdelenvelem);
return ;
}
tmp = (*env)->next;
prec = (*env);
while (tmp)
{
if (ft_strcmp(((t_envelem*)(tmp->content))->key, key) == 0)
{
prec->next = tmp->next;
ft_lstdelone(&tmp, &ft_lstdelenvelem);
return ;
}
prec = tmp;
tmp = tmp->next;
}
}