Fixed error recovery during thread creation.
[wine.git] / memory / environ.c
blob66301378414dd2c51f848492d8cd5e269b19c80d
1 /*
2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include "windef.h"
10 #include "winnls.h"
11 #include "winerror.h"
12 #include "ntddk.h"
13 #include "heap.h"
14 #include "selectors.h"
16 /* Win32 process environment database */
17 typedef struct _ENVDB
19 LPSTR environ; /* 00 Process environment strings */
20 DWORD unknown1; /* 04 Unknown */
21 LPSTR cmd_line; /* 08 Command line */
22 LPSTR cur_dir; /* 0c Current directory */
23 STARTUPINFOA *startup_info; /* 10 Startup information */
24 HANDLE hStdin; /* 14 Handle for standard input */
25 HANDLE hStdout; /* 18 Handle for standard output */
26 HANDLE hStderr; /* 1c Handle for standard error */
27 DWORD unknown2; /* 20 Unknown */
28 DWORD inherit_console; /* 24 Inherit console flag */
29 DWORD break_type; /* 28 Console events flag */
30 void *break_sem; /* 2c SetConsoleCtrlHandler semaphore */
31 void *break_event; /* 30 SetConsoleCtrlHandler event */
32 void *break_thread; /* 34 SetConsoleCtrlHandler thread */
33 void *break_handlers; /* 38 List of console handlers */
34 } ENVDB;
37 /* Format of an environment block:
38 * ASCIIZ string 1 (xx=yy format)
39 * ...
40 * ASCIIZ string n
41 * BYTE 0
42 * WORD 1
43 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
45 * Notes:
46 * - contrary to Microsoft docs, the environment strings do not appear
47 * to be sorted on Win95 (although they are on NT); so we don't bother
48 * to sort them either.
51 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
53 /* Maximum length of a Win16 environment string (including NULL) */
54 #define MAX_WIN16_LEN 128
56 /* Extra bytes to reserve at the end of an environment */
57 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
59 /* Fill the extra bytes with the program name and stuff */
60 #define FILL_EXTRA_ENV(p) \
61 *(p) = '\0'; \
62 PUT_WORD( (p) + 1, 1 ); \
63 strcpy( (p) + 3, ENV_program_name );
65 STARTUPINFOA current_startupinfo =
67 sizeof(STARTUPINFOA), /* cb */
68 0, /* lpReserved */
69 0, /* lpDesktop */
70 0, /* lpTitle */
71 0, /* dwX */
72 0, /* dwY */
73 0, /* dwXSize */
74 0, /* dwYSize */
75 0, /* dwXCountChars */
76 0, /* dwYCountChars */
77 0, /* dwFillAttribute */
78 0, /* dwFlags */
79 0, /* wShowWindow */
80 0, /* cbReserved2 */
81 0, /* lpReserved2 */
82 0, /* hStdInput */
83 0, /* hStdOutput */
84 0 /* hStdError */
87 ENVDB current_envdb =
89 0, /* environ */
90 0, /* unknown1 */
91 0, /* cmd_line */
92 0, /* cur_dir */
93 &current_startupinfo, /* startup_info */
94 0, /* hStdin */
95 0, /* hStdout */
96 0, /* hStderr */
97 0, /* unknown2 */
98 0, /* inherit_console */
99 0, /* break_type */
100 0, /* break_sem */
101 0, /* break_event */
102 0, /* break_thread */
103 0 /* break_handlers */
107 static WCHAR *cmdlineW; /* Unicode command line */
108 static WORD env_sel; /* selector to the environment */
110 /***********************************************************************
111 * ENV_FindVariable
113 * Find a variable in the environment and return a pointer to the value.
114 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
116 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
118 while (*env)
120 if (!strncasecmp( name, env, len ) && (env[len] == '='))
121 return env + len + 1;
122 env += strlen(env) + 1;
124 return NULL;
128 /***********************************************************************
129 * ENV_BuildEnvironment
131 * Build the environment for the initial process
133 ENVDB *ENV_BuildEnvironment(void)
135 extern char **environ;
136 LPSTR p, *e;
137 int size;
139 /* Compute the total size of the Unix environment */
141 size = EXTRA_ENV_SIZE;
142 for (e = environ; *e; e++) size += strlen(*e) + 1;
144 /* Now allocate the environment */
146 if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
147 current_envdb.environ = p;
148 env_sel = SELECTOR_AllocBlock( p, 0x10000, WINE_LDT_FLAGS_DATA );
150 /* And fill it with the Unix environment */
152 for (e = environ; *e; e++)
154 strcpy( p, *e );
155 p += strlen(p) + 1;
158 /* Now add the program name */
160 FILL_EXTRA_ENV( p );
161 return &current_envdb;
165 /***********************************************************************
166 * ENV_BuildCommandLine
168 * Build the command line of a process from the argv array.
170 * Note that it does NOT necessarily include the file name.
171 * Sometimes we don't even have any command line options at all.
173 BOOL ENV_BuildCommandLine( char **argv )
175 int len, quote = 0;
176 char *p, **arg;
178 for (arg = argv, len = 0; *arg; arg++) len += strlen(*arg) + 1;
179 if ((argv[0]) && (quote = (strchr( argv[0], ' ' ) != NULL))) len += 2;
180 if (!(p = current_envdb.cmd_line = HeapAlloc( GetProcessHeap(), 0, len ))) return FALSE;
181 arg = argv;
182 if (quote)
184 *p++ = '\"';
185 strcpy( p, *arg );
186 p += strlen(p);
187 *p++ = '\"';
188 *p++ = ' ';
189 arg++;
191 while (*arg)
193 strcpy( p, *arg );
194 p += strlen(p);
195 *p++ = ' ';
196 arg++;
198 if (p > current_envdb.cmd_line) p--; /* remove last space */
199 *p = 0;
200 /* now allocate the Unicode version */
201 len = MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, NULL, 0 );
202 if (!(cmdlineW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
203 return FALSE;
204 MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, cmdlineW, len );
205 return TRUE;
209 /***********************************************************************
210 * GetCommandLineA (KERNEL32.289)
212 LPSTR WINAPI GetCommandLineA(void)
214 return current_envdb.cmd_line;
217 /***********************************************************************
218 * GetCommandLineW (KERNEL32.290)
220 LPWSTR WINAPI GetCommandLineW(void)
222 return cmdlineW;
226 /***********************************************************************
227 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
229 LPSTR WINAPI GetEnvironmentStringsA(void)
231 return current_envdb.environ;
235 /***********************************************************************
236 * GetEnvironmentStringsW (KERNEL32.321)
238 LPWSTR WINAPI GetEnvironmentStringsW(void)
240 INT size;
241 LPWSTR ret;
243 RtlAcquirePebLock();
244 size = HeapSize( GetProcessHeap(), 0, current_envdb.environ );
245 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
247 LPSTR pA = current_envdb.environ;
248 LPWSTR pW = ret;
249 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
251 RtlReleasePebLock();
252 return ret;
256 /***********************************************************************
257 * FreeEnvironmentStringsA (KERNEL32.268)
259 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
261 if (ptr != current_envdb.environ)
263 SetLastError( ERROR_INVALID_PARAMETER );
264 return FALSE;
266 return TRUE;
270 /***********************************************************************
271 * FreeEnvironmentStringsW (KERNEL32.269)
273 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
275 return HeapFree( GetProcessHeap(), 0, ptr );
279 /***********************************************************************
280 * GetEnvironmentVariableA (KERNEL32.322)
282 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
284 LPCSTR p;
285 INT ret = 0;
287 if (!name || !*name)
289 SetLastError( ERROR_INVALID_PARAMETER );
290 return 0;
292 RtlAcquirePebLock();
293 if ((p = ENV_FindVariable( current_envdb.environ, name, strlen(name) )))
295 ret = strlen(p);
296 if (size <= ret)
298 /* If not enough room, include the terminating null
299 * in the returned size and return an empty string */
300 ret++;
301 if (value) *value = '\0';
303 else if (value) strcpy( value, p );
305 RtlReleasePebLock();
306 if (!ret)
307 SetLastError( ERROR_ENVVAR_NOT_FOUND );
308 return ret;
312 /***********************************************************************
313 * GetEnvironmentVariableW (KERNEL32.323)
315 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
317 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
318 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
319 DWORD res = GetEnvironmentVariableA( name, val, size );
320 HeapFree( GetProcessHeap(), 0, name );
321 if (val)
323 if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, val, -1, valW, size ))
324 valW[size-1] = 0;
325 HeapFree( GetProcessHeap(), 0, val );
327 return res;
331 /***********************************************************************
332 * SetEnvironmentVariableA (KERNEL32.641)
334 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
336 INT old_size, len, res;
337 LPSTR p, env, new_env;
338 BOOL ret = FALSE;
340 RtlAcquirePebLock();
341 env = p = current_envdb.environ;
343 /* Find a place to insert the string */
345 res = -1;
346 len = strlen(name);
347 while (*p)
349 if (!strncasecmp( name, p, len ) && (p[len] == '=')) break;
350 p += strlen(p) + 1;
352 if (!value && !*p) goto done; /* Value to remove doesn't exist */
354 /* Realloc the buffer */
356 len = value ? strlen(name) + strlen(value) + 2 : 0;
357 if (*p) len -= strlen(p) + 1; /* The name already exists */
358 old_size = HeapSize( GetProcessHeap(), 0, env );
359 if (len < 0)
361 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
362 memmove( next + len, next, old_size - (next - env) );
364 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
365 goto done;
366 if (env_sel) env_sel = SELECTOR_ReallocBlock( env_sel, new_env, old_size + len );
367 p = new_env + (p - env);
368 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
370 /* Set the new string */
372 if (value)
374 strcpy( p, name );
375 strcat( p, "=" );
376 strcat( p, value );
378 current_envdb.environ = new_env;
379 ret = TRUE;
381 done:
382 RtlReleasePebLock();
383 return ret;
387 /***********************************************************************
388 * SetEnvironmentVariableW (KERNEL32.642)
390 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
392 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
393 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
394 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
395 HeapFree( GetProcessHeap(), 0, nameA );
396 HeapFree( GetProcessHeap(), 0, valueA );
397 return ret;
401 /***********************************************************************
402 * ExpandEnvironmentStringsA (KERNEL32.216)
404 * Note: overlapping buffers are not supported; this is how it should be.
406 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
408 DWORD len, total_size = 1; /* 1 for terminating '\0' */
409 LPCSTR p, var;
411 if (!count) dst = NULL;
412 RtlAcquirePebLock();
414 while (*src)
416 if (*src != '%')
418 if ((p = strchr( src, '%' ))) len = p - src;
419 else len = strlen(src);
420 var = src;
421 src += len;
423 else /* we are at the start of a variable */
425 if ((p = strchr( src + 1, '%' )))
427 len = p - src - 1; /* Length of the variable name */
428 if ((var = ENV_FindVariable( current_envdb.environ,
429 src + 1, len )))
431 src += len + 2; /* Skip the variable name */
432 len = strlen(var);
434 else
436 var = src; /* Copy original name instead */
437 len += 2;
438 src += len;
441 else /* unfinished variable name, ignore it */
443 var = src;
444 len = strlen(src); /* Copy whole string */
445 src += len;
448 total_size += len;
449 if (dst)
451 if (count < len) len = count;
452 memcpy( dst, var, len );
453 dst += len;
454 count -= len;
457 RtlReleasePebLock();
459 /* Null-terminate the string */
460 if (dst)
462 if (!count) dst--;
463 *dst = '\0';
465 return total_size;
469 /***********************************************************************
470 * ExpandEnvironmentStringsW (KERNEL32.217)
472 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
474 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
475 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
476 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
477 if (dstA)
479 ret = MultiByteToWideChar( CP_ACP, 0, dstA, -1, dst, len );
480 HeapFree( GetProcessHeap(), 0, dstA );
482 HeapFree( GetProcessHeap(), 0, srcA );
483 return ret;
487 /***********************************************************************
488 * GetDOSEnvironment16 (KERNEL.131)
490 SEGPTR WINAPI GetDOSEnvironment16(void)
492 return MAKESEGPTR( env_sel, 0 );
496 /***********************************************************************
497 * GetStdHandle (KERNEL32.276)
499 HANDLE WINAPI GetStdHandle( DWORD std_handle )
501 switch(std_handle)
503 case STD_INPUT_HANDLE: return current_envdb.hStdin;
504 case STD_OUTPUT_HANDLE: return current_envdb.hStdout;
505 case STD_ERROR_HANDLE: return current_envdb.hStderr;
507 SetLastError( ERROR_INVALID_PARAMETER );
508 return INVALID_HANDLE_VALUE;
512 /***********************************************************************
513 * SetStdHandle (KERNEL32.506)
515 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
517 switch(std_handle)
519 case STD_INPUT_HANDLE: current_envdb.hStdin = handle; return TRUE;
520 case STD_OUTPUT_HANDLE: current_envdb.hStdout = handle; return TRUE;
521 case STD_ERROR_HANDLE: current_envdb.hStderr = handle; return TRUE;
523 SetLastError( ERROR_INVALID_PARAMETER );
524 return FALSE;
528 /***********************************************************************
529 * GetStartupInfoA (KERNEL32.273)
531 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
533 *info = current_startupinfo;
537 /***********************************************************************
538 * GetStartupInfoW (KERNEL32.274)
540 VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info )
542 info->cb = sizeof(STARTUPINFOW);
543 info->dwX = current_startupinfo.dwX;
544 info->dwY = current_startupinfo.dwY;
545 info->dwXSize = current_startupinfo.dwXSize;
546 info->dwXCountChars = current_startupinfo.dwXCountChars;
547 info->dwYCountChars = current_startupinfo.dwYCountChars;
548 info->dwFillAttribute = current_startupinfo.dwFillAttribute;
549 info->dwFlags = current_startupinfo.dwFlags;
550 info->wShowWindow = current_startupinfo.wShowWindow;
551 info->cbReserved2 = current_startupinfo.cbReserved2;
552 info->lpReserved2 = current_startupinfo.lpReserved2;
553 info->hStdInput = current_startupinfo.hStdInput;
554 info->hStdOutput = current_startupinfo.hStdOutput;
555 info->hStdError = current_startupinfo.hStdError;
556 info->lpReserved = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpReserved );
557 info->lpDesktop = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpDesktop );
558 info->lpTitle = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpTitle );