From 7af46f10e23296baa72b70e5fbc72eb35e2a9b01 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 7 May 2024 15:18:01 +0200 Subject: Add changes from input_handling This is not how it should be done, but I just need to finish this quickly. --- src/input_handling.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/input_handling.c (limited to 'src/input_handling.c') diff --git a/src/input_handling.c b/src/input_handling.c new file mode 100644 index 0000000..e4bf7d6 --- /dev/null +++ b/src/input_handling.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* input_handling.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser +#include +#include + +static char **get_cmds(int argc, char *argv[], char *envp[]) +{ + char **cmds; + int i; + char **path; + char *pwd; + + cmds = malloc(sizeof(char *) * (argc - 2)); + if (!cmds) + return (NULL); + path = get_split_path(envp); + // TODO: Free on fail + pwd = get_pwd(envp); + i = 2; + while (i < argc - 1) + { + cmds[i - 2] = get_cmd_path(argv[i], path, pwd); + i++; + } + i = 0; + while (path[i]) + free(path[i++]); + free(path); + return (cmds); +} + +t_pxdata *get_pxdata(int argc, char *argv[], char *envp[]) +{ + t_pxdata *result; + + result = malloc(sizeof(t_pxdata)); + if (!result) + return (NULL); // TODO: Check if an error message needs to be sent + result->in_fd = open(argv[1], O_RDONLY); + result->out_fd = open(argv[--argc], O_WRONLY | O_CREAT | O_TRUNC); + result->cmds = get_cmds(argc, argv, envp); + return (result); +} -- cgit v1.2.3 From ff846486ce845beab8b0fe31c227b5c5ad620bb9 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 7 May 2024 15:30:14 +0200 Subject: Still restoring --- include/pipex.h | 14 +++++++++++++- src/input_handling.c | 5 +++-- 2 files changed, 16 insertions(+), 3 deletions(-) (limited to 'src/input_handling.c') diff --git a/include/pipex.h b/include/pipex.h index 1a79cf1..8f24cc0 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,7 +6,7 @@ /* By: dkaiser +typedef struct s_pxdata +{ + int in_fd; + int out_fd; + char **cmds; +} t_pxdata; + +char **get_split_path(char *envp[]); +char *get_pwd(char *envp[]); +char *find_in_path(char *cmd, char **path); +char *get_cmd_path(char *cmd, char **path, char *pwd); +t_pxdata *get_pxdata(int argc, char *argv[], char *envp[]); #endif // PIPEX_H diff --git a/src/input_handling.c b/src/input_handling.c index e4bf7d6..ef4cec8 100644 --- a/src/input_handling.c +++ b/src/input_handling.c @@ -6,7 +6,7 @@ /* By: dkaiser #include +#include #include static char **get_cmds(int argc, char *argv[], char *envp[]) @@ -50,7 +51,7 @@ t_pxdata *get_pxdata(int argc, char *argv[], char *envp[]) if (!result) return (NULL); // TODO: Check if an error message needs to be sent result->in_fd = open(argv[1], O_RDONLY); - result->out_fd = open(argv[--argc], O_WRONLY | O_CREAT | O_TRUNC); + result->out_fd = open(argv[--argc], O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWUSR); result->cmds = get_cmds(argc, argv, envp); return (result); } -- cgit v1.2.3 From d2fc33211204c4885f54c518fb5d6f1b31160136 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 7 May 2024 16:05:24 +0200 Subject: 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(-) (limited to 'src/input_handling.c') 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); } -- cgit v1.2.3 From e682f9bfb95b28c29665f24d15464cb26abf1127 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 7 May 2024 16:37:28 +0200 Subject: Norme formatting --- include/pipex.h | 18 +++++++++--------- src/env_utils.c | 13 +++++++------ src/input_handling.c | 7 ++++--- src/main.c | 22 +++++++++++----------- 4 files changed, 31 insertions(+), 29 deletions(-) (limited to 'src/input_handling.c') diff --git a/include/pipex.h b/include/pipex.h index 8f24cc0..d1e9c77 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,7 +6,7 @@ /* By: dkaiser in_fd = open(argv[1], O_RDONLY); - result->out_fd = open(argv[--argc], O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWUSR); + result->out_fd = open(argv[--argc], O_WRONLY | O_CREAT | O_TRUNC, + S_IREAD | S_IWUSR); result->cmds = get_cmds(argc, argv, envp); return (result); } diff --git a/src/main.c b/src/main.c index f33d97a..0a0dd61 100644 --- a/src/main.c +++ b/src/main.c @@ -6,22 +6,22 @@ /* By: dkaiser #include #include #include -#include -int pipex(t_pxdata *data, char *envp[]) +int pipex(t_pxdata *data, char *envp[]) { - int p[2]; - pid_t pid; - char **cmd; - int status; + int p[2]; + pid_t pid; + char **cmd; + int status; pipe(p); pid = fork(); @@ -56,12 +56,12 @@ int pipex(t_pxdata *data, char *envp[]) return (EXIT_SUCCESS); } -int main(int argc, char *argv[], char *envp[]) { - t_pxdata *data; +int main(int argc, char *argv[], char *envp[]) +{ + t_pxdata *data; if (argc != 5) return (1); - data = get_pxdata(argc, argv, envp); /* if (data->in_fd < 0 || data->out_fd < 0) */ /* return (1); */ @@ -72,5 +72,5 @@ int main(int argc, char *argv[], char *envp[]) { /* ft_printf("CMD [%d]: %s\n", i, data->cmds[i]); */ /* i++; */ /* } */ - return pipex(data, envp); + return (pipex(data, envp)); } -- cgit v1.2.3 From 334aaa56cd3963f171afec6e852b9c07ad613828 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 8 May 2024 12:03:16 +0200 Subject: Add freeing and make norminette happy --- Makefile | 2 +- src/env_utils.c | 36 +---------------------- src/get_cmd_path.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++ src/input_handling.c | 12 ++++---- src/main.c | 81 ++++++++++++++++++++++++++++++---------------------- 5 files changed, 126 insertions(+), 77 deletions(-) create mode 100644 src/get_cmd_path.c (limited to 'src/input_handling.c') diff --git a/Makefile b/Makefile index 69c304e..8277ef4 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ HEADERS = -Iinclude -Ilibft LIBS = -Llibft -lft VPATH := src -SRC = main.c env_utils.c input_handling.c +SRC = main.c env_utils.c get_cmd_path.c input_handling.c OBJ_DIR := obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/src/env_utils.c b/src/env_utils.c index 065ffb3..4b507a2 100644 --- a/src/env_utils.c +++ b/src/env_utils.c @@ -6,7 +6,7 @@ /* By: dkaiser in_fd = open(argv[1], O_RDONLY); result->out_fd = open(argv[--argc], O_WRONLY | O_CREAT | O_TRUNC, S_IREAD | S_IWUSR); diff --git a/src/main.c b/src/main.c index 0a0dd61..68dbac5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,54 +6,76 @@ /* By: dkaiser #include #include #include -int pipex(t_pxdata *data, char *envp[]) +static void pipex_child(t_pxdata *data, char *envp[], int p[2]) { - 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], ' '); + if (cmd) { - 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); + ft_free_split(cmd); exit(status); } - else +} + +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) { - 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); + ft_free_split(cmd); + return (EXIT_SUCCESS); } + return (EXIT_FAILURE); +} + +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); - return (EXIT_SUCCESS); + ft_free_split(data->cmds); + free(data); + return (result); } int main(int argc, char *argv[], char *envp[]) @@ -63,14 +85,5 @@ int main(int argc, char *argv[], char *envp[]) if (argc != 5) return (1); 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)); } -- cgit v1.2.3