@@ -68,10 +68,6 @@ extern "C" {
68
68
#include < windows.h>
69
69
#include < io.h>
70
70
#include < cwchar>
71
-
72
- #define close _close
73
- #define open _open
74
- #define fileno _fileno
75
71
#else
76
72
#include < sys/wait.h>
77
73
#include < unistd.h>
@@ -81,6 +77,22 @@ extern "C" {
81
77
#include < sys/types.h>
82
78
}
83
79
80
+ // The Microsoft C++ compiler issues deprecation warnings
81
+ // for the standard POSIX function names.
82
+ // Its preferred implementations have a leading underscore.
83
+ // See: https://learn.microsoft.com/en-us/cpp/c-runtime-library/compatibility.
84
+ #if (defined _MSC_VER)
85
+ #define subprocess_close _close
86
+ #define subprocess_fileno _fileno
87
+ #define subprocess_open _open
88
+ #define subprocess_write _write
89
+ #else
90
+ #define subprocess_close close
91
+ #define subprocess_fileno fileno
92
+ #define subprocess_open open
93
+ #define subprocess_write write
94
+ #endif
95
+
84
96
/* !
85
97
* Getting started with reading this source code.
86
98
* The source is mainly divided into four parts:
@@ -264,7 +276,7 @@ namespace util
264
276
265
277
FILE *fp = _fdopen (os_fhandle, mode);
266
278
if (fp == 0 ) {
267
- _close (os_fhandle);
279
+ subprocess_close (os_fhandle);
268
280
throw OSError (" _fdopen" , 0 );
269
281
}
270
282
@@ -383,7 +395,7 @@ namespace util
383
395
{
384
396
size_t nwritten = 0 ;
385
397
while (nwritten < length) {
386
- int written = write (fd, buf + nwritten, length - nwritten);
398
+ int written = subprocess_write (fd, buf + nwritten, length - nwritten);
387
399
if (written == -1 ) return -1 ;
388
400
nwritten += written;
389
401
}
@@ -411,7 +423,7 @@ namespace util
411
423
#ifdef __USING_WINDOWS__
412
424
return (int )fread (buf, 1 , read_upto, fp);
413
425
#else
414
- int fd = fileno (fp);
426
+ int fd = subprocess_fileno (fp);
415
427
int rbytes = 0 ;
416
428
int eintr_cnter = 0 ;
417
429
@@ -573,10 +585,10 @@ struct input
573
585
explicit input (int fd): rd_ch_(fd) {}
574
586
575
587
// FILE pointer.
576
- explicit input (FILE* fp):input(fileno (fp)) { assert (fp); }
588
+ explicit input (FILE* fp):input(subprocess_fileno (fp)) { assert (fp); }
577
589
578
590
explicit input (const char * filename) {
579
- int fd = open (filename, O_RDONLY);
591
+ int fd = subprocess_open (filename, O_RDONLY);
580
592
if (fd == -1 ) throw OSError (" File not found: " , errno);
581
593
rd_ch_ = fd;
582
594
}
@@ -606,10 +618,10 @@ struct output
606
618
{
607
619
explicit output (int fd): wr_ch_(fd) {}
608
620
609
- explicit output (FILE* fp):output(fileno (fp)) { assert (fp); }
621
+ explicit output (FILE* fp):output(subprocess_fileno (fp)) { assert (fp); }
610
622
611
623
explicit output (const char * filename) {
612
- int fd = open (filename, O_APPEND | O_CREAT | O_RDWR, 0640 );
624
+ int fd = subprocess_open (filename, O_APPEND | O_CREAT | O_RDWR, 0640 );
613
625
if (fd == -1 ) throw OSError (" File not found: " , errno);
614
626
wr_ch_ = fd;
615
627
}
@@ -637,10 +649,10 @@ struct error
637
649
{
638
650
explicit error (int fd): wr_ch_(fd) {}
639
651
640
- explicit error (FILE* fp):error(fileno (fp)) { assert (fp); }
652
+ explicit error (FILE* fp):error(subprocess_fileno (fp)) { assert (fp); }
641
653
642
654
explicit error (const char * filename) {
643
- int fd = open (filename, O_APPEND | O_CREAT | O_RDWR, 0640 );
655
+ int fd = subprocess_open (filename, O_APPEND | O_CREAT | O_RDWR, 0640 );
644
656
if (fd == -1 ) throw OSError (" File not found: " , errno);
645
657
wr_ch_ = fd;
646
658
}
@@ -803,28 +815,28 @@ class Streams
803
815
void cleanup_fds ()
804
816
{
805
817
if (write_to_child_ != -1 && read_from_parent_ != -1 ) {
806
- close (write_to_child_);
818
+ subprocess_close (write_to_child_);
807
819
}
808
820
if (write_to_parent_ != -1 && read_from_child_ != -1 ) {
809
- close (read_from_child_);
821
+ subprocess_close (read_from_child_);
810
822
}
811
823
if (err_write_ != -1 && err_read_ != -1 ) {
812
- close (err_read_);
824
+ subprocess_close (err_read_);
813
825
}
814
826
}
815
827
816
828
void close_parent_fds ()
817
829
{
818
- if (write_to_child_ != -1 ) close (write_to_child_);
819
- if (read_from_child_ != -1 ) close (read_from_child_);
820
- if (err_read_ != -1 ) close (err_read_);
830
+ if (write_to_child_ != -1 ) subprocess_close (write_to_child_);
831
+ if (read_from_child_ != -1 ) subprocess_close (read_from_child_);
832
+ if (err_read_ != -1 ) subprocess_close (err_read_);
821
833
}
822
834
823
835
void close_child_fds ()
824
836
{
825
- if (write_to_parent_ != -1 ) close (write_to_parent_);
826
- if (read_from_parent_ != -1 ) close (read_from_parent_);
827
- if (err_write_ != -1 ) close (err_write_);
837
+ if (write_to_parent_ != -1 ) subprocess_close (write_to_parent_);
838
+ if (read_from_parent_ != -1 ) subprocess_close (read_from_parent_);
839
+ if (err_write_ != -1 ) subprocess_close (err_write_);
828
840
}
829
841
830
842
FILE* input () { return input_.get (); }
@@ -1161,8 +1173,8 @@ inline void Popen::execute_process() noexcept(false)
1161
1173
child_pid_ = fork ();
1162
1174
1163
1175
if (child_pid_ < 0 ) {
1164
- close (err_rd_pipe);
1165
- close (err_wr_pipe);
1176
+ subprocess_close (err_rd_pipe);
1177
+ subprocess_close (err_wr_pipe);
1166
1178
throw OSError (" fork failed" , errno);
1167
1179
}
1168
1180
@@ -1172,14 +1184,14 @@ inline void Popen::execute_process() noexcept(false)
1172
1184
stream_.close_parent_fds ();
1173
1185
1174
1186
// Close the read end of the error pipe
1175
- close (err_rd_pipe);
1187
+ subprocess_close (err_rd_pipe);
1176
1188
1177
1189
detail::Child chld (this , err_wr_pipe);
1178
1190
chld.execute_child ();
1179
1191
}
1180
1192
else
1181
1193
{
1182
- close (err_wr_pipe);// close child side of pipe, else get stuck in read below
1194
+ subprocess_close (err_wr_pipe);// close child side of pipe, else get stuck in read below
1183
1195
1184
1196
stream_.close_child_fds ();
1185
1197
@@ -1188,7 +1200,7 @@ inline void Popen::execute_process() noexcept(false)
1188
1200
1189
1201
FILE* err_fp = fdopen (err_rd_pipe, " r" );
1190
1202
if (!err_fp) {
1191
- close (err_rd_pipe);
1203
+ subprocess_close (err_rd_pipe);
1192
1204
throw OSError (" fdopen failed" , errno);
1193
1205
}
1194
1206
int read_bytes = util::read_atmost_n (err_fp, err_buf, SP_MAX_ERR_BUF_SIZ);
@@ -1278,13 +1290,13 @@ namespace detail {
1278
1290
1279
1291
// Close the duped descriptors
1280
1292
if (stream.read_from_parent_ != -1 && stream.read_from_parent_ > 2 )
1281
- close (stream.read_from_parent_ );
1293
+ subprocess_close (stream.read_from_parent_ );
1282
1294
1283
1295
if (stream.write_to_parent_ != -1 && stream.write_to_parent_ > 2 )
1284
- close (stream.write_to_parent_ );
1296
+ subprocess_close (stream.write_to_parent_ );
1285
1297
1286
1298
if (stream.err_write_ != -1 && stream.err_write_ > 2 )
1287
- close (stream.err_write_ );
1299
+ subprocess_close (stream.err_write_ );
1288
1300
1289
1301
// Replace the current image with the executable
1290
1302
sys_ret = execvp (parent_->exe_name_ .c_str (), parent_->cargv_ .data ());
@@ -1311,15 +1323,15 @@ namespace detail {
1311
1323
#ifdef __USING_WINDOWS__
1312
1324
util::configure_pipe (&this ->g_hChildStd_IN_Rd , &this ->g_hChildStd_IN_Wr , &this ->g_hChildStd_IN_Wr );
1313
1325
this ->input (util::file_from_handle (this ->g_hChildStd_IN_Wr , " w" ));
1314
- this ->write_to_child_ = _fileno (this ->input ());
1326
+ this ->write_to_child_ = subprocess_fileno (this ->input ());
1315
1327
1316
1328
util::configure_pipe (&this ->g_hChildStd_OUT_Rd , &this ->g_hChildStd_OUT_Wr , &this ->g_hChildStd_OUT_Rd );
1317
1329
this ->output (util::file_from_handle (this ->g_hChildStd_OUT_Rd , " r" ));
1318
- this ->read_from_child_ = _fileno (this ->output ());
1330
+ this ->read_from_child_ = subprocess_fileno (this ->output ());
1319
1331
1320
1332
util::configure_pipe (&this ->g_hChildStd_ERR_Rd , &this ->g_hChildStd_ERR_Wr , &this ->g_hChildStd_ERR_Rd );
1321
1333
this ->error (util::file_from_handle (this ->g_hChildStd_ERR_Rd , " r" ));
1322
- this ->err_read_ = _fileno (this ->error ());
1334
+ this ->err_read_ = subprocess_fileno (this ->error ());
1323
1335
#else
1324
1336
1325
1337
if (write_to_child_ != -1 ) input (fdopen (write_to_child_, " wb" ));
0 commit comments