delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/04/28/12:21:33

Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Message-ID: <372735C7.912BE891@bgs.com>
Date: Wed, 28 Apr 1999 12:22:31 -0400
From: Eugene Kanter <eugene AT bgs DOT com>
X-Mailer: Mozilla 4.51 [en] (WinNT; U)
X-Accept-Language: en
MIME-Version: 1.0
CC: cygwin AT sourceware DOT cygnus DOT com
Subject: Re: putenv does not put env into win32?
References: <199904281530 DOT KAA26761 AT modi DOT xraylith DOT wisc DOT edu>

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 AT sourceware DOT cygnus DOT com

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019