@@ -154,19 +154,6 @@ class OSError: public std::runtime_error
154
154
{}
155
155
};
156
156
157
- // --------------------------------------------------------------------
158
-
159
- // Environment Variable types
160
- #ifndef _MSC_VER
161
- using env_string_t = std::string;
162
- using env_char_t = char ;
163
- #else
164
- using env_string_t = std::wstring;
165
- using env_char_t = wchar_t ;
166
- #endif
167
- using env_map_t = std::map<env_string_t , env_string_t >;
168
- using env_vector_t = std::vector<env_char_t >;
169
-
170
157
// --------------------------------------------------------------------
171
158
namespace util
172
159
{
@@ -305,100 +292,6 @@ namespace util
305
292
if (!SetHandleInformation (*child_handle, HANDLE_FLAG_INHERIT, 0 ))
306
293
throw OSError (" SetHandleInformation" , 0 );
307
294
}
308
-
309
- // env_map_t MapFromWindowsEnvironment()
310
- // * Imports current Environment in a C-string table
311
- // * Parses the strings by splitting on the first "=" per line
312
- // * Creates a map of the variables
313
- // * Returns the map
314
- inline env_map_t MapFromWindowsEnvironment (){
315
- wchar_t *variable_strings_ptr;
316
- wchar_t *environment_strings_ptr;
317
- std::wstring delimiter (L" =" );
318
- int del_len = delimiter.length ();
319
- env_map_t mapped_environment;
320
-
321
- // Get a pointer to the environment block.
322
- environment_strings_ptr = GetEnvironmentStringsW ();
323
- // If the returned pointer is NULL, exit.
324
- if (environment_strings_ptr == NULL )
325
- {
326
- throw OSError (" GetEnvironmentStringsW" , 0 );
327
- }
328
-
329
- // Variable strings are separated by NULL byte, and the block is
330
- // terminated by a NULL byte.
331
-
332
- variable_strings_ptr = (wchar_t *) environment_strings_ptr;
333
-
334
- // Since the environment map ends with a null, we can loop until we find it.
335
- while (*variable_strings_ptr)
336
- {
337
- // Create a string from Variable String
338
- env_string_t current_line (variable_strings_ptr);
339
- // Find the first "equals" sign.
340
- auto pos = current_line.find (delimiter);
341
- // Assuming it's not missing ...
342
- if (pos!=std::wstring::npos){
343
- // ... parse the key and value.
344
- env_string_t key = current_line.substr (0 , pos);
345
- env_string_t value = current_line.substr (pos + del_len);
346
- // Map the entry.
347
- mapped_environment[key] = value;
348
- }
349
- // Jump to next line in the environment map.
350
- variable_strings_ptr += std::wcslen (variable_strings_ptr) + 1 ;
351
- }
352
- // We're done with the old environment map buffer.
353
- FreeEnvironmentStringsW (environment_strings_ptr);
354
-
355
- // Return the map.
356
- return mapped_environment;
357
- }
358
-
359
- // env_vector_t WindowsEnvironmentVectorFromMap(const env_map_t &source_map)
360
- // * Creates a vector buffer for the new environment string table
361
- // * Copies in the mapped variables
362
- // * Returns the vector
363
- inline env_vector_t WindowsEnvironmentVectorFromMap (const env_map_t &source_map)
364
- {
365
- // Make a new environment map buffer.
366
- env_vector_t environment_map_buffer;
367
- // Give it some space.
368
- environment_map_buffer.reserve (4096 );
369
-
370
- // And fill'er up.
371
- for (auto kv: source_map){
372
- // Create the line
373
- env_string_t current_line (kv.first ); current_line += L" =" ; current_line += kv.second ;
374
- // Add the line to the buffer.
375
- std::copy (current_line.begin (), current_line.end (), std::back_inserter (environment_map_buffer));
376
- // Append a null
377
- environment_map_buffer.push_back (0 );
378
- }
379
- // Append one last null because of how Windows does it's environment maps.
380
- environment_map_buffer.push_back (0 );
381
-
382
- return environment_map_buffer;
383
- }
384
-
385
- // env_vector_t CreateUpdatedWindowsEnvironmentVector(const env_map_t &changes_map)
386
- // * Merges host environment with new mapped variables
387
- // * Creates and returns string vector based on map
388
- inline env_vector_t CreateUpdatedWindowsEnvironmentVector (const env_map_t &changes_map){
389
- // Import the environment map
390
- env_map_t environment_map = MapFromWindowsEnvironment ();
391
- // Merge in the changes with overwrite
392
- for (auto & it: changes_map)
393
- {
394
- environment_map[it.first ] = it.second ;
395
- }
396
- // Create a Windows-usable Environment Map Buffer
397
- env_vector_t environment_map_strings_vector = WindowsEnvironmentVectorFromMap (environment_map);
398
-
399
- return environment_map_strings_vector;
400
- }
401
-
402
295
#endif
403
296
404
297
/* !
@@ -656,22 +549,6 @@ struct executable: string_arg
656
549
executable (T&& arg): string_arg(std::forward<T>(arg)) {}
657
550
};
658
551
659
- /* !
660
- * Option to specify environment variables required by
661
- * the spawned process.
662
- *
663
- * Eg: environment{{ {"K1", "V1"}, {"K2", "V2"},... }}
664
- */
665
- struct environment
666
- {
667
- environment (env_map_t && env):
668
- env_ (std::move(env)) {}
669
- explicit environment (const env_map_t & env):
670
- env_(env) {}
671
- env_map_t env_;
672
- };
673
-
674
-
675
552
/* !
676
553
* Used for redirecting input/output/error
677
554
*/
@@ -887,7 +764,6 @@ struct ArgumentDeducer
887
764
ArgumentDeducer (Popen* p): popen_(p) {}
888
765
889
766
void set_option (executable&& exe);
890
- void set_option (environment&& env);
891
767
void set_option (input&& inp);
892
768
void set_option (output&& out);
893
769
void set_option (error&& err);
@@ -1214,7 +1090,6 @@ class Popen
1214
1090
#endif
1215
1091
1216
1092
std::string exe_name_;
1217
- env_map_t env_;
1218
1093
1219
1094
// Command in string format
1220
1095
std::string args_;
@@ -1335,13 +1210,6 @@ inline void Popen::kill(int sig_num)
1335
1210
inline void Popen::execute_process () noexcept (false )
1336
1211
{
1337
1212
#ifdef __USING_WINDOWS__
1338
- void * environment_string_table_ptr = nullptr ;
1339
- env_vector_t environment_string_vector;
1340
- if (this ->env_ .size ()){
1341
- environment_string_vector = util::CreateUpdatedWindowsEnvironmentVector (env_);
1342
- environment_string_table_ptr = (void *)environment_string_vector.data ();
1343
- }
1344
-
1345
1213
if (exe_name_.length ()) {
1346
1214
this ->vargs_ .insert (this ->vargs_ .begin (), this ->exe_name_ );
1347
1215
this ->populate_c_argv ();
@@ -1388,7 +1256,7 @@ inline void Popen::execute_process() noexcept(false)
1388
1256
NULL , // primary thread security attributes
1389
1257
TRUE , // handles are inherited
1390
1258
creation_flags, // creation flags
1391
- environment_string_table_ptr , // use provided environment
1259
+ NULL , // use parent's environment
1392
1260
NULL , // use parent's current directory
1393
1261
&siStartInfo, // STARTUPINFOW pointer
1394
1262
&piProcInfo); // receives PROCESS_INFORMATION
@@ -1493,10 +1361,6 @@ namespace detail {
1493
1361
popen_->exe_name_ = std::move (exe.arg_value );
1494
1362
}
1495
1363
1496
- inline void ArgumentDeducer::set_option (environment&& env) {
1497
- popen_->env_ = std::move (env.env_ );
1498
- }
1499
-
1500
1364
inline void ArgumentDeducer::set_option (input&& inp) {
1501
1365
if (inp.rd_ch_ != -1 ) popen_->stream_ .read_from_parent_ = inp.rd_ch_ ;
1502
1366
if (inp.wr_ch_ != -1 ) popen_->stream_ .write_to_child_ = inp.wr_ch_ ;
@@ -1566,14 +1430,7 @@ namespace detail {
1566
1430
close (stream.err_write_ );
1567
1431
1568
1432
// Replace the current image with the executable
1569
- if (parent_->env_ .size ()) {
1570
- for (auto & kv : parent_->env_ ) {
1571
- setenv (kv.first .c_str (), kv.second .c_str (), 1 );
1572
- }
1573
- sys_ret = execvp (parent_->exe_name_ .c_str (), parent_->cargv_ .data ());
1574
- } else {
1575
- sys_ret = execvp (parent_->exe_name_ .c_str (), parent_->cargv_ .data ());
1576
- }
1433
+ sys_ret = execvp (parent_->exe_name_ .c_str (), parent_->cargv_ .data ());
1577
1434
1578
1435
if (sys_ret == -1 ) throw OSError (" execve failed" , errno);
1579
1436
0 commit comments