[✨] 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> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/22 21:14:06 by adjoly #+# #+# */
|
/* 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 getpath(t_pipex *pipex);
|
||||||
void get_arrcmd_path(t_pipex *pipex);
|
void get_arrcmd_path(t_pipex *pipex);
|
||||||
void exec_pipe(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
|
#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> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/21 10:03:04 by adjoly #+# #+# */
|
/* 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 "libft.h"
|
||||||
#include "parsing.h"
|
#include "parsing.h"
|
||||||
#include "pipex.h"
|
#include "pipex.h"
|
||||||
#include <stdlib.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
void free_pcmd(t_pcmd *cmd)
|
void check_empty_args(char **av, t_pipex *pipex)
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
{
|
||||||
char **tmp;
|
char **tmp;
|
||||||
char *tmp_av;
|
char *tmp_av;
|
||||||
@ -40,8 +24,6 @@ void check_empty_args(char **av)
|
|||||||
while (*tmp)
|
while (*tmp)
|
||||||
{
|
{
|
||||||
tmp_av = *tmp;
|
tmp_av = *tmp;
|
||||||
if (!*tmp_av)
|
|
||||||
ft_senderror(NULL, "You have empty argument");
|
|
||||||
while (*tmp_av)
|
while (*tmp_av)
|
||||||
{
|
{
|
||||||
if (*tmp_av != 32)
|
if (*tmp_av != 32)
|
||||||
@ -49,23 +31,28 @@ void check_empty_args(char **av)
|
|||||||
tmp_av++;
|
tmp_av++;
|
||||||
}
|
}
|
||||||
if (!*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++;
|
tmp++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_senderror(t_pipex *pipex, char *msg)
|
void check_files(int ac, char **av, t_pipex *pipex)
|
||||||
{
|
{
|
||||||
(void)pipex;
|
int out;
|
||||||
ft_putendl_fd(msg, STDERR_FILENO);
|
|
||||||
if (!pipex)
|
pipex->infile = open(av[1], O_RDONLY);
|
||||||
exit(EXIT_FAILURE);
|
pipex->outfile = av[ac - 1];
|
||||||
if (pipex->path)
|
if (pipex->infile == -1)
|
||||||
ft_freearr(pipex->path);
|
ft_senderror_file(pipex, "Error: Infile can't be opened");
|
||||||
if (pipex->cmd)
|
dup2(pipex->infile, STDIN_FILENO);
|
||||||
free_pcmd(pipex->cmd);
|
out = open(pipex->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||||
free(pipex);
|
if (out < 0)
|
||||||
exit(EXIT_FAILURE);
|
ft_senderror_file(pipex, "Error: Outfile can't be opened");
|
||||||
|
close(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int ac, char **av, char **env)
|
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));
|
pipex = malloc(sizeof(t_pipex));
|
||||||
if (ac != 5)
|
if (ac != 5)
|
||||||
ft_senderror(pipex, "Error : Invalid number of args");
|
{
|
||||||
check_empty_args(av);
|
free(pipex);
|
||||||
pipex->infile = open(av[1], O_RDONLY);
|
ft_putendl_fd("Error : Invalid number of args", STDERR_FILENO);
|
||||||
dup2(pipex->infile, STDIN_FILENO);
|
exit(EXIT_FAILURE);
|
||||||
pipex->outfile = av[ac - 1];
|
}
|
||||||
|
check_empty_args(av, pipex);
|
||||||
|
check_files(ac, av, pipex);
|
||||||
pipex->cmd = parse_cmd(ac - 3, av);
|
pipex->cmd = parse_cmd(ac - 3, av);
|
||||||
pipex->env = env;
|
pipex->env = env;
|
||||||
getpath(pipex);
|
getpath(pipex);
|
||||||
if (pipex->path == NULL)
|
if (pipex->path == NULL)
|
||||||
ft_senderror(pipex, "Error : Can't find path");
|
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);
|
get_arrcmd_path(pipex);
|
||||||
exec_pipe(pipex);
|
exec_pipe(pipex);
|
||||||
ft_freearr(pipex->path);
|
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> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/04 15:32:56 by adjoly #+# #+# */
|
/* 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))
|
if (!ft_strncmp(*env, "PATH=", 5))
|
||||||
{
|
{
|
||||||
pipex->path = ft_split(*env, ':');
|
pipex->path = ft_split((*env) + 5, ':');
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
env++;
|
env++;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/06 13:31:15 by adjoly #+# #+# */
|
/* 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 **path;
|
||||||
char *tmp;
|
char *tmp;
|
||||||
|
|
||||||
|
if (access(cmd, X_OK) == 0)
|
||||||
|
return (cmd);
|
||||||
path = pipex->path;
|
path = pipex->path;
|
||||||
while (*path)
|
while (*path)
|
||||||
{
|
{
|
||||||
@ -42,6 +44,7 @@ char *get_cmd_path(t_pipex *pipex, char *cmd)
|
|||||||
free(tmp);
|
free(tmp);
|
||||||
path++;
|
path++;
|
||||||
}
|
}
|
||||||
|
free(cmd);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +56,16 @@ void get_arrcmd_path(t_pipex *pipex)
|
|||||||
while ((*cmd).option)
|
while ((*cmd).option)
|
||||||
{
|
{
|
||||||
(*cmd).cmd = get_cmd_path(pipex, (*cmd).cmd);
|
(*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++;
|
cmd++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user