-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredirection.c
53 lines (52 loc) · 1.47 KB
/
redirection.c
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
#include"headers.h"
void io_redir(char *command[], char *input, char *output, int flag){
int pid;
if((pid = fork()) < 0){
printf("Child process could not be created\n");
return;
}
if(pid == 0){
int fdo, fdi;
int x, y;
//Input Redirection
if(flag == 1){
fdi = open(input, O_RDONLY, 0600);
if(fdi < 0){
perror("Couldn't open the input file");
exit(0);
}
dup2(fdi, STDIN_FILENO);
close(fdi);
if(output != NULL){
fdi = open(output, O_CREAT | O_TRUNC | O_WRONLY, 0644);
dup2(fdi, STDOUT_FILENO);
close(fdi);
}
}
else if(flag == 0){
fdo = open(output, O_CREAT | O_TRUNC | O_WRONLY, 0644);
if(fdo < 0){
perror("Couldn't open the output file");
exit(0);
}
dup2(fdo, STDOUT_FILENO);
close(fdo);
}
else if(flag == 2){
fdo = open(output, O_APPEND | O_RDWR | O_CREAT, 0644);
if(fdo < 0){
perror("Couldn't open the output file");
exit(0);
}
dup2(fdo, STDOUT_FILENO);
close(fdo);
}
if(execvp(command[0], command) == -1){
perror("execvp");
}
}
else{
int status;
while(!(wait(&status) == pid));
}
}