diff --git a/.gitignore b/.gitignore index ac2e589..5416fbf 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ *.txt *.sh *tester* +*zomb* # Vscode bullshit .vscode diff --git a/Makefile b/Makefile index ebaf77a..19f5985 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: akdovlet +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/01/01 13:57:12 by akdovlet #+# #+# # -# Updated: 2024/05/19 02:15:41 by akdovlet ### ########.fr # +# Updated: 2024/05/19 19:14:14 by akdovlet ### ########.fr # # # # **************************************************************************** # @@ -22,6 +22,8 @@ SRC := ak_pipe.c \ dr_here.c \ env_access.c \ main.c \ + open_infile.c \ + open_outfile.c \ px_split.c \ seek_and_execute.c \ setup.c diff --git a/include/pipex.h b/include/pipex.h index ba717ea..9c12f5b 100644 --- a/include/pipex.h +++ b/include/pipex.h @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/20 23:07:37 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 01:50:49 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 19:23:08 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,12 +24,13 @@ # define HARDPATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" # define ERR_MSG "pipex: %s: %s\n" # define ERR_CMD "pipex: %s: command not found\n" +# define ERR_ARG "Error: Invalid number of arguments\n" # define STDERR 2 # define CHILD 0 typedef struct s_data { - pid_t *ids; + pid_t *pid_array; char **path; char **env; char **cmd; @@ -44,10 +45,10 @@ typedef struct s_data } t_data; /******************************ak_pipe.c**************************************/ -// Child function for every command except for the last one, executes cmd -void child(int fd[2], t_data *data); // Will pipe and fork making every cmd communicate, calls child void ak_pipe(t_data *data, int i); +// Child function for every command except for the last one, executes cmd +void child(int fd[2], t_data *data); /******************************ak_pipeout.c***********************************/ // Child function for the last cmd @@ -60,11 +61,12 @@ void ak_pipeout(t_data *data, int i); void cmd_exe(t_data *data); /******************************dr_here.c**************************************/ -// Forgot about Dre -void dr_dre(t_data *data, int *fd); // Creates a pipe and forks, calls dr_dre which reads // from stdin and writes to pipe. Only called when here_doc_delimiter is set void dr_here(t_data *data); +// Forgot about Dre +void dr_dre(t_data *data, int *fd); +bool delimiter_cmp(char *s1, char *s2); /********************************env_access.c**********************************/ // Will check if env is empty, if so will return @@ -74,6 +76,13 @@ char **get_path_from_env(char **env); // Will check if file has the correct permissions, simply calls access bool file_access(char *file, int check); +/******************************open_infile.c***********************************/ +void infile_setup(t_data *data, char **av); +int infile_check(char *file); + +/*****************************open_outfile.c***********************************/ +void open_outfile(t_data *data); + /********************************free_exit.c***********************************/ // Frees everything void clear_all(t_data *data); @@ -89,7 +98,12 @@ char **px_split(char const *s, char c); void seek_and_execute(t_data *data); /******************************setup.c****************************************/ -// Iniatilizes the data struct, not here for error checks, only for setup +/* Does a lot of the heavy lifing, this isn't some silly parsing + or preemptive error check. + Instead, like the name implies, it sets all the variables to their right + values. This function allows me to not have to change my driver + code regardless of how many commands I have to run + or if "here_doc" is given*/ void setup(t_data *data, int ac, char **av, char **env); #endif diff --git a/src/ak_pipe.c b/src/ak_pipe.c index 3e02f95..f2b8657 100644 --- a/src/ak_pipe.c +++ b/src/ak_pipe.c @@ -12,18 +12,6 @@ #include "pipex.h" -void child(int fd[2], t_data *data) -{ - close(fd[0]); - if (dup2(data->hermes, STDIN_FILENO) == -1) - return (perror("dup2"), clear_exit(data, EXIT_FAILURE)); - close(data->hermes); - if (dup2(fd[1], STDOUT_FILENO) == -1) - return (perror("dup2"), clear_exit(data, EXIT_FAILURE)); - close(fd[1]); - cmd_exe(data); -} - void ak_pipe(t_data *data, int i) { int fd[2]; @@ -32,12 +20,24 @@ void ak_pipe(t_data *data, int i) return ; if (pipe(fd) == -1) return (perror("pipe"), clear_exit(data, EXIT_FAILURE)); - data->ids[i] = fork(); - if (data->ids[i] < 0) + data->pid_array[i] = fork(); + if (data->pid_array[i] < 0) return (perror("fork"), clear_exit(data, EXIT_FAILURE)); - if (data->ids[i] == CHILD) + if (data->pid_array[i] == CHILD) child(fd, data); close(fd[1]); close(data->hermes); data->hermes = fd[0]; } + +void child(int fd[2], t_data *data) +{ + close(fd[0]); + if (dup2(data->hermes, STDIN_FILENO) == -1) + return (perror("dup2"), clear_exit(data, EXIT_FAILURE)); + close(data->hermes); + if (dup2(fd[1], STDOUT_FILENO) == -1) + return (perror("dup2"), clear_exit(data, EXIT_FAILURE)); + close(fd[1]); + cmd_exe(data); +} diff --git a/src/ak_pipeout.c b/src/ak_pipeout.c index ab19205..9bf7e85 100644 --- a/src/ak_pipeout.c +++ b/src/ak_pipeout.c @@ -6,33 +6,12 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 03:05:49 by akdovlet #+# #+# */ -/* Updated: 2024/05/18 23:13:37 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 19:16:14 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -void child_out(t_data *data) -{ - if (dup2(data->hermes, STDIN_FILENO) == -1) - clear_exit(data, EXIT_FAILURE); - close(data->hermes); - if (dup2(data->outfile, STDOUT_FILENO) == -1) - clear_exit(data, EXIT_FAILURE); - close(data->outfile); - cmd_exe(data); -} - -void open_outfile(t_data *data) -{ - if (data->here_doc_delimiter) - data->outfile = open(data->outfile_name, \ - O_WRONLY | O_CREAT | O_APPEND, 0644); - else - data->outfile = open(data->outfile_name, \ - O_WRONLY | O_CREAT | O_TRUNC, 0644); -} - void ak_pipeout(t_data *data, int i) { int status; @@ -43,17 +22,28 @@ void ak_pipeout(t_data *data, int i) open_outfile(data); if (data->outfile < 0) return (perror(data->outfile_name), clear_exit(data, EXIT_FAILURE)); - data->ids[i] = fork(); - if (data->ids[i] < 0) + data->pid_array[i] = fork(); + if (data->pid_array[i] < 0) return (perror("fork"), clear_exit(data, EXIT_FAILURE)); - if (data->ids[i] == CHILD) + if (data->pid_array[i] == CHILD) child_out(data); close(data->hermes); data->hermes = 0; while (j < data->cmd_count) { - waitpid(data->ids[j], &status, 0); + waitpid(data->pid_array[j], &status, 0); data->exit_code = WEXITSTATUS(status); j++; } } + +void child_out(t_data *data) +{ + if (dup2(data->hermes, STDIN_FILENO) == -1) + clear_exit(data, EXIT_FAILURE); + close(data->hermes); + if (dup2(data->outfile, STDOUT_FILENO) == -1) + clear_exit(data, EXIT_FAILURE); + close(data->outfile); + cmd_exe(data); +} diff --git a/src/clear_exit.c b/src/clear_exit.c index 865da3a..acc7331 100644 --- a/src/clear_exit.c +++ b/src/clear_exit.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* free_exit.c :+: :+: :+: */ +/* clear_exit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/07 00:41:28 by akdovlet #+# #+# */ -/* Updated: 2024/05/18 22:56:04 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 19:14:45 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,8 @@ void clear_all(t_data *data) { ft_free(data->path); ft_free(data->cmd); - free(data->ids); - data->ids = NULL; + free(data->pid_array); + data->pid_array = NULL; if (data->infile > 0) close(data->infile); data->infile = 0; diff --git a/src/cmd_exe.c b/src/cmd_exe.c index d33f843..e06d6ce 100644 --- a/src/cmd_exe.c +++ b/src/cmd_exe.c @@ -6,28 +6,24 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/18 22:39:03 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 02:45:55 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 19:37:34 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -// NOt explicit enough void nopath_exec(char *cmd, t_data *data) { if (!file_access(cmd, X_OK)) { ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); - free(cmd); clear_exit(data, 126); } execve(cmd, data->cmd, data->env); ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno)); - free(cmd); clear_exit(data, 127); } -// Not explicit enough void nopath_run(char *cmd, t_data *data) { int i; diff --git a/src/dr_here.c b/src/dr_here.c index 1405e01..8157d60 100644 --- a/src/dr_here.c +++ b/src/dr_here.c @@ -6,24 +6,29 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/15 00:17:44 by akdovlet #+# #+# */ -/* Updated: 2024/05/18 20:52:21 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 19:04:32 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -bool delimiter_cmp(char *s1, char *s2) +void dr_here(t_data *data) { - int i; + int fd[2]; + int status; + pid_t pid; - i = -1; - while ((s1[++i] || s2[i]) && s2[i] != '\n') - if (s1[i] != s2[i]) - return (false); - if (s2[i] == '\n' && s2[i + 1] == '\0') - if (s1[i] == '\0') - return (true); - return (false); + if (pipe(fd) == -1) + clear_exit(data, EXIT_FAILURE); + pid = fork(); + if (pid == -1) + clear_exit(data, EXIT_FAILURE); + if (pid == CHILD) + dr_dre(data, fd); + close(fd[1]); + data->hermes = fd[0]; + waitpid(pid, &status, 0); + data->exit_code = WEXITSTATUS(status); } void dr_dre(t_data *data, int *fd) @@ -49,24 +54,16 @@ void dr_dre(t_data *data, int *fd) clear_exit(data, EXIT_SUCCESS); } -void dr_here(t_data *data) +bool delimiter_cmp(char *s1, char *s2) { - int fd[2]; - int status; - pid_t pid; + int i; - if (pipe(fd) == -1) - clear_exit(data, EXIT_FAILURE); - pid = fork(); - if (pid == -1) - clear_exit(data, EXIT_FAILURE); - if (pid == CHILD) - dr_dre(data, fd); - else - { - close(fd[1]); - data->hermes = fd[0]; - waitpid(pid, &status, 0); - data->exit_code = WEXITSTATUS(status); - } + i = -1; + while ((s1[++i] || s2[i]) && s2[i] != '\n') + if (s1[i] != s2[i]) + return (false); + if (s2[i] == '\n' && s2[i + 1] == '\0') + if (s1[i] == '\0') + return (true); + return (false); } diff --git a/src/env_access.c b/src/env_access.c index 0e8701b..5a4513c 100644 --- a/src/env_access.c +++ b/src/env_access.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/23 17:52:22 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 02:56:55 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 18:32:50 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,11 +19,6 @@ bool file_access(char *file, int check) return (false); } -// No need to malloc hardpath, create a tab as such: -// hardpath = { -// "path1", -// "path2", -//} char **get_path_from_env(char **env) { int i; diff --git a/src/main.c b/src/main.c index 138f044..ad54e76 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/20 23:07:20 by akdovlet #+# #+# */ -/* Updated: 2024/05/18 23:10:45 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 18:33:39 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ int main(int ac, char **av, char **env) if (ac < 5) { - ft_dprintf(STDERR, "Error: Invalid number of arguments\n"); + ft_dprintf(STDERR, ERR_ARG); return (1); } setup(&data, ac, av, env); diff --git a/src/open_infile.c b/src/open_infile.c new file mode 100644 index 0000000..6b885dd --- /dev/null +++ b/src/open_infile.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open_infile.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akdovlet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 19:09:57 by akdovlet #+# #+# */ +/* Updated: 2024/05/19 19:23:18 by akdovlet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +void infile_setup(t_data *data, char **av) +{ + data->infile = infile_check(av[1]); + if (data->infile == -1) + { + data->hermes = open("/dev/null", O_RDONLY); + if (data->hermes < 0) + return (perror(av[1]), ft_free(data->path), exit(EXIT_FAILURE)); + } + else + data->hermes = data->infile; +} + +int infile_check(char *file) +{ + int fd; + + if (!file_access(file, R_OK)) + { + ft_dprintf(STDERR, ERR_MSG, file, strerror(errno)); + return (-1); + } + else + { + fd = open(file, O_RDONLY); + if (fd < 0) + { + ft_dprintf(STDERR, ERR_MSG, file, strerror(errno)); + return (-1); + } + } + return (fd); +} diff --git a/src/open_outfile.c b/src/open_outfile.c new file mode 100644 index 0000000..c7fbe46 --- /dev/null +++ b/src/open_outfile.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* open_outfile.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: akdovlet +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/19 19:09:42 by akdovlet #+# #+# */ +/* Updated: 2024/05/19 19:09:48 by akdovlet ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +void open_outfile(t_data *data) +{ + if (data->here_doc_delimiter) + data->outfile = open(data->outfile_name, \ + O_WRONLY | O_CREAT | O_APPEND, 0644); + else + data->outfile = open(data->outfile_name, \ + O_WRONLY | O_CREAT | O_TRUNC, 0644); +} diff --git a/src/seek_and_execute.c b/src/seek_and_execute.c index f26c2c7..08768cd 100644 --- a/src/seek_and_execute.c +++ b/src/seek_and_execute.c @@ -6,7 +6,7 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/06 23:52:25 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 02:16:16 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 19:05:11 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ void seek_and_execute(t_data *data) int i; i = 0; - data->ids = malloc(sizeof(pid_t) * data->cmd_count); - if (!data->ids) + data->pid_array = malloc(sizeof(pid_t) * data->cmd_count); + if (!data->pid_array) return (clear_exit(data, EXIT_FAILURE)); if (data->here_doc_delimiter) dr_here(data); diff --git a/src/setup.c b/src/setup.c index a50a6cb..bf3c7c7 100644 --- a/src/setup.c +++ b/src/setup.c @@ -6,50 +6,12 @@ /* By: akdovlet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/06 23:41:38 by akdovlet #+# #+# */ -/* Updated: 2024/05/19 02:54:09 by akdovlet ### ########.fr */ +/* Updated: 2024/05/19 19:10:15 by akdovlet ### ########.fr */ /* */ /* ************************************************************************** */ #include "pipex.h" -int check_file(char *file) -{ - int fd; - - if (!file_access(file, R_OK)) - { - ft_dprintf(STDERR, ERR_MSG, file, strerror(errno)); - return (-1); - } - else - { - fd = open(file, O_RDONLY); - if (fd < 0) - { - ft_dprintf(STDERR, ERR_MSG, file, strerror(errno)); - return (-1); - } - } - return (fd); -} - -void infile_setup(t_data *data, char **av) -{ - data->infile = check_file(av[1]); - if (data->infile == -1) - { - data->hermes = open("/dev/null", O_RDONLY); - if (data->hermes < 0) - return (perror(av[1]), ft_free(data->path), exit(EXIT_FAILURE)); - } - else - data->hermes = data->infile; -} - -// Does a lot of the heavy lifing, this isn't some silly parsing. -// Instead, like the name implies, it sets all the variables to their right -// values. This function allows me to not have to change my driver -// code regardless how many commands I have to run or if here_doc is given void setup(t_data *data, int ac, char **av, char **env) { *data = (t_data){0};