update: pushing the final version of pipex

This commit is contained in:
Raphaël 2024-10-30 12:47:31 +01:00 committed by GitHub
parent ff670cd9d4
commit eb2d347b5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 1044 additions and 0 deletions

132
Makefile Normal file
View file

@ -0,0 +1,132 @@
# **************************************************************************** #
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/11/12 11:05:05 by rparodi #+# #+# #
# Updated: 2024/04/24 07:12:43 by rparodi ### ########.fr #
# #
# **************************************************************************** #
# Variables
# Name
NAME = pipex
LIBDIRNAME = libft
SRCDIRNAME = sources
# Commands
CC = cc
RM = rm -rf
# Flags
CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3
# Sources
LIB = libft/ft_split.c \
libft/ft_strdup.c \
libft/ft_strlcpy.c \
libft/ft_strlen.c \
libft/ft_printf.c \
libft/ft_strjoin.c \
libft/ft_memcpy.c \
libft/ft_put.c
SRC = sources/ft_exit.c \
sources/ft_check.c \
sources/ft_init.c \
sources/ft_exec.c \
sources/main.c
# Objects
OBJDIRNAME = ./objects
OBJ = $(addprefix $(OBJDIRNAME)/,$(SRC:.c=.o))
LIB_OBJ = $(addprefix $(OBJDIRNAME)/,$(LIB:.c=.o))
# Colors
GREEN = \033[32m
GREY = \033[0;90m
RED = \033[0;31m
GOLD = \033[38;5;220m
END = \033[0m
# Rules
# All (make all)
all: header $(NAME) footer
# Bonus (make bonus)
bonus: header $(OBJ) $(LIB_OBJ) footer
@mkdir -p $(OBJDIRNAME)
@mkdir -p $(OBJDIRNAME)/$(LIBDIRNAME)
@mkdir -p $(OBJDIRNAME)/$(SRCDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@cc $(CFLAGS) -o $(NAME) $(OBJ) $(LIB_OBJ)
# Clean (make clean)
clean:
@printf '$(GREY) Removing $(END)$(RED)Objects$(END)\n'
@printf '$(GREY) Removing $(END)$(RED)Objects Folder$(END)\n'
@$(RM) $(OBJDIRNAME)
# Clean (make fclean)
fclean: clean
@printf '$(GREY) Removing $(END)$(RED)Program$(END)\n'
@$(RM) $(NAME)
@echo ""
# Restart (make re)
re: header fclean all
# Dependences for all
$(NAME): $(OBJ) $(LIB_OBJ)
@mkdir -p $(OBJDIRNAME)
@mkdir -p $(OBJDIRNAME)/$(LIBDIRNAME)
@mkdir -p $(OBJDIRNAME)/$(SRCDIRNAME)
@printf '$(GREY) Creating $(END)$(GREEN)$(OBJDIRNAME)$(END)\n'
@cc $(CFLAGS) -o $(NAME) $(OBJ) $(LIB_OBJ)
# Creating the objects
$(OBJDIRNAME)/%.o: %.c
@mkdir -p $(dir $@)
@printf '$(GREY) Compiling $(END)$(GREEN)$<$(END)\n'
@cc $(CFLAGS) -o $@ -c $<
# Header
header:
@clear
@printf '\n\n'
@printf '$(GOLD) ******* ****** ******* $(END)\n'
@printf '$(GOLD) ****** *** ******* $(END)\n'
@printf '$(GOLD) ******* * ******* $(END)\n'
@printf '$(GOLD) ****** ******* $(END)\n'
@printf '$(GOLD) ******* ******* $(END)\n'
@printf '$(GOLD) ******************* ******* * $(END)\n'
@printf '$(GOLD) ******************* ******* *** $(END)\n'
@printf '$(GOLD) ****** ******* ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GOLD) ****** $(END)\n'
@printf '$(GREY) Made by rparodi$(END)\n\n'
# Footer
footer:
@printf "\n"
@printf "$(GOLD) ,_ _,$(END)\n"
@printf "$(GOLD) | \\___//|$(END)\n"
@printf "$(GOLD) |=6 6=|$(END)\n"
@printf "$(GOLD) \\=._Y_.=/$(END)\n"
@printf "$(GOLD) ) \` ( ,$(END)\n"
@printf "$(GOLD) / \\ (('$(END)\n"
@printf "$(GOLD) | | ))$(END)\n"
@printf "$(GOLD) /| | | |\\_//$(END)\n"
@printf "$(GOLD) \\| |._.| |/-\`$(END)\n"
@printf "$(GOLD) '\"' '\"'$(END)\n"
@printf ' $(GREY)The compilation is$(END) $(GOLD)finish$(END)\n $(GREY)Have a good $(END)$(GOLD)correction !$(END)\n'
# Phony
.PHONY: all bonus clean fclean re
-include ${OBJ:.o=.d}

82
includes/pipex.h Normal file
View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 10:50:20 by rparodi #+# #+# */
/* Updated: 2024/04/24 09:17:06 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIPEX_H
# define PIPEX_H
# include <unistd.h>
# include <fcntl.h>
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <sys/types.h>
# include <sys/wait.h>
# include <stdlib.h>
# include <stdarg.h>
# include <unistd.h>
# define UINT_MAX 4294967295
# define INT_MAX 2147483647
# define INT_MIN -2147483648
typedef struct s_utils
{
char *path_input;
char *path_output;
char *first_cmd_args;
char *second_cmd_args;
char *first_cmd_path;
char *second_cmd_path;
char *check_first_cmd;
char *first_cmd;
char *check_second_cmd;
char *second_cmd;
char **path;
char **envp;
int fd_output;
int fd_input;
} t_utils;
int main(int argc, char *argv[], char *args_env[]);
int ft_fprintf(int fd, const char *str, ...);
int ft_check_percent_args(int fd, char c, va_list args, int *ret_value);
int ft_open_fds(t_utils *utils, int *fd, int cmd_count);
char *ft_check_cmds(t_utils *utils, char *command);
char *ft_strjoin(char const *s1, char const *s2);
char **ft_split(char const *s, char c);
char *ft_strjoin_before_space(char *str);
char *ft_strdup(const char *s);
void ft_free(void *ptr);
void ft_pipex(t_utils *utils);
void *ft_memcpy(void *dest, const void *src, size_t n);
void ft_exec_cmd(t_utils *utils, char *cmd, char *cmd_args);
void ft_free_strs(char **strs);
void ft_check_args(int argc, char *argv[]);
void ft_errmsg(char *msg);
void ft_wait(pid_t pid1, pid_t pid2, t_utils *utils, int *fd);
void ft_close(t_utils *utils, int *fd);
void ft_exit(t_utils *p1, void *p2, short int exit_status, char *msg);
void ft_init_arge(char *arge[], t_utils *utils);
void ft_init_args(char *argv[], t_utils *utils);
void ft_putnbr_unsigned(int fd, unsigned int nb, int *ret_value);
void ft_putnbr(int fd, int nb, int *ret_value);
void ft_putchar(int fd, char c, int *ret_value);
void ft_free_struct(t_utils *utils);
void ft_putstr(int fd, char *str, int *ret_value);
void ft_init_cmd_path(char *argv[], t_utils *utils);
void ft_putnbr_base(\
unsigned long long nbr, char *base, int *ret_value, char c);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
size_t ft_strlen(const char *s);
size_t ft_strlcpy(char *dst, const char *src, size_t size);
#endif

32
libft/ft_memcpy.c Normal file
View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:49:46 by rparodi #+# #+# */
/* Updated: 2024/03/17 12:30:12 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
char *d;
const char *s;
size_t i;
i = 0;
if (!dest && !src)
return (NULL);
d = (char *)dest;
s = (char *)src;
while (i < n)
{
d[i] = s[i];
i++;
}
return ((void *)d);
}

65
libft/ft_printf.c Normal file
View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_printf.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 17:27:44 by rparodi #+# #+# */
/* Updated: 2024/03/15 14:22:23 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
int ft_check_percent_args(int fd, char c, va_list args, int *ret_value)
{
if (c == 'c')
ft_putchar(fd, (char)va_arg(args, int), ret_value);
else if (c == 's')
ft_putstr(fd, (char *)va_arg(args, char *), ret_value);
else if (c == 'i' || c == 'd')
ft_putnbr(fd, (int)va_arg(args, int), ret_value);
else if (c == '%')
ft_putchar(fd, '%', ret_value);
else if (c == 'u')
ft_putnbr_unsigned(fd, \
(unsigned int)va_arg(args, unsigned int), ret_value);
else if (c == 'x')
ft_putnbr_base((unsigned int)va_arg(args, unsigned int), \
"0123456789abcdef", ret_value, c);
else if (c == 'X')
ft_putnbr_base((unsigned int)va_arg(args, unsigned int), \
"0123456789ABCDEF", ret_value, c);
else if (c == 'p')
ft_putnbr_base((unsigned long long)va_arg(args, unsigned long long), \
"0123456789abcdef", ret_value, c);
va_end(args);
return (1);
}
int ft_fprintf(int fd, const char *s, ...)
{
size_t i;
va_list args;
char *str;
int ret_value;
ret_value = 0;
str = ft_strdup(s);
va_start(args, s);
i = 0;
while (str[i])
{
if (str[i] == '%')
{
ft_check_percent_args(fd, str[i + 1], args, &ret_value);
i++;
}
else
ft_putchar(fd, str[i], &ret_value);
i++;
}
free(str);
return (ret_value);
}

82
libft/ft_put.c Normal file
View file

@ -0,0 +1,82 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_put.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 12:13:14 by rparodi #+# #+# */
/* Updated: 2024/03/15 14:22:53 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void ft_putchar(int fd, char c, int *ret_value)
{
write(fd, &c, 1);
(*ret_value)++;
}
void ft_putnbr(int fd, int nb, int *ret_value)
{
if (nb < 0)
{
if (nb == INT_MIN)
{
write(fd, "-2147483648", 11);
*ret_value += 11;
return ;
}
nb = -nb;
ft_putchar(fd, '-', ret_value);
}
if (nb >= 10)
{
ft_putnbr(fd, nb / 10, ret_value);
nb = nb % 10;
}
if (nb < 10)
ft_putchar(fd, nb + 48, ret_value);
}
void ft_putnbr_base(\
unsigned long long nbr, char *base, int *ret_value, char c)
{
if (c == 'p')
{
if (nbr != 0)
ft_putstr(1, "0x", ret_value);
else if (nbr == 0)
{
ft_putstr(1, "(nil)", ret_value);
return ;
}
c++;
}
if (c != 'p')
{
if (nbr >= 16)
ft_putnbr_base(nbr / 16, base, ret_value, c);
ft_putchar(1, base[nbr % 16], ret_value);
}
}
void ft_putnbr_unsigned(int fd, unsigned int nb, int *ret_value)
{
if (nb >= 10)
{
ft_putnbr_unsigned(fd, nb / 10, ret_value);
nb = nb % 10;
}
if (nb < 10)
ft_putchar(fd, nb + 48, ret_value);
}
void ft_putstr(int fd, char *str, int *ret_value)
{
if (!str)
*ret_value += write(fd, "(null)", 6);
else
*ret_value += write(fd, str, ft_strlen(str));
}

93
libft/ft_split.c Normal file
View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */
/* Updated: 2024/03/11 12:21:45 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
static int count_words(const char *str, char sep)
{
int i;
int count;
i = 0;
count = 0;
while (str[i])
{
while (str[i] == sep && str[i])
i++;
if (str[i] != sep && str[i])
{
while (str[i] != sep && str[i])
i++;
count++;
}
}
return (count);
}
static char *ft_strndup(const char *s, int j)
{
int i;
char *str;
i = 0;
str = (char *)malloc((j + 1));
if (!str)
return (NULL);
while (s[i] && i < j)
{
str[i] = s[i];
i++;
}
str[i] = '\0';
return (str);
}
static char **ext_w(char **words_array, const char *str, char sep, int size)
{
int i;
int j;
i = 0;
j = 0;
while (j < size)
{
while (str[i] == sep && str[i])
i++;
str = str + i;
i = 0;
while (str[i] != sep && str[i])
i++;
words_array[j++] = ft_strndup(str, i);
str = str + i;
i = 0;
}
words_array[j] = 0;
return (words_array);
}
char **ft_split(char const *s, char c)
{
int size;
char **words_array;
size = count_words(s, c);
words_array = (char **)malloc(sizeof(char *) * (size + 1));
if (!words_array)
return (NULL);
if (size == 0)
{
words_array[0] = NULL;
return (words_array);
}
words_array = ext_w(words_array, s, c, size);
return (words_array);
}

26
libft/ft_strdup.c Normal file
View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strdup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:53:59 by rparodi #+# #+# */
/* Updated: 2024/03/12 11:26:15 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
char *ft_strdup(const char *s)
{
size_t len;
char *to_return;
len = ft_strlen(s) + 1;
to_return = (char *)malloc(sizeof(char) * len);
if (!to_return)
return (NULL);
ft_strlcpy(to_return, s, len);
return (to_return);
}

39
libft/ft_strjoin.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strjoin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 13:55:15 by rparodi #+# #+# */
/* Updated: 2024/03/12 21:48:21 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
char *ft_strjoin(char const *s1, char const *s2)
{
size_t i;
size_t save_i;
char *str;
i = 0;
str = (char *)malloc((ft_strlen(s1) + ft_strlen(s2)) + 1);
if (!str)
return (NULL);
while (s1[i] != '\0')
{
str[i] = s1[i];
i++;
}
save_i = i;
i = 0;
while (s2[i] != '\0')
{
str[save_i + i] = s2[i];
i++;
}
str[i + save_i] = '\0';
return (str);
}

31
libft/ft_strlcpy.c Normal file
View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:55:25 by rparodi #+# #+# */
/* Updated: 2024/03/12 11:26:15 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
size_t ft_strlcpy(char *dst, const char *src, size_t size)
{
size_t i;
i = 0;
while (src[i] && i + 1 < size)
{
dst[i] = src[i];
i++;
}
if (size > 0)
{
dst[i] = '\0';
i++;
}
return (ft_strlen(src));
}

23
libft/ft_strlen.c Normal file
View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 16:56:24 by rparodi #+# #+# */
/* Updated: 2024/03/17 17:37:27 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
size_t ft_strlen(const char *s)
{
size_t i;
i = 0;
while (s[i] != '\0')
i++;
return (i);
}

63
sources/ft_check.c Normal file
View file

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:15:49 by rparodi #+# #+# */
/* Updated: 2024/04/24 09:19:46 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void ft_check_args(int argc, char *argv[])
{
int i;
i = 1;
if (argc > 5)
ft_exit(NULL, NULL, 1, "Too many arguments !\n");
else if (argc < 5)
ft_exit(NULL, NULL, 1, "Not enough arguments !\n");
while (i <= 4)
{
if (argv[i][0] == '\0')
ft_exit(NULL, NULL, 1, "The args are not valid !\n");
i++;
}
if ((argv[2][0] == '.' && argv[2][1] == '\0') || \
(argv[3][0] == '.' && argv[3][1] == '\0'))
ft_exit(NULL, NULL, 1, "Don't have enought args\n");
if (access(argv[1], R_OK) == -1)
ft_exit(NULL, NULL, 1, "Don't have access of file 1\n");
}
char *ft_check_cmds(t_utils *utils, char *command)
{
size_t i;
char *str;
i = 0;
str = NULL;
if (access(command, X_OK) != -1)
return (ft_strdup(command));
else if (command && ft_strlen(command) != 1)
{
while (utils->path[i] != NULL)
{
if (str)
ft_free(str);
str = ft_strjoin(utils->path[i], command);
if (str == NULL)
ft_exit(utils, NULL, 1, \
"The alloc to check_cmds has crashed !\n");
if (access(str, F_OK) == -1 || access(str, X_OK) == -1)
i++;
else
return (str);
}
}
return (ft_exit(utils, str, 1, "No commands found !\n"), NULL);
}

100
sources/ft_exec.c Normal file
View file

@ -0,0 +1,100 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exec.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/15 14:23:10 by rparodi #+# #+# */
/* Updated: 2024/04/24 09:17:46 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void ft_close(t_utils *utils, int *fd)
{
if (utils->fd_output > 2)
close(utils->fd_output);
if (utils->fd_input > 2)
close(utils->fd_input);
if (fd[0] > 2)
close(fd[0]);
if (fd[1] > 2)
close(fd[1]);
}
int ft_open_fds(t_utils *utils, int *fd, int cmd_count)
{
if (cmd_count == 1)
{
close(fd[0]);
close(utils->fd_output);
dup2(utils->fd_input, 0);
close(utils->fd_input);
dup2(fd[1], 1);
close(fd[1]);
}
else if (cmd_count == 2)
{
close(fd[1]);
close(utils->fd_input);
dup2(fd[0], 0);
close(fd[0]);
dup2(utils->fd_output, 1);
close(utils->fd_output);
}
else
{
close(fd[0]);
close(fd[1]);
}
return (cmd_count);
}
void ft_wait(pid_t pid1, pid_t pid2, t_utils *utils, int *fd)
{
if (pid1 != 0 || pid2 != 0)
ft_open_fds(utils, fd, 0);
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
}
void ft_pipex(t_utils *utils)
{
int fd[2];
pid_t pid1;
pid_t pid2;
pipe(fd);
pid1 = fork();
if (pid1 == -1)
ft_exit(utils, NULL, 1, "Fork failed!\n");
if (pid1 == 0)
{
ft_open_fds(utils, fd, 1);
ft_exec_cmd(utils, utils->check_first_cmd, utils->first_cmd_args);
ft_close(utils, fd);
}
pid2 = fork();
if (pid2 == -1)
ft_exit(utils, NULL, 1, "Fork failed!\n");
if (pid2 == 0)
{
ft_open_fds(utils, fd, 2);
ft_exec_cmd(utils, utils->check_second_cmd, utils->second_cmd_args);
ft_close(utils, fd);
}
ft_wait(pid1, pid2, utils, fd);
}
void ft_exec_cmd(t_utils *utils, char *cmd, char *cmd_args)
{
const char **args = (const char **)ft_split(cmd_args, ' ');
if (execve(cmd, (char *const *)args, (char *const *)utils->envp) == -1)
{
ft_free_strs((char **)args);
ft_exit(utils, NULL, 1, "Execution of the command failed!\n");
}
}

83
sources/ft_exit.c Normal file
View file

@ -0,0 +1,83 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/09 18:22:08 by rparodi #+# #+# */
/* Updated: 2024/04/24 06:37:20 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void ft_free_strs(char **strs)
{
size_t i;
i = 0;
while (strs[i] != NULL)
{
ft_free(strs[i]);
i++;
}
ft_free(strs);
}
void ft_free(void *ptr)
{
if (ptr != NULL)
free(ptr);
ptr = NULL;
}
void ft_free_struct2(t_utils *utils)
{
if (utils->first_cmd)
ft_free(utils->first_cmd);
if (utils->second_cmd)
ft_free(utils->second_cmd);
if (utils->fd_output > 2)
close(utils->fd_output);
if (utils->fd_input > 2)
close(utils->fd_input);
}
void ft_free_struct(t_utils *utils)
{
if (utils->path)
ft_free_strs(utils->path);
if (utils->path_input)
ft_free(utils->path_input);
if (utils->path_output)
ft_free(utils->path_output);
if (utils->first_cmd_args)
ft_free(utils->first_cmd_args);
if (utils->second_cmd_args)
ft_free(utils->second_cmd_args);
if (utils->first_cmd_path)
ft_free(utils->first_cmd_path);
if (utils->second_cmd_path)
ft_free(utils->second_cmd_path);
if (utils->check_first_cmd)
ft_free(utils->check_first_cmd);
if (utils->check_second_cmd)
ft_free(utils->check_second_cmd);
ft_free_struct2(utils);
ft_free(utils);
}
void ft_exit(t_utils *utils, void *norman, short int exit_status, char *msg)
{
if (utils != NULL)
ft_free_struct(utils);
if (norman != NULL)
ft_free(norman);
if (exit_status == 1)
{
ft_fprintf(2, "Error:\n");
ft_fprintf(1 + exit_status, "> %s", msg);
}
exit(exit_status);
}

127
sources/ft_init.c Normal file
View file

@ -0,0 +1,127 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/12 22:15:24 by rparodi #+# #+# */
/* Updated: 2024/04/24 09:15:51 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
char *ft_cmd_args(char *str)
{
size_t i;
char *tempo;
i = 0;
tempo = NULL;
while (str[i] != '\0' && str[i] != 32)
i++;
if (str[i] != '\0')
tempo = ft_strdup(str + i + 1);
return (tempo);
}
char *ft_strjoin_before_space(char *str)
{
size_t i;
char *result;
i = 0;
if (str == NULL)
return (NULL);
while (str[i] != '\0' && str[i] != ' ')
i++;
if (str[i] == ' ')
{
result = (char *)malloc(sizeof(char) * (i + 1));
if (result == NULL)
return (NULL);
ft_memcpy(result, str, i);
result[i] = '\0';
return (result);
}
return (ft_strdup(str));
}
void ft_init_args2(char *argv[], t_utils *utils)
{
utils->first_cmd_args = ft_strdup(argv[2]);
if (!utils->first_cmd_args)
ft_exit(utils, NULL, 1, "first_cmd_args alloc has crashed !\n");
utils->second_cmd_args = ft_strdup(argv[3]);
if (!utils->first_cmd_args)
ft_exit(utils, NULL, 1, "second_cmd_args alloc has crashed !\n");
utils->fd_input = open(utils->path_input, O_RDWR);
if (utils->fd_input == -1)
ft_exit(utils, NULL, 1, "First file cannot be open !\n");
utils->fd_output = open(utils->path_output, \
O_RDWR | O_CREAT | O_TRUNC, 0644);
if (utils->fd_output == -1)
ft_exit(utils, NULL, 1, "Second file cannot be open !\n");
if (access(utils->path_input, F_OK) == -1)
ft_exit(utils, NULL, 1, "File input doesn't exist !\n");
else if (access(utils->path_input, R_OK) == -1 || \
access(utils->path_input, W_OK) == -1)
ft_exit(utils, NULL, 1, "The file 1 is not chmod correctily!\n");
else if (access(utils->path_output, R_OK) == -1 || \
access(utils->path_output, W_OK) == -1)
ft_exit(utils, NULL, 1, "The file 2 is not chmod correctily!\n");
utils->check_first_cmd = NULL;
utils->check_second_cmd = NULL;
}
void ft_init_args(char *argv[], t_utils *utils)
{
ft_init_cmd_path(argv, utils);
if (access(argv[3], X_OK) == -1)
{
utils->second_cmd = ft_strjoin_before_space(argv[3]);
if (!utils->second_cmd)
ft_exit(utils, NULL, 1, "Error during the alloc of second_cmd !\n");
utils->second_cmd_path = ft_strjoin("/", utils->second_cmd);
if (!utils->second_cmd_path)
ft_exit(utils, NULL, 1, "The join of cmd2 has failed !\n");
}
else
{
utils->first_cmd = ft_strdup(argv[3]);
utils->first_cmd_path = ft_strdup(argv[3]);
}
utils->path_output = ft_strdup(argv[4]);
if (!utils->path_output)
ft_exit(utils, NULL, 1, "path_output alloc has crashed !\n");
utils->path_input = ft_strdup(argv[1]);
if (!utils->path_input)
ft_exit(utils, NULL, 1, "path_input alloc has crashed !\n");
ft_init_args2(argv, utils);
}
void ft_init_arge(char *arge[], t_utils *utils)
{
size_t i;
char *temp;
i = 0;
temp = NULL;
while (arge[i] != NULL)
{
if (arge[i][0] == 'P' && arge[i][1] == 'A' && arge[i][2] == 'T' && \
arge[i][3] == 'H' && arge[i][4] == '=')
{
temp = ft_strdup(arge[i] + 5);
if (!temp)
ft_exit(utils, NULL, 1, "The path has a probleme");
else
utils->path = ft_split(temp, ':');
break ;
}
i++;
}
if (temp != NULL)
ft_free(temp);
}

66
sources/main.c Normal file
View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 11:46:00 by rparodi #+# #+# */
/* Updated: 2024/04/24 09:16:54 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/pipex.h"
void ft_init_cmd_path(char *argv[], t_utils *utils)
{
if (access(argv[2], X_OK) == -1)
{
utils->first_cmd = ft_strjoin_before_space(argv[2]);
if (!utils->first_cmd)
ft_exit(utils, NULL, 1, "Error during the alloc of first_cmd !\n");
utils->first_cmd_path = ft_strjoin("/", utils->first_cmd);
if (!utils->first_cmd_path)
ft_exit(utils, NULL, 1, "The join of cmd 1 has failed !\n");
}
else
{
utils->first_cmd = ft_strdup(argv[2]);
utils->first_cmd_path = ft_strdup(argv[2]);
}
}
int ft_check_path(char *arge[])
{
size_t i;
i = 0;
while (arge[i] != NULL)
{
if (arge[i][0] == 'P' && arge[i][1] == 'A' && arge[i][2] == 'T' && \
arge[i][3] == 'H' && arge[i][4] == '=')
return (1);
i++;
}
return (0);
}
int main(int argc, char *argv[], char *maiboyerlpb[])
{
t_utils *utils;
ft_check_args(argc, argv);
if (ft_check_path(maiboyerlpb) == 0 || !argv[0])
ft_exit(NULL, NULL, 1, "The path is not set!\n");
utils = (t_utils *)malloc(sizeof(t_utils));
if (!utils)
ft_exit(NULL, NULL, 1, "The allocation of the struct has crashed!\n");
utils->envp = maiboyerlpb;
ft_init_arge(maiboyerlpb, utils);
ft_init_args(argv, utils);
utils->check_first_cmd = ft_check_cmds(utils, utils->first_cmd_path);
utils->check_second_cmd = ft_check_cmds(utils, utils->second_cmd_path);
ft_pipex(utils);
ft_exit(utils, NULL, 0, "");
return (0);
}