fait en sorte que le code sois moins degeu stp

This commit is contained in:
Etienne Rey-bethbeder 2023-01-16 23:57:56 +01:00
parent bf07a7258d
commit c37e041750
51 changed files with 60 additions and 27 deletions

Binary file not shown.

Binary file not shown.

1
input Normal file → Executable file
View file

@ -0,0 +1 @@
test test

BIN
libft/ft_atoi.o Normal file

Binary file not shown.

BIN
libft/ft_atoi_check.o Normal file

Binary file not shown.

BIN
libft/ft_bzero.o Normal file

Binary file not shown.

BIN
libft/ft_calloc.o Normal file

Binary file not shown.

BIN
libft/ft_get_size.o Normal file

Binary file not shown.

BIN
libft/ft_isalnum.o Normal file

Binary file not shown.

BIN
libft/ft_isalpha.o Normal file

Binary file not shown.

BIN
libft/ft_isascii.o Normal file

Binary file not shown.

BIN
libft/ft_isdigit.o Normal file

Binary file not shown.

BIN
libft/ft_isprint.o Normal file

Binary file not shown.

BIN
libft/ft_itoa.o Normal file

Binary file not shown.

BIN
libft/ft_memchr.o Normal file

Binary file not shown.

BIN
libft/ft_memcmp.o Normal file

Binary file not shown.

BIN
libft/ft_memcpy.o Normal file

Binary file not shown.

BIN
libft/ft_memmove.o Normal file

Binary file not shown.

BIN
libft/ft_memset.o Normal file

Binary file not shown.

BIN
libft/ft_power.o Normal file

Binary file not shown.

BIN
libft/ft_putchar_fd.o Normal file

Binary file not shown.

BIN
libft/ft_putendl_fd.o Normal file

Binary file not shown.

BIN
libft/ft_putnbr_fd.o Normal file

Binary file not shown.

BIN
libft/ft_putstr_fd.o Normal file

Binary file not shown.

BIN
libft/ft_split.o Normal file

Binary file not shown.

BIN
libft/ft_strchr.o Normal file

Binary file not shown.

BIN
libft/ft_strdup.o Normal file

Binary file not shown.

BIN
libft/ft_strdup_free.o Normal file

Binary file not shown.

BIN
libft/ft_strdups.o Normal file

Binary file not shown.

BIN
libft/ft_striteri.o Normal file

Binary file not shown.

BIN
libft/ft_strjoin.o Normal file

Binary file not shown.

BIN
libft/ft_strjoin_free.o Normal file

Binary file not shown.

BIN
libft/ft_strlcat.o Normal file

Binary file not shown.

BIN
libft/ft_strlcpy.o Normal file

Binary file not shown.

BIN
libft/ft_strlen.o Normal file

Binary file not shown.

BIN
libft/ft_strlen_double.o Normal file

Binary file not shown.

BIN
libft/ft_strmapi.o Normal file

Binary file not shown.

BIN
libft/ft_strncmp.o Normal file

Binary file not shown.

BIN
libft/ft_strnstr.o Normal file

Binary file not shown.

BIN
libft/ft_strrchr.o Normal file

Binary file not shown.

BIN
libft/ft_strslen.o Normal file

Binary file not shown.

BIN
libft/ft_strtrim.o Normal file

Binary file not shown.

BIN
libft/ft_substr.o Normal file

Binary file not shown.

BIN
libft/ft_tolower.o Normal file

Binary file not shown.

BIN
libft/ft_toupper.o Normal file

Binary file not shown.

BIN
libft/libft.a Normal file

Binary file not shown.

1
output Normal file → Executable file
View file

@ -0,0 +1 @@
2

BIN
pipex Executable file

Binary file not shown.

78
pipex.c
View file

