[✨] feat: finished normally
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
9
fff
Normal file
9
fff
Normal file
@ -0,0 +1,9 @@
|
||||
asdf
|
||||
compile_commands.json
|
||||
fff
|
||||
include
|
||||
libft
|
||||
Makefile
|
||||
obj
|
||||
pipex
|
||||
src
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/22 21:14:06 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/06 17:26:47 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/07 17:02:48 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -33,5 +33,8 @@ void ft_senderror(t_pipex *pipex, char *msg);
|
||||
void getpath(t_pipex *pipex);
|
||||
void get_arrcmd_path(t_pipex *pipex);
|
||||
void exec_pipe(t_pipex *pipex);
|
||||
void ft_senderror(t_pipex *pipex, char *msg);
|
||||
void ft_senderror_file(t_pipex *pipex, char *msg);
|
||||
void free_pcmd(t_pcmd *cmd);
|
||||
|
||||
#endif
|
||||
|
BIN
obj/src/main.o
BIN
obj/src/main.o
Binary file not shown.
Binary file not shown.
BIN
obj/src/utils/ft_senderror.o
Normal file
BIN
obj/src/utils/ft_senderror.o
Normal file
Binary file not shown.
Binary file not shown.
Binary file not shown.
65
src/main.c
65
src/main.c
@ -6,31 +6,15 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/21 10:03:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/07 11:03:19 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/08 12:49:21 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
#include "parsing.h"
|
||||
#include "pipex.h"
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
void free_pcmd(t_pcmd *cmd)
|
||||
{
|
||||
t_pcmd *tmp;
|
||||
|
||||
tmp = cmd;
|
||||
while (tmp && tmp->cmd)
|
||||
{
|
||||
free(tmp->cmd);
|
||||
ft_freearr(tmp->option);
|
||||
tmp++;
|
||||
}
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
void check_empty_args(char **av)
|
||||
void check_empty_args(char **av, t_pipex *pipex)
|
||||
{
|
||||
char **tmp;
|
||||
char *tmp_av;
|
||||
@ -40,8 +24,6 @@ void check_empty_args(char **av)
|
||||
while (*tmp)
|
||||
{
|
||||
tmp_av = *tmp;
|
||||
if (!*tmp_av)
|
||||
ft_senderror(NULL, "You have empty argument");
|
||||
while (*tmp_av)
|
||||
{
|
||||
if (*tmp_av != 32)
|
||||
@ -49,23 +31,28 @@ void check_empty_args(char **av)
|
||||
tmp_av++;
|
||||
}
|
||||
if (!*tmp_av)
|
||||
ft_senderror(NULL, "You have empty argument");
|
||||
{
|
||||
ft_putendl_fd("You have empty argument", STDERR_FILENO);
|
||||
free(pipex);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
tmp++;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_senderror(t_pipex *pipex, char *msg)
|
||||
void check_files(int ac, char **av, t_pipex *pipex)
|
||||
{
|
||||
(void)pipex;
|
||||
ft_putendl_fd(msg, STDERR_FILENO);
|
||||
if (!pipex)
|
||||
exit(EXIT_FAILURE);
|
||||
if (pipex->path)
|
||||
ft_freearr(pipex->path);
|
||||
if (pipex->cmd)
|
||||
free_pcmd(pipex->cmd);
|
||||
free(pipex);
|
||||
exit(EXIT_FAILURE);
|
||||
int out;
|
||||
|
||||
pipex->infile = open(av[1], O_RDONLY);
|
||||
pipex->outfile = av[ac - 1];
|
||||
if (pipex->infile == -1)
|
||||
ft_senderror_file(pipex, "Error: Infile can't be opened");
|
||||
dup2(pipex->infile, STDIN_FILENO);
|
||||
out = open(pipex->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (out < 0)
|
||||
ft_senderror_file(pipex, "Error: Outfile can't be opened");
|
||||
close(out);
|
||||
}
|
||||
|
||||
int main(int ac, char **av, char **env)
|
||||
@ -74,18 +61,18 @@ int main(int ac, char **av, char **env)
|
||||
|
||||
pipex = malloc(sizeof(t_pipex));
|
||||
if (ac != 5)
|
||||
ft_senderror(pipex, "Error : Invalid number of args");
|
||||
check_empty_args(av);
|
||||
pipex->infile = open(av[1], O_RDONLY);
|
||||
dup2(pipex->infile, STDIN_FILENO);
|
||||
pipex->outfile = av[ac - 1];
|
||||
{
|
||||
free(pipex);
|
||||
ft_putendl_fd("Error : Invalid number of args", STDERR_FILENO);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
check_empty_args(av, pipex);
|
||||
check_files(ac, av, pipex);
|
||||
pipex->cmd = parse_cmd(ac - 3, av);
|
||||
pipex->env = env;
|
||||
getpath(pipex);
|
||||
if (pipex->path == NULL)
|
||||
ft_senderror(pipex, "Error : Can't find path");
|
||||
ft_strlcpy(*(pipex->path), *(pipex->path) + 5, \
|
||||
ft_strlen(*(pipex->path) + 5) + 1);
|
||||
get_arrcmd_path(pipex);
|
||||
exec_pipe(pipex);
|
||||
ft_freearr(pipex->path);
|
||||
|
61
src/utils/ft_senderror.c
Normal file
61
src/utils/ft_senderror.c
Normal file
@ -0,0 +1,61 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_senderror.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/07 16:59:53 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/07 17:14:06 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "pipex.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void free_pcmd(t_pcmd *cmd)
|
||||
{
|
||||
t_pcmd *tmp;
|
||||
|
||||
tmp = cmd;
|
||||
while (tmp && tmp->cmd)
|
||||
{
|
||||
free(tmp->cmd);
|
||||
ft_freearr(tmp->option);
|
||||
tmp++;
|
||||
}
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
void ft_senderror(t_pipex *pipex, char *msg)
|
||||
{
|
||||
ft_putendl_fd(msg, STDERR_FILENO);
|
||||
if (!pipex)
|
||||
exit(EXIT_FAILURE);
|
||||
if (pipex->infile > -1)
|
||||
close(pipex->infile);
|
||||
if (pipex->cmd)
|
||||
free_pcmd(pipex->cmd);
|
||||
if (pipex->path)
|
||||
ft_freearr(pipex->path);
|
||||
free(pipex);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
void ft_senderror_file(t_pipex *pipex, char *msg)
|
||||
{
|
||||
int out;
|
||||
|
||||
ft_putendl_fd(msg, STDERR_FILENO);
|
||||
if (pipex->infile > -1)
|
||||
close(pipex->infile);
|
||||
out = open(pipex->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (out == -1)
|
||||
{
|
||||
free(pipex);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
close(out);
|
||||
free(pipex);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/04 15:32:56 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/07 11:01:17 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/07 16:20:05 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -21,7 +21,7 @@ void getpath(t_pipex *pipex)
|
||||
{
|
||||
if (!ft_strncmp(*env, "PATH=", 5))
|
||||
{
|
||||
pipex->path = ft_split(*env, ':');
|
||||
pipex->path = ft_split((*env) + 5, ':');
|
||||
return ;
|
||||
}
|
||||
env++;
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/06 13:31:15 by adjoly #+# #+# */
|
||||
/* Updated: 2024/04/06 17:30:03 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/04/08 12:53:46 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -28,6 +28,8 @@ char *get_cmd_path(t_pipex *pipex, char *cmd)
|
||||
char **path;
|
||||
char *tmp;
|
||||
|
||||
if (access(cmd, X_OK) == 0)
|
||||
return (cmd);
|
||||
path = pipex->path;
|
||||
while (*path)
|
||||
{
|
||||
@ -42,6 +44,7 @@ char *get_cmd_path(t_pipex *pipex, char *cmd)
|
||||
free(tmp);
|
||||
path++;
|
||||
}
|
||||
free(cmd);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
@ -53,6 +56,16 @@ void get_arrcmd_path(t_pipex *pipex)
|
||||
while ((*cmd).option)
|
||||
{
|
||||
(*cmd).cmd = get_cmd_path(pipex, (*cmd).cmd);
|
||||
if (!(*cmd).cmd)
|
||||
{
|
||||
ft_freearr((*cmd).option);
|
||||
if (pipex->cmd[1].cmd)
|
||||
{
|
||||
ft_freearr(pipex->cmd[1].option);
|
||||
free(pipex->cmd[1].cmd);
|
||||
}
|
||||
ft_senderror(pipex, "Error: Command not found");
|
||||
}
|
||||
cmd++;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user