summaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-05-07 16:05:24 +0200
committerDominik Kaiser2024-05-07 16:05:24 +0200
commitd2fc33211204c4885f54c518fb5d6f1b31160136 (patch)
tree219fa21550e6bb3fc0060336e75a25e091c9ee27 /src/main.c
parentff846486ce845beab8b0fe31c227b5c5ad620bb9 (diff)
downloadpipex-d2fc33211204c4885f54c518fb5d6f1b31160136.tar.gz
pipex-d2fc33211204c4885f54c518fb5d6f1b31160136.zip
Restore progress
I deleted almost my entire progress of today, but now everything should be restored.
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c66
1 files changed, 59 insertions, 7 deletions
diff --git a/src/main.c b/src/main.c
index 30c3139..f33d97a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -6,19 +6,71 @@
/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 13:35:21 by dkaiser #+# #+# */
-/* Updated: 2024/05/02 14:38:11 by dkaiser ### ########.fr */
+/* Updated: 2024/05/07 16:04:43 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "pipex.h"
+#include <stdlib.h>
+#include <sys/_types/_pid_t.h>
+#include <unistd.h>
+#include <stdio.h>
+
+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);
}