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