-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_redir.c
More file actions
49 lines (35 loc) · 764 Bytes
/
io_redir.c
File metadata and controls
49 lines (35 loc) · 764 Bytes
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
#include "io_redir.h"
//Should be called from a forked process
//Returns 0 if redirect successful, -1 otherwise
int redirect_in(char* fn){
if(fn == NULL)
return -1;
int fd = open(fn, O_RDONLY);
if(fd == -1)
return -1;
//printf("\tFile %s found!\n", fn);
dup2(fd, 0);
close(fd);
return 0;
}
//Should be called from a forked process
//Returns 0 if redirect successful, -1 otherwise
int redirect_out(char* fn, int append){
if(fn == NULL)
return -1;
int fd;
printf("%d\n",append );
if(append == 1)
fd = open(fn, O_CREAT | O_WRONLY | O_APPEND, S_IREAD | S_IWRITE);
else
fd = open(fn, O_CREAT | O_WRONLY | O_TRUNC, S_IREAD | S_IWRITE);
dup2(fd, 1);
close(fd);
return 0;
}
void reset_in(){
}
void reset_out(){
}
void reset(){
}