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:
54
srcs/exec.c
54
srcs/exec.c
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user