2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
16 #include "wine/winbase16.h"
19 #include "selectors.h"
21 /* Win32 process environment database */
24 LPSTR environ; /* 00 Process environment strings */
25 DWORD unknown1; /* 04 Unknown */
26 LPSTR cmd_line; /* 08 Command line */
27 LPSTR cur_dir; /* 0c Current directory */
28 STARTUPINFOA *startup_info; /* 10 Startup information */
29 HANDLE hStdin; /* 14 Handle for standard input */
30 HANDLE hStdout; /* 18 Handle for standard output */
31 HANDLE hStderr; /* 1c Handle for standard error */
32 DWORD unknown2; /* 20 Unknown */
33 DWORD inherit_console; /* 24 Inherit console flag */
34 DWORD break_type; /* 28 Console events flag */
35 void *break_sem; /* 2c SetConsoleCtrlHandler semaphore */
36 void *break_event; /* 30 SetConsoleCtrlHandler event */
37 void *break_thread; /* 34 SetConsoleCtrlHandler thread */
38 void *break_handlers; /* 38 List of console handlers */
42 /* Format of an environment block:
43 * ASCIIZ string 1 (xx=yy format)
48 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
51 * - contrary to Microsoft docs, the environment strings do not appear
52 * to be sorted on Win95 (although they are on NT); so we don't bother
53 * to sort them either.
56 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
58 /* Maximum length of a Win16 environment string (including NULL) */
59 #define MAX_WIN16_LEN 128
61 /* Extra bytes to reserve at the end of an environment */
62 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
64 /* Fill the extra bytes with the program name and stuff */
65 #define FILL_EXTRA_ENV(p) \
67 PUT_UA_WORD( (p) + 1, 1 ); \
68 strcpy( (p) + 3, ENV_program_name );
70 STARTUPINFOA current_startupinfo =
72 sizeof(STARTUPINFOA), /* cb */
80 0, /* dwXCountChars */
81 0, /* dwYCountChars */
82 0, /* dwFillAttribute */
98 ¤t_startupinfo, /* startup_info */
103 0, /* inherit_console */
107 0, /* break_thread */
108 0 /* break_handlers */
112 static WCHAR *cmdlineW; /* Unicode command line */
113 static WORD env_sel; /* selector to the environment */
115 /***********************************************************************
118 * Find a variable in the environment and return a pointer to the value.
119 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
121 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
125 if (!strncasecmp( name, env, len ) && (env[len] == '='))
126 return env + len + 1;
127 env += strlen(env) + 1;
133 /***********************************************************************
134 * ENV_BuildEnvironment
136 * Build the environment for the initial process
138 ENVDB *ENV_BuildEnvironment(void)
140 extern char **environ;
144 /* Compute the total size of the Unix environment */
146 size = EXTRA_ENV_SIZE;
147 for (e = environ; *e; e++) size += strlen(*e) + 1;
149 /* Now allocate the environment */
151 if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
152 current_envdb.environ = p;
153 env_sel = SELECTOR_AllocBlock( p, 0x10000, WINE_LDT_FLAGS_DATA );
155 /* And fill it with the Unix environment */
157 for (e = environ; *e; e++)
163 /* Now add the program name */
166 return ¤t_envdb;
170 /***********************************************************************
171 * ENV_BuildCommandLine
173 * Build the command line of a process from the argv array.
175 * Note that it does NOT necessarily include the file name.
176 * Sometimes we don't even have any command line options at all.
178 * We must quote and escape characters so that the argv array can be rebuilt
179 * from the command line:
180 * - spaces and tabs must be quoted
182 * - quotes must be escaped
184 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
185 * resulting in an odd number of '\' followed by a '"'
188 * - '\'s that are not followed by a '"' can be left as is
192 BOOL ENV_BuildCommandLine( char **argv )
198 for (arg = argv; *arg; arg++)
200 int has_space,bcount;
210 if (*a==' ' || *a=='\t') {
212 } else if (*a=='"') {
213 /* doubling of '\' preceeding a '"',
214 * plus escaping of said '"'
222 len+=(a-*arg)+1 /* for the separating space */;
224 len+=2; /* for the quotes */
227 if (!(current_envdb.cmd_line = HeapAlloc( GetProcessHeap(), 0, len )))
230 p = current_envdb.cmd_line;
231 for (arg = argv; *arg; arg++)
233 int has_space,has_quote;
236 /* Check for quotes and spaces in this argument */
237 has_space=has_quote=0;
240 if (*a==' ' || *a=='\t') {
244 } else if (*a=='"') {
252 /* Now transfer it to the command line */
269 /* Double all the '\\' preceeding this '"', plus one */
270 for (i=0;i<=bcount;i++)
288 if (p > current_envdb.cmd_line)
289 p--; /* remove last space */
292 /* now allocate the Unicode version */
293 len = MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, NULL, 0 );
294 if (!(cmdlineW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
296 MultiByteToWideChar( CP_ACP, 0, current_envdb.cmd_line, -1, cmdlineW, len );
301 /***********************************************************************
302 * GetCommandLineA (KERNEL32.@)
304 * WARNING: there's a Windows incompatibility lurking here !
305 * Win32s always includes the full path of the program file,
306 * whereas Windows NT only returns the full file path plus arguments
307 * in case the program has been started with a full path.
308 * Win9x seems to have inherited NT behaviour.
310 * Note that both Start Menu Execute and Explorer start programs with
311 * fully specified quoted app file paths, which is why probably the only case
312 * where you'll see single file names is in case of direct launch
313 * via CreateProcess or WinExec.
315 * Perhaps we should take care of Win3.1 programs here (Win32s "feature").
317 * References: MS KB article q102762.txt (special Win32s handling)
319 LPSTR WINAPI GetCommandLineA(void)
321 return current_envdb.cmd_line;
324 /***********************************************************************
325 * GetCommandLineW (KERNEL32.@)
327 LPWSTR WINAPI GetCommandLineW(void)
333 /***********************************************************************
334 * GetEnvironmentStrings (KERNEL32.@)
335 * GetEnvironmentStringsA (KERNEL32.@)
337 LPSTR WINAPI GetEnvironmentStringsA(void)
339 return current_envdb.environ;
343 /***********************************************************************
344 * GetEnvironmentStringsW (KERNEL32.@)
346 LPWSTR WINAPI GetEnvironmentStringsW(void)
352 size = HeapSize( GetProcessHeap(), 0, current_envdb.environ );
353 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
355 LPSTR pA = current_envdb.environ;
357 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
364 /***********************************************************************
365 * FreeEnvironmentStringsA (KERNEL32.@)
367 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
369 if (ptr != current_envdb.environ)
371 SetLastError( ERROR_INVALID_PARAMETER );
378 /***********************************************************************
379 * FreeEnvironmentStringsW (KERNEL32.@)
381 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
383 return HeapFree( GetProcessHeap(), 0, ptr );
387 /***********************************************************************
388 * GetEnvironmentVariableA (KERNEL32.@)
390 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
397 SetLastError( ERROR_INVALID_PARAMETER );
401 if ((p = ENV_FindVariable( current_envdb.environ, name, strlen(name) )))
406 /* If not enough room, include the terminating null
407 * in the returned size and return an empty string */
409 if (value) *value = '\0';
411 else if (value) strcpy( value, p );
415 SetLastError( ERROR_ENVVAR_NOT_FOUND );
420 /***********************************************************************
421 * GetEnvironmentVariableW (KERNEL32.@)
423 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
425 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
426 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
427 DWORD res = GetEnvironmentVariableA( name, val, size );
428 HeapFree( GetProcessHeap(), 0, name );
431 if (size > 0 && !MultiByteToWideChar( CP_ACP, 0, val, -1, valW, size ))
433 HeapFree( GetProcessHeap(), 0, val );
439 /***********************************************************************
440 * SetEnvironmentVariableA (KERNEL32.@)
442 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
444 INT old_size, len, res;
445 LPSTR p, env, new_env;
449 env = p = current_envdb.environ;
451 /* Find a place to insert the string */
457 if (!strncasecmp( name, p, len ) && (p[len] == '=')) break;
460 if (!value && !*p) goto done; /* Value to remove doesn't exist */
462 /* Realloc the buffer */
464 len = value ? strlen(name) + strlen(value) + 2 : 0;
465 if (*p) len -= strlen(p) + 1; /* The name already exists */
466 old_size = HeapSize( GetProcessHeap(), 0, env );
469 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
470 memmove( next + len, next, old_size - (next - env) );
472 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
474 if (env_sel) env_sel = SELECTOR_ReallocBlock( env_sel, new_env, old_size + len );
475 p = new_env + (p - env);
476 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
478 /* Set the new string */
486 current_envdb.environ = new_env;
495 /***********************************************************************
496 * SetEnvironmentVariableW (KERNEL32.@)
498 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
500 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
501 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
502 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
503 HeapFree( GetProcessHeap(), 0, nameA );
504 HeapFree( GetProcessHeap(), 0, valueA );
509 /***********************************************************************
510 * ExpandEnvironmentStringsA (KERNEL32.@)
512 * Note: overlapping buffers are not supported; this is how it should be.
514 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
516 DWORD len, total_size = 1; /* 1 for terminating '\0' */
519 if (!count) dst = NULL;
526 if ((p = strchr( src, '%' ))) len = p - src;
527 else len = strlen(src);
531 else /* we are at the start of a variable */
533 if ((p = strchr( src + 1, '%' )))
535 len = p - src - 1; /* Length of the variable name */
536 if ((var = ENV_FindVariable( current_envdb.environ,
539 src += len + 2; /* Skip the variable name */
544 var = src; /* Copy original name instead */
549 else /* unfinished variable name, ignore it */
552 len = strlen(src); /* Copy whole string */
559 if (count < len) len = count;
560 memcpy( dst, var, len );
567 /* Null-terminate the string */
577 /***********************************************************************
578 * ExpandEnvironmentStringsW (KERNEL32.@)
580 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
582 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
583 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
584 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
587 ret = MultiByteToWideChar( CP_ACP, 0, dstA, -1, dst, len );
588 HeapFree( GetProcessHeap(), 0, dstA );
590 HeapFree( GetProcessHeap(), 0, srcA );
595 /***********************************************************************
596 * GetDOSEnvironment (KERNEL.131)
597 * GetDOSEnvironment16 (KERNEL32.@)
599 SEGPTR WINAPI GetDOSEnvironment16(void)
601 return MAKESEGPTR( env_sel, 0 );
605 /***********************************************************************
606 * GetStdHandle (KERNEL32.@)
608 HANDLE WINAPI GetStdHandle( DWORD std_handle )
612 case STD_INPUT_HANDLE: return current_envdb.hStdin;
613 case STD_OUTPUT_HANDLE: return current_envdb.hStdout;
614 case STD_ERROR_HANDLE: return current_envdb.hStderr;
616 SetLastError( ERROR_INVALID_PARAMETER );
617 return INVALID_HANDLE_VALUE;
621 /***********************************************************************
622 * SetStdHandle (KERNEL32.@)
624 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
628 case STD_INPUT_HANDLE: current_envdb.hStdin = handle; return TRUE;
629 case STD_OUTPUT_HANDLE: current_envdb.hStdout = handle; return TRUE;
630 case STD_ERROR_HANDLE: current_envdb.hStderr = handle; return TRUE;
632 SetLastError( ERROR_INVALID_PARAMETER );
637 /***********************************************************************
638 * GetStartupInfoA (KERNEL32.@)
640 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
642 *info = current_startupinfo;
646 /***********************************************************************
647 * GetStartupInfoW (KERNEL32.@)
649 VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info )
651 info->cb = sizeof(STARTUPINFOW);
652 info->dwX = current_startupinfo.dwX;
653 info->dwY = current_startupinfo.dwY;
654 info->dwXSize = current_startupinfo.dwXSize;
655 info->dwXCountChars = current_startupinfo.dwXCountChars;
656 info->dwYCountChars = current_startupinfo.dwYCountChars;
657 info->dwFillAttribute = current_startupinfo.dwFillAttribute;
658 info->dwFlags = current_startupinfo.dwFlags;
659 info->wShowWindow = current_startupinfo.wShowWindow;
660 info->cbReserved2 = current_startupinfo.cbReserved2;
661 info->lpReserved2 = current_startupinfo.lpReserved2;
662 info->hStdInput = current_startupinfo.hStdInput;
663 info->hStdOutput = current_startupinfo.hStdOutput;
664 info->hStdError = current_startupinfo.hStdError;
665 info->lpReserved = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpReserved );
666 info->lpDesktop = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpDesktop );
667 info->lpTitle = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpTitle );