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 |
|
27 |
#include <utilities/ascConfig.h> |
28 |
|
29 |
#ifdef __WIN32__ |
30 |
# include <io.h> |
31 |
#else |
32 |
# include <unistd.h> |
33 |
#endif |
34 |
|
35 |
#include "redirectStdStreams.h" |
36 |
|
37 |
static FILE *f_stdin_file = NULL; |
38 |
static int f_stdin_handle = -1; |
39 |
static int f_stdin_fileno = -1; |
40 |
|
41 |
FILE *redirect_stdin(CONST char *filename) |
42 |
{ |
43 |
assert(NULL != filename); |
44 |
|
45 |
/* close the old file, if any */ |
46 |
if (NULL != f_stdin_file) { |
47 |
fflush(f_stdin_file); |
48 |
fclose(f_stdin_file); |
49 |
f_stdin_file = NULL; |
50 |
} |
51 |
fflush(stdin); |
52 |
|
53 |
/* the 1st time through, save the stdin fileno */ |
54 |
if (-1 == f_stdin_fileno) { |
55 |
f_stdin_fileno = fileno(stdin); |
56 |
} |
57 |
|
58 |
/* if this is the 1st redirection, save the file handle */ |
59 |
if (-1 == f_stdin_handle) { |
60 |
if (-1 == (f_stdin_handle = dup(f_stdin_fileno))) { |
61 |
return NULL; |
62 |
} |
63 |
} |
64 |
if (NULL == (f_stdin_file = freopen(filename, "r", stdin))) { |
65 |
reset_stdin(); |
66 |
} |
67 |
return f_stdin_file; |
68 |
} |
69 |
|
70 |
|
71 |
FILE *reset_stdin(void) |
72 |
{ |
73 |
if (NULL != f_stdin_file) { |
74 |
fflush(f_stdin_file); |
75 |
fclose(f_stdin_file); |
76 |
f_stdin_file = NULL; |
77 |
} |
78 |
/* if stdin has been redirected, restore it to original stream */ |
79 |
if ((-1 == f_stdin_handle) || |
80 |
(0 != dup2(f_stdin_handle, f_stdin_fileno))) { |
81 |
return NULL; |
82 |
} |
83 |
/* reset stored handle and return */ |
84 |
f_stdin_handle = -1; |
85 |
return stdin; |
86 |
} |
87 |
|
88 |
static FILE *f_stdout_file = NULL; |
89 |
static int f_stdout_handle = -1; |
90 |
static int f_stdout_fileno = -1; |
91 |
|
92 |
FILE *redirect_stdout(CONST char *filename) |
93 |
{ |
94 |
assert(NULL != filename); |
95 |
|
96 |
/* close the old file, if any */ |
97 |
if (NULL != f_stdout_file) { |
98 |
fflush(f_stdout_file); |
99 |
fclose(f_stdout_file); |
100 |
f_stdout_file = NULL; |
101 |
} |
102 |
fflush(stdout); |
103 |
|
104 |
/* the 1st time through, save the stdout fileno */ |
105 |
if (-1 == f_stdout_fileno) { |
106 |
f_stdout_fileno = fileno(stdout); |
107 |
} |
108 |
|
109 |
/* if this is the 1st redirection, save the file handle */ |
110 |
if (-1 == f_stdout_handle) { |
111 |
if (-1 == (f_stdout_handle = dup(f_stdout_fileno))) { |
112 |
return NULL; |
113 |
} |
114 |
} |
115 |
if (NULL == (f_stdout_file = freopen(filename, "w", stdout))) { |
116 |
reset_stdout(); |
117 |
} |
118 |
return f_stdout_file; |
119 |
} |
120 |
|
121 |
|
122 |
FILE *reset_stdout(void) |
123 |
{ |
124 |
if (NULL != f_stdout_file) { |
125 |
fflush(f_stdout_file); |
126 |
fclose(f_stdout_file); |
127 |
f_stdout_file = NULL; |
128 |
} |
129 |
/* if stdout has been redirected, restore it to original stream */ |
130 |
if ((-1 == f_stdout_handle) || |
131 |
(0 != dup2(f_stdout_handle, f_stdout_fileno))) { |
132 |
return NULL; |
133 |
} |
134 |
/* reset stored handle and return */ |
135 |
f_stdout_handle = -1; |
136 |
return stdout; |
137 |
} |
138 |
|
139 |
static FILE *f_stderr_file = NULL; |
140 |
static int f_stderr_handle = -1; |
141 |
static int f_stderr_fileno = -1; |
142 |
|
143 |
FILE *redirect_stderr(CONST char *filename) |
144 |
{ |
145 |
assert(NULL != filename); |
146 |
|
147 |
/* close the old file, if any */ |
148 |
if (NULL != f_stderr_file) { |
149 |
fflush(f_stderr_file); |
150 |
fclose(f_stderr_file); |
151 |
f_stderr_file = NULL; |
152 |
} |
153 |
fflush(stderr); |
154 |
|
155 |
/* the 1st time through, save the stderr fileno */ |
156 |
if (-1 == f_stderr_fileno) { |
157 |
f_stderr_fileno = fileno(stderr); |
158 |
} |
159 |
|
160 |
/* if this is the 1st redirection, save the file handle */ |
161 |
if (-1 == f_stderr_handle) { |
162 |
if (-1 == (f_stderr_handle = dup(f_stderr_fileno))) { |
163 |
return NULL; |
164 |
} |
165 |
} |
166 |
if (NULL == (f_stderr_file = freopen(filename, "w", stderr))) { |
167 |
reset_stderr(); |
168 |
} |
169 |
return f_stderr_file; |
170 |
} |
171 |
|
172 |
|
173 |
FILE *reset_stderr(void) |
174 |
{ |
175 |
if (NULL != f_stderr_file) { |
176 |
fflush(f_stderr_file); |
177 |
fclose(f_stderr_file); |
178 |
f_stderr_file = NULL; |
179 |
} |
180 |
/* if stderr has been redirected, restore it to original stream */ |
181 |
if ((-1 == f_stderr_handle) || |
182 |
(0 != dup2(f_stderr_handle, f_stderr_fileno))) { |
183 |
return NULL; |
184 |
} |
185 |
/* reset stored handle and return */ |
186 |
f_stderr_handle = -1; |
187 |
return stderr; |
188 |
} |
189 |
|