This commit is contained in:
mcolonna 2024-05-16 17:05:14 +02:00
parent 0c3d0ff95b
commit 75bbeed855
3 changed files with 34 additions and 42 deletions

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
/* Updated: 2024/05/15 18:10:34 by mcolonna ### ########.fr */
/* Updated: 2024/05/16 17:30:01 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -77,8 +77,7 @@ void minishell_error(const char *msg);
// Call to write the error and exit the program.
void fatal_error(const char *msg);
// Call to write the error and return in exec_command
int exec_error(void);
///// PATH /////
// Get the PATH values.

View file

@ -6,7 +6,7 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 15:51:56 by mcolonna #+# #+# */
/* Updated: 2024/05/13 09:39:38 by jschaft ### ########.fr */
/* Updated: 2024/05/16 17:29:43 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,10 +28,3 @@ void fatal_error(const char *msg)
print_line(err_remember, 2, msg);
exit(1);
}
// Error creating pipes in exec_command
int exec_error(void)
{
perror("Error: Unable to create pipes\n");
return (10);
}

View file

@ -6,26 +6,12 @@
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
/* Updated: 2024/05/16 13:25:23 by mcolonna ### ########.fr */
/* Updated: 2024/05/16 16:11:30 by mcolonna ### ########.fr */
/* */
/* ************************************************************************** */
#include "include.h"
// Free every element of tab, then tab itself.
static void free_tab(char **tab)
{
int i;
i = 0;
while (tab[i] != NULL)
{
free(tab[i]);
i++;
}
free(tab);
}
// Execute the call with stdin = in and stdout = out.
// Find the program in $PATH
static int execute_call(t_memclass mc, t_call call, int in, int out,
@ -37,17 +23,19 @@ static int execute_call(t_memclass mc, t_call call, int in, int out,
program_path = search_path(mc, (const char **)path, call.program);
if (!program_path)
return (127);
return (minishell_error(str_join(fatal_error, mc,
"command not found: ", str_join(fatal_error, mc,
call.program, "\n"))), 127);
pid = fork();
if (pid < 0)
return (1);
return (minishell_error("errno"), errno);
if (pid == 0)
{
dup2(in, 0);
dup2(out, 1);
if (dup2(in, 0) < 0 || dup2(out, 1) < 0)
return (minishell_error("errno"), errno);
execve(program_path, call.argv, envp);
return (minishell_error("errno"), errno);
}
free_tab(path);
return (0);
}
@ -56,6 +44,7 @@ int execute_command(t_memclass mc, t_command command, char *const envp[])
t_pipes *pipes;
int i;
int nb_calls;
int r;
// get nb calls
nb_calls = 0;
@ -63,38 +52,49 @@ int execute_command(t_memclass mc, t_command command, char *const envp[])
nb_calls++;
// create pipes
pipes = malloc(sizeof(t_pipes) * (nb_calls + 1));
pipes = mem_alloc(err_remember, mc, sizeof(t_pipes) * (nb_calls + 1));
if (err_get())
return (minishell_error("errno"), errno);
i = 0;
pipes[i].pipe[0] = command.input_fd;
while (++i < nb_calls)
if (pipe(pipes[i].pipe) < 0)
return (exec_error());
return (minishell_error("errno"), errno);
pipes[i].pipe[1] = command.output_fd;
i = -1;
// call all programs
i = -1;
while (++i < nb_calls)
execute_call(mc, command.calls[i], pipes[i].pipe[0], pipes[i + 1].pipe[1], envp);
{
r = execute_call(mc, command.calls[i], pipes[i].pipe[0], pipes[i + 1].pipe[1], envp);
if (r)
return (r);
}
// close all
i = 0;
if (pipes[i].pipe[0] != 0)
close(pipes[i].pipe[0]);
if (close(pipes[i].pipe[0]) < 0)
minishell_error("errno");
while (++i < nb_calls)
{
close(pipes[i].pipe[0]);
close(pipes[i].pipe[1]);
if (close(pipes[i].pipe[0]) < 0)
minishell_error("errno");
if (close(pipes[i].pipe[1]) < 0)
minishell_error("errno");
}
if (pipes[i].pipe[1] != 1)
close(pipes[i].pipe[1]);
if (close(pipes[i].pipe[1]) < 0)
minishell_error("errno");
// wait the end of all programs
i = nb_calls;
tf_printf("before\n");
while (wait(NULL) != -1)
;
tf_printf("in\n");
tf_printf("after\n"); // TODO debug
if (errno != ECHILD)
// TODO error
return (minishell_error("errno"), errno);
free(pipes);
return (0);
}