@ -29,19 +29,23 @@ int get_next(char *cmd, char c)
return (i); return (i);
} }
char *get_flags(char *cmd) char **get_flags(char *cmd)
{ {
int i; size_t i;
int y; int y;
char *new_str; char *new_str;
char **flags;
i = get_next(cmd, '-') + 1; i = get_next(cmd, ' ') + 1;
new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1); new_str = ft_calloc(ft_strlen(cmd) - i + 1, 1);
y = 0; y = 0;
while (cmd[i]) while (cmd[i])
new_str[y++] = cmd[i++]; new_str[y++] = cmd[i++];
new_str[y] = '\0'; new_str[y] = '\0';
return (new_str); flags = ft_split(new_str, ' ');
i = 0;
free(new_str);
return (flags);
} }
char *get_command(char *cmd) char *get_command(char *cmd)
@ -57,6 +61,28 @@ char *get_command(char *cmd)
return (new_str); 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 parsing(char *argv[], t_data *data)
{ {
int fd1; int fd1;
@ -65,7 +91,7 @@ int parsing(char *argv[], t_data *data)
fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT); fd2 = open(argv[4], O_WRONLY | O_TRUNC | O_CREAT);
if (fd2 < 0) if (fd2 < 0)
return (-1); return (-1);
fd1 = open(argv[1], O_RDONLY | O_TRUNC | O_CREAT); fd1 = open(argv[1], O_RDONLY);
if (fd1 < 0) if (fd1 < 0)
{ {
close(fd2); close(fd2);
@ -74,10 +100,12 @@ int parsing(char *argv[], t_data *data)
data->cmd1 = get_command(argv[2]); data->cmd1 = get_command(argv[2]);
data->cmd2 = get_command(argv[3]); data->cmd2 = get_command(argv[3]);
data->flg1 = get_flags(argv[2]); data->flg1 = get_flags(argv[2]);
data->flg1 = add_cmd(data->flg1, data->cmd1);
data->flg2 = get_flags(argv[3]); data->flg2 = get_flags(argv[3]);
data->flg2 = add_cmd(data->flg2, data->cmd2);
data->fl1 = argv[1]; data->fl1 = argv[1];
data->fl2 = argv[4]; data->fl2 = argv[4];
data->ct_fl1 = read_file(fd1); //data->ct_fl1 = read_file(fd1);
data->fd1 = fd1; data->fd1 = fd1;
data->fd2 = fd2; data->fd2 = fd2;
return (0); return (0);
@ -87,33 +115,35 @@ char *find_command(char **env, char *cmd)
{ {
char *path; char *path;
char *s; char *s;
int fd;
int i; int i;
int y; int y;
while (*env != NULL) while (*env != NULL)
{ {
if (ft_strnstr(*env, "PATH=/", ft_strlen(*env))) if (ft_strnstr(*env, "PATH=/", ft_strlen(*env))
&& ft_strchr(*env, ':') > 0)
break; break;
env++; env++;
} }
i = 4; i = 5;
y = 4; y = 0;
s = malloc(ft_strlen(*env) + 1); s = ft_calloc(ft_strlen(*env) + 1, 1);
while (*env[i]) while ((*env)[i])
{ {
if (*env[i] != ':') if ((*env)[i] != ':')
s[y++] = *env[i]; s[y++] = (*env)[i];
else else
{ {
s = ft_strjoin(s, "/");
path = ft_strjoin(s, cmd); path = ft_strjoin(s, cmd);
fd = open(path, O_RDONLY); if (access(path, X_OK) == 0)
free(path);
if (fd != -1)
{ {
free(s); free(s);
return (path); return (path);
} }
free(s);
free(path);
s = ft_calloc(ft_strlen(*env) + 1, 1);
y = 0; y = 0;
} }
i++; i++;
@ -127,7 +157,7 @@ void first_cmd(int fd[2], t_data data)
dup2(fd[1], 1); dup2(fd[1], 1);
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
execve(data.cmd1, &data.flg1, NULL); execve(data.cmd1, data.flg1, data.env);
} }
void second_cmd(int fd[2], t_data data) void second_cmd(int fd[2], t_data data)
@ -136,7 +166,7 @@ void second_cmd(int fd[2], t_data data)
dup2(data.fd2, 1); dup2(data.fd2, 1);
close(fd[0]); close(fd[0]);
close(fd[1]); close(fd[1]);
execve(data.cmd2, &data.flg2, NULL); execve(data.cmd2, data.flg2, data.env);
} }
int pipex(t_data data) int pipex(t_data data)
@ -160,14 +190,14 @@ int pipex(t_data data)
return (0); return (0);
} }
#include <stdio.h>
int main(int argc, char *argv[], char **env) int main(int argc, char *argv[], char **env)
{ {
t_data data; t_data data;
//(void)argc; //(void)argc;
//(void)argv; //(void)argv;
//char **test = ft_split(argv[2], ' ');
//execve(find_command(env, argv[1]), test, env);
if (argc < 5) if (argc < 5)
return (1); return (1);
if (parsing(argv, &data)) if (parsing(argv, &data))
@ -178,9 +208,9 @@ int main(int argc, char *argv[], char **env)
data.cmd2 = find_command(env, data.cmd2); data.cmd2 = find_command(env, data.cmd2);
if (data.cmd2 == NULL) if (data.cmd2 == NULL)
return (4); return (4);
data.env = env;
if (pipex(data)) if (pipex(data))
return (5); return (5);
free(data.fl1);
close(data.fd1); close(data.fd1);
close(data.fd2); close(data.fd2);
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */ /* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */ /* Created: 2023/01/11 15:33:34 by erey-bet #+# #+# */
/* Updated: 2023/01/12 17:34:59 by erey-bet ### ########.fr */ /* Updated: 2023/01/16 18:52:06 by erey-bet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,14 +23,15 @@
typedef struct s_data typedef struct s_data
{ {
char *cmd1; char *cmd1;
char *flg1; char **flg1;
char *cmd2; char *cmd2;
char *flg2; char **flg2;
char *fl1; char *fl1;
char *fl2; char *fl2;
char *ct_fl1; char *ct_fl1;
int fd1; int fd1;
int fd2; int fd2;
char **env;
} t_data; } t_data;
#endif #endif

BIN
pipex.o Normal file

Binary file not shown.