refactoring exec

This commit is contained in:
LuckyLaszlo
2021-12-01 17:24:32 +01:00
parent b08da252de
commit b3f74c4179
9 changed files with 60 additions and 27 deletions

View File

@@ -6,14 +6,12 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/29 12:26:08 by lperrey ### ########.fr */
/* Updated: 2021/12/01 16:07:48 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01
int exec_cmd_line(t_all *c)
{
if (!pipeline(c))
@@ -23,3 +21,9 @@ int exec_cmd_line(t_all *c)
}
return (1);
}
/*
2.9.1 Simple Commands - Command Search and Execution
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
#tag_18_09_01_01
*/

View File

@@ -6,19 +6,24 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/30 13:37:09 by lperrey ### ########.fr */
/* Updated: 2021/12/01 17:16:55 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int search_cmd_path(char *cmd_name, char **cmd_path,
char *path[]);
char *path[]);
static t_builtin_f search_builtin(char *cmd_name);
static int handle_access_error(char *file_name);
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
// faire des test sur la valeur de errno selon les cas (if directory, if pathname invalid, ...)
/*
2.8.2 Exit Status for Commands
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
#tag_18_08_02
*/
// TODO : faire des test sur la valeur de errno selon les cas (if directory, if pathname invalid, ...)
int cmd_find_access(t_cmd *cmd, char *path[])
{
@@ -29,29 +34,26 @@ int cmd_find_access(t_cmd *cmd, char *path[])
else
{
cmd->path = ft_strdup(cmd->argv[0]);
if (!cmd->path)
if (!cmd->path)
return (0);
}
}
else
{
cmd->builtin_func = search_builtin(cmd->argv[0]);
if (cmd->builtin_func)
cmd->builtin_f = search_builtin(cmd->argv[0]);
if (cmd->builtin_f)
return (1);
if (search_cmd_path(cmd->argv[0], &cmd->path, path) == -1)
return (0);
if (!cmd->path)
{
cmd->error = EXIT_CMD_NOT_FOUND;
ft_putstr_fd("minishell: ", 2);
ft_putstr_fd(cmd->argv[0], 2);
ft_putstr_fd(": command not found\n", 2);
shell_error(cmd->argv[0], ": command not found");
}
}
return (1);
}
static int search_cmd_path(char *cmd_name, char **cmd_path, char *path[])
{
int i;

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/29 12:43:52 by lperrey ### ########.fr */
/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -22,7 +22,7 @@ int pipeline(t_all *c)
return (0);
if (!pipeline_find_access(c->pipeline, c->path))
return (0);
if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_func)
if (ft_2d_arrlen(c->pipeline) == 1 && c->pipeline[0]->builtin_f)
simple_command_builtin(c->pipeline[0], c);
else
wait_subshell(pipeline_exec(c->pipeline, c));
@@ -69,8 +69,6 @@ static int pipeline_find_access(t_cmd *pipeline[], char *path[])
return (1);
}
// TODO : Change exit status as in documentation :
// https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
static pid_t pipeline_exec(t_cmd *pipeline[], t_all *c)
{
int i;

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/27 11:07:19 by lperrey ### ########.fr */
/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -37,7 +37,7 @@ int simple_command_builtin(t_cmd *cmd, t_all *c)
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
}
set_last_exit_status(cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
set_last_exit_status(cmd->builtin_f(ft_2d_arrlen(cmd->argv), cmd->argv, c));
if (!restore_stdio(stdin_dup, stdout_dup))
return (EXIT_FAILURE);
return (EXIT_SUCCESS);

View File

@@ -6,7 +6,7 @@
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/16 01:57:38 by lperrey #+# #+# */
/* Updated: 2021/11/29 12:43:55 by lperrey ### ########.fr */
/* Updated: 2021/12/01 16:49:37 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
@@ -28,8 +28,8 @@ int cmd_exec_in_subshell(t_cmd *cmd, t_all *c)
if (dup2(cmd->fd_out, STDOUT_FILENO) == -1)
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
close_pipeline_fd(c->pipeline);
if (cmd->builtin_func)
exit_free(c, cmd->builtin_func(ft_2d_arrlen(cmd->argv), cmd->argv, c));
if (cmd->builtin_f)
exit_free(c, cmd->builtin_f(ft_2d_arrlen(cmd->argv), cmd->argv, c));
else if (execve(cmd->path, cmd->argv, environ) == -1)
return (ft_reti_perror_io(EXIT_FAILURE, "execve() ", cmd->argv[0]));
}