From d2fc33211204c4885f54c518fb5d6f1b31160136 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 7 May 2024 16:05:24 +0200 Subject: [PATCH] Restore progress I deleted almost my entire progress of today, but now everything should be restored. --- src/env_utils.c | 22 +++++++++++---- src/input_handling.c | 5 ++-- src/main.c | 66 +++++++++++++++++++++++++++++++++++++++----- 3 files changed, 78 insertions(+), 15 deletions(-) diff --git a/src/env_utils.c b/src/env_utils.c index 4e02032..5195c2f 100644 --- a/src/env_utils.c +++ b/src/env_utils.c @@ -6,10 +6,11 @@ /* By: dkaiser +#include +#include +#include + +int pipex(t_pxdata *data, char *envp[]) +{ + int p[2]; + pid_t pid; + char **cmd; + int status; + + pipe(p); + pid = fork(); + if (pid < 0) + return (EXIT_FAILURE); + if (pid == 0) + { + close(p[0]); + dup2(data->in_fd, 0); + dup2(p[1], 1); + if (!data->cmds[0]) + exit(127); + cmd = ft_split(data->cmds[0], ' '); // TODO: Free on fail + status = execve(cmd[0], cmd, envp); + free(cmd); + exit(status); + } + else + { + close(p[1]); + dup2(p[0], 0); + dup2(data->out_fd, 1); + if (!data->cmds[1]) + return (EXIT_FAILURE); + cmd = ft_split(data->cmds[1], ' '); // TODO: Free on fail + if (execve(cmd[0], cmd, envp)) + return (EXIT_FAILURE); + free(cmd); + } + close(data->in_fd); + close(data->out_fd); + return (EXIT_SUCCESS); +} int main(int argc, char *argv[], char *envp[]) { - if (!argc || !argv || !envp) + t_pxdata *data; + + if (argc != 5) return (1); - // 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]) + data = get_pxdata(argc, argv, envp); + /* if (data->in_fd < 0 || data->out_fd < 0) */ + /* return (1); */ + /* ft_printf("IN: %d\nOUT: %d\n", data->in_fd, data->out_fd); */ + /* int i = 0; */ + /* while (data->cmds[i]) */ + /* { */ + /* ft_printf("CMD [%d]: %s\n", i, data->cmds[i]); */ + /* i++; */ + /* } */ + return pipex(data, envp); } -- 2.47.2