/
ioRedirection.c
56 lines (51 loc) · 1.01 KB
/
ioRedirection.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
54
55
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/stat.h>
/* IO Redirection */
int stdOut;
int stdIn;
void saveOut(){
dup2(STDOUT_FILENO, stdOut);
}
void loadOut(){
dup2(stdOut, STDOUT_FILENO);
}
void saveIn(){
dup2(STDIN_FILENO, stdIn);
}
void loadIn(){
dup2(stdIn, STDIN_FILENO);
}
int redirectInput(const char *filename){
int canAccess = 0;
//int fd;
canAccess = access(filename, R_OK);
if(canAccess == 0){
freopen(filename, "r", stdin);
//fd = open(filename, O_WRONLY, 0777);
//dup2(fd, STDIN_FILENO);
//close(fd);
}
return 1;
}
int _redirectOutput(const char *filename, int append){
int fd;
if(append==1)
fd = open(filename, O_CREAT | O_WRONLY, 0777);
else{
fd = open(filename, O_CREAT | O_TRUNC | O_WRONLY, 0777);
}
dup2(fd,STDOUT_FILENO);
close(fd);
return 1;
}
int redirectOutput(const char *filename){
return _redirectOutput(filename, 0);
}
/* /IO Redirection */