feature: added env, setenv & unsetenv builtins

added -i option & command execution to env
added setenv & unsetenv builtins
This commit is contained in:
Tanguy Maze
2019-10-10 16:56:40 +02:00
parent 58459bc0c1
commit d22bc173f1
4 changed files with 156 additions and 78 deletions

View File

@@ -6,7 +6,7 @@
/* 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/26 15:20:58 by tmaze ### ########.fr */ /* Updated: 2019/10/10 13:49:33 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -27,9 +27,48 @@ static void put_env(t_env *env)
} }
} }
t_env *envcpy(t_env *env)
{
t_env *cpy;
cpy = NULL;
if (env != NULL && (cpy = (t_env*)ft_memalloc(sizeof(t_env))) != NULL)
if ((cpy->key = ft_strdup(env->key)) == NULL
|| (cpy->val = ft_strdup(env->val)) == NULL)
lstdelelem(&cpy);
return (cpy);
}
int cmd_env(char** argv, t_env** env) int cmd_env(char** argv, t_env** env)
{ {
if (!argv[1]) int i;
put_env(*env); t_env *it;
t_env *new;
t_env *env_cp;
i = 1;
env_cp = NULL;
if (!ft_strequ(argv[i] , "-i") && (env != NULL))
{
it = *env;
while (it)
{
if ((new = envcpy(it)) == NULL)
{
lstdel(&env_cp);
ft_putstr_fd("minishell: env: Env copy error\n", 2);
return (2);
}
lstaddend(&env_cp, new);
it = it->next;
}
}
if (ft_strequ(argv[i] , "-i"))
i++;
if (argv[i])
exec_cmd(argv + i, &env_cp);
else
put_env(env_cp);
lstdel(&env_cp);
return (0); return (0);
} }

View File

@@ -6,7 +6,7 @@
/* 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/26 15:27:06 by tmaze ### ########.fr */ /* Updated: 2019/10/10 14:31:47 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -14,7 +14,17 @@
int cmd_setenv(char** argv, t_env** env) int cmd_setenv(char** argv, t_env** env)
{ {
(void)argv; t_env *new;
(void)env;
return (0); if (argv[1] && (new = lstnew(argv[1])) != NULL)
{
lstaddend(env, new);
return (0);
} else if (argv[1]) {
ft_printf("minishell: setenv: memory error\n");
return (2);
} else {
printf("usage: setenv [KEY]=[value]\n");
return (1);
}
} }

View File

@@ -6,7 +6,7 @@
/* 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/26 15:27:17 by tmaze ### ########.fr */ /* Updated: 2019/10/10 16:05:56 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -14,7 +14,26 @@
int cmd_unsetenv(char** argv, t_env** env) int cmd_unsetenv(char** argv, t_env** env)
{ {
(void)argv; t_env **it;
(void)env; t_env *tmp;
return (0);
it = env;
if (!argv[1])
{
ft_printf("usage: unsetenv [KEY]\n");
return (1);
}
while (*it)
{
if (ft_strequ((*it)->key, argv[1]))
{
tmp = *it;
*it = tmp->next;
lstdelelem(&tmp);
return (0);
}
it = &((*it)->next);
}
ft_printf("Entry %s not found\n", argv[1]);
return (1);
} }

View File

@@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */ /* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
/* Updated: 2019/09/27 16:52:50 by tmaze ### ########.fr */ /* Updated: 2019/10/10 14:05:31 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -36,26 +36,22 @@ char *check_path_slash(char *exec)
int i; int i;
struct stat info; struct stat info;
if (exec && ft_isin(exec, '/') != -1 && ft_isin(exec, '.') == -1) if ((i = access(exec, F_OK)) != 0)
put_error(exec, "no such file or directory");
if (i != 0)
return (exec);
if ((i = stat(exec, &info)) != 0)
put_error(exec, "can't determine info");
if (i != 0)
return (exec);
if (!S_ISREG(info.st_mode))
{ {
if ((i = access(exec, F_OK)) != 0) put_error(exec, "not an executable file");
put_error(exec, "no such file or directory");
if (i != 0)
return (exec);
if ((i = stat(exec, &info)) != 0)
put_error(exec, "can't determine info");
if (i != 0)
return (exec);
if (!S_ISREG(info.st_mode))
{
put_error(exec, "not an executable file");
return (exec);
}
if ((i = access(exec, X_OK)) != 0)
put_error(exec, "permission denied");
return (exec); return (exec);
} }
return (NULL); if ((i = access(exec, X_OK)) != 0)
put_error(exec, "permission denied");
return (exec);
} }
char *check_path_dot(char *exec, t_env *env) char *check_path_dot(char *exec, t_env *env)
@@ -64,31 +60,28 @@ char *check_path_dot(char *exec, t_env *env)
char *tmp; char *tmp;
char *ret; char *ret;
if (exec && exec[0] == '.') if ((path = lstgetelem("PWD", env)) == NULL
{
if ((path = lstgetelem("PWD", env)) == NULL
|| path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL) || path->val == NULL || (tmp = ft_strjoin(path->val, "/")) == NULL)
{ {
put_error(exec, "memory error"); put_error(exec, "memory error");
return (exec); return (exec);
}
if ((ret = ft_strjoin(tmp, exec)) == NULL)
put_error(exec, "memory error");
ft_strdel(&tmp);
if (ret == NULL)
return (exec);
if (access(ret, F_OK) == 0)
{
if (access(ret, X_OK) == 0)
return (ret);
put_error(exec, "permission denied");
ft_strdel(&ret);
return (exec);
}
put_error(exec, "no such file or directory");
ft_strdel(&ret);
} }
return (NULL); if ((ret = ft_strjoin(tmp, exec)) == NULL)
put_error(exec, "memory error");
ft_strdel(&tmp);
if (ret == NULL)
return (exec);
if (access(ret, F_OK) == 0)
{
if (access(ret, X_OK) == 0)
return (ret);
put_error(exec, "permission denied");
ft_strdel(&ret);
return (exec);
}
put_error(exec, "no such file or directory");
ft_strdel(&ret);
return (exec);
} }
char *check_path(char *exec, t_env *env) char *check_path(char *exec, t_env *env)
@@ -100,39 +93,56 @@ char *check_path(char *exec, t_env *env)
char *ret; char *ret;
ret = NULL; ret = NULL;
if ((ret = check_path_slash(exec)) != NULL if (exec)
|| (ret = check_path_dot(exec, env)) != NULL)
return (ret);
if ((path = lstgetelem("PATH", env)) == NULL
|| path->val == NULL
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
return (NULL);
i = 0;
while (path_elems[i])
{ {
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL) if (ft_isin(exec, '/') != -1 && ft_isin(exec, '.') == -1)
put_error(exec, "memory error");
if (tmp == NULL)
break ;
if ((ret = ft_strjoin(tmp, exec)) == NULL)
{ {
put_error(exec, "memory error"); if ((ret = check_path_slash(exec)) != NULL)
ft_strdel(&tmp); return (ret);
break ; else
return (NULL);
} }
ft_strdel(&tmp); if (exec[0] == '.')
if (access(ret, F_OK) == 0)
{ {
if (access(ret, X_OK) == 0) if ((ret = check_path_dot(exec, env)) != NULL)
return (ret);
else
return (NULL);
}
if ((path = lstgetelem("PATH", env)) == NULL
|| path->val == NULL
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
{
put_error(exec, "could not resolve path");
return (NULL);
}
i = 0;
while (path_elems[i])
{
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL)
put_error(exec, "memory error");
if (tmp == NULL)
break ; break ;
put_error(exec, "permission denied"); if ((ret = ft_strjoin(tmp, exec)) == NULL)
{
put_error(exec, "memory error");
ft_strdel(&tmp);
break ;
}
ft_strdel(&tmp);
if (access(ret, F_OK) == 0)
{
if (access(ret, X_OK) == 0)
break ;
put_error(exec, "permission denied");
}
ft_strdel(&ret);
i++;
} }
ft_strdel(&ret); if (path_elems[i] == NULL)
i++; put_error(exec, "command not found");
ft_del_words_tables(&path_elems);
} }
if (path_elems[i] == NULL)
put_error(exec, "command not found");
ft_del_words_tables(&path_elems);
return (ret); return (ret);
} }