[ADD] multiple pipe
This commit is contained in:
parent
149bafef88
commit
9d70f4ccb2
4 changed files with 72 additions and 17 deletions
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* include.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 14:15:12 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/26 12:33:20 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/05/13 10:22:54 by jschaft ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -18,6 +18,7 @@
|
|||
# include <errno.h>
|
||||
# include <string.h>
|
||||
# include <sys/types.h>
|
||||
# include <sys/wait.h>
|
||||
# include <unistd.h>
|
||||
# include <fcntl.h>
|
||||
# include <stdlib.h>
|
||||
|
@ -51,6 +52,14 @@ typedef struct s_command
|
|||
int output_fd; // fd to use with '>' redirection (1 by default)
|
||||
} t_command;
|
||||
|
||||
// To make a list of pipe when needed
|
||||
typedef struct s_pipes
|
||||
{
|
||||
int pipe[2]; // simple pipe
|
||||
int i; //index of the pipe
|
||||
int inout; //in case of first or last pipe
|
||||
} t_pipes;
|
||||
|
||||
// Return the t_command representing the command given by the user.
|
||||
// If error, return a t_command wth the value .error = true.
|
||||
t_command parse_command(const t_memclass mc, const char *command);
|
||||
|
@ -70,6 +79,8 @@ 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.
|
||||
|
|
11
src/error.c
11
src/error.c
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* error.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 15:51:56 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/25 13:41:57 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/05/13 09:39:38 by jschaft ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -28,3 +28,10 @@ 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);
|
||||
}
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/24 13:48:00 by jschaft #+# #+# */
|
||||
/* Updated: 2024/04/26 12:31:49 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/05/13 10:17:48 by jschaft ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -27,29 +27,43 @@ static void free_tab(char **tab)
|
|||
}
|
||||
|
||||
// TODO is t_call.argv ended by NULL?
|
||||
static void fork_exec(const char *path, t_call call, const int inout[2],
|
||||
char *const envp[])
|
||||
static void fork_exec(const char *path, t_call call, t_pipes in, t_pipes out,
|
||||
char *const envp[])
|
||||
{
|
||||
dup2(inout[0], 0);
|
||||
dup2(inout[1], 1);
|
||||
if (in.i != 0)
|
||||
dup2(in.pipe[1], 0);
|
||||
else
|
||||
dup2(in.inout, 0);
|
||||
if (out.inout == 0)
|
||||
dup2(out.pipe[0], 1);
|
||||
else
|
||||
dup2(out.inout, 1);
|
||||
execve(path, call.argv, envp);
|
||||
}
|
||||
|
||||
static int execute_call(t_memclass mc, t_call call, const int inout[2],
|
||||
char *const envp[])
|
||||
static int execute_call(t_memclass mc, t_command cmd, t_pipes in, t_pipes out,
|
||||
char *const envp[])
|
||||
{
|
||||
char **const path = get_path(envp);
|
||||
pid_t pid;
|
||||
const char *program_path;
|
||||
int status;
|
||||
int i;
|
||||
|
||||
program_path = search_path(mc, (const char **)path, call.program);
|
||||
i = in.i;
|
||||
program_path = search_path(mc, (const char **)path, cmd.calls[i].program);
|
||||
if (i == 0)
|
||||
in.inout = cmd.input_fd;
|
||||
if (out.inout == -1)
|
||||
out.inout = cmd.output_fd;
|
||||
if (!program_path)
|
||||
return (127);
|
||||
pid = fork();
|
||||
if (pid < 0)
|
||||
return (1);
|
||||
if (pid == 0)
|
||||
fork_exec(program_path, call, inout, envp);
|
||||
fork_exec(program_path, cmd.calls[i], in, out, envp);
|
||||
waitpid(pid, &status, 0);
|
||||
free_tab(path);
|
||||
return (0);
|
||||
}
|
||||
|
@ -57,7 +71,30 @@ static int execute_call(t_memclass mc, t_call call, const int inout[2],
|
|||
// TODO works with only one call (no pipe).
|
||||
int execute_command(t_memclass mc, t_command command, char *const envp[])
|
||||
{
|
||||
const int inout[] = {0, 1};
|
||||
t_pipes *pipes;
|
||||
int i;
|
||||
|
||||
return (execute_call(mc, command.calls[0], inout, envp));
|
||||
i = 0;
|
||||
while (command.calls[i].program != NULL)
|
||||
i++;
|
||||
pipes = malloc(sizeof(t_pipes) * i + 1);
|
||||
i = 0;
|
||||
while (command.calls[i].program != NULL)
|
||||
{
|
||||
if (pipe(pipes[i].pipe) < 0)
|
||||
return (exec_error());
|
||||
pipes[i].inout = 0;
|
||||
i++;
|
||||
}
|
||||
pipes[i].inout = -1;
|
||||
i = 0;
|
||||
while (command.calls[i].program != NULL)
|
||||
{
|
||||
pipes[i].i = i;
|
||||
pipes[i + 1].i = i + 1;
|
||||
execute_call(mc, command, pipes[i], pipes[i + 1], envp);
|
||||
i++;
|
||||
}
|
||||
free(pipes);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
@ -3,10 +3,10 @@
|
|||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* By: jschaft <cecile.schaft@orange.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/23 14:33:45 by mcolonna #+# #+# */
|
||||
/* Updated: 2024/04/26 12:24:58 by mcolonna ### ########.fr */
|
||||
/* Updated: 2024/04/26 14:06:48 by jschaft ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue