Skip to content

Commit

Permalink
Streamlined, simplified, easier to read, thanks Jojo
Browse files Browse the repository at this point in the history
  • Loading branch information
akdovlet committed May 18, 2024
1 parent 6d86433 commit 12e32ee
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 214 deletions.
9 changes: 6 additions & 3 deletions 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/17 15:30:15 by akdovlet ### ########.fr #
# Updated: 2024/05/18 23:22:02 by akdovlet ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -17,10 +17,10 @@ LIBFT := $(LIBDIR)/libft/libft.a

SRC := ak_pipe.c \
ak_pipeout.c \
check_file.c \
clear_exit.c \
cmd_exe.c \
dr_here.c \
env_access.c \
free_exit.c \
main.c \
px_split.c \
seek_and_execute.c \
Expand All @@ -38,6 +38,8 @@ all: create_dirs $(NAME)

bonus: create_dirs $(BNAME)

both: all bonus

create_dirs: $(BUILD)

$(BUILD):
Expand All @@ -62,6 +64,7 @@ clean:

fclean: clean
@if [ -f $(NAME) ]; then $(RM) -rf $(NAME) && echo "\033[1;31m\tDeleted: $(NAME)\033[0m"; fi
@if [ -f $(BNAME) ]; then $(RM) -rf $(BNAME) && echo "\033[1;31m\tDeleted: $(BNAME)\033[0m"; fi
@$(MAKE) --no-print-directory fclean -C $(LIBDIR)/libft

re: fclean all
Expand Down
59 changes: 30 additions & 29 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/17 18:28:31 by akdovlet ### ########.fr */
/* Updated: 2024/05/18 23:10:26 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,72 +22,73 @@
# include <sys/wait.h>

# define HARDPATH "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# define CHILD 0
# define ERR_MSG "pipex: %s: %s\n"
# define ERR_CMD "pipex: %s: command not found\n"
# define STDERR 2

typedef struct s_data
{
pid_t *ids;
char **av;
int ac;
char **path;
char **env;
char **cmd;
int fd[2];
int first;
int last;
int hermes;
char **commands;
char *here_doc_delimiter;
char *outfile_name;
int cmd_count;
int infile;
int outfile;
int hermes;
int exit_code;
int here_doc;
} t_data;

/******************************ak_pipe.c**************************************/

// Child function for every command except for the last one, executes cmd
// 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
// Will pipe and fork making every cmd communicate, calls child
void ak_pipe(t_data *data, int i);

/******************************ak_pipeout.c***********************************/

// Child function for the last cmd
// Child function for the last cmd
void child_out(t_data *data);
// Will fork the last cmd and will redirect it to outfile
// Will fork the last cmd and will redirect it to outfile
void ak_pipeout(t_data *data, int i);

/******************************check_file.c***********************************/

// Checks for reading rights on a given file, opens it if ok
int check_file(char *file);
/******************************cmd_exe.c**************************************/
// Will execute the command and return the correct exit code
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);

/********************************env_access.c**********************************/
char **parse_env(char **env);
// Will check if env is empty, if so will return a hard-coded path. If env exits,
// Will return the path from the env. Otherwise returns the current dir
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);

/********************************free_exit.c***********************************/
void ft_free(char **str);
// 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
char **px_split(char const *s, char c);

/******************************seek_and_execute.c*****************************/
void cmd_exe(t_data *data);
void find_exec(char *cmd, t_data *data);
// Driver code, will try and execute each command and make them communicate
void seek_and_execute(t_data *data);

/******************************setup.c****************************************/
void infile_setup(t_data *data, char **av);
bool setup(t_data *data, int ac, char **av, char **env);

void nopath_exec(char *cmd, t_data *data);
// Iniatilizes the data struct, not here for error checks, only for setup
void setup(t_data *data, int ac, char **av, char **env);

