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:
Tanguy MAZE 2019-01-16 17:30:11 +01:00
parent d21269c7ef
commit dcc7d43dd9
3 changed files with 76 additions and 53 deletions

View File

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2019/01/16 13:46:47 by tmaze ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,32 +14,7 @@
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);
(void)argv;
(void)env;
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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}};
*/
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 ret;
char **env_tab;
char *path;
size_t i;
static t_builtin builtins[S_BIN] = {{"echo", &cmd_echo},
{"cd", &cmd_cd}, {"setenv", &cmd_setenv},
{"env", &cmd_env}, {"unsetenv", &cmd_unsetenv}};
i = 0;
path = NULL;
while (i < S_BIN)
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)
if ((env_tab = envlsttotab(*env)) == NULL)
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)
{
execve(argv[0], argv, env_tab);
if (path != NULL)
execve(path, argv, env_tab);
ft_putendl_fd("minishell: error", 2);
ft_strdel(&path);
ft_del_words_tables(&env_tab);
exit(-1);
}

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,6 +106,8 @@ int main(void)
ft_putstr("$> ");
ft_putstr(FT_RESET);
ret = get_next_line(1, &cmd);
if (cmd != NULL && ft_strlen(cmd) != 0)
{
if (ret == -1)
{
ft_lstdel(&lst_env, &ft_lstdelenvelem);
@ -129,6 +131,8 @@ int main(void)
exec_cmd(tab_cmd, &lst_env);
ft_del_words_tables(&tab_cmd);
}
ft_strdel(&cmd);
}
ft_lstdel(&lst_env, &ft_lstdelenvelem);
return (0);
}