This commit is contained in:
xamora 2023-01-16 11:55:27 +01:00
parent ce946aeed1
commit 76d8f65ca2
51 changed files with 79 additions and 29 deletions

View file

@ -13,11 +13,11 @@ NAME = pipex
all: ${NAME}
${NAME}: ${OBJS}
make -C libft && print
make -C libft
${CC} ${CFLAGS} -o ${NAME} ${OBJS} ${LIBS}
bonus: ${OBJS_BONUS}
make -C libft && print
make -C libft
${CC} ${CFLAGS} -o ${NAME} ${OBJS_BONUS} ${LIBS}
%.o:%.c

Binary file not shown.

Binary file not shown.

1
input
View file

@ -1 +0,0 @@
test

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
pipex

Binary file not shown.

99
pipex.c
View file

@ -19,7 +19,7 @@ char *read_file(int fd)
return (str);
}
int get_next(char *cmd, char c)
int get_next(char *cmd, char c)
{
int i;
@ -36,7 +36,7 @@ char *get_flags(char *cmd)
char *new_str;
i = get_next(cmd, ' ');
new_str = ft_calloc(ft_strlen(cmd) - i + 1);
new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1);
y = i;
while (cmd[i])
new_str[y++] = cmd[i++];
@ -49,7 +49,7 @@ char *get_command(char *cmd)
int i;
char *new_str;
new_str = ft_calloc(get_next(cmd, ' ') + 1);
new_str = ft_calloc(get_next(cmd, ' ') + 1, 1);
i = -1;
while (cmd[++i] != ' ')
new_str[i] = cmd[i];
@ -65,7 +65,7 @@ int parsing(char *argv[], t_data *data)
fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT);
if (fd2 < 0)
return (-1);
fd1 = open(argv[1], O_RDONLY);
fd1 = open(argv[1], O_RDONLY | O_TRUNC | O_CREAT);
if (fd1 < 0)
{
close(fd2);
@ -78,63 +78,110 @@ int parsing(char *argv[], t_data *data)
data->fl1 = argv[1];
data->fl2 = argv[4];
data->ct_fl1 = read_file(fd1);
close(fd1);
data->fd1 = fd1;
data->fd2 = fd2;
return (0);
}
int find_command(char **env)
char *find_command(char **env, char *cmd)
{
char *path;
char *s;
int fd;
int i;
int y;
while (*env != NULL)
{
if (ft_strnstr(*env, "PATH=/", ft_strlen(*env)))
s = *env;
break;
env++;
}
i = 4;
y = 4;
s = malloc(ft_strlen(*env) + 1);
while (*env[i])
{
if (*env[i] != ':')
s[y++] = *env[i];
else
{
path = ft_strjoin(s, cmd);
fd = open(path, O_RDONLY);
free(path);
if (fd != -1)
{
free(s);
return (path);
}
y = 0;
}
i++;
}
return (NULL);
}
int first_cmd(int fd[2], t_data data)
void first_cmd(int fd[2], t_data data)
{
dup2(data.fd1, 0);
dup2(fd[1], 1);
close(fd[0]);
execve(data->cmd1)
close(fd[1]);
execve(data.cmd1, &data.flg1, NULL);
}
int second_cmd(int fd[2], t_data data)
void second_cmd(int fd[2], t_data data)
{
dup2(fd[0], 0);
dup2(data.fd2, 1);
close(fd[0]);
close(fd[1]);
execve(data.cmd2, &data.flg2, NULL);
}
int pipex(t_data data)
{
int fd[2]
int id;
int fd[2];
int cmd1;
int cmd2;
if (pipe(fd) < -1)
return (1);
id = fork();
if (id == 0)
1_cmd(fd, data);
else
2_cmd(fd, data);
cmd1 = fork();
if (cmd1 == 0)
first_cmd(fd, data);
cmd2 = fork();
if (cmd2 == 0)
second_cmd(fd, data);
close(fd[0]);
close(fd[1]);
waitpid(cmd1, NULL, 0);
waitpid(cmd2, NULL, 0);
return (0);
}
#include <stdio.h>
int main(int argc, char *argv[], char **env)
{
//t_data data;
t_data data;
(void)argc;
(void)argv;
/*if (argc < 5)
//(void)argc;
//(void)argv;
if (argc < 5)
return (1);
if (parsing(argv, &data))
return (2);
find_command(env);
if (pipex(data))
data.cmd1 = find_command(env, data.cmd1);
if (data.cmd1 == NULL)
return (3);
free(data.fl1);*/
data.cmd2 = find_command(env, data.cmd2);
if (data.cmd2 == NULL)
return (4);
if (pipex(data))
return (5);
free(data.fl1);
close(data.fd1);
close(data.fd2);
return (0);
}

View file

@ -17,6 +17,8 @@
# include "get_next_line/get_next_line.h"
# include <fcntl.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/wait.h>
typedef struct s_data
{
@ -27,6 +29,8 @@ typedef struct s_data
char *fl1;
char *fl2;
char *ct_fl1;
int fd1;
int fd2;
} t_data;
#endif

BIN
pipex.o

Binary file not shown.