Lines 23-28
Link Here
|
23 |
#include <errno.h> // errno |
23 |
#include <errno.h> // errno |
24 |
#include <stdlib.h> // free |
24 |
#include <stdlib.h> // free |
25 |
|
25 |
|
|
|
26 |
#ifndef WIN32 |
27 |
#include <unistd.h> // dup, dup2, close |
28 |
#include <fcntl.h> // fcntl, F_GETFD, F_SETFD |
29 |
#endif |
30 |
|
26 |
#include "util.h" |
31 |
#include "util.h" |
27 |
|
32 |
|
28 |
#define BUFLEN 500 |
33 |
#define BUFLEN 500 |
Lines 55-65
Link Here
|
55 |
* we have many interpreters (with different PID), so the |
60 |
* we have many interpreters (with different PID), so the |
56 |
* thread-safe issue doesn't come up as well. |
61 |
* thread-safe issue doesn't come up as well. |
57 |
*/ |
62 |
*/ |
|
|
63 |
/* |
64 |
* As originally implemented, this technique grossly violates |
65 |
* the Principle of Least Astonishment on Unix by destroying |
66 |
* the output redirections of any TCL program that uses tcl-snmptools. |
67 |
* Therefore we attempt to preserve the original stdout/stderr. |
68 |
*/ |
69 |
#ifndef WIN32 |
70 |
static int stdout_fd, stderr_fd; |
71 |
static fpos_t stdout_pos, stderr_pos; |
72 |
static long stdout_flags, stderr_flags; |
73 |
#endif |
74 |
|
58 |
char *captureOutput(void) { |
75 |
char *captureOutput(void) { |
59 |
extern char *tempfile; |
76 |
extern char *tempfile; |
60 |
FILE *fd; |
77 |
FILE *fd; |
61 |
tempfile = tempnam(TMP, "TEMP"); |
78 |
tempfile = tempnam(TMP, "TEMP"); |
62 |
|
79 |
|
|
|
80 |
#ifndef WIN32 |
81 |
/* preserve the original stdout */ |
82 |
fflush(stdout); |
83 |
fgetpos(stdout, &stdout_pos); |
84 |
stdout_flags = fcntl(fileno(stdout), F_GETFD); |
85 |
stdout_fd = dup(fileno(stdout)); |
86 |
#endif |
87 |
|
63 |
fd = freopen(tempfile, "a", stdout); |
88 |
fd = freopen(tempfile, "a", stdout); |
64 |
if(fd == NULL) { |
89 |
if(fd == NULL) { |
65 |
printres("%s", strerror(errno)); |
90 |
printres("%s", strerror(errno)); |
Lines 68-73
Link Here
|
68 |
} |
93 |
} |
69 |
setvbuf(fd, (char *) NULL, _IONBF, 0); |
94 |
setvbuf(fd, (char *) NULL, _IONBF, 0); |
70 |
|
95 |
|
|
|
96 |
#ifndef WIN32 |
97 |
/* preserve the original stderr */ |
98 |
fflush(stderr); |
99 |
fgetpos(stderr, &stderr_pos); |
100 |
stderr_flags = fcntl(fileno(stderr), F_GETFD); |
101 |
stderr_fd = dup(fileno(stderr)); |
102 |
#endif |
103 |
|
71 |
fd = freopen(tempfile, "a", stderr); |
104 |
fd = freopen(tempfile, "a", stderr); |
72 |
if(fd == NULL) { |
105 |
if(fd == NULL) { |
73 |
printres("%s", strerror(errno)); |
106 |
printres("%s", strerror(errno)); |
Lines 83-88
Link Here
|
83 |
int restoreOutput(char *tempfile) { |
116 |
int restoreOutput(char *tempfile) { |
84 |
int errors = 0; |
117 |
int errors = 0; |
85 |
|
118 |
|
|
|
119 |
#ifdef WIN32 |
86 |
/* Ignore errors but include in the output and return value */ |
120 |
/* Ignore errors but include in the output and return value */ |
87 |
if(freopen(TTY, "w", stdout) == NULL) { |
121 |
if(freopen(TTY, "w", stdout) == NULL) { |
88 |
errors++; |
122 |
errors++; |
Lines 92-97
Link Here
|
92 |
errors++; |
126 |
errors++; |
93 |
printres("%s", strerror(errno)); |
127 |
printres("%s", strerror(errno)); |
94 |
} |
128 |
} |
|
|
129 |
#else |
130 |
/* restore the original stdout */ |
131 |
fflush(stdout); |
132 |
dup2(stdout_fd, fileno(stdout)); |
133 |
fcntl(fileno(stdout), F_SETFD, stdout_flags); |
134 |
close(stdout_fd); |
135 |
clearerr(stdout); |
136 |
fsetpos(stdout, &stdout_pos); |
137 |
|
138 |
|
139 |
/* restore the original stderr */ |
140 |
fflush(stderr); |
141 |
dup2(stderr_fd, fileno(stderr)); |
142 |
fcntl(fileno(stderr), F_SETFD, stderr_flags); |
143 |
close(stderr_fd); |
144 |
clearerr(stderr); |
145 |
fsetpos(stderr, &stderr_pos); |
146 |
#endif |
95 |
|
147 |
|
96 |
if(remove(tempfile)) { |
148 |
if(remove(tempfile)) { |
97 |
errors++; |
149 |
errors++; |
Lines 106-111
Link Here
|
106 |
FILE *fd; |
158 |
FILE *fd; |
107 |
int errors = 0; |
159 |
int errors = 0; |
108 |
|
160 |
|
|
|
161 |
#ifdef WIN32 |
109 |
/* Ignore errors but include in the output and return value */ |
162 |
/* Ignore errors but include in the output and return value */ |
110 |
if(freopen(TTY, "w", stdout) == NULL) { |
163 |
if(freopen(TTY, "w", stdout) == NULL) { |
111 |
errors++; |
164 |
errors++; |
Lines 115-120
Link Here
|
115 |
errors++; |
168 |
errors++; |
116 |
printres("%s", strerror(errno)); |
169 |
printres("%s", strerror(errno)); |
117 |
} |
170 |
} |
|
|
171 |
#else |
172 |
/* restore the original stdout */ |
173 |
fflush(stdout); |
174 |
dup2(stdout_fd, fileno(stdout)); |
175 |
close(stdout_fd); |
176 |
clearerr(stdout); |
177 |
fsetpos(stdout, &stdout_pos); |
178 |
|
179 |
/* restore the original stderr */ |
180 |
fflush(stderr); |
181 |
dup2(stderr_fd, fileno(stderr)); |
182 |
close(stderr_fd); |
183 |
clearerr(stderr); |
184 |
fsetpos(stderr, &stderr_pos); |
185 |
#endif |
118 |
|
186 |
|
119 |
fd = fopen(tempfile, "r"); |
187 |
fd = fopen(tempfile, "r"); |
120 |
if(fd == NULL) { |
188 |
if(fd == NULL) { |