Skip to content

Commit 50f4ea8

Browse files
committed
Update main.c, print.c, all.h, struct.h, utils.c, and lib.h
1 parent 3f493c5 commit 50f4ea8

File tree

18 files changed

+195
-181
lines changed

18 files changed

+195
-181
lines changed

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ BUILD_BONUS_DIR = build_bonus
33
SRC_DIR = src
44
BUILD_DIR = build
55
NAME = pipex
6-
CFLAG = -Wall -Wextra -Werror -I./includes/
6+
CFLAG = -Wall -Wextra -Werror -I./includes/ -g3
77

88
SRC_MAIN = ./bin/main/main.c \
99
./bin/pipex/bonus.c \
1010
./bin/pipex/pipex.c \
11-
./bin/pipex/mandatory.c \
1211
./bin/print/print.c \
1312
./bin/print/ft_printf.c \
1413
./bin/print/print_functions.c \
1514
./bin/print/print_functions2.c \
1615
./bin/print/utils.c \
1716
./bin/setup/args.c \
18-
./bin/setup/bool.c \
17+
./bin/setup/setup.c \
1918
./bin/setup/env.c \
19+
./bin/utils/bool.c \
2020
./bin/utils/exec.c \
2121
./bin/utils/free.c \
2222
./bin/utils/path.c \
@@ -26,15 +26,15 @@ SRC_MAIN = ./bin/main/main.c \
2626
SRC_BONUS = ./bin/main/main_bonus.c \
2727
./bin/pipex/bonus.c \
2828
./bin/pipex/pipex.c \
29-
./bin/pipex/mandatory.c \
3029
./bin/print/print.c \
31-
./bin/print/ft_printf.c \
30+
./bin/print/ft_printf.c \
3231
./bin/print/print_functions.c \
3332
./bin/print/print_functions2.c \
3433
./bin/print/utils.c \
3534
./bin/setup/args.c \
36-
./bin/setup/bool.c \
35+
./bin/setup/setup.c \
3736
./bin/setup/env.c \
37+
./bin/utils/bool.c \
3838
./bin/utils/exec.c \
3939
./bin/utils/free.c \
4040
./bin/utils/path.c \

bin/main/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/30 23:05:38 by tauer #+# #+# */
9-
/* Updated: 2024/04/20 19:02:51 by tauer ### ########.fr */
9+
/* Updated: 2024/04/22 15:33:51 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

bin/pipex/bonus.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/04/03 18:29:11 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 14:09:26 by tauer ### ########.fr */
9+
/* Updated: 2024/04/22 15:37:21 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -36,11 +36,13 @@ void redir(t_data *data, int in, int out)
3636

3737
void child_process(t_data *data, int *tube, t_arg *arg, bool RescueCmd)
3838
{
39+
if (!arg->path || !arg->name[0])
40+
return (texit(data, EXIT_FAILURE));
3941
close(tube[0]);
4042
if (data->pip.pos == 1)
4143
redir(data, data->pip.in_fd, tube[1]);
4244
else if (RescueCmd && data->pip.pos == data->env.argc - 2)
43-
redir(data, data->pip.safetyFd, data->pip.ou_fd);
45+
redir(data, data->pip.safety_fd, data->pip.ou_fd);
4446
else if (data->pip.pos == data->env.argc - 2)
4547
redir(data, tube[0], data->pip.ou_fd);
4648
else
@@ -75,17 +77,3 @@ void forker(t_data *data, bool RescueCmd)
7577
texit(data, EXIT_FAILURE);
7678
choose_proccess(data, tube, pid, RescueCmd);
7779
}
78-
79-
void bonus(t_data *data)
80-
{
81-
data->pip.pos = 0;
82-
while (data->pip.pos < data->env.argc - 2)
83-
{
84-
data->pip.pos++;
85-
forker(data, false);
86-
}
87-
while (wait(NULL) > 0)
88-
;
89-
close(data->pip.in_fd);
90-
close(data->pip.ou_fd);
91-
}

bin/pipex/mandatory.c

Lines changed: 0 additions & 21 deletions
This file was deleted.

bin/pipex/pipex.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/04/20 13:31:51 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 14:09:40 by tauer ### ########.fr */
9+
/* Updated: 2024/04/22 15:36:54 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -33,13 +33,13 @@ void check_err(t_data *data, int *status)
3333
}
3434
}
3535

36-
bool tryRescueCmd(t_data *data, int status)
36+
bool try_rescue_cmd(t_data *data, int status)
3737
{
38-
data->pip.pos = data->env.argc - 2;
3938
if (status == 3 || status == 4)
4039
texit(data, EXIT_FAILURE);
4140
else if (status == 2)
4241
{
42+
data->pip.pos = data->env.argc - 2;
4343
terror("executing rescue cmd", true);
4444
forker(data, true);
4545
}
@@ -50,10 +50,29 @@ bool pipex(t_data *data, int BONUS)
5050
{
5151
check_err(data, &BONUS);
5252
if (BONUS != false && BONUS != true)
53-
return (tryRescueCmd(data, BONUS));
53+
return (try_rescue_cmd(data, BONUS));
5454
else if (!BONUS && data->env.argc == 4)
5555
return (no_bonus(data), false);
5656
else if (BONUS && data->env.argc >= 4)
5757
return (bonus(data), false);
5858
return (terror("wrong number of args", true), true);
5959
}
60+
61+
void bonus(t_data *data)
62+
{
63+
while (data->pip.pos < data->env.argc - 2)
64+
{
65+
data->pip.pos++;
66+
forker(data, false);
67+
}
68+
while (wait(NULL) > 0)
69+
;
70+
close_if_fd(data);
71+
}
72+
73+
void no_bonus(t_data *data)
74+
{
75+
if (data->env.argc <= 5)
76+
bonus(data);
77+
texit(data, EXIT_FAILURE);
78+
}

bin/print/print.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/28 22:28:03 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 14:27:17 by tauer ### ########.fr */
9+
/* Updated: 2024/04/22 15:24:43 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include <all.h>
1414

1515
void terror(char *err_msg, bool isLast)
1616
{
17-
// write(2, " ", 2);
1817
write(2, err_msg, ft_strlen(err_msg));
1918
if (!isLast)
2019
write(2, " -> ", 4);

bin/print/utils.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/11/14 22:02:29 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 14:24:58 by tauer ### ########.fr */
9+
/* Updated: 2024/04/22 15:24:49 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include <all.h>
1414

1515
void write_line(void)
1616
{
17-
unsigned int max;
17+
unsigned int max;
1818

1919
max = 20;
2020
write(1, "----+-------+", 14);
21-
while(max--)
21+
while (max--)
2222
write(1, "-", 1);
2323
write(1, "\n", 1);
2424
}

bin/setup/args.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/28 22:36:00 by tauer #+# #+# */
9-
/* Updated: 2024/04/20 13:27:57 by tauer ### ########.fr */
9+
/* Updated: 2024/04/22 15:32:43 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -19,7 +19,8 @@ bool choose_type(t_data *data, t_arg *arg)
1919
else if ((is_brut(arg) && arg->path) || is_nopath(data, arg))
2020
return (arg->fd = -1, false);
2121
return (arg->path = NULL, arg->fd = -1, arg->type = "ERR",
22-
terror("arg is no fd or cmd. trying exec last command...", true), false);
22+
terror("arg is no fd or cmd. trying exec last command...", true),
23+
false);
2324
}
2425

2526
bool data_element(t_data *data, t_arg *arg, t_arg *list, size_t i)
@@ -74,15 +75,8 @@ bool create_list(t_data *data)
7475
return (true);
7576
while (data->env.argv[i])
7677
if (add_element(data, i++))
77-
return (terror("failed to add element in list", false), free_list(data),
78-
true);
78+
return (terror("failed to add element in list", false),
79+
free_list(data), true);
7980
reverse_argv(data);
8081
return (false);
8182
}
82-
83-
bool set_arg(t_data *data)
84-
{
85-
if (create_list(data))
86-
return (free_tab(data->env.path), terror("failed to create args list", false), true);
87-
return (false);
88-
}

bin/setup/env.c

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/28 22:25:18 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 13:06:12 by tauer ### ########.fr */
9+
/* Updated: 2024/04/22 15:29:35 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -40,23 +40,3 @@ bool set_path(t_data *data)
4040
return (terror("failed to split path", false), true);
4141
return (false);
4242
}
43-
44-
bool set_env(int argc, char **argv, char **envp, t_data *data)
45-
{
46-
if (argc < 4 || !argv)
47-
return (terror("bad args", false), true);
48-
if (envp)
49-
data->env.envp = envp;
50-
if (set_path(data))
51-
return (true);
52-
return (data->env.argc = argc - 1, data->env.argv = argv + 1, false);
53-
}
54-
55-
void null_all(t_data *data)
56-
{
57-
data->env.argc = 0;
58-
data->env.argv = NULL;
59-
data->env.envp = NULL;
60-
data->env.path = NULL;
61-
data->pip.pos = 0;
62-
}

bin/setup/setup.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* bool.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: tauer <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2024/04/01 14:13:51 by tauer #+# #+# */
9+
/* Updated: 2024/04/22 15:29:15 by tauer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include <all.h>
14+
15+
void null_all(t_data *data)
16+
{
17+
data->env.argc = 0;
18+
data->env.argv = NULL;
19+
data->env.envp = NULL;
20+
data->env.path = NULL;
21+
data->pip.pos = 0;
22+
}
23+
24+
bool set_env(int argc, char **argv, char **envp, t_data *data)
25+
{
26+
if (argc < 4 || !argv)
27+
return (terror("bad args", false), true);
28+
if (envp)
29+
data->env.envp = envp;
30+
if (set_path(data))
31+
return (true);
32+
return (data->env.argc = argc - 1, data->env.argv = argv + 1, false);
33+
}
34+
35+
bool set_arg(t_data *data)
36+
{
37+
if (create_list(data))
38+
return (free_tab(data->env.path), terror("failed to create args list",
39+
false), true);
40+
return (false);
41+
}
42+
43+
bool setup(int argc, char **argv, char **envp, t_data *data)
44+
{
45+
null_all(data);
46+
if (set_env(argc, argv, envp, data))
47+
return (terror("set_env failure", true), true);
48+
if (set_arg(data))
49+
return (terror("set arg failure", true), true);
50+
return (false);
51+
}

0 commit comments

Comments
 (0)