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

Diff of /trunk/test/redirectStdStreams.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 60 by jds, Sun Oct 30 01:38:20 2005 UTC revision 61 by jds, Mon Nov 14 02:37:20 2005 UTC
# Line 23  Line 23 
23    
24  #include <stdio.h>  #include <stdio.h>
25  #include <assert.h>  #include <assert.h>
26    #ifdef __WIN32__
27    #include <io.h>
28    #else
29    #include <unistd.h>
30    #endif
31  #include "utilities/ascConfig.h"  #include "utilities/ascConfig.h"
32  #include "redirectStdStreams.h"  #include "redirectStdStreams.h"
33    
34  FILE *f_stderr_file = NULL;  static FILE *f_stdin_file = NULL;
35  FILE *f_stdin_file = NULL;  static int f_stdin_handle = -1;
36    static int f_stdin_fileno = -1;
37    
38  FILE *redirect_stderr(CONST char *filename)  FILE *redirect_stdin(CONST char *filename)
39  {  {
40    assert(NULL != filename);    assert(NULL != filename);
41    if (NULL != f_stderr_file) {  
42      fclose(f_stderr_file);    /* 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    return (f_stderr_file = freopen(filename, "w", stderr));    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  FILE *reset_stderr(void)  
68    FILE *reset_stdin(void)
69  {  {
70    if (NULL != f_stderr_file) {    if (NULL != f_stdin_file) {
71      fclose(f_stderr_file);      fflush(f_stdin_file);
72        fclose(f_stdin_file);
73        f_stdin_file = NULL;
74    }    }
75    return freopen("CON", "w", stderr);    /* 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  FILE *redirect_stdin(CONST char *filename)  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);    assert(NULL != filename);
92    if (NULL != f_stdin_file) {  
93      fclose(f_stdin_file);    /* 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_stdin_file = freopen(filename, "r", stdin));    return f_stdout_file;
116  }  }
117    
118  FILE *reset_stdin(void)  
119    FILE *reset_stdout(void)
120  {  {
121    if (NULL != f_stdin_file) {    if (NULL != f_stdout_file) {
122      fclose(f_stdin_file);      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    return freopen("CON", "r", stdin);    /* reset stored handle and return */
183      f_stderr_handle = -1;
184      return stderr;
185  }  }
186    

Legend:
Removed from v.60  
changed lines
  Added in v.61

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