[7858] | 1 | /* $Id: environ.cpp,v 1.15 2002-02-10 13:12:51 sandervl Exp $ */
|
---|
[2042] | 2 |
|
---|
| 3 | /*
|
---|
| 4 | * Win32 environment file functions for OS/2
|
---|
| 5 | *
|
---|
| 6 | * Copyright 1998 Sander van Leeuwen
|
---|
| 7 | * Copyright 1998 Patrick Haller
|
---|
| 8 | * Copyright 1998 Peter Fitzsimmons
|
---|
| 9 | * Copyright 1998 Knut St. Osmundsen
|
---|
| 10 | *
|
---|
| 11 | * Parts based on Wine code (ExpandEnvironmentStringsA/W)
|
---|
| 12 | * (memory\environ.c; 991114)
|
---|
| 13 | *
|
---|
| 14 | * Copyright 1996, 1998 Alexandre Julliard
|
---|
| 15 | *
|
---|
| 16 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
| 17 | *
|
---|
| 18 | */
|
---|
| 19 | #include <odin.h>
|
---|
| 20 | #include <odinwrap.h>
|
---|
| 21 | #include <os2sel.h>
|
---|
| 22 |
|
---|
| 23 | #include <os2win.h>
|
---|
| 24 | #include <winnt.h>
|
---|
| 25 | #include <winnls.h>
|
---|
| 26 | #include <stdlib.h>
|
---|
[21302] | 27 | #include <stdio.h>
|
---|
[2042] | 28 | #include <string.h>
|
---|
| 29 | #include <heapstring.h>
|
---|
| 30 |
|
---|
| 31 | #include <misc.h>
|
---|
[5481] | 32 | #include <unicode.h>
|
---|
[2042] | 33 |
|
---|
[2802] | 34 | #define DBG_LOCALLOG DBG_environ
|
---|
| 35 | #include "dbglocal.h"
|
---|
| 36 |
|
---|
[4380] | 37 |
|
---|
| 38 | ODINDEBUGCHANNEL(KERNEL32-ENVIRONMENT)
|
---|
| 39 |
|
---|
| 40 |
|
---|
[21302] | 41 | //list of important OS/2 environment variables that must not be removed
|
---|
| 42 | //when creating a new process
|
---|
[21916] | 43 | static const char *lpReservedEnvStrings[] = {
|
---|
[21302] | 44 | "HOSTNAME",
|
---|
| 45 | "TZ",
|
---|
| 46 | "USE_HOSTS_FIRST",
|
---|
| 47 | "MMBASE",
|
---|
| 48 | "USER_INI",
|
---|
| 49 | "SYSTEM_INI",
|
---|
| 50 | "DPATH",
|
---|
| 51 | "LANG",
|
---|
| 52 | "NCDEBUG",
|
---|
| 53 | "NLSPATH",
|
---|
| 54 | "TCPLANG",
|
---|
| 55 | "DLSINI",
|
---|
| 56 | "INIT_FILE_NAMES",
|
---|
| 57 | "INIT_FILE_RANGES",
|
---|
| 58 | "NWDBPATH",
|
---|
| 59 | "ETC",
|
---|
| 60 | "WP_OBJHANDLE",
|
---|
| 61 | "SOMIR",
|
---|
| 62 | "SOMDDIR",
|
---|
| 63 | "TMP",
|
---|
| 64 | "TEMP",
|
---|
| 65 | };
|
---|
| 66 |
|
---|
[2042] | 67 | //******************************************************************************
|
---|
| 68 | //******************************************************************************
|
---|
[21302] | 69 | void InitEnvironment()
|
---|
| 70 | {
|
---|
| 71 | CHAR szVar[512];
|
---|
| 72 | static BOOL fInit = FALSE;
|
---|
| 73 |
|
---|
| 74 | if(fInit) return;
|
---|
| 75 |
|
---|
| 76 | //TEMP is a standard environment variable in Windows, but is not always
|
---|
| 77 | //present in OS/2, so make sure it is.
|
---|
[21916] | 78 | if(GetEnvironmentVariableA("TEMP", szVar, sizeof(szVar)) == 0)
|
---|
[21302] | 79 | {
|
---|
| 80 | if(GetEnvironmentVariableA("TMP", szVar, sizeof(szVar)) == 0) {
|
---|
| 81 | //then we just use the windows directory for garbage
|
---|
| 82 | GetWindowsDirectoryA(szVar, sizeof(szVar));
|
---|
| 83 | }
|
---|
| 84 | SetEnvironmentVariableA("TEMP", szVar);
|
---|
| 85 | }
|
---|
| 86 | }
|
---|
[21916] | 87 |
|
---|
| 88 | extern "C" {
|
---|
| 89 |
|
---|
[21302] | 90 | //******************************************************************************
|
---|
| 91 | //******************************************************************************
|
---|
[7858] | 92 | LPSTR WIN32API GetEnvironmentStringsA()
|
---|
[2042] | 93 | {
|
---|
[21302] | 94 | InitEnvironment();
|
---|
[4380] | 95 | return (LPSTR) O32_GetEnvironmentStrings();
|
---|
[2042] | 96 | }
|
---|
| 97 | //******************************************************************************
|
---|
| 98 | //******************************************************************************
|
---|
[7849] | 99 | LPWSTR WIN32API GetEnvironmentStringsW()
|
---|
[2042] | 100 | {
|
---|
[4380] | 101 | char *envstrings = (char *)O32_GetEnvironmentStrings();
|
---|
| 102 | char *tmp;
|
---|
| 103 | LPWSTR wenvstrings;
|
---|
| 104 | int len, i;
|
---|
[2042] | 105 |
|
---|
[21302] | 106 | InitEnvironment();
|
---|
| 107 |
|
---|
[2042] | 108 | if(envstrings == NULL)
|
---|
| 109 | return(NULL);
|
---|
| 110 |
|
---|
| 111 | tmp = envstrings;
|
---|
| 112 | len = 0;
|
---|
| 113 | while(*tmp != 0)
|
---|
| 114 | {
|
---|
| 115 | len += strlen(tmp)+1;
|
---|
| 116 | tmp = envstrings + len;
|
---|
| 117 | }
|
---|
| 118 | len++; //terminating 0
|
---|
[21302] | 119 | wenvstrings = (LPWSTR)HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
|
---|
| 120 | for(i=0;i<len;i++)
|
---|
[2042] | 121 | {
|
---|
| 122 | wenvstrings[i] = envstrings[i];
|
---|
| 123 | }
|
---|
| 124 | return(wenvstrings);
|
---|
| 125 | }
|
---|
| 126 | //******************************************************************************
|
---|
| 127 | //******************************************************************************
|
---|
[7849] | 128 | BOOL WIN32API FreeEnvironmentStringsA(LPSTR envstrings)
|
---|
[2042] | 129 | {
|
---|
| 130 | return(TRUE);
|
---|
| 131 | }
|
---|
| 132 | //******************************************************************************
|
---|
| 133 | //******************************************************************************
|
---|
[7849] | 134 | BOOL WIN32API FreeEnvironmentStringsW(LPWSTR envstrings)
|
---|
[2042] | 135 | {
|
---|
[21302] | 136 | HeapFree(GetProcessHeap(), 0, envstrings);
|
---|
[2042] | 137 | return(TRUE);
|
---|
| 138 | }
|
---|
| 139 | //******************************************************************************
|
---|
| 140 | //******************************************************************************
|
---|
[7849] | 141 | BOOL WIN32API SetEnvironmentVariableA(LPCSTR lpName, LPCSTR lpValue)
|
---|
[2042] | 142 | {
|
---|
[4380] | 143 | dprintf(("KERNEL32: SetEnvironmentVariable %s to %s\n", lpName, lpValue));
|
---|
| 144 | return O32_SetEnvironmentVariable(lpName, lpValue);
|
---|
[2042] | 145 | }
|
---|
| 146 | //******************************************************************************
|
---|
| 147 | //******************************************************************************
|
---|
[7849] | 148 | BOOL WIN32API SetEnvironmentVariableW(LPCWSTR lpName, LPCWSTR lpValue)
|
---|
[2042] | 149 | {
|
---|
[4380] | 150 | char *asciiname, *asciivalue;
|
---|
| 151 | BOOL rc;
|
---|
[2042] | 152 |
|
---|
[4380] | 153 | asciiname = UnicodeToAsciiString((LPWSTR)lpName);
|
---|
| 154 | asciivalue = UnicodeToAsciiString((LPWSTR)lpValue);
|
---|
| 155 | dprintf(("KERNEL32: SetEnvironmentVariable %s to %s\n", asciiname, asciivalue));
|
---|
| 156 | rc = O32_SetEnvironmentVariable(asciiname, asciivalue);
|
---|
| 157 | FreeAsciiString(asciivalue);
|
---|
| 158 | FreeAsciiString(asciiname);
|
---|
| 159 | return(rc);
|
---|
[2042] | 160 | }
|
---|
| 161 | //******************************************************************************
|
---|
| 162 | //******************************************************************************
|
---|
[7849] | 163 | DWORD WIN32API GetEnvironmentVariableA(LPCSTR lpName, LPSTR lpBuffer,
|
---|
| 164 | DWORD nSize)
|
---|
[2042] | 165 | {
|
---|
[4451] | 166 | dprintf(("GetEnvironmentVariableA %s", lpName));
|
---|
[4380] | 167 | return O32_GetEnvironmentVariable(lpName, lpBuffer, nSize);
|
---|
[2042] | 168 | }
|
---|
| 169 | //******************************************************************************
|
---|
| 170 | //******************************************************************************
|
---|
[7849] | 171 | DWORD WIN32API GetEnvironmentVariableW(LPCWSTR lpName, LPWSTR lpBuffer,
|
---|
| 172 | DWORD nSize)
|
---|
[2042] | 173 | {
|
---|
| 174 | char *astring, *asciibuffer;
|
---|
| 175 | DWORD rc;
|
---|
| 176 |
|
---|
[5596] | 177 | if (!lpName || !*lpName)
|
---|
| 178 | {
|
---|
| 179 | dprintf(("GetEnvironmentVariableW: invalid name!"));
|
---|
| 180 | SetLastError(ERROR_INVALID_PARAMETER);
|
---|
| 181 | return 0;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[4451] | 184 | if(nSize) {
|
---|
| 185 | asciibuffer = (char *)malloc(nSize+1);
|
---|
| 186 | *asciibuffer = 0;
|
---|
| 187 | }
|
---|
| 188 | else asciibuffer = NULL;
|
---|
| 189 |
|
---|
[4380] | 190 | astring = UnicodeToAsciiString((LPWSTR)lpName);
|
---|
[2042] | 191 |
|
---|
[7849] | 192 | rc = GetEnvironmentVariableA(astring, asciibuffer, nSize);
|
---|
[5596] | 193 | if(asciibuffer)
|
---|
| 194 | AsciiToUnicode(asciibuffer, lpBuffer);
|
---|
[4380] | 195 | FreeAsciiString(astring);
|
---|
[4451] | 196 | if(asciibuffer)
|
---|
| 197 | free(asciibuffer);
|
---|
[4380] | 198 | return(rc);
|
---|
[2042] | 199 | }
|
---|
| 200 | /***********************************************************************
|
---|
| 201 | * ENV_FindVariable
|
---|
| 202 | *
|
---|
| 203 | * Find a variable in the environment and return a pointer to the value.
|
---|
| 204 | * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
|
---|
| 205 | */
|
---|
| 206 | static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
|
---|
| 207 | {
|
---|
| 208 | while (*env)
|
---|
| 209 | {
|
---|
| 210 | if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
|
---|
| 211 | return env + len + 1;
|
---|
| 212 | env += strlen(env) + 1;
|
---|
| 213 | }
|
---|
| 214 | return NULL;
|
---|
| 215 | }
|
---|
| 216 | /*****************************************************************************
|
---|
| 217 | * Name : DWORD WIN32API ExpandEnvironmentStringsA
|
---|
| 218 | * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
|
---|
| 219 | * strings and replaces them with their defined values.
|
---|
| 220 | * Parameters: LPCSTR lpSrc pointer to string with environment variables
|
---|
| 221 | * LPSTR lpDst pointer to string with expanded environment variables
|
---|
| 222 | * DWORD nSize maximum characters in expanded string
|
---|
| 223 | * Variables :
|
---|
| 224 | * Result : If the function succeeds, the return value is the number of
|
---|
| 225 | * characters stored in the destination buffer. If the number of
|
---|
| 226 | * characters is greater than the size of the destination buffer,
|
---|
| 227 | * the return value is the size of the buffer required to hold
|
---|
| 228 | * the expanded strings.
|
---|
| 229 | * If the function fails, the return value is zero
|
---|
| 230 | * Remark :
|
---|
[21916] | 231 | * Status :
|
---|
[2042] | 232 | *
|
---|
| 233 | *****************************************************************************/
|
---|
| 234 |
|
---|
[7849] | 235 | DWORD WIN32API ExpandEnvironmentStringsA(LPCSTR src, LPSTR dst, DWORD count)
|
---|
[2042] | 236 | {
|
---|
[4380] | 237 | DWORD len, total_size = 1; /* 1 for terminating '\0' */
|
---|
| 238 | LPCSTR p, var;
|
---|
[2042] | 239 |
|
---|
[4380] | 240 | dprintf(("KERNEL32:ExpandEnvironmentStringsA '%s', %08x, %08x",
|
---|
| 241 | src, dst, count
|
---|
| 242 | ));
|
---|
[2042] | 243 |
|
---|
[4224] | 244 | if (!count) dst = NULL;
|
---|
| 245 |
|
---|
| 246 | while (*src)
|
---|
[2042] | 247 | {
|
---|
[4224] | 248 | if (*src != '%')
|
---|
[2042] | 249 | {
|
---|
[21355] | 250 | if ((p = strchr( src, '%' )) != NULL) len = p - src;
|
---|
[4224] | 251 | else len = strlen(src);
|
---|
| 252 | var = src;
|
---|
| 253 | src += len;
|
---|
[2042] | 254 | }
|
---|
| 255 | else /* we are at the start of a variable */
|
---|
| 256 | {
|
---|
[21355] | 257 | if ((p = strchr( src + 1, '%' )) != NULL)
|
---|
[2042] | 258 | {
|
---|
[21916] | 259 | len = p - src - 1; /* Length of the variable name */
|
---|
[2042] | 260 | if ((var = ENV_FindVariable( GetEnvironmentStringsA(),
|
---|
[21355] | 261 | src + 1, len )) != NULL)
|
---|
[2042] | 262 | {
|
---|
[4224] | 263 | src += len + 2; /* Skip the variable name */
|
---|
[2042] | 264 | len = strlen(var);
|
---|
| 265 | }
|
---|
| 266 | else
|
---|
| 267 | {
|
---|
[4224] | 268 | var = src; /* Copy original name instead */
|
---|
[2042] | 269 | len += 2;
|
---|
[4224] | 270 | src += len;
|
---|
[2042] | 271 | }
|
---|
| 272 | }
|
---|
| 273 | else /* unfinished variable name, ignore it */
|
---|
| 274 | {
|
---|
[4224] | 275 | var = src;
|
---|
| 276 | len = strlen(src); /* Copy whole string */
|
---|
| 277 | src += len;
|
---|
[2042] | 278 | }
|
---|
| 279 | }
|
---|
| 280 | total_size += len;
|
---|
[4224] | 281 | if (dst)
|
---|
[2042] | 282 | {
|
---|
[4224] | 283 | if (count < len) len = count;
|
---|
| 284 | memcpy( dst, var, len );
|
---|
| 285 | dst += len;
|
---|
| 286 | count -= len;
|
---|
[2042] | 287 | }
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /* Null-terminate the string */
|
---|
[4224] | 291 | if (dst)
|
---|
[2042] | 292 | {
|
---|
[4224] | 293 | if (!count) dst--;
|
---|
| 294 | *dst = '\0';
|
---|
[2042] | 295 | }
|
---|
| 296 | return total_size;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /*****************************************************************************
|
---|
| 300 | * Name : DWORD WIN32API ExpandEnvironmentStringsW
|
---|
| 301 | * Purpose : The ExpandEnvironmentStringsA function expands environment-variable
|
---|
| 302 | * strings and replaces them with their defined values.
|
---|
| 303 | * Parameters: LPCWSTR lpSrc pointer to string with environment variables
|
---|
| 304 | * LPWSTR lpDst pointer to string with expanded environment variables
|
---|
| 305 | * DWORD nSize maximum characters in expanded string
|
---|
| 306 | * Variables :
|
---|
| 307 | * Result : If the function succeeds, the return value is the number of
|
---|
| 308 | * characters stored in the destination buffer. If the number of
|
---|
| 309 | * characters is greater than the size of the destination buffer,
|
---|
| 310 | * the return value is the size of the buffer required to hold
|
---|
| 311 | * the expanded strings.
|
---|
| 312 | * If the function fails, the return value is zero
|
---|
| 313 | * Remark :
|
---|
[21916] | 314 | * Status :
|
---|
[2042] | 315 | *
|
---|
| 316 | *****************************************************************************/
|
---|
| 317 |
|
---|
[7849] | 318 | DWORD WIN32API ExpandEnvironmentStringsW(LPCWSTR lpSrc, LPWSTR lpDst,
|
---|
| 319 | DWORD nSize)
|
---|
[2042] | 320 | {
|
---|
[4380] | 321 | LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, lpSrc );
|
---|
| 322 | LPSTR dstA = lpDst ? (LPSTR)HeapAlloc( GetProcessHeap(), 0, nSize ) : NULL;
|
---|
[2042] | 323 |
|
---|
[4380] | 324 | dprintf(("KERNEL32:ExpandEnvironmentStringsW(%08x,%08x,%08x)", lpSrc, lpDst, nSize));
|
---|
[4224] | 325 |
|
---|
[4380] | 326 | DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, nSize );
|
---|
| 327 | if (dstA)
|
---|
[2042] | 328 | {
|
---|
[4380] | 329 | lstrcpyAtoW( lpDst, dstA );
|
---|
| 330 | HeapFree( GetProcessHeap(), 0, dstA );
|
---|
[2042] | 331 | }
|
---|
[4380] | 332 | HeapFree( GetProcessHeap(), 0, srcA );
|
---|
| 333 | return ret;
|
---|
[2042] | 334 | }
|
---|
[21916] | 335 |
|
---|
| 336 | } // extern "C"
|
---|
| 337 |
|
---|
[2042] | 338 | //******************************************************************************
|
---|
[21302] | 339 | // Create a new process environment block based on input from the application
|
---|
| 340 | // Make sure important OS/2 variables are added or else some services might
|
---|
| 341 | // fail in the child process. (gethostname relies on SET HOSTNAME)
|
---|
[2042] | 342 | //******************************************************************************
|
---|
[21302] | 343 | char *CreateNewEnvironment(char *lpEnvironment)
|
---|
| 344 | {
|
---|
| 345 | char *tmpenvold = lpEnvironment;
|
---|
| 346 | char *tmpenvnew, *newenv;
|
---|
| 347 | int newsize = 0, len;
|
---|
[21916] | 348 |
|
---|
[21302] | 349 | dprintf(("New environment:"));
|
---|
| 350 | while(*tmpenvold) {
|
---|
| 351 | dprintf(("%s", tmpenvold));
|
---|
| 352 | len = strlen(tmpenvold);
|
---|
| 353 | newsize += len+1;
|
---|
| 354 | tmpenvold += len+1;
|
---|
| 355 | }
|
---|
| 356 | newsize++; //extra null terminator
|
---|
| 357 |
|
---|
| 358 | for(int i=0;i<sizeof(lpReservedEnvStrings)/sizeof(char *);i++) {
|
---|
| 359 | if(!ENV_FindVariable(lpEnvironment, lpReservedEnvStrings[i], strlen(lpReservedEnvStrings[i]))) {
|
---|
| 360 | len = GetEnvironmentVariableA(lpReservedEnvStrings[i], NULL, 0);
|
---|
| 361 | if(len) {
|
---|
| 362 | newsize += strlen(lpReservedEnvStrings[i]) + 1 + len+1; //var = value \0
|
---|
| 363 | }
|
---|
| 364 | }
|
---|
| 365 | }
|
---|
| 366 | newsize++; //extra 0 terminator
|
---|
| 367 |
|
---|
| 368 | newenv = (char *)malloc(newsize);
|
---|
| 369 | if(newenv == NULL) {
|
---|
| 370 | DebugInt3();
|
---|
| 371 | return NULL;
|
---|
| 372 | }
|
---|
| 373 | memset(newenv, 0, newsize);
|
---|
| 374 | tmpenvold = (char *)lpEnvironment;
|
---|
| 375 | tmpenvnew = newenv;
|
---|
| 376 | while(*tmpenvold) {
|
---|
| 377 | strcat(tmpenvnew, tmpenvold);
|
---|
| 378 | len = strlen(tmpenvnew);
|
---|
| 379 | tmpenvnew += len+1;
|
---|
| 380 | tmpenvold += len+1;
|
---|
| 381 | }
|
---|
[21916] | 382 | int i;
|
---|
[21302] | 383 | for(i=0;i<sizeof(lpReservedEnvStrings)/sizeof(char *);i++) {
|
---|
| 384 | if(!ENV_FindVariable(lpEnvironment, lpReservedEnvStrings[i], strlen(lpReservedEnvStrings[i]))) {
|
---|
| 385 | len = GetEnvironmentVariableA(lpReservedEnvStrings[i], NULL, 0);
|
---|
| 386 | if(len) {
|
---|
| 387 | char *tmp = (char *)malloc(len+1);
|
---|
| 388 | len = GetEnvironmentVariableA(lpReservedEnvStrings[i], tmp, len+1);
|
---|
| 389 | if(len) {
|
---|
| 390 | sprintf(tmpenvnew, "%s=%s", lpReservedEnvStrings[i], tmp);
|
---|
| 391 | tmpenvnew += strlen(tmpenvnew) + 1;
|
---|
| 392 | }
|
---|
| 393 | free(tmp);
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 | }
|
---|
| 397 | *tmpenvnew = 0; //final null terminator
|
---|
| 398 |
|
---|
| 399 | #ifdef DEBUG
|
---|
| 400 | tmpenvnew = newenv;
|
---|
| 401 | dprintf(("Combined new environment:"));
|
---|
| 402 | while(*tmpenvnew) {
|
---|
| 403 | dprintf(("%s", tmpenvnew));
|
---|
| 404 | len = strlen(tmpenvnew);
|
---|
| 405 | tmpenvnew += len+1;
|
---|
| 406 | }
|
---|
| 407 | #endif
|
---|
| 408 | return newenv;
|
---|
| 409 | }
|
---|
| 410 | //******************************************************************************
|
---|
| 411 | //******************************************************************************
|
---|