/[ascend]/trunk/test/redirectStdStreams.c
ViewVC logotype

Annotation of /trunk/test/redirectStdStreams.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2649 - (hide annotations) (download) (as text)
Wed Dec 12 12:39:25 2012 UTC (9 years, 11 months ago) by jpye
File MIME type: text/x-csrc
File size: 4756 byte(s)
Fixing GPL header, removing postal address (rpmlint incorrect-fsf-address)
1 jds 59 /*
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 jpye 2649 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 jds 59 */
21    
22     #include <stdio.h>
23 johnpye 451 #include <assert.h>
24    
25 jpye 2323 #include <ascend/general/platform.h>
26 johnpye 451
27 jds 61 #ifdef __WIN32__
28 johnpye 451 # include <io.h>
29 jds 61 #else
30 johnpye 451 # include <unistd.h>
31 jds 61 #endif
32 johnpye 451
33 jds 59 #include "redirectStdStreams.h"
34 johnpye 956
35 jpye 2649 #if 0
36 jds 59
37 jds 61 static FILE *f_stdin_file = NULL;
38     static int f_stdin_handle = -1;
39     static int f_stdin_fileno = -1;
40 jds 59
41 jds 61 FILE *redirect_stdin(CONST char *filename)
42 jds 59 {
43     assert(NULL != filename);
44 jds 61
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 jds 59 }
51 jds 61 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 jds 59 }
69    
70 jds 61
71     FILE *reset_stdin(void)
72 jds 59 {
73 jds 61 if (NULL != f_stdin_file) {
74     fflush(f_stdin_file);
75     fclose(f_stdin_file);
76     f_stdin_file = NULL;
77 jds 59 }
78 jds 61 /* 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 jds 59 }
87    
88 jds 61 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 jds 59 {
94     assert(NULL != filename);
95 jds 61
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 jds 59 }
102 jds 61 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 jds 59 }
120    
121 jds 61
122     FILE *reset_stdout(void)
123 jds 59 {
124 jds 61 if (NULL != f_stdout_file) {
125     fflush(f_stdout_file);
126     fclose(f_stdout_file);
127     f_stdout_file = NULL;
128 jds 59 }
129 jds 61 /* 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 jds 59 }
138    
139 jds 61 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 johnpye 956 {
175     fprintf(stderr,"\n\n\n\n\nREDIRECTING STDERR!\n\n\n\n");
176 jds 61 if (NULL != f_stderr_file) {
177     fflush(f_stderr_file);
178     fclose(f_stderr_file);
179     f_stderr_file = NULL;
180     }
181     /* if stderr has been redirected, restore it to original stream */
182     if ((-1 == f_stderr_handle) ||
183     (0 != dup2(f_stderr_handle, f_stderr_fileno))) {
184     return NULL;
185     }
186     /* reset stored handle and return */
187     f_stderr_handle = -1;
188     return stderr;
189     }
190 johnpye 956
191     #endif
192 jds 61

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22