File tree 1 file changed +18
-9
lines changed
1 file changed +18
-9
lines changed Original file line number Diff line number Diff line change 3
3
#include < stdio.h>
4
4
#include < stdlib.h>
5
5
6
- #ifndef _WIN32
6
+ #ifdef _WIN32
7
+ # include < io.h>
8
+ # define fileno _fileno
9
+ # define dup _dup
10
+ # define dup2 _dup2
11
+ # define close _close
12
+ # include < fcntl.h>
13
+ #else
7
14
# include < unistd.h>
8
15
#endif
9
16
@@ -30,33 +37,35 @@ namespace LFortran
30
37
{
31
38
public:
32
39
RedirectStdout (std::string &out) : _out{out} {
33
- # ifndef _WIN32
40
+ stdout_fileno = fileno (stdout);
34
41
std::cout << std::flush;
35
42
fflush (stdout);
36
- saved_stdout = dup (STDOUT_FILENO);
37
- if (pipe (out_pipe) != 0 ) {
43
+ saved_stdout = dup (stdout_fileno);
44
+ #ifdef _WIN32
45
+ if (_pipe (out_pipe, 65536 , O_BINARY) != 0 ) {
46
+ #else
47
+ if (pipe (out_pipe) != 0 ) {
48
+ #endif
38
49
throw LFortranException (" pipe() failed" );
39
50
}
40
- dup2 (out_pipe[1 ], STDOUT_FILENO );
51
+ dup2 (out_pipe[1 ], stdout_fileno );
41
52
close (out_pipe[1 ]);
42
53
printf (" X" );
43
- #endif
44
54
}
45
55
46
56
~RedirectStdout () {
47
- #ifndef _WIN32
48
57
fflush (stdout);
49
58
read (out_pipe[0 ], buffer, MAX_LEN);
50
- dup2 (saved_stdout, STDOUT_FILENO );
59
+ dup2 (saved_stdout, stdout_fileno );
51
60
_out = std::string (&buffer[1 ]);
52
- #endif
53
61
}
54
62
private:
55
63
std::string &_out;
56
64
static const size_t MAX_LEN=1024 ;
57
65
char buffer[MAX_LEN+1 ] = {0 };
58
66
int out_pipe[2 ];
59
67
int saved_stdout;
68
+ int stdout_fileno;
60
69
};
61
70
62
71
class custom_interpreter : public xeus ::xinterpreter
You can’t perform that action at this time.
0 commit comments