Skip to content

Commit

Permalink
More formatting for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
akdovlet committed May 19, 2024
1 parent fe41609 commit e208f2a
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 137 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
*.txt
*.sh
*tester*
*zomb*

# Vscode bullshit
.vscode
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand All @@ -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
Expand Down
28 changes: 21 additions & 7 deletions include/pipex.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
30 changes: 15 additions & 15 deletions src/ak_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}
42 changes: 16 additions & 26 deletions src/ak_pipeout.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,12 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
Expand All @@ -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);
}
8 changes: 4 additions & 4 deletions src/clear_exit.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_exit.c :+: :+: :+: */
/* clear_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions src/cmd_exe.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,24 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
Expand Down
55 changes: 26 additions & 29 deletions src/dr_here.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,29 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
Expand All @@ -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);
}
7 changes: 1 addition & 6 deletions src/env_access.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand All @@ -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);
Expand Down
Loading

0 comments on commit e208f2a

Please sign in to comment.