1 |
/* |
2 |
* Standard stream redirection for ASCEND unit tests |
3 |
* |
4 |
* Copyright (C) 2005 Jerry St.Clair |
5 |
* |
6 |
* This file is part of the Ascend Environment. |
7 |
* |
8 |
* The Ascend Environment is free software; you can redistribute it |
9 |
* and/or modify it under the terms of the GNU General Public License as |
10 |
* published by the Free Software Foundation; either version 2 of the |
11 |
* License, or (at your option) any later version. |
12 |
* |
13 |
* The Ascend Environment is distributed in hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 |
* General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with the program; if not, write to the Free Software Foundation, |
20 |
* Inc., 675 Mass Ave, Cambridge, MA 02139 USA. Check the file named |
21 |
* COPYING. |
22 |
*/ |
23 |
|
24 |
#include <stdio.h> |
25 |
#include <assert.h> |
26 |
#ifdef __WIN32__ |
27 |
#include <io.h> |
28 |
#else |
29 |
#include <unistd.h> |
30 |
#endif |
31 |
#include "utilities/ascConfig.h" |
32 |
#include "redirectStdStreams.h" |
33 |
|
34 |
static FILE *f_stdin_file = NULL; |
35 |
static int f_stdin_handle = -1; |
36 |
static int f_stdin_fileno = -1; |
37 |
|
38 |
FILE *redirect_stdin(CONST char *filename) |
39 |
{ |
40 |
assert(NULL != filename); |
41 |
|
42 |
/* close the old file, if any */ |
43 |
if (NULL != f_stdin_file) { |
44 |
fflush(f_stdin_file); |
45 |
fclose(f_stdin_file); |
46 |
f_stdin_file = NULL; |
47 |
} |
48 |
fflush(stdin); |
49 |
|
50 |
/* the 1st time through, save the stdin fileno */ |
51 |
if (-1 == f_stdin_fileno) { |
52 |
f_stdin_fileno = fileno(stdin); |
53 |
} |
54 |
|
55 |
/* if this is the 1st redirection, save the file handle */ |
56 |
if (-1 == f_stdin_handle) { |
57 |
if (-1 == (f_stdin_handle = dup(f_stdin_fileno))) { |
58 |
return NULL; |
59 |
} |
60 |
} |
61 |
if (NULL == (f_stdin_file = freopen(filename, "r", stdin))) { |
62 |
reset_stdin(); |
63 |
} |
64 |
return f_stdin_file; |
65 |
} |
66 |
|
67 |
|
68 |
FILE *reset_stdin(void) |
69 |
{ |
70 |
if (NULL != f_stdin_file) { |
71 |
fflush(f_stdin_file); |
72 |
fclose(f_stdin_file); |
73 |
f_stdin_file = NULL; |
74 |
} |
75 |
/* if stdin has been redirected, restore it to original stream */ |
76 |
if ((-1 == f_stdin_handle) || |
77 |
(0 != dup2(f_stdin_handle, f_stdin_fileno))) { |
78 |
return NULL; |
79 |
} |
80 |
/* reset stored handle and return */ |
81 |
f_stdin_handle = -1; |
82 |
return stdin; |
83 |
} |
84 |
|
85 |
static FILE *f_stdout_file = NULL; |
86 |
static int f_stdout_handle = -1; |
87 |
static int f_stdout_fileno = -1; |
88 |
|
89 |
FILE *redirect_stdout(CONST char *filename) |
90 |
{ |
91 |
assert(NULL != filename); |
92 |
|
93 |
/* close the old file, if any */ |
94 |
if (NULL != f_stdout_file) { |
95 |
fflush(f_stdout_file); |
96 |
fclose(f_stdout_file); |
97 |
f_stdout_file = NULL; |
98 |
} |
99 |
fflush(stdout); |
100 |
|
101 |
/* the 1st time through, save the stdout fileno */ |
102 |
if (-1 == f_stdout_fileno) { |
103 |
f_stdout_fileno = fileno(stdout); |
104 |
} |
105 |
|
106 |
/* if this is the 1st redirection, save the file handle */ |
107 |
if (-1 == f_stdout_handle) { |
108 |
if (-1 == (f_stdout_handle = dup(f_stdout_fileno))) { |
109 |
return NULL; |
110 |
} |
111 |
} |
112 |
if (NULL == (f_stdout_file = freopen(filename, "w", stdout))) { |
113 |
reset_stdout(); |
114 |
} |
115 |
return f_stdout_file; |
116 |
} |
117 |
|
118 |
|
119 |
FILE *reset_stdout(void) |
120 |
{ |
121 |
if (NULL != f_stdout_file) { |
122 |
fflush(f_stdout_file); |
123 |
fclose(f_stdout_file); |
124 |
f_stdout_file = NULL; |
125 |
} |
126 |
/* if stdout has been redirected, restore it to original stream */ |
127 |
if ((-1 == f_stdout_handle) || |
128 |
(0 != dup2(f_stdout_handle, f_stdout_fileno))) { |
129 |
return NULL; |
130 |
} |
131 |
/* reset stored handle and return */ |
132 |
f_stdout_handle = -1; |
133 |
return stdout; |
134 |
} |
135 |
|
136 |
static FILE *f_stderr_file = NULL; |
137 |
static int f_stderr_handle = -1; |
138 |
static int f_stderr_fileno = -1; |
139 |
|
140 |
FILE *redirect_stderr(CONST char *filename) |
141 |
{ |
142 |
assert(NULL != filename); |
143 |
|
144 |
/* close the old file, if any */ |
145 |
if (NULL != f_stderr_file) { |
146 |
fflush(f_stderr_file); |
147 |
fclose(f_stderr_file); |
148 |
f_stderr_file = NULL; |
149 |
} |
150 |
fflush(stderr); |
151 |
|
152 |
/* the 1st time through, save the stderr fileno */ |
153 |
if (-1 == f_stderr_fileno) { |
154 |
f_stderr_fileno = fileno(stderr); |
155 |
} |
156 |
|
157 |
/* if this is the 1st redirection, save the file handle */ |
158 |
if (-1 == f_stderr_handle) { |
159 |
if (-1 == (f_stderr_handle = dup(f_stderr_fileno))) { |
160 |
return NULL; |
161 |
} |
162 |
} |
163 |
if (NULL == (f_stderr_file = freopen(filename, "w", stderr))) { |
164 |
reset_stderr(); |
165 |
} |
166 |
return f_stderr_file; |
167 |
} |
168 |
|
169 |
|
170 |
FILE *reset_stderr(void) |
171 |
{ |
172 |
if (NULL != f_stderr_file) { |
173 |
fflush(f_stderr_file); |
174 |
fclose(f_stderr_file); |
175 |
f_stderr_file = NULL; |
176 |
} |
177 |
/* if stderr has been redirected, restore it to original stream */ |
178 |
if ((-1 == f_stderr_handle) || |
179 |
(0 != dup2(f_stderr_handle, f_stderr_fileno))) { |
180 |
return NULL; |
181 |
} |
182 |
/* reset stored handle and return */ |
183 |
f_stderr_handle = -1; |
184 |
return stderr; |
185 |
} |
186 |
|