-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprocess_redir.c
More file actions
133 lines (124 loc) · 2.54 KB
/
process_redir.c
File metadata and controls
133 lines (124 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "header.h"
void process_redir(char **arg, int narg)
{
char *exec_arg[SIZE];
char *redir_arg[SIZE];
int exec_index = 0;
int redir_index = 0;
int fd;
int flag = 1;
// arg에서 exec할 구문을 분리
for(int i = 0; i <= narg; i++)
{
// NULL을 만나면 중단(파이프 별로 리디렉션하기 위함)
if(!arg[i])
{
*(exec_arg + exec_index) = NULL;
*(redir_arg + redir_index) = NULL;
break;
}
else if(!strncmp("<", arg[i], 2) || !strncmp(">", arg[i], 2) || !strncmp(">>", arg[i], 3))
{
// < > >> 저장
*(redir_arg + redir_index++) = *(arg + i++);
// < > >> 뒷 리디렉션 대상 구문 저장
*(redir_arg + redir_index++) = *(arg + i);
}
else // 실행할 구문 저장
*(exec_arg + exec_index++) = *(arg + i);
}
redir_index = 0;
while(flag)
{
// < > >> 에 따라 수행할 동작 flag로 설정
if(!redir_arg[redir_index])
flag = 0;
else if(!strncmp("<", redir_arg[redir_index], 2))
flag = 1;
else if(!strncmp(">", redir_arg[redir_index], 2))
flag = 2;
else if(!strncmp(">>", redir_arg[redir_index], 3))
flag = 3;
else
flag = 0;
// < > >> 다음 명령어를 읽기
redir_index++;
// 리디렉션 수행
switch(flag)
{
case 1 :
fd = open(redir_arg[redir_index++], O_RDONLY, 0644);
if (fd < 0)
{
perror("open() error");
exit(-1);
}
else
{
if(dup2(fd, STDIN_FILENO) < 0)
{
perror("dup2() error");
exit(-1);
}
if(close(fd) < 0)
{
perror("close() error");
exit(-1);
}
}
break;
case 2 :
fd = open(redir_arg[redir_index++], O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
{
perror("open() error");
exit(-1);
}
else
{
if(dup2(fd, STDOUT_FILENO) < 0)
{
perror("dup2() error");
exit(-1);
}
if(close(fd) < 0)
{
perror("close() error");
exit(-1);
}
}
break;
case 3 :
fd = open(redir_arg[redir_index++], O_WRONLY | O_CREAT | O_APPEND, 0644);
if (fd < 0)
{
perror("open() error");
exit(-1);
}
else
{
if(dup2(fd, STDOUT_FILENO) < 0)
{
perror("dup2() error");
exit(-1);
}
if(close(fd) < 0)
{
perror("close() error");
exit(-1);
}
}
break;
default :
flag = 0;
}
}
execvp(*exec_arg, exec_arg);
// 커맨드가 없으면 실행 안 함, 실행에 실패하면 에러 메세지
if(*exec_arg)
{
fprintf(stderr, "Command '%s' not found.\n", *exec_arg);
exit(-1);
}
return;
}