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,13 +6,13 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
#define S_BIN 2
#define S_BIN 4
char **envlsttotab(t_list *env)
{
@@ -47,22 +47,22 @@ char **envlsttotab(t_list *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;
char **env_tab;
size_t i;
static t_builtin builtins[S_BIN] = {{"echo", &cmd_echo},
{"cd", &cmd_cd}};
{"cd", &cmd_cd}, {"env", &cmd_env},
{"setenv", &cmd_setenv}};
i = 0;
while (i < S_BIN)
{
if (ft_strcmp(argv[0], builtins[i].cmd) == 0)
return ((*(builtins[i].f))(argv, env));
i++;
}
if ((env_tab = envlsttotab(env)) == NULL)
if (ft_strcmp(argv[0], builtins[i++].cmd) == 0)
return ((*(builtins[i - 1].f))(argv, env));
if ((ret = access(argv[0], X_OK)) == -1)
ft_putendl("minishell: file not found or not executable");
if (ret == -1 || (env_tab = envlsttotab(*env)) == NULL)
return (-1);
if ((ret = fork()) == 0)
{
@@ -72,10 +72,9 @@ int exec_cmd(char **argv, t_list *env)
exit(-1);
}
else if (ret == -1)
{
ft_putendl_fd("minishell: error", 2);
if (ret == -1)
return (-1);
}
waitpid(ret, NULL, 0);
ft_del_words_tables(&env_tab);
return (0);