WIP check path to launch executable
- able to launch ls from path - full user entry check (non existing exec, empty command..) still to do
This commit is contained in:
parent
d21269c7ef
commit
dcc7d43dd9
@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
|
/* Created: 2019/01/07 16:44:40 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/01/13 17:00:19 by tmaze ### ########.fr */
|
/* Updated: 2019/01/16 13:46:47 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,32 +14,7 @@
|
|||||||
|
|
||||||
int cmd_cd(char **argv, t_list **env)
|
int cmd_cd(char **argv, t_list **env)
|
||||||
{
|
{
|
||||||
int i;
|
(void)argv;
|
||||||
int ret;
|
(void)env;
|
||||||
char *path;
|
return (0);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
54
srcs/exec.c
54
srcs/exec.c
@ -6,7 +6,7 @@
|
|||||||
/* 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/15 15:11:22 by tmaze ### ########.fr */
|
/* Updated: 2019/01/16 17:26:43 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -47,27 +47,71 @@ char **envlsttotab(t_list *env)
|
|||||||
** , {"setenv", &cmd_senv}, {"unsetenv", &cmd_senv}, {"env", &cmd_env}};
|
** , {"setenv", &cmd_senv}, {"unsetenv", &cmd_senv}, {"env", &cmd_env}};
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
char *check_path(char *exec, t_list *env)
|
||||||
|
{
|
||||||
|
size_t i;
|
||||||
|
t_envelem *path;
|
||||||
|
char **path_elems;
|
||||||
|
char *tmp;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
ret = NULL;
|
||||||
|
if ((path = env_getelemfromkey("PATH", env)) == NULL
|
||||||
|
|| path->val == NULL)
|
||||||
|
return (NULL);
|
||||||
|
if ((path_elems = ft_strsplit(path->val,':')) == NULL)
|
||||||
|
return (NULL);
|
||||||
|
while (path_elems[i])
|
||||||
|
{
|
||||||
|
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL)
|
||||||
|
break ;
|
||||||
|
if ((ret = ft_strjoin(tmp, exec)) == NULL)
|
||||||
|
ft_strdel(&tmp);
|
||||||
|
if (ret == NULL)
|
||||||
|
break ;
|
||||||
|
ft_strdel(&tmp);
|
||||||
|
if (access(ret, F_OK) == 0 && access(ret, X_OK) == 0)
|
||||||
|
break ;
|
||||||
|
ft_strdel(&ret);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
ft_del_words_tables(&path_elems);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
char *path;
|
||||||
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}, {"setenv", &cmd_setenv},
|
{"cd", &cmd_cd}, {"setenv", &cmd_setenv},
|
||||||
{"env", &cmd_env}, {"unsetenv", &cmd_unsetenv}};
|
{"env", &cmd_env}, {"unsetenv", &cmd_unsetenv}};
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
path = NULL;
|
||||||
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 - 1].f))(argv, env));
|
||||||
if ((ret = access(argv[0], X_OK)) == -1)
|
if ((env_tab = envlsttotab(*env)) == NULL)
|
||||||
ft_putendl("minishell: file not found or not executable");
|
|
||||||
if (ret == -1 || (env_tab = envlsttotab(*env)) == NULL)
|
|
||||||
return (-1);
|
return (-1);
|
||||||
|
if (argv[0][0] == '/' && access(argv[0], F_OK) == 0 && access(argv[0], X_OK) == 0)
|
||||||
|
path = argv[0];
|
||||||
|
else if ((path = check_path(argv[0], *env)) == NULL)
|
||||||
|
ft_putendl_fd("minishell: error path check", 2);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_del_words_tables(&env_tab);
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
if ((ret = fork()) == 0)
|
if ((ret = fork()) == 0)
|
||||||
{
|
{
|
||||||
execve(argv[0], argv, env_tab);
|
if (path != NULL)
|
||||||
|
execve(path, argv, env_tab);
|
||||||
ft_putendl_fd("minishell: error", 2);
|
ft_putendl_fd("minishell: error", 2);
|
||||||
|
ft_strdel(&path);
|
||||||
ft_del_words_tables(&env_tab);
|
ft_del_words_tables(&env_tab);
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
42
srcs/main.c
42
srcs/main.c
@ -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/13 16:59:24 by tmaze ### ########.fr */
|
/* Updated: 2019/01/16 17:06:51 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -106,28 +106,32 @@ int main(void)
|
|||||||
ft_putstr("$> ");
|
ft_putstr("$> ");
|
||||||
ft_putstr(FT_RESET);
|
ft_putstr(FT_RESET);
|
||||||
ret = get_next_line(1, &cmd);
|
ret = get_next_line(1, &cmd);
|
||||||
if (ret == -1)
|
if (cmd != NULL && ft_strlen(cmd) != 0)
|
||||||
{
|
{
|
||||||
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
if (ret == -1)
|
||||||
|
{
|
||||||
|
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
||||||
|
ft_strdel(&cmd);
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
if (ft_strcmp("exit", cmd) == 0)
|
||||||
|
{
|
||||||
|
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
||||||
|
ft_strdel(&cmd);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else if ((tab_cmd = ft_strsplit(cmd, ' ')) == NULL)
|
||||||
|
{
|
||||||
|
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
||||||
|
ft_strdel(&cmd);
|
||||||
|
ft_putendl("error split");
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
ft_strdel(&cmd);
|
ft_strdel(&cmd);
|
||||||
return (2);
|
exec_cmd(tab_cmd, &lst_env);
|
||||||
}
|
ft_del_words_tables(&tab_cmd);
|
||||||
if (ft_strcmp("exit", cmd) == 0)
|
|
||||||
{
|
|
||||||
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
|
||||||
ft_strdel(&cmd);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
else if ((tab_cmd = ft_strsplit(cmd, ' ')) == NULL)
|
|
||||||
{
|
|
||||||
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
|
||||||
ft_strdel(&cmd);
|
|
||||||
ft_putendl("error split");
|
|
||||||
return (2);
|
|
||||||
}
|
}
|
||||||
ft_strdel(&cmd);
|
ft_strdel(&cmd);
|
||||||
exec_cmd(tab_cmd, &lst_env);
|
|
||||||
ft_del_words_tables(&tab_cmd);
|
|
||||||
}
|
}
|
||||||
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
ft_lstdel(&lst_env, &ft_lstdelenvelem);
|
||||||
return (0);
|
return (0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user