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

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);
}