2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
15 #include "selectors.h"
17 /* Win32 process environment database */
20 LPSTR environ
; /* 00 Process environment strings */
21 DWORD unknown1
; /* 04 Unknown */
22 LPSTR cmd_line
; /* 08 Command line */
23 LPSTR cur_dir
; /* 0c Current directory */
24 STARTUPINFOA
*startup_info
; /* 10 Startup information */
25 HANDLE hStdin
; /* 14 Handle for standard input */
26 HANDLE hStdout
; /* 18 Handle for standard output */
27 HANDLE hStderr
; /* 1c Handle for standard error */
28 DWORD unknown2
; /* 20 Unknown */
29 DWORD inherit_console
; /* 24 Inherit console flag */
30 DWORD break_type
; /* 28 Console events flag */
31 void *break_sem
; /* 2c SetConsoleCtrlHandler semaphore */
32 void *break_event
; /* 30 SetConsoleCtrlHandler event */
33 void *break_thread
; /* 34 SetConsoleCtrlHandler thread */
34 void *break_handlers
; /* 38 List of console handlers */
38 /* Format of an environment block:
39 * ASCIIZ string 1 (xx=yy format)
44 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
47 * - contrary to Microsoft docs, the environment strings do not appear
48 * to be sorted on Win95 (although they are on NT); so we don't bother
49 * to sort them either.
52 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
54 /* Maximum length of a Win16 environment string (including NULL) */
55 #define MAX_WIN16_LEN 128
57 /* Extra bytes to reserve at the end of an environment */
58 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
60 /* Fill the extra bytes with the program name and stuff */
61 #define FILL_EXTRA_ENV(p) \
63 PUT_UA_WORD( (p) + 1, 1 ); \
64 strcpy( (p) + 3, ENV_program_name );
66 STARTUPINFOA current_startupinfo
=
68 sizeof(STARTUPINFOA
), /* cb */
76 0, /* dwXCountChars */
77 0, /* dwYCountChars */
78 0, /* dwFillAttribute */
94 ¤t_startupinfo
, /* startup_info */
99 0, /* inherit_console */
103 0, /* break_thread */
104 0 /* break_handlers */
108 static WCHAR
*cmdlineW
; /* Unicode command line */
109 static WORD env_sel
; /* selector to the environment */
111 /***********************************************************************
114 * Find a variable in the environment and return a pointer to the value.
115 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
117 static LPCSTR
ENV_FindVariable( LPCSTR env
, LPCSTR name
, INT len
)
121 if (!strncasecmp( name
, env
, len
) && (env
[len
] == '='))
122 return env
+ len
+ 1;
123 env
+= strlen(env
) + 1;
129 /***********************************************************************
130 * ENV_BuildEnvironment
132 * Build the environment for the initial process
134 ENVDB
*ENV_BuildEnvironment(void)
136 extern char **environ
;
140 /* Compute the total size of the Unix environment */
142 size
= EXTRA_ENV_SIZE
;
143 for (e
= environ
; *e
; e
++) size
+= strlen(*e
) + 1;
145 /* Now allocate the environment */
147 if (!(p
= HeapAlloc( GetProcessHeap(), 0, size
))) return NULL
;
148 current_envdb
.environ
= p
;
149 env_sel
= SELECTOR_AllocBlock( p
, 0x10000, WINE_LDT_FLAGS_DATA
);
151 /* And fill it with the Unix environment */
153 for (e
= environ
; *e
; e
++)
159 /* Now add the program name */
162 return ¤t_envdb
;
166 /***********************************************************************
167 * ENV_BuildCommandLine
169 * Build the command line of a process from the argv array.
171 * Note that it does NOT necessarily include the file name.
172 * Sometimes we don't even have any command line options at all.
174 BOOL
ENV_BuildCommandLine( char **argv
)
179 for (arg
= argv
, len
= 0; *arg
; arg
++) len
+= strlen(*arg
) + 1;
180 if ((argv
[0]) && (quote
= (strchr( argv
[0], ' ' ) != NULL
))) len
+= 2;
181 if (!(p
= current_envdb
.cmd_line
= HeapAlloc( GetProcessHeap(), 0, len
))) return FALSE
;
199 if (p
> current_envdb
.cmd_line
) p
--; /* remove last space */
201 /* now allocate the Unicode version */
202 len
= MultiByteToWideChar( CP_ACP
, 0, current_envdb
.cmd_line
, -1, NULL
, 0 );
203 if (!(cmdlineW
= HeapAlloc( GetProcessHeap(), 0, len
* sizeof(WCHAR
) )))
205 MultiByteToWideChar( CP_ACP
, 0, current_envdb
.cmd_line
, -1, cmdlineW
, len
);
210 /***********************************************************************
211 * GetCommandLineA (KERNEL32.289)
213 LPSTR WINAPI
GetCommandLineA(void)
215 return current_envdb
.cmd_line
;
218 /***********************************************************************
219 * GetCommandLineW (KERNEL32.290)
221 LPWSTR WINAPI
GetCommandLineW(void)
227 /***********************************************************************
228 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
230 LPSTR WINAPI
GetEnvironmentStringsA(void)
232 return current_envdb
.environ
;
236 /***********************************************************************
237 * GetEnvironmentStringsW (KERNEL32.321)
239 LPWSTR WINAPI
GetEnvironmentStringsW(void)
245 size
= HeapSize( GetProcessHeap(), 0, current_envdb
.environ
);
246 if ((ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )) != NULL
)
248 LPSTR pA
= current_envdb
.environ
;
250 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
257 /***********************************************************************
258 * FreeEnvironmentStringsA (KERNEL32.268)
260 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
262 if (ptr
!= current_envdb
.environ
)
264 SetLastError( ERROR_INVALID_PARAMETER
);
271 /***********************************************************************
272 * FreeEnvironmentStringsW (KERNEL32.269)
274 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
276 return HeapFree( GetProcessHeap(), 0, ptr
);
280 /***********************************************************************
281 * GetEnvironmentVariableA (KERNEL32.322)
283 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
290 SetLastError( ERROR_INVALID_PARAMETER
);
294 if ((p
= ENV_FindVariable( current_envdb
.environ
, name
, strlen(name
) )))
299 /* If not enough room, include the terminating null
300 * in the returned size and return an empty string */
302 if (value
) *value
= '\0';
304 else if (value
) strcpy( value
, p
);
308 SetLastError( ERROR_ENVVAR_NOT_FOUND
);
313 /***********************************************************************
314 * GetEnvironmentVariableW (KERNEL32.323)
316 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
318 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
319 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
320 DWORD res
= GetEnvironmentVariableA( name
, val
, size
);
321 HeapFree( GetProcessHeap(), 0, name
);
324 if (size
> 0 && !MultiByteToWideChar( CP_ACP
, 0, val
, -1, valW
, size
))
326 HeapFree( GetProcessHeap(), 0, val
);
332 /***********************************************************************
333 * SetEnvironmentVariableA (KERNEL32.641)
335 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
337 INT old_size
, len
, res
;
338 LPSTR p
, env
, new_env
;
342 env
= p
= current_envdb
.environ
;
344 /* Find a place to insert the string */
350 if (!strncasecmp( name
, p
, len
) && (p
[len
] == '=')) break;
353 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
355 /* Realloc the buffer */
357 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
358 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
359 old_size
= HeapSize( GetProcessHeap(), 0, env
);
362 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
363 memmove( next
+ len
, next
, old_size
- (next
- env
) );
365 if (!(new_env
= HeapReAlloc( GetProcessHeap(), 0, env
, old_size
+ len
)))
367 if (env_sel
) env_sel
= SELECTOR_ReallocBlock( env_sel
, new_env
, old_size
+ len
);
368 p
= new_env
+ (p
- env
);
369 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
371 /* Set the new string */
379 current_envdb
.environ
= new_env
;
388 /***********************************************************************
389 * SetEnvironmentVariableW (KERNEL32.642)
391 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
393 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
394 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
395 BOOL ret
= SetEnvironmentVariableA( nameA
, valueA
);
396 HeapFree( GetProcessHeap(), 0, nameA
);
397 HeapFree( GetProcessHeap(), 0, valueA
);
402 /***********************************************************************
403 * ExpandEnvironmentStringsA (KERNEL32.216)
405 * Note: overlapping buffers are not supported; this is how it should be.
407 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
409 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
412 if (!count
) dst
= NULL
;
419 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
420 else len
= strlen(src
);
424 else /* we are at the start of a variable */
426 if ((p
= strchr( src
+ 1, '%' )))
428 len
= p
- src
- 1; /* Length of the variable name */
429 if ((var
= ENV_FindVariable( current_envdb
.environ
,
432 src
+= len
+ 2; /* Skip the variable name */
437 var
= src
; /* Copy original name instead */
442 else /* unfinished variable name, ignore it */
445 len
= strlen(src
); /* Copy whole string */
452 if (count
< len
) len
= count
;
453 memcpy( dst
, var
, len
);
460 /* Null-terminate the string */
470 /***********************************************************************
471 * ExpandEnvironmentStringsW (KERNEL32.217)
473 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
475 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
476 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
477 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
480 ret
= MultiByteToWideChar( CP_ACP
, 0, dstA
, -1, dst
, len
);
481 HeapFree( GetProcessHeap(), 0, dstA
);
483 HeapFree( GetProcessHeap(), 0, srcA
);
488 /***********************************************************************
489 * GetDOSEnvironment16 (KERNEL.131)
491 SEGPTR WINAPI
GetDOSEnvironment16(void)
493 return MAKESEGPTR( env_sel
, 0 );
497 /***********************************************************************
498 * GetStdHandle (KERNEL32.276)
500 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
504 case STD_INPUT_HANDLE
: return current_envdb
.hStdin
;
505 case STD_OUTPUT_HANDLE
: return current_envdb
.hStdout
;
506 case STD_ERROR_HANDLE
: return current_envdb
.hStderr
;
508 SetLastError( ERROR_INVALID_PARAMETER
);
509 return INVALID_HANDLE_VALUE
;
513 /***********************************************************************
514 * SetStdHandle (KERNEL32.506)
516 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
520 case STD_INPUT_HANDLE
: current_envdb
.hStdin
= handle
; return TRUE
;
521 case STD_OUTPUT_HANDLE
: current_envdb
.hStdout
= handle
; return TRUE
;
522 case STD_ERROR_HANDLE
: current_envdb
.hStderr
= handle
; return TRUE
;
524 SetLastError( ERROR_INVALID_PARAMETER
);
529 /***********************************************************************
530 * GetStartupInfoA (KERNEL32.273)
532 VOID WINAPI
GetStartupInfoA( LPSTARTUPINFOA info
)
534 *info
= current_startupinfo
;
538 /***********************************************************************
539 * GetStartupInfoW (KERNEL32.274)
541 VOID WINAPI
GetStartupInfoW( LPSTARTUPINFOW info
)
543 info
->cb
= sizeof(STARTUPINFOW
);
544 info
->dwX
= current_startupinfo
.dwX
;
545 info
->dwY
= current_startupinfo
.dwY
;
546 info
->dwXSize
= current_startupinfo
.dwXSize
;
547 info
->dwXCountChars
= current_startupinfo
.dwXCountChars
;
548 info
->dwYCountChars
= current_startupinfo
.dwYCountChars
;
549 info
->dwFillAttribute
= current_startupinfo
.dwFillAttribute
;
550 info
->dwFlags
= current_startupinfo
.dwFlags
;
551 info
->wShowWindow
= current_startupinfo
.wShowWindow
;
552 info
->cbReserved2
= current_startupinfo
.cbReserved2
;
553 info
->lpReserved2
= current_startupinfo
.lpReserved2
;
554 info
->hStdInput
= current_startupinfo
.hStdInput
;
555 info
->hStdOutput
= current_startupinfo
.hStdOutput
;
556 info
->hStdError
= current_startupinfo
.hStdError
;
557 info
->lpReserved
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpReserved
);
558 info
->lpDesktop
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpDesktop
);
559 info
->lpTitle
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpTitle
);