[✨] feat: started exec

This commit is contained in:
KeyZox committed 2024-04-03 16:41:58 +02:00
1 parent 7d09b4cb31
commit 4e0a049e1d
13 files changed
+134 -30

No files matched your search

Binary file not shown.
Binary file not shown.
Binary file not shown.
+3 -1
View File
@@ -6,13 +6,15 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 16:54:40 by adjoly #+# #+# */ /* Created: 2024/03/28 16:54:40 by adjoly #+# #+# */
/* Updated: 2024/03/29 10:41:40 by adjoly ### ########.fr */ /* Updated: 2024/04/03 13:56:08 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PARSING_H #ifndef PARSING_H
# define PARSING_H # define PARSING_H
# include "libft.h"
typedef struct s_pcmd typedef struct s_pcmd
{ {
char *cmd; char *cmd;
+15 -4
View File
@@ -6,16 +6,27 @@
/* 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/03/22 21:16:23 by adjoly ### ########.fr */ /* Updated: 2024/04/03 14:24:09 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef PIPEX_H #ifndef PIPEX_H
# define PIPEX_H # define PIPEX_H
typedef struct t_pipex # include "parsing.h"
{ # include "libft.h"
} s_pipex; # include <stdlib.h>
# include <unistd.h>
# include <fcntl.h>
typedef struct s_pipex
{
t_pcmd *cmd;
int infile;
int outfile;
} t_pipex;
void ft_senderror(t_pipex *pipex, char *msg);
#endif #endif
BIN
View File
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
Executable
BIN
View File
Binary file not shown.
+63 -20
View File
@@ -6,40 +6,83 @@
/* 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/02 11:08:15 by adjoly ### ########.fr */ /* Updated: 2024/04/03 16:30:07 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
//#include "libft.h"
#include "libft.h" #include "libft.h"
#include "parsing.h" #include "parsing.h"
#include "pipex.h"
#include <stdlib.h>
#include <unistd.h>
int main(int ac, char **av) void free_pcmd(t_pcmd *cmd)
{ {
t_pcmd *cmd;
t_pcmd *tmp; t_pcmd *tmp;
char **option_tmp;
cmd = parse_cmd(ac, av + 2);
tmp = cmd; tmp = cmd;
while (tmp && (*tmp).cmd) while (tmp && tmp->cmd)
{ {
ft_printf("cmd : %s\n", (*tmp).cmd);
option_tmp = (*tmp).option;
while (*option_tmp)
{
ft_printf("%s\n", *option_tmp);
option_tmp++;
}
tmp++;
}
tmp = cmd;
while (tmp && (*tmp).cmd)
{
if (tmp->option)
ft_freearr(tmp->option);
free(tmp->cmd); free(tmp->cmd);
ft_freearr(tmp->option);
tmp++; tmp++;
} }
free(cmd); free(cmd);
}
void check_empty_args(char **av)
{
char **tmp;
char *tmp_av;
tmp = av;
tmp++;
while (*tmp)
{
tmp_av = *tmp;
if (!*tmp_av)
ft_senderror(NULL, "You have empty argument");
while (*tmp_av)
{
if (*tmp_av != 32)
break ;
tmp_av++;
}
if (!*tmp_av)
ft_senderror(NULL, "You have empty argument");
tmp++;
}
}
void ft_senderror(t_pipex *pipex, char *msg)
{
(void)pipex;
ft_putendl_fd(msg, STDERR_FILENO);
if (!pipex)
exit(EXIT_FAILURE);
if (pipex->cmd)
free_pcmd(pipex->cmd);
free(pipex);
exit(EXIT_FAILURE);
}
int main(int ac, char **av, char **env)
{
t_pipex *pipex;
check_empty_args(av);
pipex = malloc(sizeof(t_pipex));
pipex->infile = open(av[1], O_RDONLY);
pipex->outfile = open(av[ac - 1], O_RDONLY);
pipex->cmd = parse_cmd(ac - 3, av);
while (*env)
{
ft_putstr_fd(*env, STDOUT_FILENO);
env++;
}
free_pcmd(pipex->cmd);
free(pipex);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }
+7 -5
View File
@@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 21:13:26 by adjoly #+# #+# */ /* Created: 2024/03/22 21:13:26 by adjoly #+# #+# */
/* Updated: 2024/04/02 11:08:57 by adjoly ### ########.fr */ /* Updated: 2024/04/03 14:20:56 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@@ -44,18 +44,20 @@ t_pcmd split_cmd(char *cmd_av)
t_pcmd *parse_cmd(int ac, char **av) t_pcmd *parse_cmd(int ac, char **av)
{ {
char **tmp; char **tmp;
int i;
t_pcmd *cmd_arr; t_pcmd *cmd_arr;
t_pcmd *cmd_tmp; t_pcmd *cmd_tmp;
(void)ac; i = 0;
cmd_arr = ft_calloc(ac, sizeof(t_pcmd) + 1); cmd_arr = ft_calloc(ac + 1, sizeof(t_pcmd));
cmd_tmp = cmd_arr; cmd_tmp = cmd_arr;
tmp = av; tmp = av + 2;
while (*tmp) while (i < ac)
{ {
*cmd_tmp = split_cmd(*tmp); *cmd_tmp = split_cmd(*tmp);
cmd_tmp++; cmd_tmp++;
tmp++; tmp++;
i++;
} }
return (cmd_arr); return (cmd_arr);
} }
+46
View File
@@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* test.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 15:36:42 by adjoly #+# #+# */
/* Updated: 2024/04/03 16:27:03 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
char *cmd[] = {"sleep", "5", 0};
char *path = "/bin/sleep";
char *cmd2[] = {"sleep", "5", 0};
char *path2 = "/bin/sleep";
int fd[2];
if (pipe(fd) == -1)
return(1);
int pid = fork();
if (!pid)
{
dup2(fd[1], STDOUT_FILENO);
close(fd[0]);
close(fd[1]);
execve(path, cmd, NULL);
}
close(fd[1]);
int pid2 = fork();
if (!pid2)
{
dup2(fd[0], STDIN_FILENO);
close(fd[0]);
execve(path2, cmd2, NULL);
}
close(fd[1]);
waitpid(pid, NULL, 0);
waitpid(pid2, NULL, 0);
}