maybe leaks + no check error

This commit is contained in:
Alexandre POMMIER 2022-01-21 04:00:43 +01:00
parent ccffe2b3c8
commit 719711ee19
7 changed files with 123 additions and 85 deletions

View File

@ -26,6 +26,7 @@ SRCS = ft_memset.c \
ft_substr.c \ ft_substr.c \
ft_strtrim.c \ ft_strtrim.c \
ft_split.c \ ft_split.c \
ft_strjoin.c \
ft_itoa.c \ ft_itoa.c \
ft_strmapi.c \ ft_strmapi.c \
ft_putchar_fd.c \ ft_putchar_fd.c \

41
libft/ft_strjoin.c Normal file
View File

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* 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 */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_strjoin(char *save, char *s2)
{
char *dest;
int i;
int j;
i = 0;
j = 0;
if (!save && !s2)
return (0);
dest = malloc(ft_strlen(save) + ft_strlen(s2) + 1);
while (save && save[i])
{
dest[j] = save[i];
j++;
i++;
}
i = 0;
while (s2 && s2[i])
{
dest[j] = s2[i];
j++;
i++;
}
dest[j] = 0;
return (dest);
}

View File

@ -10,7 +10,7 @@
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../Utils/push_swap.h" #include "../pipex.h"
char *ft_free(char *save, int *end) char *ft_free(char *save, int *end)
{ {

View File

@ -6,11 +6,11 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/14 06:05:54 by apommier #+# #+# */ /* Created: 2020/12/14 06:05:54 by apommier #+# #+# */
/* Updated: 2022/01/17 21:42:00 by apommier ### ########.fr */ /* Updated: 2022/01/20 21:58:39 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../Utils/push_swap.h" #include "../pipex.h"
char *ft_strjoin(char *save, char *s2) char *ft_strjoin(char *save, char *s2)
{ {

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/12/11 03:21:27 by apommier #+# #+# */ /* Created: 2020/12/11 03:21:27 by apommier #+# #+# */
/* Updated: 2022/01/18 01:32:40 by apommier ### ########.fr */ /* Updated: 2022/01/20 21:46:04 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,6 +45,7 @@ char *ft_strrchr(const char *s, int c);
int ft_strncmp(const char *s1, const char *s2, size_t n); int ft_strncmp(const char *s1, const char *s2, size_t n);
size_t ft_strlcpy(char *dst, const char *src, size_t size); size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlcat(char *dst, const char *src, size_t size); size_t ft_strlcat(char *dst, const char *src, size_t size);
char *ft_strjoin(char *save, char *s2);
char *ft_strnstr(const char *big, const char *little, size_t len); char *ft_strnstr(const char *big, const char *little, size_t len);
long ft_atoi(const char *nptr); long ft_atoi(const char *nptr);
void *ft_calloc(size_t nmenb, size_t size); void *ft_calloc(size_t nmenb, size_t size);

147
main.c
View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/19 12:45:03 by apommier #+# #+# */ /* Created: 2022/01/19 12:45:03 by apommier #+# #+# */
/* Updated: 2022/01/21 01:16:22 by apommier ### ########.fr */ /* Updated: 2022/01/21 03:56:28 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,101 +47,90 @@ char *get_command(char *exec, char **envp)
i = 0; i = 0;
swap = 0; swap = 0;
cmd = ft_strjoin("/", exec); cmd = 0;
path = get_path(envp); path = get_path(envp);
swap = ft_strjoin(path[i], cmd); swap = ft_strjoin(path[i], "/");
while (access(swap, F_OK) == -1 && path[i++]) while (access(swap, F_OK) && path[i++])
{ {
if (swap) if (swap)
free(swap); free(swap);
swap = ft_strjoin(path[i], cmd); swap = ft_strjoin(path[i], "/");
cmd = swap;
swap = ft_strjoin(swap, exec);
free(cmd);
} }
printf("cmd = %s path = %s\n", cmd, swap); if (!path[i])
free(cmd); return (0);
return (swap); return (swap);
} }
void pipex(int fd1, int fd2, char **envp, char **argv) void child_process(char **envp, char **argv, int *end)
{ {
int pipe_tab[2]; int fd1;
char **exec;
char *cmd1;
fd1 = open(argv[1], O_RDONLY);
exec = ft_split(argv[2], ' ');
cmd1 = get_command(exec[0], envp);
if (dup2(end[1], 1) == -1)
{
perror("Error");
exit(1);
}
if (dup2(fd1, 0) == -1)
{
perror("Error");
exit(1);
}
close(end[0]);
close(fd1);
if (execve(cmd1, exec, envp) == -1)
perror("Error");
free(cmd1);
}
void parent_process(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);
if (dup2(fd2, 1) == -1)
exit(1);
close(end[1]);
close(fd2);
if (execve(cmd2, exec, env) == -1)
perror("Error");
}
int main(int argc, char **argv, char **envp)
{
int end[2];
char *cmd1;
char *cmd2;
pid_t parent; pid_t parent;
pipe(end);
parent = fork(); parent = fork();
if (parent < 0) if (parent < 0)
{ {
printf("here\n"); printf("here\n");
perror("Error"); perror("Error");
return ; return (0);
} }
if (!parent) if (!parent)
child_process(fd1, envp, argv, pipe_tab); child_process(envp, argv, end);
else else
parent_process(fd2, envp, argv, pipe_tab); parent_process(envp, argv, end);
printf("pipex end\n"); return (0);
}
void child_process(int fd1, char **envp, char **argv, int *pipe_tab)
{
int i;
char *cmd1;
cmd1 = 0;
i = 0;
if (dup2(fd1, 0) == -1)
{
perror("Error");
return ;
}
if (dup2(pipe_tab[1], 1) == -1)
{
perror("Error");
return ;
}
close(pipe_tab[0]);
close(fd1);
cmd1 = get_command(argv[2], envp);
if (cmd1)
{
execve(cmd1, argv, envp);
free(cmd1);
}
}
void parent_process(int fd2, char **envp, char **argv, int *pipe_tab)
{
int i;
int status;
char *cmd2;
waitpid(-1, &status, 0);
cmd2 = 0;
i = 0;
if (!dup2(fd2, 1))
return ;
if (!dup2(pipe_tab[0], 0))
return ;
close(pipe_tab[1]);
close(fd2);
cmd2 = get_command(argv[3], envp);
if (cmd2)
{
execve(cmd2, argv, envp);
free(cmd2);
}
}
int main(int argc, char **argv, char **envp)
{
int fd1;
int fd2;
char *path;
//if (check_error)
// return (0);
fd1 = open(argv[1], O_RDONLY);
fd2 = open(argv[4], O_RDWR | O_CREAT | O_TRUNC, 0644);
if (!fd1 || !fd2)
return (0);
pipex(fd1, fd2 , envp, argv);
printf("--end--\n");
return (1);
} }

10
pipex.h
View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/19 12:45:13 by apommier #+# #+# */ /* Created: 2022/01/19 12:45:13 by apommier #+# #+# */
/* Updated: 2022/01/20 19:38:53 by apommier ### ########.fr */ /* Updated: 2022/01/21 03:00:34 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,4 +15,10 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include "./libft/libft.h" #include "./libft/libft.h"
char **get_path(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);