Skip to content

Commit 95b836e

Browse files
committed
added bonus
1 parent f9d20cd commit 95b836e

File tree

6 files changed

+316
-15
lines changed

6 files changed

+316
-15
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,6 @@ Mkfile.old
5252
dkms.conf
5353
en.subject_pipex.pdf
5454
/pipex-tester/
55+
assets/
56+
outfile
57+
pipex

src/Makefile

+8-14
Original file line numberDiff line numberDiff line change
@@ -14,37 +14,31 @@ SRCS := pipex_utils.c \
1414

1515
OBJS := pipex_utils.o \
1616

17-
# SRCS_BONUS := bonus/ft_printf_bonus.c bonus/ft_uitoa_hex_bonus.c \
18-
# bonus/ft_ptoa_bonus.c bonus/ft_uitoa_bonus.c bonus/ft_printf_utils_bonus.c \
19-
# bonus/ft_parserflags_bonus.c bonus/ft_fmtspecifiers_bonus.c \
20-
# bonus/ft_add_precision_bonus.c bonus/ft_format_utils_bonus.c\
17+
SRCS_BONUS := bonus/pipex_utils_bonus.c \
2118

22-
# OBJS_BONUS := $(SRCS_BONUS:.c=.o)
19+
OBJS_BONUS := bonus/pipex_utils_bonus.o \
2320

2421
all: $(NAME)
2522

2623
$(OBJS): $(SRCS)
2724
$(CC) $(CFLAGS) -c $*.c -o $@
2825

29-
# $(OBJS_BONUS): $(SRCS_BONUS)
30-
# $(CC) $(CFLAGS) -c $*.c -o $@
26+
$(OBJS_BONUS): $(SRCS_BONUS)
27+
$(CC) $(CFLAGS) -c $*.c -o $@
3128