#endif
3 changes: 2 additions & 1 deletion lib/libft/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/09/15 16:05:42 by akdovlet #+# #+# #
# Updated: 2024/05/16 22:40:48 by akdovlet ### ########.fr #
# Updated: 2024/05/18 22:56:46 by akdovlet ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -46,6 +46,7 @@ SRC := ak_countword.c \
ft_strcmp.c \
ft_strncmp.c \
ft_bzero.c \
ft_free.c \
ft_strchr.c \
ft_strrchr.c \
ft_memmove.c \
Expand Down
2 changes: 1 addition & 1 deletion lib/libft/include/ft_dprintf.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/05/09 15:06:16 by akdovlet #+# #+# */
/* Updated: 2024/05/10 18:25:51 by akdovlet ### ########.fr */
/* Updated: 2024/05/18 21:29:42 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
3 changes: 2 additions & 1 deletion lib/libft/include/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/10 17:27:13 by adovleto #+# #+# */
/* Updated: 2024/05/17 10:12:30 by akdovlet ### ########.fr */
/* Updated: 2024/05/18 22:56:26 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -47,6 +47,7 @@ void *ft_memchr(const void *s, int c, size_t n);
void *ft_memmove(void *dest, const void *src, size_t n);
void *ft_memset(void *s, int c, size_t n);
void free_array(char **strs, int len);
void ft_free(char **str);

void ft_putchar_fd(char c, int fd);
void ft_putendl_fd(char *s, int fd);
Expand Down
30 changes: 14 additions & 16 deletions src/check_file.c → lib/libft/src/ft_free.c
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_file.c :+: :+: :+: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/06 23:36:19 by akdovlet #+# #+# */
/* Updated: 2024/05/17 16:00:10 by akdovlet ### ########.fr */
/* Created: 2024/05/18 22:55:52 by akdovlet #+# #+# */
/* Updated: 2024/05/18 22:56:08 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"
#include "libft.h"

int check_file(char *file)
void ft_free(char **str)
{
int fd;
int i;

if (!file_access(file, R_OK))
i = 0;
if (!str)
return ;
while (str[i])
{
ft_dprintf(2, "pipex: %s: %s\n", file, strerror(errno));
return (-1);
free(str[i]);
i++;
}
else
{
fd = open(file, O_RDONLY);
if (fd < 0)
return (perror(file), -1);
}
return (fd);
free(str);
str = NULL;
}
4 changes: 3 additions & 1 deletion lib/libft/src/ft_multi_split.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/01/07 22:07:22 by akdovlet #+# #+# */
/* Updated: 2024/05/14 18:54:46 by akdovlet ### ########.fr */
/* Updated: 2024/05/18 20:27:02 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -48,6 +48,8 @@ char **ft_multi_split(char *str, char *sep)
char **strs;

tab = NULL;
if (!str)
return (NULL);
word_count = ak_countword(str, sep, &tab);
if (word_count == -1)
return (free(tab), NULL);
Expand Down
6 changes: 3 additions & 3 deletions lib/libft/src/ft_split.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/12 12:51:50 by akdovlet #+# #+# */
/* Updated: 2024/01/07 21:53:06 by akdovlet ### ########.fr */
/* Updated: 2024/05/18 23:20:50 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -67,7 +67,7 @@ static char *ft_strndup_custom(char const *s, int *retindex, int index, int n)
return (dup);
}

static void ft_free(char **strs, int i)
static void split_free(char **strs, int i)
{
while (i >= 0)
{
Expand Down Expand Up @@ -98,7 +98,7 @@ char **ft_split(char const *s, char c)
{
splitter[j] = ft_strndup_custom(s, &i, i, ft_charcount(s, i, c));
if (!splitter[j++])
return (ft_free(splitter, j - 1), NULL);
return (split_free(splitter, j - 1), NULL);
}
i++;
}
Expand Down
3 changes: 1 addition & 2 deletions lib/libft/src/printf_dprint.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 15:18:45 by akdovlet #+# #+# */
/* Updated: 2024/05/17 18:13:56 by akdovlet ### ########.fr */
/* Updated: 2024/05/18 21:29:38 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "ft_dprintf.h"
#include <stdio.h>

int print_buffer(int fd, char *buffer, int *j)
{
Expand Down
16 changes: 8 additions & 8 deletions src/ak_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ void child(int fd[2], t_data *data)
{
close(fd[0]);
if (dup2(data->hermes, STDIN_FILENO) == -1)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
return (perror("dup2"), clear_exit(data, EXIT_FAILURE));
close(data->hermes);
if (dup2(fd[1], STDOUT_FILENO) == -1)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
return (perror("dup2"), clear_exit(data, EXIT_FAILURE));
close(fd[1]);
cmd_exe(data);
}
Expand All @@ -28,14 +28,14 @@ void ak_pipe(t_data *data, int i)
{
int fd[2];

if (data->first == -1 && i == 2)
if (data->infile == -1 && i == 0)
return ;
if (pipe(fd) == -1)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
data->ids[i - (2 + data->here_doc)] = fork();
if (data->ids[i - (2 + data->here_doc)] < 0)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
if (!data->ids[i - (2 + data->here_doc)])
return (perror("pipe"), clear_exit(data, EXIT_FAILURE));
data->ids[i] = fork();
if (data->ids[i] < 0)
return (perror("fork"), clear_exit(data, EXIT_FAILURE));
if (data->ids[i] == CHILD)
child(fd, data);
close(fd[1]);
close(data->hermes);
Expand Down
26 changes: 13 additions & 13 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/17 10:22:50 by akdovlet ### ########.fr */
/* Updated: 2024/05/18 23:13:37 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,19 +17,19 @@ void child_out(t_data *data)
if (dup2(data->hermes, STDIN_FILENO) == -1)
clear_exit(data, EXIT_FAILURE);
close(data->hermes);
if (dup2(data->last, STDOUT_FILENO) == -1)
if (dup2(data->outfile, STDOUT_FILENO) == -1)
clear_exit(data, EXIT_FAILURE);
close(data->last);
close(data->outfile);
cmd_exe(data);
}

void open_outfile(t_data *data)
{
if (data->here_doc)
data->last = open(data->av[data->ac - 1], \
if (data->here_doc_delimiter)
data->outfile = open(data->outfile_name, \
O_WRONLY | O_CREAT | O_APPEND, 0644);
else
data->last = open(data->av[data->ac - 1], \
data->outfile = open(data->outfile_name, \
O_WRONLY | O_CREAT | O_TRUNC, 0644);
}

Expand All @@ -41,16 +41,16 @@ void ak_pipeout(t_data *data, int i)
j = 0;
status = 0;
open_outfile(data);
if (data->last < 0)
return (perror(data->av[data->ac -1]), clear_exit(data, EXIT_FAILURE));
data->ids[i - (2 + data->here_doc)] = fork();
if (data->ids[i - (2 + data->here_doc)] < 0)
return (perror("pipex"), clear_exit(data, EXIT_FAILURE));
if (!data->ids[i - (2 + data->here_doc)])
if (data->outfile < 0)
return (perror(data->outfile_name), clear_exit(data, EXIT_FAILURE));
data->ids[i] = fork();
if (data->ids[i] < 0)
return (perror("fork"), clear_exit(data, EXIT_FAILURE));
if (data->ids[i] == CHILD)
child_out(data);
close(data->hermes);
data->hermes = 0;
while (j < data->ac - (3 + data->here_doc))
while (j < data->cmd_count)
{
waitpid(data->ids[j], &status, 0);
data->exit_code = WEXITSTATUS(status);
Expand Down
Loading

0 comments on commit 12e32ee

Please sign in to comment.