0.jesaisplus
This commit is contained in:
parent
0e1e022665
commit
e72d1a3e95
2
Makefile
2
Makefile
|
@ -1,5 +1,5 @@
|
|||
GNL = get_next_line/get_next_line.c get_next_line/get_next_line_utils.c
|
||||
SRCS = pipex.c ${GNL}
|
||||
SRCS = mandatory/pipex.c mandatory/pipex_utils.c mandatory/pipex_parsing.c ${GNL}
|
||||
SRCS_BONUS =
|
||||
OBJS = ${SRCS:.c=.o}
|
||||
OBJS_BONUS = ${SRCS_BONUS:.c=.o}
|
||||
|
|
108
bonus/pipex.c
Normal file
108
bonus/pipex.c
Normal file
|
@ -0,0 +1,108 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipex.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../pipex.h"
|
||||
|
||||
int free_all(t_data *data)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (data->fd1 > 0 && data->cmd1)
|
||||
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);
|
||||
return (1);
|
||||
}
|
||||
|
||||
void first_cmd(int fd[2], t_data data)
|
||||
{
|
||||
dup2(data.fd1, 0);
|
||||
dup2(fd[1], 1);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
execve(data.cmd1, data.flg1, data.env);
|
||||
free_all(&data);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void second_cmd(int fd[2], 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 pipex(t_data data)
|
||||
{
|
||||
int fd[2];
|
||||
int cmd1;
|
||||
int cmd2;
|
||||
|
||||
if (pipe(fd) < -1)
|
||||
return (1);
|
||||
if (data.fd1 > 0 && data.cmd1)
|
||||
{
|
||||
cmd1 = fork();
|
||||
if (cmd1 < 0)
|
||||
return (1);
|
||||
if (cmd1 == 0)
|
||||
first_cmd(fd, data);
|
||||
}
|
||||
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);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char **env)
|
||||
{
|
||||
t_data data;
|
||||
|
||||
if (argc < 5)
|
||||
return (1);
|
||||
if (parsing(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)
|
||||
{
|
||||
print_error(1, argv[3]);
|
||||
return (free_all(&data));
|
||||
}
|
||||
data.env = env;
|
||||
if (pipex(data))
|
||||
return (free_all(&data));
|
||||
free_all(&data);
|
||||
return (0);
|
||||
}
|
94
bonus/pipex_parsing.c
Normal file
94
bonus/pipex_parsing.c
Normal file
|
@ -0,0 +1,94 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipex_parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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));
|
||||
}
|
117
bonus/pipex_utils.c
Normal file
117
bonus/pipex_utils.c
Normal file
|
@ -0,0 +1,117 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipex_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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);
|
||||
}
|
23
input
23
input
|
@ -1 +1,22 @@
|
|||
test test
|
||||
hello that's why we are here s, your name
|
||||
im not kidding
|
||||
what's your name
|
||||
please can we talk
|
||||
commandes are not wrking, if you want send me in private
|
||||
how can i reach you dud, s
|
||||
pelase give me a wrod that end with a
|
||||
oh do you mean likw palama
|
||||
what !!!
|
||||
palama
|
||||
sorry id don't know what this mean
|
||||
me too i don't know,
|
||||
so way you say it
|
||||
not your bussnis
|
||||
hahahahahah
|
||||
why you ??
|
||||
i wnat it to end with s
|
||||
okay
|
||||
what bout lives
|
||||
emmm thats nice
|
||||
and eyes
|
||||
emmm thats batter
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/28 09:52:05 by erey-bet #+# #+# */
|
||||
/* Updated: 2022/10/12 16:14:08 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/01/17 15:40:25 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -106,7 +106,7 @@ char **ft_split(char const *s, char c)
|
|||
iyx[2] = 0;
|
||||
if (s == NULL)
|
||||
return (NULL);
|
||||
strs = malloc(sizeof(char *) * (get_len_all(s, c) + 1));
|
||||
strs = malloc(sizeof(char *) * (get_len_all(s, c) + 2));
|
||||
if (strs == NULL)
|
||||
return (NULL);
|
||||
if (ft_split_bis(s, c, strs, iyx) == NULL)
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/27 14:47:27 by erey-bet #+# #+# */
|
||||
/* Updated: 2022/12/13 13:24:11 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/01/17 16:25:07 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/15 11:46:17 by erey-bet #+# #+# */
|
||||
/* Updated: 2022/09/27 08:52:38 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/01/18 18:04:47 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
108
mandatory/pipex.c
Normal file
108
mandatory/pipex.c
Normal file
|
@ -0,0 +1,108 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipex.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../pipex.h"
|
||||
|
||||
int free_all(t_data *data)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (data->fd1 > 0 && data->cmd1)
|
||||
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);
|
||||
return (1);
|
||||
}
|
||||
|
||||
void first_cmd(int fd[2], t_data data)
|
||||
{
|
||||
dup2(data.fd1, 0);
|
||||
dup2(fd[1], 1);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
execve(data.cmd1, data.flg1, data.env);
|
||||
free_all(&data);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void second_cmd(int fd[2], 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 pipex(t_data data)
|
||||
{
|
||||
int fd[2];
|
||||
int cmd1;
|
||||
int cmd2;
|
||||
|
||||
if (pipe(fd) < -1)
|
||||
return (1);
|
||||
if (data.fd1 > 0 && data.cmd1)
|
||||
{
|
||||
cmd1 = fork();
|
||||
if (cmd1 < 0)
|
||||
return (1);
|
||||
if (cmd1 == 0)
|
||||
first_cmd(fd, data);
|
||||
}
|
||||
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);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char **env)
|
||||
{
|
||||
t_data data;
|
||||
|
||||
if (argc != 5)
|
||||
return (1);
|
||||
if (parsing(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)
|
||||
{
|
||||
print_error(1, argv[3]);
|
||||
return (free_all(&data));
|
||||
}
|
||||
data.env = env;
|
||||
if (pipex(data))
|
||||
return (free_all(&data));
|
||||
free_all(&data);
|
||||
return (0);
|
||||
}
|
94
mandatory/pipex_parsing.c
Normal file
94
mandatory/pipex_parsing.c
Normal file
|
@ -0,0 +1,94 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipex_parsing.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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));
|
||||
}
|
117
mandatory/pipex_utils.c
Normal file
117
mandatory/pipex_utils.c
Normal file
|
@ -0,0 +1,117 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* pipex_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* 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 */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#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);
|
||||
}
|
5
output
5
output
|
@ -1 +1,4 @@
|
|||
2
|
||||
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
|
||||
|
|
217
pipex.c
217
pipex.c
|
@ -1,217 +0,0 @@
|
|||
#include "pipex.h"
|
||||
|
||||
char *read_file(int fd)
|
||||
{
|
||||
char *str;
|
||||
int len;
|
||||
int old_len;
|
||||
|
||||
str = get_next_line(fd);
|
||||
len = ft_strlen(str);
|
||||
old_len = 0;
|
||||
while (len != old_len)
|
||||
{
|
||||
old_len = len;
|
||||
str = ft_strjoin_free(str, get_next_line(fd), 2);
|
||||
len = ft_strlen(str);
|
||||
}
|
||||
str[len] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
int get_next(char *cmd, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (cmd[i] != c)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
char **get_flags(char *cmd)
|
||||
{
|
||||
size_t i;
|
||||
int y;
|
||||
char *new_str;
|
||||
char **flags;
|
||||
|
||||
i = get_next(cmd, ' ') + 1;
|
||||
new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1);
|
||||
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;
|
||||
|
||||
new_str = ft_calloc(get_next(cmd, ' ') + 1, 1);
|
||||
i = -1;
|
||||
while (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] = cmd;
|
||||
return (flg);
|
||||
}
|
||||
|
||||
int parsing(char *argv[], t_data *data)
|
||||
{
|
||||
int fd1;
|
||||
int fd2;
|
||||
|
||||
fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT);
|
||||
if (fd2 < 0)
|
||||
return (-1);
|
||||
fd1 = open(argv[1], O_RDONLY);
|
||||
if (fd1 < 0)
|
||||
{
|
||||
close(fd2);
|
||||
return (-1);
|
||||
}
|
||||
data->cmd1 = get_command(argv[2]);
|
||||
data->cmd2 = get_command(argv[3]);
|
||||
data->flg1 = get_flags(argv[2]);
|
||||
data->flg1 = add_cmd(data->flg1, data->cmd1);
|
||||
data->flg2 = get_flags(argv[3]);
|
||||
data->flg2 = add_cmd(data->flg2, data->cmd2);
|
||||
data->fl1 = argv[1];
|
||||
data->fl2 = argv[4];
|
||||
//data->ct_fl1 = read_file(fd1);
|
||||
data->fd1 = fd1;
|
||||
data->fd2 = fd2;
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *find_command(char **env, char *cmd)
|
||||
{
|
||||
char *path;
|
||||
char *s;
|
||||
int i;
|
||||
int y;
|
||||
|
||||
while (*env != NULL)
|
||||
{
|
||||
if (ft_strnstr(*env, "PATH=/", ft_strlen(*env))
|
||||
&& ft_strchr(*env, ':') > 0)
|
||||
break;
|
||||
env++;
|
||||
}
|
||||
i = 5;
|
||||
y = 0;
|
||||
s = ft_calloc(ft_strlen(*env) + 1, 1);
|
||||
while ((*env)[i])
|
||||
{
|
||||
if ((*env)[i] != ':')
|
||||
s[y++] = (*env)[i];
|
||||
else
|
||||
{
|
||||
s = ft_strjoin(s, "/");
|
||||
path = ft_strjoin(s, cmd);
|
||||
if (access(path, X_OK) == 0)
|
||||
{
|
||||
free(s);
|
||||
return (path);
|
||||
}
|
||||
free(s);
|
||||
free(path);
|
||||
s = ft_calloc(ft_strlen(*env) + 1, 1);
|
||||
y = 0;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void first_cmd(int fd[2], t_data data)
|
||||
{
|
||||
dup2(data.fd1, 0);
|
||||
dup2(fd[1], 1);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
execve(data.cmd1, data.flg1, data.env);
|
||||
}
|
||||
|
||||
void second_cmd(int fd[2], t_data data)
|
||||
{
|
||||
dup2(fd[0], 0);
|
||||
dup2(data.fd2, 1);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
execve(data.cmd2, data.flg2, data.env);
|
||||
}
|
||||
|
||||
int pipex(t_data data)
|
||||
{
|
||||
int fd[2];
|
||||
int cmd1;
|
||||
int cmd2;
|
||||
|
||||
if (pipe(fd) < -1)
|
||||
return (1);
|
||||
cmd1 = fork();
|
||||
if (cmd1 == 0)
|
||||
first_cmd(fd, data);
|
||||
cmd2 = fork();
|
||||
if (cmd2 == 0)
|
||||
second_cmd(fd, data);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
waitpid(cmd1, NULL, 0);
|
||||
waitpid(cmd2, NULL, 0);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[], char **env)
|
||||
{
|
||||
t_data data;
|
||||
|
||||
//(void)argc;
|
||||
//(void)argv;
|
||||
//char **test = ft_split(argv[2], ' ');
|
||||
//execve(find_command(env, argv[1]), test, env);
|
||||
if (argc < 5)
|
||||
return (1);
|
||||
if (parsing(argv, &data))
|
||||
return (2);
|
||||
data.cmd1 = find_command(env, data.cmd1);
|
||||
if (data.cmd1 == NULL)
|
||||
return (3);
|
||||
data.cmd2 = find_command(env, data.cmd2);
|
||||
if (data.cmd2 == NULL)
|
||||
return (4);
|
||||
data.env = env;
|
||||
if (pipex(data))
|
||||
return (5);
|
||||
close(data.fd1);
|
||||
close(data.fd2);
|
||||
return (0);
|
||||
}
|
12
pipex.h
12
pipex.h
|
@ -6,7 +6,7 @@
|
|||
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */
|
||||
/* Updated: 2023/01/16 18:52:06 by erey-bet ### ########.fr */
|
||||
/* Updated: 2023/01/19 17:03:23 by erey-bet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
@ -34,4 +34,14 @@ typedef struct s_data
|
|||
char **env;
|
||||
} 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(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
|
||||
|
|
83
pipex_parsing.c
Normal file
83
pipex_parsing.c
Normal file
|
@ -0,0 +1,83 @@
|
|||
|
||||
#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));
|
||||
}
|
106
pipex_utils.c
Normal file
106
pipex_utils.c
Normal file
|
@ -0,0 +1,106 @@
|
|||
|
||||
#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);
|
||||
}
|
Loading…
Reference in a new issue