3229
$(NAME): $(OBJS)
3330
make all -C $(LIBFT)
34-
# cp $(LIBFT)/*.o .
3531
ar rcs $(NAME).a ./*.o
3632
$(CC) $(CFLAGS) $(NAME).c -o $(NAME) $(NAME).a $(LIBFT)/$(LIBFT).a
3733

38-
# bonus: fclean $(OBJS_BONUS)
39-
# make all -C $(LIBFT)
40-
# cp $(LIBFT)/*.o .
41-
# cp bonus/*.o .
42-
# ar rcs $(NAME) ./*.o
34+
bonus: fclean $(OBJS_BONUS)
35+
make all -C $(LIBFT)
36+
ar rcs bonus/$(NAME).a bonus/*.o
37+
$(CC) $(CFLAGS) bonus/$(NAME)_bonus.c -o $(NAME) bonus/$(NAME).a $(LIBFT)/$(LIBFT).a
4338

4439
clean:
4540
make clean -C $(LIBFT)
4641
rm -f ./*.o
47-
rm -f ./bonus/*.o
4842

4943
fclean: clean
5044
make fclean -C $(LIBFT)

src/bonus/pipex_bonus.c

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* pipex_bonus.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: joneves- <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/06/16 22:44:40 by joneves- #+# #+# */
9+
/* Updated: 2024/07/16 23:08:09 by joneves- ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "pipex_bonus.h"
14+
15+
static t_cmds *ft_parser(int argc, char **argv, char **envp)
16+
{
17+
t_cmds *cmds;
18+
char *pathname;
19+
int i;
20+
int n;
21+
22+
i = 2;
23+
n = 0;
24+
cmds = (t_cmds *) malloc((argc - 2) * sizeof(t_cmds));
25+
if (!cmds)
26+
ft_error_handler("malloc()", ERROR_MALLOC, NULL, 0);
27+
while (i < (argc - 1))
28+
{
29+
cmds[n].args = ft_split(argv[i], ' ');
30+
pathname = ft_findpath(envp, cmds[n].args);
31+
if (!pathname)
32+
ft_printf("pipex: Command not found: %s", cmds[n].args[0]);
33+
cmds[n].pathname = pathname;
34+
i++;
35+
n++;
36+
}
37+
cmds[n].args = NULL;
38+
cmds[n].pathname = NULL;
39+
return (cmds);
40+
}
41+
42+
static int ft_open(char *pathname, int mode, t_cmds *cmds)
43+
{
44+
int fd;
45+
46+
if (mode == 1)
47+
{
48+
if (access(pathname, F_OK) != 0 && access(pathname, R_OK) != 0
49+
&& access(pathname, X_OK) != 0)
50+
{
51+
perror("open()");
52+
fd = open("/dev/null", O_RDONLY);
53+
}
54+
else
55+
fd = open(pathname, O_RDONLY);
56+
}
57+
if (mode == 2)
58+
fd = open(pathname, O_WRONLY | O_CREAT | O_TRUNC, 0666);
59+
if (fd == -1)
60+
{
61+
ft_error_handler("open()", ERROR_FILE_OPEN, cmds, 0);
62+
}
63+
return (fd);
64+
}
65+
66+
static int ft_pipex(int fd_in, int fd_out, t_cmds cmds, char **envp)
67+
{
68+
if (!cmds.pathname)
69+
fd_in = open("/dev/null", O_RDONLY);
70+
dup2(fd_in, STDIN_FILENO);
71+
close(fd_in);
72+
dup2(fd_out, STDOUT_FILENO);
73+
close(fd_out);
74+
if (cmds.pathname)
75+
{
76+
if (execve(cmds.pathname, cmds.args, envp) == -1)
77+
{
78+
ft_printf("%s", strerror(ENOENT));
79+
return (-1);
80+
}
81+
}
82+
return (0);
83+
}
84+
85+
int main(int argc, char **argv, char **envp)
86+
{
87+
t_cmds *cmds;
88+
pid_t *pid = malloc ((argc - 3) * sizeof(pid_t));
89+
int fds[2];
90+
int i = 0;
91+
92+
if (argc < 5)
93+
ft_error_handler(strerror(EINVAL), ERROR_ARGUMENTS, NULL, 1);
94+
cmds = ft_parser(argc, argv, envp);
95+
pid[0] = fork();
96+
if (pid[0] == -1)
97+
ft_error_handler("fork()", ERROR_FORK, cmds, 0);
98+
if (pid[0] == 0)
99+
{
100+
ft_printf("Dentro do PID 0\n");
101+
int fd_in = ft_open(argv[1], 1, cmds);
102+
while (cmds[i].args)
103+
{
104+
if (pipe(fds) == -1)
105+
ft_error_handler("pipe()", ERROR_PIPE, cmds, 0);
106+
pid[i + 1] = fork();
107+
if (pid[i + 1] == -1)
108+
ft_error_handler("fork()", ERROR_FORK, cmds, 0);
109+
if (pid[i + 1] == 0)
110+
{
111+
ft_printf("Dentro do PID %d -- execute: %s \n", i, cmds[i].args[0]);
112+
close(fds[0]);
113+
if (ft_pipex(fd_in, fds[1], cmds[i], envp) == -1)
114+
ft_error_handler("execve()", ERROR_EXECVE, cmds, 0);
115+
}
116+
else
117+
{
118+
// waitpid(pid[i + 1], NULL, 0);
119+
fd_in = ft_open(".tmp", 2, cmds);
120+
close(fds[1]);
121+
dup2(fds[0], fd_in);
122+
close(fds[0]);
123+
}
124+
i++;
125+
}
126+
}
127+
else
128+
{
129+
waitpid(pid[0], NULL, 0);
130+
ft_printf("Gerandando dados...");
131+
// int out = open(fds[0], O_RDONLY);
132+
char *line = get_next_line(fd_in);
133+
while (line)
134+
{
135+
ft_printf("%s", line);
136+
line = get_next_line(fd_in);
137+
line++;
138+
}
139+
140+
// close(fds[1]);
141+
// if (ft_pipex(fds[0], ft_open(argv[4], 2, cmds), cmds[1], envp) == -1)
142+
// ft_error_handler("execve()", ERROR_EXECVE, cmds, 0);
143+
// unlink(".tmp");
144+
free(pid);
145+
}
146+
return (ft_free_args(cmds));
147+
}
148+

src/bonus/pipex_bonus.h

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* pipex_bonus.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: joneves- <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/04/26 20:07:30 by joneves- #+# #+# */
9+
/* Updated: 2024/07/14 11:55:31 by joneves- ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef PIPEX_H
14+
# define PIPEX_H
15+
16+
# define SUCCESS 0
17+
# define ERROR_ARGUMENTS 2
18+
# define ERROR_FILE_OPEN 3
19+
# define ERROR_FILE_OPEN_OUT 4
20+
# define ERROR_FILE_EXIST_OR_READ 5
21+
# define ERROR_MALLOC 6
22+
# define ERROR_PIPE 7
23+
# define ERROR_FORK 8
24+
# define ERROR_EXECVE 9
25+
# define ERROR_NOT_FOUND 10
26+
27+
# include "../libft/libft.h"
28+
# include <stdio.h>
29+
# include <stdlib.h>
30+
# include <unistd.h>
31+
# include <fcntl.h>
32+
# include <sys/wait.h>
33+
# include <string.h>
34+
# include <errno.h>
35+
36+
typedef struct t_cmds
37+
{
38+
char *pathname;
39+
char **args;
40+
} t_cmds;
41+
42+
int ft_free_args(t_cmds *cmds);
43+
int ft_free_paths(char **paths, int i);
44+
void ft_error_handler(char *error, int signal, t_cmds *cmds, int mode);
45+
char *ft_findpath(char **envp, char **cmds);
46+
char *merge(char *s1, char *s2);
47+
48+
// ref https://www.rozmichelle.com/pipes-forks-dups/
49+
50+
// argv[0] argv[1] argv[2] argv[3] argv[4]
51+
// ./pipex "../infile" cmd cmd outfile
52+
53+
//refator main
54+
//refataror free
55+
56+
//utilizar meu printf
57+
58+
#endif //PIPEX_H

src/bonus/pipex_utils_bonus.c

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* pipex_utils_bonus.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: joneves- <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/06/24 17:50:31 by joneves- #+# #+# */
9+
/* Updated: 2024/07/14 11:55:11 by joneves- ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "pipex_bonus.h"
14+
15+
int ft_free_args(t_cmds *cmds)
16+
{
17+
int i;
18+
int x;
19+
20+
i = 0;
21+
while (cmds[i].args)
22+
{
23+
x = 0;
24+
while (cmds[i].args[x])
25+
{
26+
free(cmds[i].args[x]);
27+
x++;
28+
}
29+
free(cmds[i].args);
30+
free(cmds[i].pathname);
31+
i++;
32+
}
33+
free(cmds);
34+
return (0);
35+
}
36+
37+
int ft_free_paths(char **paths, int i)
38+
{
39+
while (paths[++i])
40+
free(paths[i]);
41+
free(paths);
42+
return (0);
43+
}
44+
45+
void ft_error_handler(char *error, int signal, t_cmds *cmds, int mode)
46+
{
47+
if (mode == 0)
48+
{
49+
if (cmds)
50+
ft_free_args(cmds);
51+
perror(error);
52+
exit (signal);
53+
}
54+
else
55+
{
56+
ft_printf("pipex: %s", error);
57+
exit (signal);
58+
}
59+
}
60+
61+
char *merge(char *s1, char *s2)
62+
{
63+
char *merge;
64+
65+
merge = ft_strjoin(s1, s2);
66+
free(s1);
67+
s1 = NULL;
68+
return (merge);
69+
}
70+
71+
char *ft_findpath(char **envp, char **cmds)
72+
{
73+
char **paths;
74+
char *pathname;
75+
int i;
76+
77+
i = 0;
78+
if (access(cmds[0], F_OK) == 0 && access(cmds[0], X_OK) == 0)
79+
return (cmds[0]);
80+
else if (envp[i])
81+
{
82+
while (!ft_strnstr(envp[i], "PATH=", 5))
83+
i++;
84+
paths = ft_split(envp[i] + 5, ':');
85+
i = 0;
86+
while (paths[i])
87+
{
88+
pathname = merge(merge(paths[i], "/"), cmds[0]);
89+
if (access(pathname, F_OK) == 0 && access(pathname, X_OK) == 0)
90+
return (ft_free_paths(paths, i), pathname);
91+
free(pathname);
92+
pathname = NULL;
93+
i++;
94+
}
95+
}
96+
free(pathname);
97+
return (free(paths), NULL);
98+
}

src/pipex.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: joneves- <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/06/16 22:44:40 by joneves- #+# #+# */
9-
/* Updated: 2024/07/14 11:44:52 by joneves- ### ########.fr */
9+
/* Updated: 2024/07/14 11:58:53 by joneves- ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

0 commit comments

Comments
 (0)