Skip to content

Commit

Permalink
got 99 problems but
Browse files Browse the repository at this point in the history
  • Loading branch information
akdovlet committed May 15, 2024
1 parent ac886b0 commit d90e7e7
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 111 deletions.
25 changes: 13 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,22 @@
# By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/01/01 13:57:12 by akdovlet #+# #+# #
# Updated: 2024/05/15 00:48:39 by akdovlet ### ########.fr #
# Updated: 2024/05/15 03:37:02 by akdovlet ### ########.fr #
# #
# **************************************************************************** #

NAME = pipex
LIBDIR = lib
LIBFT = $(LIBDIR)/libft/libft.a

SRC := check_file.c \
free.c \
main.c \
dr_here.c \
ak_pipe.c \
parsing.c \
px_split.c \
NAME := pipex
LIBDIR := lib
LIBFT := $(LIBDIR)/libft/libft.a

SRC := ak_pipe.c \
ak_pipeout.c \
check_file.c \
dr_here.c \
env_access.c \
free_exit.c \
main.c \
px_split.c \
seek_and_execute.c \
setup.c
SRC_DIR := src
Expand Down
44 changes: 29 additions & 15 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/15 01:32:15 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 03:35:50 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -43,37 +43,51 @@ typedef struct s_data
int here_doc;
} t_data;

/******************************check_file.c***********************************/
int check_file(char *file);
/******************************ak_pipe.c**************************************/

// Child function for every command except for the last one, executes cmd
void child(int fd[2], t_data *data, int i);
// Will pipe and fork making every cmd communicate, calls child
void ak_pipe(t_data *data, int i);

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

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

/********************************free.c***************************************/
void ft_free(char **str);
void clear_all(t_data *data);
void clear_exit(t_data *data, int exit_code);
/******************************check_file.c***********************************/

/******************************ak_pipe.c*********************************/
void ak_pipe(t_data *data, int i);
void ak_pipeout(t_data *data, int i);
// Checks for reading rights on a given file, opens it if ok
int check_file(char *file);

/******************************dr_here.c**************************************/

// Forgot about Dre
void dr_dre(t_data *data, int *fd);
//
void dr_here(t_data *data);

/********************************env_access.c**********************************/
char **parse_env(char **env);
bool file_access(char *file, int check);
void find_exec(char *cmd, t_data *data);

/******************************check_file.c***********************************/
/********************************free_exit.c***********************************/
void ft_free(char **str);
void clear_all(t_data *data);
void clear_exit(t_data *data, int exit_code);

/******************************px_split.c**************************************/

char **px_split(char const *s, char c);

/******************************seek_and_execute.c*****************************/
void seek_and_execute(t_data *data);
void cmd_exe(t_data *data);
void find_exec(char *cmd, t_data *data);
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);
#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/09 18:01:33 by akdovlet ### ########.fr #
# Updated: 2024/05/15 03:08:03 by akdovlet ### ########.fr #
# #
# **************************************************************************** #

Expand Down Expand Up @@ -43,6 +43,7 @@ SRC := ak_countword.c \
ft_putnbr_fd.c \
ft_calloc.c \
ft_putendl_fd.c \
ft_strcmp.c \
ft_strncmp.c \
ft_bzero.c \
ft_strchr.c \
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/10 15:54:52 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 03:07:42 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -69,6 +69,7 @@ size_t ft_strlcat(char *dst, const char *src, size_t size);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlen(const char *s);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
int ft_strcmp(char *s1, char *s2);
int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strnstr(const char *big, const char *little, size_t len);
char *ft_strrchr(const char *s, int c);
Expand Down
25 changes: 25 additions & 0 deletions lib/libft/src/ft_strcmp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 03:06:59 by akdovlet #+# #+# */
/* Updated: 2024/05/15 03:07:05 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s2)
{
if (*s1 != *s2)
return (*s1 - *s2);
s1++;
s2++;
}
return (*s1 - *s2);
}
37 changes: 1 addition & 36 deletions src/ak_pipe.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ void child(int fd[2], t_data *data, int i)
if (dup2(data->hermes, STDIN_FILENO) == -1)
return (perror("pipex1"), clear_exit(data, EXIT_FAILURE));
close(data->hermes);
if(dup2(fd[1], STDOUT_FILENO) == -1)
if (dup2(fd[1], STDOUT_FILENO) == -1)
return (perror("pipex2"), clear_exit(data, EXIT_FAILURE));
close(fd[1]);
cmd_exe(data);
Expand All @@ -41,38 +41,3 @@ void ak_pipe(t_data *data, int i)
close(data->hermes);
data->hermes = fd[0];
}

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)
clear_exit(data, EXIT_FAILURE);
close(data->last);
cmd_exe(data);
}

void ak_pipeout(t_data *data, int i)
{
int status;
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_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)])
child_out(data);
close(data->hermes);
while (j < data->ac - 3)
{
waitpid(data->ids[j],&status, 0);
data->exit_code = WEXITSTATUS(status);
j++;
}
}
49 changes: 49 additions & 0 deletions src/ak_pipeout.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ak_pipeout.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 03:05:49 by akdovlet #+# #+# */
/* Updated: 2024/05/15 03:06:25 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->last, STDOUT_FILENO) == -1)
clear_exit(data, EXIT_FAILURE);
close(data->last);
cmd_exe(data);
}

void ak_pipeout(t_data *data, int i)
{
int status;
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_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)])
child_out(data);
close(data->hermes);
while (j < data->ac - 3)
{
waitpid(data->ids[j], &status, 0);
data->exit_code = WEXITSTATUS(status);
j++;
}
}
4 changes: 2 additions & 2 deletions src/check_file.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:36:19 by akdovlet #+# #+# */
/* Updated: 2024/05/12 15:12:57 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 02:57:09 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -17,7 +17,7 @@ int check_file(char *file)
int fd;

if (!file_access(file, R_OK))
return(perror(file), -1);
return (perror(file), -1);
else
{
fd = open(file, O_RDONLY);
Expand Down
15 changes: 2 additions & 13 deletions src/dr_here.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,12 @@
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/15 00:17:44 by akdovlet #+# #+# */
/* Updated: 2024/05/15 02:06:07 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 03:36:38 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"

int ft_strcmp(char *s1, char *s2)
{
while (*s1 && *s2)
{
if (*s1 != *s2)
return (*s1 - *s2);
s1++;
s2++;
}
return (*s1 - *s2);
}

void dr_dre(t_data *data, int *fd)
{
char *line;
Expand All @@ -43,6 +31,7 @@ void dr_dre(t_data *data, int *fd)
write(fd[1], line, ft_strlen(line));
free(line);
}
close(fd[1]);
exit(EXIT_SUCCESS);
}

Expand Down
8 changes: 2 additions & 6 deletions src/parsing.c → src/env_access.c
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* env_access.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akdovlet <akdovlet@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/23 17:52:22 by akdovlet #+# #+# */
/* Updated: 2024/05/14 18:15:23 by akdovlet ### ########.fr */
/* Updated: 2024/05/15 03:24:00 by akdovlet ### ########.fr */
/* */
/* ************************************************************************** */

#include "pipex.h"
#include "libft.h"
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>

bool file_access(char *file, int check)
{
Expand Down
Loading

0 comments on commit d90e7e7

Please sign in to comment.