www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to modify process environment variables

reply Ky-Anh Huynh <saigon example.net> writes:
Hi,

Is it possible to change the current process's environment 
variables?

I have looked at `std/process.d` source code, and there is only a 
private method `createEnv` used when new (sub)process is created.

In C `putEnv` the answer is positive: 
http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME)

I come to this question as I want to set some custom variables 
for my unittests. My program reads some tokens from system 
environments, and it's convenient if I can simulate different 
cases for testings.

Thanks for your reading and support.
Oct 16 2017
next sibling parent reply ketmar <ketmar ketmar.no-ip.org> writes:
Ky-Anh Huynh wrote:

 Hi,

 Is it possible to change the current process's environment variables?

 I have looked at `std/process.d` source code, and there is only a private 
 method `createEnv` used when new (sub)process is created.

 In C `putEnv` the answer is positive: 
 http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME)

 I come to this question as I want to set some custom variables for my 
 unittests. My program reads some tokens from system environments, and 
 it's convenient if I can simulate different cases for testings.

 Thanks for your reading and support.
you can use libc's `putenv()` in D too, it is ok. just import `core.sys.posix.stdlib`, it is there. D is not antagonistic to C, and doesn't try to replace the whole libc with it's own libraries. so if you see something that libc has and you'd like to use -- just do it! ;-)
Oct 16 2017
parent reply Ky-Anh Huynh <saigon example.net> writes:
On Tuesday, 17 October 2017 at 04:56:23 UTC, ketmar wrote:
 you can use libc's `putenv()` in D too, it is ok. just import 
 `core.sys.posix.stdlib`,  it is there. D is not antagonistic to 
 C, and doesn't try to replace the whole libc with it's own 
 libraries. so if you see something that libc has and you'd like 
 to use -- just do it! ;-)
I see :) I have always tried to avoid C if possible :D
Oct 16 2017
parent reply Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Tuesday, 17 October 2017 at 05:57:50 UTC, Ky-Anh Huynh wrote:
 On Tuesday, 17 October 2017 at 04:56:23 UTC, ketmar wrote:
 you can use libc's `putenv()` in D too, it is ok. just import 
 `core.sys.posix.stdlib`,  it is there. D is not antagonistic 
 to C, and doesn't try to replace the whole libc with it's own 
 libraries. so if you see something that libc has and you'd 
 like to use -- just do it! ;-)
I see :) I have always tried to avoid C if possible :D
As an alternative, a search on code.dlang.org turned up this lib: http://code.dlang.org/packages/dotenv
Oct 17 2017
parent Ky-Anh Huynh <saigon example.net> writes:
On Tuesday, 17 October 2017 at 08:42:09 UTC, Rene Zwanenburg 
wrote:
 On Tuesday, 17 October 2017 at 05:57:50 UTC, Ky-Anh Huynh wrote:
 On Tuesday, 17 October 2017 at 04:56:23 UTC, ketmar wrote:
 you can use libc's `putenv()` in D too, it is ok. just import 
 `core.sys.posix.stdlib`,  it is there. D is not antagonistic 
 to C, and doesn't try to replace the whole libc with it's own 
 libraries. so if you see something that libc has and you'd 
 like to use -- just do it! ;-)
I see :) I have always tried to avoid C if possible :D
As an alternative, a search on code.dlang.org turned up this lib: http://code.dlang.org/packages/dotenv
Awesome. I will take a look definitely. I have a similar way in my NodeJS team :D
Oct 17 2017
prev sibling parent reply Jacob Carlborg <doob me.com> writes:
On 2017-10-17 06:51, Ky-Anh Huynh wrote:
 Hi,
 
 Is it possible to change the current process's environment variables?
 
 I have looked at `std/process.d` source code, and there is only a 
 private method `createEnv` used when new (sub)process is created.
 
 In C `putEnv` the answer is positive: 
 http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME)
 
 I come to this question as I want to set some custom variables for my 
 unittests. My program reads some tokens from system environments, and 
 it's convenient if I can simulate different cases for testings.
 
 Thanks for your reading and support.
Use std.process.environment [1] and assign to it like an associative array. [1] https://dlang.org/phobos/std_process.html#.environment.opIndexAssign -- /Jacob Carlborg
Oct 17 2017
parent Ky-Anh Huynh <saigon example.net> writes:
On Tuesday, 17 October 2017 at 11:49:32 UTC, Jacob Carlborg wrote:
 On 2017-10-17 06:51, Ky-Anh Huynh wrote:
 Hi,
 
 Is it possible to change the current process's environment 
 variables?
 
 I have looked at `std/process.d` source code, and there is 
 only a private method `createEnv` used when new (sub)process 
 is created.
 
 In C `putEnv` the answer is positive: 
 http://man7.org/linux/man-pages/man3/putenv.3.html (FIXME)
 
 I come to this question as I want to set some custom variables 
 for my unittests. My program reads some tokens from system 
 environments, and it's convenient if I can simulate different 
 cases for testings.
 
 Thanks for your reading and support.
Use std.process.environment [1] and assign to it like an associative array. [1] https://dlang.org/phobos/std_process.html#.environment.opIndexAssign
Oh thanks a lot for your pointing out, Jacob. That's the thing I'm looking for. The C version is not so bad though ``` import core.sys.posix.stdlib; import std.string: toStringz; string jenkinsToken = "TEST_TOKEN="; putenv(cast(char*)jenkinsToken); ```
Oct 17 2017