Skip to content

Commit 72da739

Browse files
author
Youngho Cho
committed
fix 73+20 , 104, 105, 130 etc
1 parent 3d91337 commit 72da739

11 files changed

+112
-131
lines changed

.DS_Store

-2 KB
Binary file not shown.

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
minishell_tester

Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
NAME = minishell
22
cc = cc
3-
CFLAGS = -g -Wall -Werror -Wextra
3+
CFLAGS = -g
44
RM = rm -rf
55

66
LIB_DIR = ./lib/
@@ -55,7 +55,6 @@ EXECUTE_LIST = make_child.c\
5555
heredoc_utils.c\
5656
redirect_no_fork.c\
5757
redirect_fork.c\
58-
redirect_fork_util.c\
5958
err_exit.c
6059

6160
SRCS = $(addprefix $(SRC_DIR), $(SRCS_LIST)) $(addprefix $(PARSING_DIR), $(PARSING_LIST)) $(addprefix $(BUILTIN_DIR), $(BUILTIN_LIST)) $(addprefix $(EXECUTE_DIR), $(EXECUTE_LIST))

env_rule.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,13 @@ heredoc 입력 받는중에 sigint들어오면 뒤에 파이프가 있더라도
169169
sigquit받았을 경우 "Quit: 3" stderr 에 출력하고 종료
170170

171171
파이프로 실행중에 중간에 실패하는 명령어가 생겨도 뒤에는 계속 실행되야 한다
172-
-> 리다이렉션 실패 시 명령어 멈춰버리는 문제
172+
-> 리다이렉션 실패 시 명령어 멈춰버리는 문제
173+
174+
65번 파일 리다이렉션 검사 -> 잘되는대? 에러메시지 한번만 나옴, 에러코드 잘 나옴
175+
176+
73번 파이프에서 앞 커맨드가 실패하면 뒤 커맨드 실행 제대로 안됨
177+
-> echo 명령어 실행 시 파이프 인덱스가 안 넘어가고 있었음 😢
178+
-> 고치니까 정답 20개 증가 😄
179+
180+
77번 cat <missing | cat :
181+
-> 첫번째 명령어에 없는 파일이 리다이렉션으로 들어올 경우 리다이렉션 실패 처리하면서 파이프를 닫아 줘야 함

