This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project. See the Cygwin home page for more information.
Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: putenv does not put env into win32?


Mumit Khan wrote:
> 
> Ooops. Try this instead:
> 
>   #include <windows.h>
> 
>   int add_path (const char *newdir)
>   {
>     char newpath[1024];
>   #ifdef __CYGWIN__
>     char scratch[1024];
>   #endif
>     const char *pathvar = getenv ("PATH");
>     sprintf (newpath, "PATH=%s:%s", newdir, pathvar);
>     putenv (newpath);
>     printf ("New path: %s\n", newpath);
> 
>   #ifdef __CYGWIN__
>     /* now do the win32 path. */
>     sprintf (scratch, "%s:%s", newdir, pathvar);
>     cygwin32_posix_to_win32_path_list (scratch, newpath);
>     SetEnvironmentVariable ("PATH", newpath);
>     printf ("New path: %s\n", newpath);
>   #endif
>     return 0;
>   }
> 
> and you can now call add_path with Cygwin pathname that will get
> translated to win32 pathname correctly:
> 
>   add_path ("/usr/local/lib");
> 
> Now since dlopen essentially is the same as LoadLibrary (and returns
> the same handle), you could just use dlopen and dlclose (which will
> call the unload library routine). I don't know the details of who's
> calling the unload routine, so I can't judge if that will work for
> you.
> 

The SetEnvironmentVariable is exactly what I was looking for.
Unfortunately MSVC help does not mention it in setenv man page. The code
you posted works just fine. I only changed fixed buffers to dynamically
allocated ones:

static int add_path (const char *newdir)
{
    char * newpath = alloca(8 + strlen(newdir) + (getenv("PATH") ?
strlen(getenv("PATH")): 0));
#ifdef __CYGWIN__
    char * scratch = alloca(8 + strlen(newdir) + (getenv("PATH") ?
strlen(getenv("PATH")): 0));
#endif
    const char *pathvar = getenv ("PATH");
    sprintf (newpath, "PATH=%s:%s", newdir, pathvar);
    putenv (newpath);
    printf ("New path: %s\n", newpath);
    
#ifdef __CYGWIN__
    /* now do the win32 path. */
    sprintf (scratch, "%s:%s", newdir, pathvar);
    cygwin32_posix_to_win32_path_list (scratch, newpath);
    SetEnvironmentVariable ("PATH", newpath);
    printf ("New path: %s\n", newpath);
#endif
    return 0;
}


I verified that if SetEnvironmentVariable is present, LoadLibrary will
work as expected.

I guess cygwin putenv() MUST call SetEnvironmentVariable() at the end. I
see no excuses for that.

Just for test, I tried to call just SetEnvironmentVariable() and it does
not update cygwin variable. Are there the different sets of variables?

Eugene.

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com