feature: added env, setenv & unsetenv builtins
added -i option & command execution to env added setenv & unsetenv builtins
This commit is contained in:
@@ -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);
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
|
||||||
|
if (argv[1] && (new = lstnew(argv[1])) != NULL)
|
||||||
|
{
|
||||||
|
lstaddend(env, new);
|
||||||
return (0);
|
return (0);
|
||||||
|
} else if (argv[1]) {
|
||||||
|
ft_printf("minishell: setenv: memory error\n");
|
||||||
|
return (2);
|
||||||
|
} else {
|
||||||
|
printf("usage: setenv [KEY]=[value]\n");
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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;
|
||||||
|
|
||||||
|
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);
|
return (0);
|
||||||
|
}
|
||||||
|
it = &((*it)->next);
|
||||||
|
}
|
||||||
|
ft_printf("Entry %s not found\n", argv[1]);
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
@@ -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,8 +36,6 @@ 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)
|
if ((i = access(exec, F_OK)) != 0)
|
||||||
put_error(exec, "no such file or directory");
|
put_error(exec, "no such file or directory");
|
||||||
if (i != 0)
|
if (i != 0)
|
||||||
@@ -54,8 +52,6 @@ char *check_path_slash(char *exec)
|
|||||||
if ((i = access(exec, X_OK)) != 0)
|
if ((i = access(exec, X_OK)) != 0)
|
||||||
put_error(exec, "permission denied");
|
put_error(exec, "permission denied");
|
||||||
return (exec);
|
return (exec);
|
||||||
}
|
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *check_path_dot(char *exec, t_env *env)
|
char *check_path_dot(char *exec, t_env *env)
|
||||||
@@ -64,8 +60,6 @@ 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)
|
||||||
{
|
{
|
||||||
@@ -87,8 +81,7 @@ char *check_path_dot(char *exec, t_env *env)
|
|||||||
}
|
}
|
||||||
put_error(exec, "no such file or directory");
|
put_error(exec, "no such file or directory");
|
||||||
ft_strdel(&ret);
|
ft_strdel(&ret);
|
||||||
}
|
return (exec);
|
||||||
return (NULL);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
char *check_path(char *exec, t_env *env)
|
char *check_path(char *exec, t_env *env)
|
||||||
@@ -100,13 +93,29 @@ 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)
|
{
|
||||||
|
if (ft_isin(exec, '/') != -1 && ft_isin(exec, '.') == -1)
|
||||||
|
{
|
||||||
|
if ((ret = check_path_slash(exec)) != NULL)
|
||||||
return (ret);
|
return (ret);
|
||||||
|
else
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
if (exec[0] == '.')
|
||||||
|
{
|
||||||
|
if ((ret = check_path_dot(exec, env)) != NULL)
|
||||||
|
return (ret);
|
||||||
|
else
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
if ((path = lstgetelem("PATH", env)) == NULL
|
if ((path = lstgetelem("PATH", env)) == NULL
|
||||||
|| path->val == NULL
|
|| path->val == NULL
|
||||||
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
|
|| (path_elems = ft_strsplit(path->val, ':')) == NULL)
|
||||||
|
{
|
||||||
|
put_error(exec, "could not resolve path");
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
}
|
||||||
i = 0;
|
i = 0;
|
||||||
while (path_elems[i])
|
while (path_elems[i])
|
||||||
{
|
{
|
||||||
@@ -133,6 +142,7 @@ char *check_path(char *exec, t_env *env)
|
|||||||
if (path_elems[i] == NULL)
|
if (path_elems[i] == NULL)
|
||||||
put_error(exec, "command not found");
|
put_error(exec, "command not found");
|
||||||
ft_del_words_tables(&path_elems);
|
ft_del_words_tables(&path_elems);
|
||||||
|
}
|
||||||
return (ret);
|
return (ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user