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

Annotation of /trunk/test/redirectStdStreams.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 97 - (hide annotations) (download) (as text)
Fri Dec 9 03:49:19 2005 UTC (18 years ago) by jds
File MIME type: text/x-csrc
File size: 4760 byte(s)
Minor fixes to:
 - continue killing compiler warnings on gcc & msvc 
 - start working on function/data pointer mismatches
 - documentation tweaks

Fixed ascMalloc.c memory logging problems.  This has not been tested on linux/unix.  If it fails to compile, please revert to the previous version and let me know.
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     * 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 jds 97 #include "utilities/ascConfig.h"
27 jds 61 #ifdef __WIN32__
28     #include <io.h>
29     #else
30     #include <unistd.h>
31     #endif
32 jds 59 #include "redirectStdStreams.h"
33    
34 jds 61 static FILE *f_stdin_file = NULL;
35     static int f_stdin_handle = -1;
36     static int f_stdin_fileno = -1;
37 jds 59
38 jds 61 FILE *redirect_stdin(CONST char *filename)
39 jds 59 {
40     assert(NULL != filename);
41 jds 61
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 jds 59 }
48 jds 61 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 jds 59 }
66    
67 jds 61
68     FILE *reset_stdin(void)
69 jds 59 {
70 jds 61 if (NULL != f_stdin_file) {
71     fflush(f_stdin_file);
72     fclose(f_stdin_file);
73     f_stdin_file = NULL;
74 jds 59 }
75 jds 61 /* 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 jds 59 }
84    
85 jds 61 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 jds 59 {
91     assert(NULL != filename);
92 jds 61
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 jds 59 }
99 jds 61 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 jds 59 }
117    
118 jds 61
119     FILE *reset_stdout(void)
120 jds 59 {
121 jds 61 if (NULL != f_stdout_file) {
122     fflush(f_stdout_file);
123     fclose(f_stdout_file);
124     f_stdout_file = NULL;
125 jds 59 }
126 jds 61 /* 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 jds 59 }
135    
136 jds 61 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    

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