]> git.dkaiser.de - 42/pipex.git/commitdiff
Fast and bad solution
authorDominik Kaiser <dkaiser@2-E-9.42heilbronn.de>
Tue, 30 Apr 2024 15:40:06 +0000 (17:40 +0200)
committerDominik Kaiser <dkaiser@2-E-9.42heilbronn.de>
Tue, 30 Apr 2024 15:40:06 +0000 (17:40 +0200)
This is just to get a feeling on how to solve the problem in general

src/main.c

index 01c022a9ab9e4554d6ae54ffc1fa62aa87d2e6c7..1fdc764d3c09e234f51b1654d7809ea6c0705033 100644 (file)
@@ -6,19 +6,44 @@
 /*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/04/29 13:35:21 by dkaiser           #+#    #+#             */
-/*   Updated: 2024/04/29 15:27:14 by dkaiser          ###   ########.fr       */
+/*   Updated: 2024/04/30 17:37:09 by dkaiser          ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #include "pipex.h"
+#include <unistd.h>
+#include <fcntl.h>
 
-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])
 }