This commit is contained in:
Alexandre POMMIER 2022-01-21 08:10:02 +01:00
parent 719711ee19
commit f6d338cd4d
5 changed files with 141 additions and 71 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/07 00:54:12 by apommier #+# #+# */
/* Updated: 2022/01/17 11:34:08 by apommier ### ########.fr */
/* Updated: 2022/01/21 08:09:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -71,7 +71,7 @@ char **ft_split(char const *s, char c)
while (s[i] == c && s[i])
i++;
}
dest = (char **)malloc(sizeof(char *) * (i + j));
dest = (char **)calloc(sizeof(char *), (i + j));
if (!dest)
return (0);
dest[j] = 0;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/20 21:44:01 by apommier #+# #+# */
/* Updated: 2022/01/20 21:57:45 by apommier ### ########.fr */
/* Updated: 2022/01/21 08:07:04 by apommier ### ########.fr */
/* */
/* ************************************************************************** */

111
main.c
View File

@ -6,14 +6,13 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/19 12:45:03 by apommier #+# #+# */
/* Updated: 2022/01/21 03:56:28 by apommier ### ########.fr */
/* Updated: 2022/01/21 08:02:42 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
#include <stdio.h>
char **get_path(char **envp)
char **get_path(char **env)
{
int i;
int j;
@ -24,12 +23,16 @@ char **get_path(char **envp)
j = 0;
line = 0;
swap = 0;
while (envp[i++] && envp[i])
while (env[i++] && env[i])
{
if (!ft_strncmp(envp[i], "PATH=", 5))
if (!ft_strncmp(env[i], "PATH=", 5))
{
swap = ft_substr(envp[i], 5, ft_strlen(envp[i]));
swap = ft_substr(env[i], 5, ft_strlen(env[i]));
if (!swap)
exit(1);
line = ft_split(swap, ':');
if (!line)
exit(1);
free(swap);
return (line);
}
@ -38,7 +41,7 @@ char **get_path(char **envp)
return (0);
}
char *get_command(char *exec, char **envp)
char *get_command(char **exec, char **env)
{
char **path;
char *cmd;
@ -46,91 +49,97 @@ char *get_command(char *exec, char **envp)
int i;
i = 0;
swap = 0;
cmd = 0;
path = get_path(envp);
swap = 0;
path = get_path(env);
if (exec[0][0] != '/')
swap = ft_strjoin(path[i], "/");
else
swap = ft_strdup(path[i]);
while (access(swap, F_OK) && path[i++])
{
if (swap)
free(swap);
swap = ft_strjoin(path[i], "/");
cmd = swap;
swap = ft_strjoin(swap, exec);
swap = ft_strjoin(swap, exec[0]);
free(cmd);
}
if (!path[i])
return (0);
exit_command(path, exec, swap, i);
return (swap);
}
void child_process(char **envp, char **argv, int *end)
void child_process(int fd1, char **env, char **argv, int *end)
{
int fd1;
char **exec;
char *cmd1;
char *cmd;
int i;
fd1 = open(argv[1], O_RDONLY);
exec = ft_split(argv[2], ' ');
cmd1 = get_command(exec[0], envp);
i = 0;
if (dup2(end[1], 1) == -1)
{
perror("Error");
exit(1);
}
error("dup2 fail");
if (dup2(fd1, 0) == -1)
{
perror("Error");
exit(1);
}
error("dup2 fail");
close(end[0]);
close(fd1);
if (execve(cmd1, exec, envp) == -1)
perror("Error");
free(cmd1);
exec = ft_split(argv[2], ' ');
cmd = get_command(exec, env);
if (execve(cmd, exec, env) == -1)
{
free_double(exec);
free(cmd);
error("Execve went wrong");
}
free_double(exec);
free(cmd);
}
void parent_process(char **env, char **argv, int *end)
void parent_process(int fd2, char **env, char **argv, int *end)
{
char **exec;
char *cmd2;
int status;
int fd2;
waitpid(-1, &status, 0);
exec = ft_split(argv[3], ' ');
fd2 = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0666);
cmd2 = get_command(exec[0], env);
if (dup2(end[0], 0) == -1)
exit(1);
error("dup2 fail");
if (dup2(fd2, 1) == -1)
exit(1);
error("dup2 fail");
close(end[1]);
close(fd2);
exec = ft_split(argv[3], ' ');
cmd2 = get_command(exec, env);
if (execve(cmd2, exec, env) == -1)
perror("Error");
{
free_double(exec);
free(cmd2);
error("Execve went wrong");
}
free_double(exec);
free(cmd2);
}
int main(int argc, char **argv, char **envp)
int main(int argc, char **argv, char **env)
{
int end[2];
char *cmd1;
char *cmd2;
pid_t parent;
int fd1;
int fd2;
if (argc != 5)
error("Bad number of argument");
fd1 = open(argv[1], O_RDONLY);
fd2 = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0666);
if (fd2 == -1 || fd1 == -1)
{
error("Bad file descriptor");
}
pipe(end);
parent = fork();
if (parent < 0)
{
printf("here\n");
perror("Error");
return (0);
}
error("Fork Error");
if (!parent)
child_process(envp, argv, end);
child_process(fd1, env, argv, end);
else
parent_process(envp, argv, end);
return (0);
parent_process(fd2, env, argv, end);
exit(1);
}

19
pipex.h
View File

@ -6,19 +6,30 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/19 12:45:13 by apommier #+# #+# */
/* Updated: 2022/01/21 03:00:34 by apommier ### ########.fr */
/* Updated: 2022/01/21 08:07:31 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIPEX_H
# define PIPEX_H
# include <sys/types.h>
# include <sys/stat.h>
# include <fcntl.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <stdio.h>
# include <errno.h>
# include "./libft/libft.h"
char **get_path(char **envp);
char *get_command(char *exec, char **envp);
char *get_command(char **exec, char **envp);
void pipex(char **envp, char **argv);
void child_process(char **envp, char **argv, int *pipe_tab);
void parent_process(char **envp, char **argv, int *pipe_tab);
void child_process(int fd1, char **envp, char **argv, int *pipe_tab);
void parent_process(int fd2, char **envp, char **argv, int *pipe_tab);
void error(char *error);
void exit_command(char **path, char **exec, char *swap, int i);
void free_double(char **tab);
#endif

50
utils.c Normal file
View File

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/21 08:03:23 by apommier #+# #+# */
/* Updated: 2022/01/21 08:04:01 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
void error(char *error)
{
perror(error);
exit(1);
}
void exit_command(char **path, char **exec, char *swap, int i)
{
if (!path[i])
{
i = 0;
while (path[i])
free(path[i++]);
free(path);
i = 0;
while (exec[i])
free(exec[i++]);
free(exec);
free(swap);
error("command not found");
}
i = 0;
while (path[i++])
free(path[i]);
free(path);
}
void free_double(char **tab)
{
int i;
i = 0;
while (tab[i++])
free(tab[i]);
free(tab);
}