-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathredirect.c
78 lines (72 loc) · 1.62 KB
/
redirect.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#define aerror(msg) { printf(msg); exit(1); }
/**
* Author: Rajmani Arya
* Title: redirect.c
* this four functions are provided to change input
* or output redirection any time in your program with
* any file.
* It is very useful in IPC and Networking Programming.
* NOTE: Here switching is done with directly filename
* If anyone wants same thing with file descriptors.
*
* void switchStdin(const int newfd)
* {
* fflush(stdin);
* fgetpos(stdin, &pos_in);
* fd_in = dup(fileno(stdin));
* dup2(newfd, fileno(stdin));
* }
*/
static int fd_in, fd_out;
static fpos_t pos_in, pos_out;
void switchStdin(const char *newStream)
{
fflush(stdin);
fgetpos(stdin, &pos_in);
fd_in = dup(fileno(stdin));
freopen(newStream, "r", stdin);
}
void revertStdin()
{
fflush(stdin);
dup2(fd_in, fileno(stdin));
close(fd_in);
clearerr(stdin);
fsetpos(stdin, &pos_in);
}
void switchStdout(const char *newStream)
{
fflush(stdout);
fgetpos(stdout, &pos_out);
fd_out = dup(fileno(stdout));
freopen(newStream, "w", stdout);
}
void revertStdout()
{
fflush(stdout);
dup2(fd_out, fileno(stdout));
close(fd_out);
clearerr(stdout);
fsetpos(stdout, &pos_out);
}
int main(int argc, char * argv[]) {
/*switchStdin("in.txt");
switchStdout("out.txt");
char buf[128];
scanf("%s", buf);
printf("%s\n", buf);
revertStdin();
revertStdout();
scanf("%s", buf);
printf("%s\n", buf);
*/char tmp[128];
char buf[32] = "Hello";
sprintf(tmp, "%d %s", 4, buf);
printf("%s\n", tmp);
return 0;
}