Skip to content

Commit

Permalink
Been there done that
Browse files Browse the repository at this point in the history
  • Loading branch information
akdovlet committed May 20, 2024
1 parent e208f2a commit c28da88
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 60 deletions.
20 changes: 16 additions & 4 deletions include/pipex.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/20 23:07:37 by akdovlet #+# #+# */
/* Updated: 2024/05/19 19:23:08 by akdovlet ### ########.fr */
/* Updated: 2024/05/20 18:21:05 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PIPEX_H
# define PIPEX_H

# include "libft.h"
# include "get_next_line.h"
# include <stdbool.h>
# include <stdio.h>
# include <errno.h>
Expand Down Expand Up @@ -59,13 +58,22 @@ void ak_pipeout(t_data *data, int i);
/******************************cmd_exe.c**************************************/
// Will execute the command and return the correct exit code
void cmd_exe(t_data *data);
/*if command is provided with a path such as "/usr/bin/cat"
check if it exits, if it's executable and then send it traight to execve*/
void path_run(char *cmd, t_data *data);
/*If no path is given, find its path with access() and
then send it to nopath_exec()*/
void nopath_run(char *cmd, t_data *data);
/*Function that checks execution rights and send the command to execve*/
void nopath_exec(char *cmd, t_data *data);

/******************************dr_here.c**************************************/
// 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);
// looks for the delimiter by excluding the '\n' character at the end
bool delimiter_cmp(char *s1, char *s2);

/********************************env_access.c**********************************/
Expand All @@ -77,17 +85,21 @@ char **get_path_from_env(char **env);
bool file_access(char *file, int check);

/******************************open_infile.c***********************************/
/* Precise setup. If the infile isn't open to communicate, we will open
/dev/null which is a file that contains EOF, as to not break a command by
sending it an invalid fd. This is to mimic the behavior of bash*/
void infile_setup(t_data *data, char **av);
int infile_check(char *file);

/*****************************open_outfile.c***********************************/
/* Open differently in case here_doc */
void open_outfile(t_data *data);

/********************************free_exit.c***********************************/
// Calls clear_all and exits with the right exit code
void clear_all_exit(t_data *data, int exit_code);
// Frees everything
void clear_all(t_data *data);
// Calls clear_all and exits with the right exit code
void clear_exit(t_data *data, int exit_code);

/******************************px_split.c**************************************/
// ft_split with a twist: adds '/' add the end of each word
Expand Down
8 changes: 4 additions & 4 deletions src/ak_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ void ak_pipe(t_data *data, int i)
if (data->infile == -1 && i == 0)
return ;
if (pipe(fd) == -1)
return (perror("pipe"), clear_exit(data, EXIT_FAILURE));
return (perror("pipe"), clear_all_exit(data, EXIT_FAILURE));
data->pid_array[i] = fork();
if (data->pid_array[i] < 0)
return (perror("fork"), clear_exit(data, EXIT_FAILURE));
return (perror("fork"), clear_all_exit(data, EXIT_FAILURE));
if (data->pid_array[i] == CHILD)
child(fd, data);
close(fd[1]);
Expand All @@ -34,10 +34,10 @@ 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));
return (perror("dup2"), clear_all_exit(data, EXIT_FAILURE));
close(data->hermes);
if (dup2(fd[1], STDOUT_FILENO) == -1)
return (perror("dup2"), clear_exit(data, EXIT_FAILURE));
return (perror("dup2"), clear_all_exit(data, EXIT_FAILURE));
close(fd[1]);
cmd_exe(data);
}
10 changes: 5 additions & 5 deletions src/ak_pipeout.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/05/15 03:05:49 by akdovlet #+# #+# */
/* Updated: 2024/05/19 19:16:14 by akdovlet ### ########.fr */
/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,10 +21,10 @@ void ak_pipeout(t_data *data, int i)
status = 0;
open_outfile(data);
if (data->outfile < 0)
return (perror(data->outfile_name), clear_exit(data, EXIT_FAILURE));
return (perror(data->outfile_name), clear_all_exit(data, EXIT_FAILURE));
data->pid_array[i] = fork();
if (data->pid_array[i] < 0)
return (perror("fork"), clear_exit(data, EXIT_FAILURE));
return (perror("fork"), clear_all_exit(data, EXIT_FAILURE));
if (data->pid_array[i] == CHILD)
child_out(data);
close(data->hermes);
Expand All @@ -40,10 +40,10 @@ void ak_pipeout(t_data *data, int i)
void child_out(t_data *data)
{
if (dup2(data->hermes, STDIN_FILENO) == -1)
clear_exit(data, EXIT_FAILURE);
clear_all_exit(data, EXIT_FAILURE);
close(data->hermes);
if (dup2(data->outfile, STDOUT_FILENO) == -1)
clear_exit(data, EXIT_FAILURE);
clear_all_exit(data, EXIT_FAILURE);
close(data->outfile);
cmd_exe(data);
}
16 changes: 8 additions & 8 deletions src/clear_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
/* +:+ +:+ +:+ */
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/07 00:41:28 by akdovlet #+# #+# */
/* Updated: 2024/05/19 19:14:45 by akdovlet ### ########.fr */
/* Created: 2024/05/20 18:19:12 by akdovlet #+# #+# */
/* Updated: 2024/05/20 18:19:20 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"

void clear_all_exit(t_data *data, int exit_code)
{
clear_all(data);
exit(exit_code);
}

void clear_all(t_data *data)
{
ft_free(data->path);
Expand All @@ -27,9 +33,3 @@ void clear_all(t_data *data)
if (data->outfile > 0)
close(data->outfile);
}

void clear_exit(t_data *data, int exit_code)
{
clear_all(data);
exit(exit_code);
}
60 changes: 30 additions & 30 deletions src/cmd_exe.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,39 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 22:39:03 by akdovlet #+# #+# */
/* Updated: 2024/05/19 19:37:34 by akdovlet ### ########.fr */
/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"

