From: Dominik Kaiser Date: Tue, 30 Apr 2024 15:40:06 +0000 (+0200) Subject: Fast and bad solution X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=2692c96e1e4a995b0315ece2888e3255a205b802;p=42%2Fpipex.git Fast and bad solution This is just to get a feeling on how to solve the problem in general --- diff --git a/src/main.c b/src/main.c index 01c022a..1fdc764 100644 --- a/src/main.c +++ b/src/main.c @@ -6,19 +6,44 @@ /* By: dkaiser +#include -int main(int argc, char *argv[]) { - if (argc != 5) - return (1); +int main(void) +{ + char *args1[] = {"/bin/cat", 0}; + char *args2[] = {"/usr/bin/grep", "libft", 0}; + int p[2]; + + pipe(p); + + pid_t pid = fork(); + if (pid < 0) + { + exit(1); + } + if (pid == 0) + { + close(p[0]); + int infd = open("in.txt", O_RDONLY); + dup2(infd, 0); + dup2(p[1], 1); + + execve(args1[0], args1, NULL); + } + else + { + wait(NULL); + close(p[1]); + dup2(p[0], 0); + int outfd = open("out.txt", O_WRONLY); + dup2(outfd, 1); + execve(args2[0], args2, NULL); + } - // Read content of file1 (argv[1]) - // Execute cmd1 (argv[2]) with file1 on stdin - // pipe the output of cmd1 into cmd2 - // Execute cmd2 (argv[3]) with the piped input on stdin - // Write output of cmd2 into file2 (argv[4]) }