includes/minishell.h

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ycho2 <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/09/07 14:17:28 by hyoyoon #+# #+# */
9-
/* Updated: 2024/10/02 18:44:16 by ycho2 ### ########.fr */
9+
/* Updated: 2024/10/03 03:12:15 by ycho2 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -197,14 +197,15 @@ int ft_env(t_blackhole *blackhole);
197197
int ft_export(t_blackhole *blackhole);
198198
int ft_pwd(void);
199199
int ft_cd(t_blackhole *blackhole);
200-
int ft_echo(t_blackhole *blackhole);
200+
int ft_echo(t_blackhole *blackhole, int pipe_i);
201201
int ft_exit(t_blackhole *blackhole);
202202

203203
void make_child(t_blackhole *blackhole);
204204

205205
void execute_command(t_blackhole *blackhole);
206206
int check_cmd_type(t_inner_block *cur_cmd);
207-
void execute_builtin(t_blackhole *blackhole, int cmd_type);
207+
void execute_builtin(t_blackhole *blackhole,
208+
int cmd_type, int pipe_i);
208209
int execute_nbuiltin(t_inner_block_list *cmd_list,
209210
t_env_list *env_list);
210211

@@ -230,7 +231,4 @@ void err_exit(char *field1, char *field2);
230231

231232
void execute_child(t_blackhole *blackhole, int pipe_i);
232233

233-
int redir_out_fork_word(int *flag,
234-
t_inner_block *cur_redir, int *fd);
235-
236234
#endif

src/builtin/echo.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* echo.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: hyoyoon <hyoyoon@student.42.fr> +#+ +:+ +#+ */
6+
/* By: ycho2 <ycho2@student.42seoul.kr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/09/23 15:47:46 by hyoyoon #+# #+# */
9-
/* Updated: 2024/09/30 20:21:38 by hyoyoon ### ########.fr */
9+
/* Updated: 2024/10/03 00:56:55 by ycho2 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -30,14 +30,14 @@ static int check_no_new_line(const char *str)
3030
return (0);
3131
}
3232

33-
int ft_echo(t_blackhole *blackhole)
33+
int ft_echo(t_blackhole *blackhole, int pipe_i)
3434
{
3535
char *str;
3636
int new_line;
3737
int is_option;
3838
t_inner_block *current_node;
3939

40-
current_node = blackhole->parsed_input->cmd_list->head->next;
40+
current_node = blackhole->parsed_input[pipe_i].cmd_list->head->next;
4141
new_line = 1;
4242
is_option = 1;
4343
while (current_node != NULL)

src/execute/execute_command.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
/* ::: :::::::: */
44
/* execute_command.c :+: :+: :+: */
55
/* +:+ +:+ +:+ */
6-
/* By: hyoyoon <hyoyoon@student.42.fr> +#+ +:+ +#+ */
6+
/* By: ycho2 <ycho2@student.42seoul.kr> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/09/23 09:54:32 by ycho2 #+# #+# */
9-
/* Updated: 2024/10/02 19:32:25 by hyoyoon ### ########.fr */
9+
/* Updated: 2024/10/03 00:54:45 by ycho2 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -33,7 +33,7 @@ void execute_command(t_blackhole *blackhole)
3333
if (head_cmd_type == B_NULL)
3434
blackhole->exit_code = 0;
3535
else
36-
execute_builtin(blackhole, head_cmd_type);
36+
execute_builtin(blackhole, head_cmd_type, 0);
3737
dup2(tmp_std_in, STDIN_FILENO);
3838
dup2(tmp_std_out, STDOUT_FILENO);
3939
}
@@ -66,10 +66,10 @@ int check_cmd_type(t_inner_block *cur_cmd)
6666
return (type);
6767
}
6868

69-
void execute_builtin(t_blackhole *blackhole, int cmd_type)
69+
void execute_builtin(t_blackhole *blackhole, int cmd_type, int pipe_i)
7070
{
7171
if (cmd_type == B_ECHO)
72-
blackhole->exit_code = ft_echo(blackhole);
72+
blackhole->exit_code = ft_echo(blackhole, pipe_i);
7373
else if (cmd_type == B_CD)
7474
blackhole->exit_code = ft_cd(blackhole);
7575
else if (cmd_type == B_PWD)

src/execute/make_child.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ycho2 <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/09/22 14:39:07 by hyoyoon #+# #+# */
9-
/* Updated: 2024/10/02 21:29:56 by ycho2 ### ########.fr */
9+
/* Updated: 2024/10/03 04:54:59 by ycho2 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -31,9 +31,7 @@ void make_child(t_blackhole *blackhole)
3131
child_util.last_child_pid = -1;
3232
child_util.last_child_status = -1;
3333
while (child_util.pipe_i <= blackhole->pipe_cnt)
34-
{
3534
ft_redirect_fork(&child_util, blackhole);
36-
}
3735
set_terminal(1);
3836
signal(SIGINT, ignore_signal);
3937
pid = 0;
@@ -53,10 +51,19 @@ static int ft_redirect_fork(t_child_util *child_util, t_blackhole *blackhole)
5351
pipe(child_util->pipefd);
5452
child_util->childfd[0] = STDIN_FILENO;
5553
child_util->childfd[1] = STDOUT_FILENO;
54+
if (child_util->pipe_i != 0)
55+
child_util->childfd[0] = child_util->prev_pipe;
56+
if (child_util->pipe_i != child_util->pipecnt)
57+
child_util->childfd[1] = child_util->pipefd[1];
5658
if (set_child_redir(
5759
blackhole->parsed_input[child_util->pipe_i].redirection_list,
5860
child_util) == 1)
5961
{
62+
// if (child_util->pipe_i != child_util->pipecnt)
63+
// close(child_util->pipefd[1]);
64+
// close(child_util->pipefd[0]);
65+
// if (child_util->pipe_i != 0)
66+
// close(child_util->prev_pipe);
6067
child_util->pipe_i++;
6168
blackhole->exit_code = 1;
6269
return (1);

src/execute/make_child_util.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: ycho2 <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/10/02 18:04:20 by ycho2 #+# #+# */
9-
/* Updated: 2024/10/02 18:39:10 by ycho2 ### ########.fr */
9+
/* Updated: 2024/10/03 00:54:14 by ycho2 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -19,7 +19,7 @@ void execute_child(t_blackhole *blackhole, int pipe_i)
1919

2020
if (cmd_type <= 6)
2121
{
22-
execute_builtin(blackhole, cmd_type);
22+
execute_builtin(blackhole, cmd_type, pipe_i);
2323
exit(EXIT_SUCCESS);
2424
}
2525
else

src/execute/redirect_fork.c

+75-68
Original file line numberDiff line numberDiff line change
@@ -6,117 +6,124 @@
66
/* By: ycho2 <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/09/28 19:10:33 by ycho2 #+# #+# */
9-
/* Updated: 2024/10/02 19:24:44 by ycho2 ### ########.fr */
9+
/* Updated: 2024/10/03 04:55:26 by ycho2 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

1313
#include "minishell.h"
1414

15-
static int redir_input_fork(t_inner_block_list *redir_l,
16-
t_child_util *child_util);
17-
static int redir_output_fork(t_inner_block_list *redir_l,
18-
t_child_util *child_util);
19-
static void set_pipe(t_child_util *child_util);
20-
static int redir_in_fork_word(int *flag, int *fd,
21-
t_inner_block *cur_redir);
15+
static int ft_redir_append(int *fd_out, char *file_name);
16+
static int ft_redir_heredoc(int *fd_in, char *delimeter);
17+
static int ft_redir_output(int *fd_out, char *file_name);
18+
static int ft_redir_input(int *fd_in, char *file_name);
19+
static int ft_set_child_redir_word(int *flag, int *fd_in,
20+
int *fd_out, t_inner_block *cur_redir);
2221

23-
int set_child_redir(t_inner_block_list *redirect_list, t_child_util *child_util)
24-
{
25-
int err_flag;
26-
27-
set_pipe(child_util);
28-
err_flag = redir_input_fork(redirect_list, child_util);
29-
if (err_flag)
30-
return (1);
31-
err_flag = redir_output_fork(redirect_list, child_util);
32-
if (err_flag)
33-
return (1);
34-
return (0);
35-
}
36-
37-
static int redir_input_fork(t_inner_block_list *redirect_list,
22+
int set_child_redir(t_inner_block_list *redirect_list,
3823
t_child_util *child_util)
3924
{
40-
int fd;
25+
int fd_out;
26+
int fd_in;
4127
int flag;
4228
t_inner_block *cur_redir;
4329

44-
fd = -1;
30+
fd_in = -1;
31+
fd_out = -1;
4532
flag = 0;
4633
cur_redir = redirect_list->head;
4734
while (cur_redir)
4835
{
4936
if (cur_redir->type == WORD)
5037
{
51-
if (redir_in_fork_word(&flag, &fd, cur_redir))
38+
if (ft_set_child_redir_word(&flag, &fd_in, &fd_out, cur_redir) == 1)
5239
return (1);
5340
}
5441
else
5542
flag = cur_redir->type;
5643
cur_redir = cur_redir->next;
5744
}
58-
if (fd >= 0)
59-
child_util->childfd[0] = fd;
45+
if (fd_in >= 0)
46+
child_util->childfd[0] = fd_in;
47+
if (fd_out >= 0)
48+
child_util->childfd[1] = fd_out;
6049
return (0);
6150
}
6251

63-
static int redir_in_fork_word(int *flag, int *fd,
64-
t_inner_block *cur_redir)
52+
static int ft_set_child_redir_word(int *flag, int *fd_in,
53+
int *fd_out, t_inner_block *cur_redir)
6554
{
6655
if (*flag == REDIR_IN)
6756
{
68-
if (*fd > 0)
69-
close(*fd);
70-
*fd = open(cur_redir->str, O_RDONLY, 0);
71-
if (*fd < 0)
72-
{
73-
err_exit(cur_redir->str, strerror(errno));
57+
if (ft_redir_input(fd_in, cur_redir->str))
7458
return (1);
75-
}
7659
}
7760
else if (*flag == HEREDOC)
7861
{
79-
if (*fd > 0)
80-
close(*fd);
81-
*fd = open("/var/tmp/tmp.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
82-
if (ft_heredoc(cur_redir->str, *fd) == 1)
62+
if (ft_redir_heredoc(fd_in, cur_redir->str))
63+
return (1);
64+
}
65+
else if (*flag == REDIR_OUT)
66+
{
67+
if (ft_redir_output(fd_out, cur_redir->str))
68+
return (1);
69+
}
70+
else if (*flag == REDIR_APPEND)
71+
{
72+
if (ft_redir_append(fd_out, cur_redir->str))
8373
return (1);
84-
close(*fd);
85-
*fd = open("/var/tmp/tmp.txt", O_RDONLY);
8674
}
8775
return (0);
8876
}
8977

90-
static int redir_output_fork(t_inner_block_list *redirect_list,
91-
t_child_util *child_util)
78+
static int ft_redir_append(int *fd_out, char *file_name)
9279
{
93-
int fd;
94-
int flag;
95-
t_inner_block *cur_redir;
80+
if (*fd_out > 0)
81+
close(*fd_out);
82+
*fd_out = open(file_name, O_WRONLY | O_CREAT | O_APPEND, 0644);
83+
if (*fd_out < 0)
84+
{
85+
err_exit(file_name, strerror(errno));
86+
return (1);
87+
}
88+
else
89+
return (0);
90+
}
9691

97-
flag = 0;
98-
fd = -1;
99-
cur_redir = redirect_list->head;
100-
while (cur_redir)
92+
static int ft_redir_input(int *fd_in, char *file_name)
93+
{
94+
if (*fd_in > 0)
95+
close(*fd_in);
96+
*fd_in = open(file_name, O_RDONLY, 0);
97+
if (*fd_in < 0)
10198
{
102-
if (cur_redir->type == WORD)
103-
{
104-
if (redir_out_fork_word(&flag, cur_redir, &fd))
105-
return (1);
106-
}
107-
else
108-
flag = cur_redir->type;
109-
cur_redir = cur_redir->next;
99+
err_exit(file_name, strerror(errno));
100+
return (1);
110101
}
111-
if (fd >= 0)
112-
child_util->childfd[1] = fd;
113-
return (0);
102+
else
103+
return (0);
114104
}
115105

116-
static void set_pipe(t_child_util *child_util)
106+
static int ft_redir_heredoc(int *fd_in, char *delimeter)
117107
{
118-
if (child_util->pipe_i != 0)
119-
child_util->childfd[0] = child_util->prev_pipe;
120-
if (child_util->pipe_i != child_util->pipecnt)
121-
child_util->childfd[1] = child_util->pipefd[1];
108+
if (*fd_in > 0)
109+
close(*fd_in);
110+
*fd_in = open("/var/tmp/tmp.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
111+
if (ft_heredoc(delimeter, *fd_in) == 1)
112+
return (1);
113+
close(*fd_in);
114+
*fd_in = open("/var/tmp/tmp.txt", O_RDONLY);
115+
return (0);
122116
}
117+
118+
static int ft_redir_output(int *fd_out, char *file_name)
119+
{
120+
if (*fd_out > 0)
121+
close(*fd_out);
122+
*fd_out = open(file_name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
123+
if (*fd_out < 0)
124+
{
125+
err_exit(file_name, strerror(errno));
126+
return (1);
127+
}
128+
return (0);
129+
}

0 commit comments

Comments
 (0)