void nopath_exec(char *cmd, t_data *data)
void cmd_exe(t_data *data)
{
if (!file_access(cmd, X_OK))
if (!data->cmd[0])
{
ft_dprintf(STDERR, ERR_CMD, data->cmd[0], strerror(errno));
clear_all_exit(data, 127);
}
if (ft_strchr(data->cmd[0], '/'))
path_run(data->cmd[0], data);
else
nopath_run(data->cmd[0], data);
}

void path_run(char *cmd, t_data *data)
{
if (!file_access(cmd, F_OK))
{
ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno));
clear_exit(data, 126);
clear_all_exit(data, 127);
}
else if (!file_access(cmd, X_OK))
{
ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno));
clear_all_exit(data, 126);
}
execve(cmd, data->cmd, data->env);
ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno));
clear_exit(data, 127);
clear_all_exit(data, 127);
}

void nopath_run(char *cmd, t_data *data)
Expand All @@ -35,41 +52,24 @@ void nopath_run(char *cmd, t_data *data)
{
full_path = ft_strjoin(data->path[i], cmd);
if (!full_path)
return (clear_exit(data, EXIT_FAILURE));
return (clear_all_exit(data, EXIT_FAILURE));
if (file_access(full_path, F_OK))
nopath_exec(full_path, data);
free(full_path);
i++;
}
ft_dprintf(STDERR, ERR_CMD, data->cmd[0]);
clear_exit(data, 127);
clear_all_exit(data, 127);
}

void path_run(char *cmd, t_data *data)
void nopath_exec(char *cmd, t_data *data)
{
if (!file_access(cmd, F_OK))
{
ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno));
clear_exit(data, 127);
}
else if (!file_access(cmd, X_OK))
if (!file_access(cmd, X_OK))
{
ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno));
clear_exit(data, 126);
clear_all_exit(data, 126);
}
execve(cmd, data->cmd, data->env);
clear_exit(data, 127);
}

void cmd_exe(t_data *data)
{
if (!data->cmd[0])
{
ft_dprintf(STDERR, ERR_CMD, data->cmd[0], strerror(errno));
clear_exit(data, 127);
}
if (ft_strchr(data->cmd[0], '/'))
path_run(data->cmd[0], data);
else
nopath_run(data->cmd[0], data);
ft_dprintf(STDERR, ERR_MSG, cmd, strerror(errno));
clear_all_exit(data, 127);
}
10 changes: 5 additions & 5 deletions src/dr_here.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/05/15 00:17:44 by akdovlet #+# #+# */
/* Updated: 2024/05/19 19:04:32 by akdovlet ### ########.fr */
/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,10 +19,10 @@ void dr_here(t_data *data)
pid_t pid;

if (pipe(fd) == -1)
clear_exit(data, EXIT_FAILURE);
clear_all_exit(data, EXIT_FAILURE);
pid = fork();
if (pid == -1)
clear_exit(data, EXIT_FAILURE);
clear_all_exit(data, EXIT_FAILURE);
if (pid == CHILD)
dr_dre(data, fd);
close(fd[1]);
Expand All @@ -41,7 +41,7 @@ void dr_dre(t_data *data, int *fd)
write(STDOUT_FILENO, "> ", 2);
line = get_next_line(STDIN_FILENO);
if (!line)
clear_exit(data, EXIT_FAILURE);
clear_all_exit(data, EXIT_FAILURE);
if (delimiter_cmp(data->here_doc_delimiter, line))
{
free(line);
Expand All @@ -51,7 +51,7 @@ void dr_dre(t_data *data, int *fd)
free(line);
}
close(fd[1]);
clear_exit(data, EXIT_SUCCESS);
clear_all_exit(data, EXIT_SUCCESS);
}

bool delimiter_cmp(char *s1, char *s2)
Expand Down
8 changes: 4 additions & 4 deletions src/seek_and_execute.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/05/06 23:52:25 by akdovlet #+# #+# */
/* Updated: 2024/05/19 19:05:11 by akdovlet ### ########.fr */
/* Updated: 2024/05/20 18:06:43 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,20 +19,20 @@ void seek_and_execute(t_data *data)
i = 0;
data->pid_array = malloc(sizeof(pid_t) * data->cmd_count);
if (!data->pid_array)
return (clear_exit(data, EXIT_FAILURE));
return (clear_all_exit(data, EXIT_FAILURE));
if (data->here_doc_delimiter)
dr_here(data);
while (i < data->cmd_count - 1)
{
data->cmd = ft_multi_split(data->commands[i], " '");
if (!data->cmd)
return (clear_exit(data, EXIT_FAILURE));
return (clear_all_exit(data, EXIT_FAILURE));
ak_pipe(data, i);
ft_free(data->cmd);
i++;
}
data->cmd = ft_multi_split(data->commands[i], " '");
if (!data->cmd)
return (clear_exit(data, EXIT_FAILURE));
return (clear_all_exit(data, EXIT_FAILURE));
ak_pipeout(data, i);
}

0 comments on commit c28da88

Please sign in to comment.