@@ -3206,22 +3206,41 @@ namespace Cpp {
3206
3206
int m_DupFD = -1 ;
3207
3207
3208
3208
public:
3209
+ #if defined(_WIN32)
3209
3210
StreamCaptureInfo (int FD) : m_TempFile(tmpfile(), std::fclose), m_FD(FD) {
3210
3211
if (!m_TempFile) {
3211
3212
perror (" StreamCaptureInfo: Unable to create temp file" );
3212
3213
return ;
3213
3214
}
3214
3215
3215
- m_DupFD = dup (FD);
3216
+ m_DupFD = _dup (FD);
3216
3217
3217
3218
// Flush now or can drop the buffer when dup2 is called with Fd later.
3218
3219
// This seems only neccessary when piping stdout or stderr, but do it
3219
3220
// for ttys to avoid over complicated code for minimal benefit.
3220
3221
::fflush (FD == STDOUT_FILENO ? stdout : stderr);
3222
+ if (_dup2 (_fileno (m_TempFile.get ()), FD) < 0 )
3223
+ perror (" StreamCaptureInfo:" );
3224
+ }
3225
+ #else
3226
+ StreamCaptureInfo (int FD) : m_TempFile(tmpfile(), std::fclose), m_FD(FD) {
3227
+
3228
+ if (!m_TempFile) {
3229
+ perror (" StreamCaptureInfo: Unable to create temp file" );
3230
+ return ;
3231
+ }
3232
+
3233
+ m_DupFD = dup (FD);
3221
3234
3235
+ // Flush now or can drop the buffer when dup2 is called with Fd later.
3236
+ // This seems only neccessary when piping stdout or stderr, but do it
3237
+ // for ttys to avoid over complicated code for minimal benefit.
3238
+ ::fflush (FD == STDOUT_FILENO ? stdout : stderr);
3222
3239
if (dup2 (fileno (m_TempFile.get ()), FD) < 0 )
3223
3240
perror (" StreamCaptureInfo:" );
3224
3241
}
3242
+ #endif
3243
+
3225
3244
StreamCaptureInfo (const StreamCaptureInfo&) = delete ;
3226
3245
StreamCaptureInfo& operator =(const StreamCaptureInfo&) = delete ;
3227
3246
StreamCaptureInfo (StreamCaptureInfo&&) = delete ;
@@ -3235,8 +3254,11 @@ namespace Cpp {
3235
3254
assert (m_DupFD != -1 && " Multiple calls to GetCapturedString" );
3236
3255
3237
3256
fflush (nullptr );
3238
-
3257
+ #if defined(_WIN32)
3258
+ if (_dup2 (m_DupFD, m_FD) < 0 )
3259
+ #else
3239
3260
if (dup2 (m_DupFD, m_FD) < 0 )
3261
+ #endif
3240
3262
perror (" StreamCaptureInfo:" );
3241
3263
// Go to the end of the file.
3242
3264
if (fseek (m_TempFile.get (), 0L , SEEK_END) != 0 )
@@ -3263,7 +3285,11 @@ namespace Cpp {
3263
3285
content[newLen++] = ' \0 ' ; // Just to be safe.
3264
3286
3265
3287
std::string result = content.get ();
3288
+ #if defined(_WIN32)
3289
+ _close (m_DupFD);
3290
+ #else
3266
3291
close (m_DupFD);
3292
+ #endif
3267
3293
m_DupFD = -1 ;
3268
3294
return result;
3269
3295
}
0 commit comments