From a6ed49ade372120580d121a4b9c4c7a93ab74465 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Fri, 10 Oct 2003 23:33:21 +0000 Subject: [PATCH] Don't reference the environ global variable from kernel32, it may fail to link (reported by Gerald Pfeifer). --- dlls/kernel/process.c | 42 +++++++++++++++++++++++------------------- include/wine/library.h | 1 + libs/wine/loader.c | 3 +++ 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/dlls/kernel/process.c b/dlls/kernel/process.c index a0a8129306f..80b4276c92c 100644 --- a/dlls/kernel/process.c +++ b/dlls/kernel/process.c @@ -325,13 +325,12 @@ static HMODULE load_pe_exe( const WCHAR *name, HANDLE file ) } /*********************************************************************** - * build_environment + * build_initial_environment * * Build the Win32 environment from the Unix environment */ -static BOOL build_initial_environment(void) +static BOOL build_initial_environment( char **environ ) { - extern char **environ; ULONG size = 1; char **e; WCHAR *p, *endptr; @@ -603,7 +602,7 @@ static RTL_USER_PROCESS_PARAMETERS *init_user_process_params( size_t info_size ) * * Main process initialisation code */ -static BOOL process_init( char *argv[] ) +static BOOL process_init( char *argv[], char **environ ) { BOOL ret; size_t info_size = 0; @@ -685,7 +684,7 @@ static BOOL process_init( char *argv[] ) } /* Copy the parent environment */ - if (!build_initial_environment()) return FALSE; + if (!build_initial_environment( environ )) return FALSE; /* Parse command line arguments */ OPTIONS_ParseOptions( !info_size ? argv : NULL ); @@ -744,7 +743,7 @@ void __wine_kernel_init(void) PEB *peb = NtCurrentTeb()->Peb; /* Initialize everything */ - if (!process_init( __wine_main_argv )) exit(1); + if (!process_init( __wine_main_argv, __wine_main_environ )) exit(1); __wine_main_argv++; /* remove argv[0] (wine itself) */ __wine_main_argc--; @@ -975,6 +974,19 @@ static char **build_argv( const WCHAR *cmdlineW, int reserved ) } +/*********************************************************************** + * alloc_env_string + * + * Allocate an environment string; helper for build_envp + */ +static char *alloc_env_string( const char *name, const char *value ) +{ + char *ret = malloc( strlen(name) + strlen(value) + 1 ); + strcpy( ret, name ); + strcat( ret, value ); + return ret; +} + /*********************************************************************** * build_envp * @@ -1003,32 +1015,24 @@ static char **build_envp( const WCHAR *envW, const WCHAR *extra_envW ) if (!(env = malloc( length ))) return NULL; WideCharToMultiByte( CP_UNIXCP, 0, envW, p - envW, env, length, NULL, NULL ); - count += 3; + count += 4; if ((envp = malloc( count * sizeof(*envp) ))) { - extern char **environ; char **envptr = envp; - char **unixptr = environ; char *p; /* first the extra strings */ if (extra_env) for (p = extra_env; *p; p += strlen(p) + 1) *envptr++ = p; /* then put PATH, HOME and WINEPREFIX from the unix env */ - for (unixptr = environ; unixptr && *unixptr; unixptr++) - if (!memcmp( *unixptr, "PATH=", 5 ) || - !memcmp( *unixptr, "HOME=", 5 ) || - !memcmp( *unixptr, "WINEPREFIX=", 11 )) *envptr++ = *unixptr; + if ((p = getenv("PATH"))) *envptr++ = alloc_env_string( "PATH=", p ); + if ((p = getenv("HOME"))) *envptr++ = alloc_env_string( "HOME=", p ); + if ((p = getenv("WINEPREFIX"))) *envptr++ = alloc_env_string( "WINEPREFIX=", p ); /* now put the Windows environment strings */ for (p = env; *p; p += strlen(p) + 1) { if (!memcmp( p, "PATH=", 5 )) /* store PATH as WINEPATH */ - { - char *winepath = malloc( strlen(p) + 5 ); - strcpy( winepath, "WINE" ); - strcpy( winepath + 4, p ); - *envptr++ = winepath; - } + *envptr++ = alloc_env_string( "WINEPATH=", p + 5 ); else if (memcmp( p, "HOME=", 5 ) && memcmp( p, "WINEPATH=", 9 ) && memcmp( p, "WINEPREFIX=", 11 )) *envptr++ = p; diff --git a/include/wine/library.h b/include/wine/library.h index 1f72a0fbaf0..d6a73fc6a38 100644 --- a/include/wine/library.h +++ b/include/wine/library.h @@ -50,6 +50,7 @@ extern void wine_dll_unload( void *handle ); extern int __wine_main_argc; extern char **__wine_main_argv; extern WCHAR **__wine_main_wargv; +extern char **__wine_main_environ; /* debugging */ diff --git a/libs/wine/loader.c b/libs/wine/loader.c index fe725e6a786..ece5dc44e0d 100644 --- a/libs/wine/loader.c +++ b/libs/wine/loader.c @@ -45,6 +45,7 @@ int __wine_main_argc = 0; char **__wine_main_argv = NULL; WCHAR **__wine_main_wargv = NULL; +char **__wine_main_environ = NULL; extern void init_argv0_path( const char *argv0 ); /* config.c */ @@ -414,6 +415,7 @@ void *wine_dll_load_main_exe( const char *name, char *error, int errorsize, */ void wine_init( int argc, char *argv[], char *error, int error_size ) { + extern char **environ; int file_exists; void *ntdll; void (*init_func)(void); @@ -422,6 +424,7 @@ void wine_init( int argc, char *argv[], char *error, int error_size ) init_argv0_path( argv[0] ); __wine_main_argc = argc; __wine_main_argv = argv; + __wine_main_environ = environ; if (!(ntdll = dlopen_dll( "ntdll.dll", error, error_size, 0, &file_exists ))) return; if (!(init_func = wine_dlsym( ntdll, "__wine_process_init", error, error_size ))) return;