diff options
| author | Dominik Kaiser | 2024-05-08 13:05:37 +0200 |
|---|---|---|
| committer | GitHub | 2024-05-08 13:05:37 +0200 |
| commit | 50fd99cba74b758be23aafc4f76b5e63ef86977f (patch) | |
| tree | 1fa796501488d746f2d22c7bee80e5212dc830cc /src/main.c | |
| parent | b05f8b0d22aa0bcf2d993d56b62055e042d55853 (diff) | |
| parent | 334aaa56cd3963f171afec6e852b9c07ad613828 (diff) | |
| download | pipex-50fd99cba74b758be23aafc4f76b5e63ef86977f.tar.gz pipex-50fd99cba74b758be23aafc4f76b5e63ef86977f.zip | |
Merging dev into master
This should be all.
Diffstat (limited to 'src/main.c')
| -rw-r--r-- | src/main.c | 83 |
1 files changed, 74 insertions, 9 deletions
@@ -6,19 +6,84 @@ /* 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/08 11:53:26 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" #include "pipex.h" +#include <stdio.h> +#include <stdlib.h> +#include <sys/_types/_pid_t.h> +#include <unistd.h> -int main(int argc, char *argv[], char *envp[]) { - if (!argc || !argv || !envp) - return (1); +static void pipex_child(t_pxdata *data, char *envp[], int p[2]) +{ + char **cmd; + int status; + + close(p[0]); + dup2(data->in_fd, 0); + dup2(p[1], 1); + if (!data->cmds[0]) + exit(127); + cmd = ft_split(data->cmds[0], ' '); + if (cmd) + { + status = execve(cmd[0], cmd, envp); + ft_free_split(cmd); + exit(status); + } +} + +static int pipex_parent(t_pxdata *data, char *envp[], int p[2]) +{ + char **cmd; + + 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], ' '); + if (cmd) + { + if (execve(cmd[0], cmd, envp)) + return (EXIT_FAILURE); + ft_free_split(cmd); + return (EXIT_SUCCESS); + } + return (EXIT_FAILURE); +} - // 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]) +static int pipex(t_pxdata *data, char *envp[]) +{ + int result; + int p[2]; + pid_t pid; + + pipe(p); + result = EXIT_SUCCESS; + pid = fork(); + if (pid < 0) + return (EXIT_FAILURE); + if (pid == 0) + pipex_child(data, envp, p); + else + result = pipex_parent(data, envp, p); + close(data->in_fd); + close(data->out_fd); + ft_free_split(data->cmds); + free(data); + return (result); +} + +int main(int argc, char *argv[], char *envp[]) +{ + t_pxdata *data; + + if (argc != 5) + return (1); + data = get_pxdata(argc, argv, envp); + return (pipex(data, envp)); } |
