This commit is contained in:
Etienne Rey-bethbeder 2023-01-20 18:51:53 +01:00
parent e72d1a3e95
commit dac4fe9717
12 changed files with 148 additions and 272 deletions

View file

@ -1,6 +1,6 @@
GNL = get_next_line/get_next_line.c get_next_line/get_next_line_utils.c
SRCS = mandatory/pipex.c mandatory/pipex_utils.c mandatory/pipex_parsing.c ${GNL}
SRCS_BONUS =
SRCS_BONUS = bonus/pipex.c bonus/pipex_utils.c bonus/pipex_parsing.c ${GNL}
OBJS = ${SRCS:.c=.o}
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
LIBS = libft/libft.a

View file

@ -6,102 +6,120 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */
/* Updated: 2023/01/19 16:57:40 by erey-bet ### ########.fr */
/* Updated: 2023/01/20 17:58:22 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../pipex.h"
#include "pipex.h"
int free_all(t_data *data)
{
int i;
int y;
if (data->fd1 > 0 && data->cmd1)
if (data->fd1 > 0 && data->cmds[0])
close(data->fd1);
close(data->fd2);
i = 0;
while (data->flg1[i])
free(data->flg1[i++]);
free(data->flg1);
i = 0;
while (data->flg2[i])
free(data->flg2[i++]);
free(data->flg2);
free(data->cmd1);
free(data->cmd2);
y = -1;
while (++y < data->argc - 3)
{
i = -1;
while (data->flgs[y][++i])
free(data->flgs[y][i]);
free(data->flgs[y]);
}
free(data->flgs);
i = -1;
while (++i < data->argc - 3)
free(data->cmds[i]);
return (1);
}
void first_cmd(int fd[2], t_data data)
void make_cmd(int fd[2][2], t_data data, int fl1, int fl2)
{
dup2(data.fd1, 0);
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
execve(data.cmd1, data.flg1, data.env);
dup2(fl1, 0);
dup2(fl2, 1);
close(fd[0][0]);
close(fd[0][1]);
close(fd[1][0]);
close(fd[1][1]);
execve(data.cmds[0], data.flgs[0], data.env);
}
void cmd(int fd[2][2], t_data data, int i, int t)
{
if (t == 0)
make_cmd(fd, data, data.fd1, 0);
else if (t == 1)
make_cmd(fd, data, (i + 1) % 2, i % 2);
else if (t == 2)
make_cmd(fd, data, (i + 1) % 2, data.fd2);
free_all(&data);
exit(1);
}
void second_cmd(int fd[2], t_data data)
int pipex(int argc, t_data data)
{
dup2(fd[0], 0);
dup2(data.fd2, 1);
close(fd[0]);
close(fd[1]);
execve(data.cmd2, data.flg2, data.env);
free_all(&data);
exit(1);
}
int fd[2][2];
int *fk;
int i;
int pipex(t_data data)
{
int fd[2];
int cmd1;
int cmd2;
if (pipe(fd) < -1)
if (pipe(fd[0]) < -1)
return (1);
if (data.fd1 > 0 && data.cmd1)
{
cmd1 = fork();
if (cmd1 < 0)
if (pipe(fd[1]) < -1)
return (1);
if (cmd1 == 0)
first_cmd(fd, data);
fk = ft_calloc(argc - 3, sizeof(int));
i = -1;
while (++i < argc - 3)
{
fk[i] = fork();
if (fk < 0)
return (1);
if (fk == 0)
{
if (i == 0 && data.fd1 > 0 && data.cmds[0])
cmd(fd, data, i, 0);
else if (i == argc - 3)
cmd(fd, data, i, 1);
else
cmd(fd, data, i, 2);
}
cmd2 = fork();
if (cmd2 < 0)
return (1);
if (cmd2 == 0)
second_cmd(fd, data);
close(fd[0]);
close(fd[1]);
if (data.fd1 > 0 && data.cmd1)
waitpid(cmd1, NULL, 0);
waitpid(cmd2, NULL, 0);
}
close(fd[0][0]);
close(fd[0][1]);
close(fd[1][0]);
close(fd[1][1]);
if (data.fd1 > 0 && data.cmds[0])
waitpid(fk[0], NULL, 0);
i = -1;
while (++i < argc - 3)
waitpid(fk[i], NULL, 0);
return (0);
}
int main(int argc, char *argv[], char **env)
{
t_data data;
int i;
if (argc < 5)
return (1);
if (parsing(argv, &data))
if (parsing(argc, argv, &data))
return (2);
data.cmd1 = find_command(env, data.cmd1);
if (data.cmd1 == NULL)
print_error(1, argv[2]);
data.cmd2 = find_command(env, data.cmd2);
if (data.cmd2 == NULL)
data.argc = argc;
i = -1;
while (++i < argc - 3)
{
print_error(1, argv[3]);
data.cmds[i] = find_command(env, data.cmds[i]);
if (data.cmds[i] == NULL)
{
print_error(1, argv[2 + i]);
if (i == argc - 4)
return (free_all(&data));
}
}
data.env = env;
if (pipex(data))
if (pipex(argc, data))
return (free_all(&data));
free_all(&data);
return (0);

45
bonus/pipex.h Normal file
View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */
/* Updated: 2023/01/20 17:48:08 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIPEX_H
# define PIPEX_H
# include "../libft/libft.h"
# include "../get_next_line/get_next_line.h"
# include <fcntl.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/wait.h>
typedef struct s_data
{
char **cmds;
char ***flgs;
char *fl1;
char *fl2;
int fd1;
int fd2;
char **env;
int argc;
} t_data;
int get_next(char *cmd, char c);
int print_error(int type, char *error);
char **get_flags(char *cmd);
char *get_command(char *cmd);
char **add_cmd(char **flg, char *cmd);
int parsing(int argc, char *argv[], t_data *data);
char *test_acces(char **s, char **cmd, char **path, int *y);
char *cmd_exist(char **env, char **cmd, int y, char **s);
char *find_command(char **env, char *cmd);
#endif

View file

@ -6,24 +6,30 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */
/* Updated: 2023/01/19 17:05:16 by erey-bet ### ########.fr */
/* Updated: 2023/01/20 17:49:08 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../pipex.h"
#include "pipex.h"
int parsing(char *argv[], t_data *data)
int parsing(int argc, char *argv[], t_data *data)
{
int i;
data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT);
if (data->fd2 < 0)
return (1);
data->fd1 = open(argv[1], O_RDONLY);
if (data->fd1 < 0)
print_error(0, argv[1]);
data->cmd1 = get_command(argv[2]);
data->cmd2 = get_command(argv[3]);
data->flg1 = ft_split(argv[2], ' ');
data->flg2 = ft_split(argv[3], ' ');
data->cmds = ft_calloc(argc - 2, sizeof(char *));
data->flgs = ft_calloc(argc - 2, sizeof(char *));
i = -1;
while (++i < argc - 4)
{
data->cmds[i] = get_command(argv[2 + i]);
data->flgs[i] = ft_split(argv[2 + i], ' ');
}
data->fl1 = argv[1];
data->fl2 = argv[4];
return (0);

View file

@ -6,11 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */
/* Updated: 2023/01/19 17:05:24 by erey-bet ### ########.fr */
/* Updated: 2023/01/20 17:48:55 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../pipex.h"
#include "pipex.h"
int get_next(char *cmd, char c)
{

View file

@ -6,11 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 18:04:58 by erey-bet #+# #+# */
/* Updated: 2023/01/19 17:30:04 by erey-bet ### ########.fr */
/* Updated: 2023/01/20 14:53:55 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../pipex.h"
#include "pipex.h"
int free_all(t_data *data)
{

View file

@ -6,15 +6,15 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */
/* Updated: 2023/01/19 17:03:23 by erey-bet ### ########.fr */
/* Updated: 2023/01/20 14:55:13 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIPEX_H
# define PIPEX_H
# include "libft/libft.h"
# include "get_next_line/get_next_line.h"
# include "../libft/libft.h"
# include "../get_next_line/get_next_line.h"
# include <fcntl.h>
# include <unistd.h>
# include <sys/types.h>

View file

@ -6,11 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 17:03:01 by erey-bet #+# #+# */
/* Updated: 2023/01/19 17:05:16 by erey-bet ### ########.fr */
/* Updated: 2023/01/20 14:54:05 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../pipex.h"
#include "pipex.h"
int parsing(char *argv[], t_data *data)
{

View file

@ -6,11 +6,11 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/19 17:02:51 by erey-bet #+# #+# */
/* Updated: 2023/01/19 17:05:24 by erey-bet ### ########.fr */
/* Updated: 2023/01/20 14:55:47 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "../pipex.h"
#include "pipex.h"
int get_next(char *cmd, char c)
{

4
output
View file

@ -1,4 +0,0 @@
hello that's why we are here s, your name
what's your name
commandes are not wrking, if you want send me in private
emmm thats nice

View file

@ -1,83 +0,0 @@
#include "../pipex.h"
int parsing(char *argv[], t_data *data)
{
data->fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT);
if (data->fd2 < 0)
return (1);
data->fd1 = open(argv[1], O_RDONLY);
if (data->fd1 < 0)
print_error(0, argv[1]);
data->cmd1 = get_command(argv[2]);
data->cmd2 = get_command(argv[3]);
data->flg1 = ft_split(argv[2], ' ');
data->flg2 = ft_split(argv[3], ' ');
data->fl1 = argv[1];
data->fl2 = argv[4];
return (0);
}
char *test_acces(char **s, char **cmd, char **path, int *y)
{
*s = ft_strjoin_free(*s, "/", 1);
if (*s == NULL)
return (NULL);
*path = ft_strjoin_free(*s, *cmd, 1);
if (*path == NULL)
return (NULL);
if (access(*path, X_OK) == 0)
{
free(*cmd);
return (*path);
}
free(*path);
*path = NULL;
*y = 0;
return (NULL);
}
char *cmd_exist(char **env, char **cmd, int y, char **s)
{
char *path;
int i;
i = 5;
while ((*env)[i])
{
if ((*env)[i] != ':')
(*s)[y++] = (*env)[i];
else
{
test_acces(s, cmd, &path, &y);
if (path)
return (path);
*s = ft_calloc(ft_strlen(*env) + 1, 1);
if (*s == NULL)
return (NULL);
}
i++;
}
free(*s);
free(*cmd);
return (NULL);
}
char *find_command(char **env, char *cmd)
{
char *s;
int y;
while (*env != NULL)
{
if (ft_strnstr(*env, "PATH=/", ft_strlen(*env))
&& ft_strchr(*env, ':') > 0)
break ;
env++;
}
y = 0;
s = ft_calloc(ft_strlen(*env) + 1, 1);
if (s == NULL)
return (NULL);
return (cmd_exist(env, &cmd, y, &s));
}

View file

@ -1,106 +0,0 @@
#include "../pipex.h"
int get_next(char *cmd, char c)
{
int i;
i = 0;
while (cmd[i] != c)
{
if (!cmd[i])
return (-1);
i++;
}
return (i);
}
int print_error(int type, char *error)
{
if (type == 0)
{
write(2, "zsh: no such file or directory: ", 32);
write(2, error, ft_strlen(error));
}
if (type == 1)
{
write(2, "zsh: command not found: ", 24);
if (get_next(error, ' ') == -1)
write(2, error, ft_strlen(error));
else
write(2, error, ft_strlen(error)
- (ft_strlen(error) - get_next(error, ' ')));
}
write(2, "\n", 1);
return (1);
}
char **get_flags(char *cmd)
{
int i;
int y;
char *new_str;
char **flags;
i = get_next(cmd, ' ') + 1;
if (i == 0)
{
flags = ft_calloc(2, sizeof(char *));
return (flags);
}
new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1);
if (new_str == NULL)
return (NULL);
y = 0;
while (cmd[i])
new_str[y++] = cmd[i++];
new_str[y] = '\0';
flags = ft_split(new_str, ' ');
i = 0;
free(new_str);
return (flags);
}
char *get_command(char *cmd)
{
int i;
char *new_str;
i = get_next(cmd, ' ');
if (i != -1)
new_str = ft_calloc(get_next(cmd, ' ') + 1, 1);
else
new_str = ft_calloc(ft_strlen(cmd) + 1, 1);
if (new_str == NULL)
return (NULL);
i = -1;
while (cmd[++i] != ' ' && cmd[i])
new_str[i] = cmd[i];
new_str[i] = '\0';
return (new_str);
}
char **add_cmd(char **flg, char *cmd)
{
char *tmp;
int i;
int check;
tmp = NULL;
i = 0;
check = 0;
while (check == 0)
{
if (flg[i] == NULL)
check = 1;
if (tmp != NULL)
flg[i] = tmp;
tmp = flg[i];
i++;
}
flg[0] = ft_strdup(cmd);
if (flg[0] == NULL)
return (NULL);
flg[i] = NULL;
return (flg);
}