-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
416 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#SETUP | ||
NAME = pipex | ||
CC = gcc | ||
FLAGS = -Wall -Wextra -Werror | ||
RM = rm -rf | ||
|
||
#FILES AND PATH | ||
HEADER_SRCS = pipex.h pipex_bonus.h | ||
HEADER_DIR = includes/ | ||
HEADER = $(addprefix $(HEADER_DIR), $(HEADER_SRCS)) | ||
|
||
MPATH_SRCS = pipex.c childs.c error.c free.c | ||
MPATH_DIR = mandatory/ | ||
MPATH = $(addprefix $(MPATH_DIR), $(MPATH_SRCS)) | ||
OBJ_M = $(MPATH:.c=.o) | ||
|
||
BPATH_SRCS = pipex_bonus.c error_bonus.c here_doc_bonus.c\ | ||
files_bonus.c free_bonus.c child_bonus.c | ||
BPATH_DIR = bonus/ | ||
BPATH = $(addprefix $(BPATH_DIR), $(BPATH_SRCS)) | ||
OBJ_B = $(BPATH:.c=.o) | ||
|
||
FUNC_SRCS = ft_strncmp.c ft_strdup.c ft_split.c ft_strjoin.c | ||
FUNC_DIR = functions/ | ||
FUNC = $(addprefix $(FUNC_DIR), $(FUNC_SRCS))\ | ||
gnl/get_next_line_utils.c gnl/get_next_line.c | ||
OBJ_F = $(FUNC:.c=.o) | ||
|
||
#COMMANDS | ||
%.o: %.c $(HEADER) Makefile | ||
@${CC} ${FLAGS} -c $< -o $@ | ||
|
||
$(NAME): $(OBJ_F) $(OBJ_M) | ||
@$(CC) $(OBJ_F) $(OBJ_M) -o $(NAME) | ||
@echo -e "$(GREEN)$(NAME) created!$(DEFAULT)" | ||
|
||
all: $(NAME) | ||
|
||
bonus: $(OBJ_F) $(OBJ_B) | ||
@$(CC) $(OBJ_F) $(OBJ_B) -o $(NAME) | ||
@echo -e "$(GREEN)$(NAME) created!$(DEFAULT)" | ||
|
||
clean: | ||
@$(RM) $(OBJ_M) | ||
@$(RM) $(OBJ_F) | ||
@$(RM) $(OBJ_B) | ||
@echo -e "$(YELLOW)object files deleted!$(DEFAULT)" | ||
|
||
fclean: clean | ||
@$(RM) $(NAME) | ||
@echo -e "$(RED)all deleted!$(DEFAULT)" | ||
|
||
re: fclean all | ||
|
||
.PHONY: all clean fclean bonus re | ||
|
||
#COLORS | ||
RED = \033[1;31m | ||
GREEN = \033[1;32m | ||
YELLOW = \033[1;33m | ||
DEFAULT = \033[0m |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* pipex.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/07 14:48:42 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 20:44:01 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef PIPEX_H | ||
# define PIPEX_H | ||
|
||
/* to write, read, close, access, pipe, dup, dup2, execve, fork */ | ||
# include <unistd.h> | ||
# include <sys/types.h> | ||
# include <sys/uio.h> | ||
|
||
/* malloc, free, exit */ | ||
# include <stdlib.h> | ||
|
||
/* open, unlink */ | ||
# include <fcntl.h> | ||
|
||
/* waitpid, wait */ | ||
# include <sys/wait.h> | ||
|
||
/* strerror */ | ||
# include <string.h> | ||
|
||
/*to perror*/ | ||
# include <stdio.h> | ||
|
||
/* to get_next_line */ | ||
# include "../gnl/get_next_line.h" | ||
|
||
# define ERR_INFILE "Infile" | ||
# define ERR_OUTFILE "Outfile" | ||
# define ERR_INPUT "Invalid number of arguments.\n" | ||
# define ERR_PIPE "Pipe" | ||
# define ERR_CMD "Command not found\n" | ||
|
||
typedef struct s_pipex | ||
{ | ||
pid_t pid1; | ||
pid_t pid2; | ||
int tube[2]; | ||
int infile; | ||
int outfile; | ||
char *paths; | ||
char **cmd_paths; | ||
char **cmd_args; | ||
char *cmd; | ||
}t_pipex; | ||
|
||
/* childs.c */ | ||
void first_child(t_pipex pipex, char *argv[], char *envp[]); | ||
void second_child(t_pipex pipex, char *argv[], char *envp[]); | ||
|
||
/* free.c */ | ||
void parent_free(t_pipex *pipex); | ||
void child_free(t_pipex *pipex); | ||
|
||
/* error.c */ | ||
void msg_error(char *err); | ||
int msg(char *err); | ||
|
||
/* funcions */ | ||
char *ft_strjoin(char const *s1, char const *s2); | ||
char *ft_strdup(const char *src); | ||
char **ft_split(char const *s, char c); | ||
int ft_strncmp(const char *s1, const char *s2, size_t n); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* pipex_bonus.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/13 16:09:50 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 21:56:59 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef PIPEX_BONUS_H | ||
# define PIPEX_BONUS_H | ||
|
||
/* to write, read, close, access, pipe, dup, dup2, execve, fork */ | ||
# include <unistd.h> | ||
# include <sys/types.h> | ||
# include <sys/uio.h> | ||
|
||
/* malloc, free, exit */ | ||
# include <stdlib.h> | ||
|
||
/* open, unlink */ | ||
# include <fcntl.h> | ||
|
||
/* waitpid, wait */ | ||
# include <sys/wait.h> | ||
|
||
/* strerror */ | ||
# include <string.h> | ||
|
||
/*to perror*/ | ||
# include <stdio.h> | ||
|
||
/* to get_next_line */ | ||
# include "../gnl/get_next_line.h" | ||
|
||
# define ERR_INFILE "Infile" | ||
# define ERR_OUTFILE "Outfile" | ||
# define ERR_INPUT "Invalid number of arguments.\n" | ||
# define ERR_PIPE "Pipe" | ||
# define ERR_ENVP "Environment" | ||
# define ERR_CMD "Command not found: " | ||
# define ERR_HEREDOC "here_doc" | ||
|
||
typedef struct s_ppxb | ||
{ | ||
int infile; | ||
int outfile; | ||
char *env_path; | ||
char **cmd_paths; | ||
char *cmd; | ||
char **cmd_args; | ||
int here_doc; | ||
pid_t pid; | ||
int cmd_nmbs; | ||
int pipe_nmbs; | ||
int *pipe; | ||
int idx; | ||
}t_ppxb; | ||
|
||
/* pipex_bonus.c */ | ||
void close_pipes(t_ppxb *pipex); | ||
|
||
/* child_bonus.c */ | ||
void child(t_ppxb pipex, char **argv, char **envp); | ||
|
||
/* free.c */ | ||
void parent_free(t_ppxb *pipex); | ||
void child_free(t_ppxb *pipex); | ||
void pipe_free(t_ppxb *pipex); | ||
|
||
/* files_bonus.c */ | ||
char *find_path(char **envp); | ||
void get_infile(char **argv, t_ppxb *pipex); | ||
void get_outfile(char *argv, t_ppxb *pipex); | ||
|
||
/* here_doc_bonus.c */ | ||
int args_in(char *arg, t_ppxb *pipex); | ||
void here_doc(char *argv, t_ppxb *pipex); | ||
|
||
/* error_bonus.c */ | ||
void msg_error(char *err); | ||
void msg_pipe(char *arg); | ||
int msg(char *err); | ||
|
||
/* funcions */ | ||
char *ft_strjoin(char const *s1, char const *s2); | ||
char *ft_strdup(const char *src); | ||
char **ft_split(char const *s, char c); | ||
int ft_strncmp(const char *s1, const char *s2, size_t n); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* childs.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/09 14:58:40 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 20:13:30 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex.h" | ||
|
||
static char *get_cmd(char **paths, char *cmd) | ||
{ | ||
char *tmp; | ||
char *command; | ||
|
||
while (*paths) | ||
{ | ||
tmp = ft_strjoin(*paths, "/"); | ||
command = ft_strjoin(tmp, cmd); | ||
free(tmp); | ||
if (access(command, 0) == 0) | ||
return (command); | ||
free(command); | ||
paths++; | ||
} | ||
return (NULL); | ||
} | ||
|
||
void first_child(t_pipex pipex, char *argv[], char *envp[]) | ||
{ | ||
dup2(pipex.tube[1], 1); | ||
close(pipex.tube[0]); | ||
dup2(pipex.infile, 0); | ||
pipex.cmd_args = ft_split(argv[2], ' '); | ||
pipex.cmd = get_cmd(pipex.cmd_paths, pipex.cmd_args[0]); | ||
if (!pipex.cmd) | ||
{ | ||
child_free(&pipex); | ||
msg(ERR_CMD); | ||
exit(1); | ||
} | ||
execve(pipex.cmd, pipex.cmd_args, envp); | ||
} | ||
|
||
void second_child(t_pipex pipex, char *argv[], char *envp[]) | ||
{ | ||
dup2(pipex.tube[0], 0); | ||
close(pipex.tube[1]); | ||
dup2(pipex.outfile, 1); | ||
pipex.cmd_args = ft_split(argv[3], ' '); | ||
pipex.cmd = get_cmd(pipex.cmd_paths, pipex.cmd_args[0]); | ||
if (!pipex.cmd) | ||
{ | ||
child_free(&pipex); | ||
msg(ERR_CMD); | ||
exit(1); | ||
} | ||
execve(pipex.cmd, pipex.cmd_args, envp); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* error.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/08 17:43:45 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 13:28:06 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex.h" | ||
|
||
int msg(char *err) | ||
{ | ||
write(2, err, ft_strlen(err)); | ||
return (1); | ||
} | ||
|
||
void msg_error(char *err) | ||
{ | ||
perror(err); | ||
exit (1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* free.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: bcaffere <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2021/10/09 14:56:20 by bcaffere #+# #+# */ | ||
/* Updated: 2021/10/13 15:33:27 by bcaffere ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "../includes/pipex.h" | ||
|
||
void parent_free(t_pipex *pipex) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
close(pipex->infile); | ||
close(pipex->outfile); | ||
while (pipex->cmd_paths[i]) | ||
{ | ||
free(pipex->cmd_paths[i]); | ||
i++; | ||
} | ||
free(pipex->cmd_paths); | ||
} | ||
|
||
void child_free(t_pipex *pipex) | ||
{ | ||
int i; | ||
|
||
i = 0; | ||
while (pipex->cmd_args[i]) | ||
{ | ||
free(pipex->cmd_args[i]); | ||
i++; | ||
} | ||
free(pipex->cmd_args); | ||
free(pipex->cmd); | ||
} |
Oops, something went wrong.