www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 1004] New: Changed environment not passed to child process

reply d-bugmail puremagic.com writes:

           Summary: Changed environment not passed to child process
           Product: D
           Version: 1.007
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: t_demmer web.de

   Bug in spawnvp (and maybe others):
   Only the initial environment is copied to the child process,
   modifications get lost
   Compile and run, type "SET",
   you should see a line 
   but you don't.
   Works fine with gdc -mno-cygwin,
   so I guess it is a phobos bug.
import std.process;

extern(Windows) void  SetEnvironmentVariableA(char *lpName, char *lpValue);

main(char[][] args){
  return spawnvp(0,"cmd.exe",args);

I did not find guidelines for setting values for Priority,
"Assign To", and "Cc", so sorry for bothering everyone...

Feb 24 2007
parent d-bugmail puremagic.com writes:

These two versions work for me, someone with a way better knowledge
of D may optimize the clumsy version of spawnvp

int spawnvpe(int mode, char[] pathname, char[][]argv, char[][] envi){
  char** argv_ = cast(char**)alloca((char*).sizeof * (1 + argv.length));
  char** envi_ = cast(char**)alloca((char*).sizeof * (1 + envi.length));
  toAStringz(argv, argv_);
  toAStringz(envi, envi_);

  return std.c.process.spawnvpe(mode, toStringz(pathname), argv_, envi_ );

spawnvp(int mode, char[] pathname, char[][] argv){
  char** argv_ = cast(char**)alloca((char*).sizeof * (1 + argv.length));
  toAStringz(argv, argv_);

  char[][] env;  
  char *eBase=cast(char *)GetEnvironmentStringsA();
  for(char *str=eBase; *str; ++str){
    int slen = std.string.strlen(str);
    env.length = env.length+1;
    env[env.length-1].length = slen;
    size_t i = 0;
      env[env.length-1][i++]= *str++;
  char** env_ = cast(char**)alloca((char*).sizeof * (1 + env.length));
  toAStringz(env, env_);
  FreeEnvironmentStringsA(cast(char **)eBase);
  return std.c.process.spawnvpe(mode, toStringz(pathname), argv_, env_ );

Feb 24 2007