From f39d94a900c03d8727b2ce9933bc81eecc063334 Mon Sep 17 00:00:00 2001 From: Akhmed Dovletov Date: Tue, 14 May 2024 20:59:30 +0200 Subject: [PATCH] Who's reading this shit --- lib/libft/src/ft_multi_split.c | 6 +++-- lib/libft/src/ft_strchr.c | 6 +++-- src/ak_pipe.c | 24 ++++++++++---------- src/free.c | 8 ++++++- src/parsing.c | 28 +----------------------- src/seek_and_execute.c | 40 ++++++++++++++++++++++++++++++---- 6 files changed, 64 insertions(+), 48 deletions(-) diff --git a/lib/libft/src/ft_multi_split.c b/lib/libft/src/ft_multi_split.c index 3f6a7a8..ad9bf94 100644 --- a/lib/libft/src/ft_multi_split.c +++ b/lib/libft/src/ft_multi_split.c @@ -6,11 +6,12 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/07 22:07:22 by akdovlet #+# #+# */ -/* Updated: 2024/01/14 16:32:59 by akdovlet ### ########.fr */ +/* Updated: 2024/05/14 18:54:46 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" +#include static char **ft_splitter(char *str, char *sep, int *tab, int word_count) { @@ -46,9 +47,10 @@ char **ft_multi_split(char *str, char *sep) int word_count; char **strs; + tab = NULL; word_count = ak_countword(str, sep, &tab); if (word_count == -1) - return (NULL); + return (free(tab), NULL); strs = ft_splitter(str, sep, tab, word_count); free(tab); return (strs); diff --git a/lib/libft/src/ft_strchr.c b/lib/libft/src/ft_strchr.c index 9d781cc..3f241b1 100644 --- a/lib/libft/src/ft_strchr.c +++ b/lib/libft/src/ft_strchr.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/09/10 20:55:38 by akdovlet #+# #+# */ -/* Updated: 2024/01/03 21:00:57 by akdovlet ### ########.fr */ +/* Updated: 2024/05/14 18:35:56 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,14 @@ char *ft_strchr(const char *s, int c) int i; char *str; + if (!s) + return (NULL); str = (char *)s; i = 0; while (str[i] != (char)c) { if (str[i] == '\0') - return (0); + return (NULL); i++; } return (str + i); diff --git a/src/ak_pipe.c b/src/ak_pipe.c index bcc78d8..7901119 100644 --- a/src/ak_pipe.c +++ b/src/ak_pipe.c @@ -32,6 +32,7 @@ void ak_pipe(t_data *data, int i) return ; if (pipe(fd) == -1) return (perror("pipex"), clear_exit(data, EXIT_FAILURE)); + printf("i is: %d\n", i); data->ids[i - 2] = fork(); if (data->ids[i - 2] < 0) return (perror("pipex"), clear_exit(data, EXIT_FAILURE)); @@ -45,10 +46,10 @@ void ak_pipe(t_data *data, int i) void child_out(t_data *data) { if (dup2(data->hermes, STDIN_FILENO) == -1) - perror("pipex"); + clear_exit(data, EXIT_FAILURE); close(data->hermes); if (dup2(data->last, STDOUT_FILENO) == -1) - perror("pipex"); + clear_exit(data, EXIT_FAILURE); close(data->last); cmd_exe(data, data->ac - 2); } @@ -59,25 +60,24 @@ void ak_pipeout(t_data *data, int i) int j; j = 0; + status = 0; data->last = open(data->av[data->ac - 1], O_WRONLY | O_CREAT | O_TRUNC, 0644); if (data->last < 0) - return (perror(data->av[data->ac -1]), clear_all(data), exit(EXIT_FAILURE)); + return (perror(data->av[data->ac -1]), clear_exit(data, EXIT_FAILURE)); data->ids[i - 2] = fork(); + printf("last i is: %d\n", i); if (data->ids[i - 2] < 0) - { - clear_all(data); - return (perror("pipex")); - } + return (perror("pipex"), clear_exit(data, EXIT_FAILURE)); if (!data->ids[i - 2]) child_out(data); - close(data->first); close(data->hermes); while (j < data->ac - 3) { - if (waitpid(data->ids[j],&status, 0) == -1) - perror("waitpid"); - if (WIFEXITED(status)) - data->exit_code = WEXITSTATUS(status); + printf("ids value is: %d\n", data->ids[j]); + waitpid(data->ids[j],&status, 0); + printf("status is: %d\n", status); + data->exit_code = WEXITSTATUS(status); + printf("exit_code is: %d\n", data->exit_code); j++; } } diff --git a/src/free.c b/src/free.c index fdf78d1..8ba6384 100644 --- a/src/free.c +++ b/src/free.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 00:41:28 by akdovlet #+# #+# */ -/* Updated: 2024/05/13 17:02:02 by akdovlet ### ########.fr */ +/* Updated: 2024/05/14 19:47:09 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,10 @@ void clear_all(t_data *data) data->ids = NULL; if (data->first > 0) close(data->first); + data->first = 0; + if (data->hermes > 0) + close(data->hermes); + data->hermes = 0; if (data->last > 0) close(data->last); } @@ -29,6 +33,8 @@ void ft_free(char **str) int i; i = 0; + if (!str) + return ; while (str[i]) { free(str[i]); diff --git a/src/parsing.c b/src/parsing.c index ee55af8..64f40b0 100644 --- a/src/parsing.c +++ b/src/parsing.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 17:52:22 by akdovlet #+# #+# */ -/* Updated: 2024/05/13 17:41:36 by akdovlet ### ########.fr */ +/* Updated: 2024/05/14 18:15:23 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,10 +29,7 @@ char **parse_env(char **env) i = 0; if (!env[0]) - { - return (px_split(HARDPATH, ':')); - } while (env[i]) { if (!ft_strncmp("PATH=", env[i], 5)) @@ -41,26 +38,3 @@ char **parse_env(char **env) } return (ft_split("./", '\0')); } - -void find_exec(char *cmd, t_data *data) -{ - int i; - char *full_path; - - i = 0; - full_path = NULL; - while (data->path[i]) - { - full_path = ft_strjoin(data->path[i], cmd); - if (!full_path) - return (clear_exit(data, EXIT_FAILURE)); - if (!access(full_path, X_OK)) - { - execve(full_path, data->cmd, data->env); - free(full_path); - clear_exit(data, 127); - } - free(full_path); - i++; - } -} diff --git a/src/seek_and_execute.c b/src/seek_and_execute.c index 05887a8..2052e8a 100644 --- a/src/seek_and_execute.c +++ b/src/seek_and_execute.c @@ -6,14 +6,45 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/06 23:52:25 by akdovlet #+# #+# */ -/* Updated: 2024/05/13 17:47:47 by akdovlet ### ########.fr */ +/* Updated: 2024/05/14 20:29:55 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" +void find_exec(char *cmd, t_data *data) +{ + int i; + char *full_path; + + i = 0; + full_path = NULL; + while (data->path[i]) + { + full_path = ft_strjoin(data->path[i], cmd); + if (!full_path) + return (clear_exit(data, EXIT_FAILURE)); + if (!access(full_path, X_OK)) + { + execve(full_path, data->cmd, data->env); + free(full_path); + perror("pipex"); + clear_exit(data, 127); + } + free(full_path); + i++; + } + ft_dprintf(STDERR_FILENO, "pipex: %s: command not found\n", data->cmd[0]); + clear_exit(data, 127); +} + void cmd_exe(t_data *data, int i) { + if (!data->cmd[0]) + { + ft_dprintf(STDERR_FILENO, "%s: command not found\n", data->cmd[0]); + clear_exit(data, 127); + } if (ft_strchr(data->cmd[0], '/')) { if (access(data->cmd[0], X_OK) == -1) @@ -24,8 +55,8 @@ void cmd_exe(t_data *data, int i) else { execve(data->cmd[0], data->cmd, data->env); - perror("execve"); - clear_exit(data, EXIT_FAILURE); + ft_dprintf(STDERR_FILENO, "%s: command not found\n", data->cmd[0]); + clear_exit(data, 127); } } else @@ -38,11 +69,12 @@ void seek_and_execute(t_data *data) i = 2; data->ids = malloc(sizeof(pid_t) * (data->ac - 3)); + printf("size is: %d\n", data->ac - 3); if (!data->ids) return (clear_exit(data, EXIT_FAILURE)); while (i < data->ac - 2) { - data->cmd = ft_multi_split(data->av[i], " \t\n'"); + data->cmd = ft_multi_split(data->av[i], " '"); if (!data->cmd) return (clear_exit(data, EXIT_FAILURE)); ak_pipe(data, i);