2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
14 #include "selectors.h"
16 /* Format of an environment block:
17 * ASCIIZ string 1 (xx=yy format)
22 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
25 * - contrary to Microsoft docs, the environment strings do not appear
26 * to be sorted on Win95 (although they are on NT); so we don't bother
27 * to sort them either.
30 static const char ENV_program_name
[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
32 /* Maximum length of a Win16 environment string (including NULL) */
33 #define MAX_WIN16_LEN 128
35 /* Extra bytes to reserve at the end of an environment */
36 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
38 /* Fill the extra bytes with the program name and stuff */
39 #define FILL_EXTRA_ENV(p) \
41 PUT_WORD( (p) + 1, 1 ); \
42 strcpy( (p) + 3, ENV_program_name );
44 STARTUPINFOA current_startupinfo
=
46 sizeof(STARTUPINFOA
), /* cb */
54 0, /* dwXCountChars */
55 0, /* dwYCountChars */
56 0, /* dwFillAttribute */
72 ¤t_startupinfo
, /* startup_info */
77 0, /* inherit_console */
82 0, /* break_handlers */
83 CRITICAL_SECTION_INIT
, /* section */
88 /***********************************************************************
91 * Find a variable in the environment and return a pointer to the value.
92 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
94 static LPCSTR
ENV_FindVariable( LPCSTR env
, LPCSTR name
, INT len
)
98 if (!strncasecmp( name
, env
, len
) && (env
[len
] == '='))
100 env
+= strlen(env
) + 1;
106 /***********************************************************************
107 * ENV_BuildEnvironment
109 * Build the environment for the initial process
111 BOOL
ENV_BuildEnvironment(void)
113 extern char **environ
;
117 /* Compute the total size of the Unix environment */
119 size
= EXTRA_ENV_SIZE
;
120 for (e
= environ
; *e
; e
++) size
+= strlen(*e
) + 1;
122 /* Now allocate the environment */
124 if (!(p
= HeapAlloc( GetProcessHeap(), 0, size
))) return FALSE
;
125 current_envdb
.environ
= p
;
126 current_envdb
.env_sel
= SELECTOR_AllocBlock( p
, 0x10000, WINE_LDT_FLAGS_DATA
);
128 /* And fill it with the Unix environment */
130 for (e
= environ
; *e
; e
++)
136 /* Now add the program name */
143 /***********************************************************************
144 * GetCommandLineA (KERNEL32.289)
146 LPSTR WINAPI
GetCommandLineA(void)
148 return current_envdb
.cmd_line
;
151 /***********************************************************************
152 * GetCommandLineW (KERNEL32.290)
154 LPWSTR WINAPI
GetCommandLineW(void)
156 EnterCriticalSection( ¤t_envdb
.section
);
157 if (!current_envdb
.cmd_lineW
)
158 current_envdb
.cmd_lineW
= HEAP_strdupAtoW( GetProcessHeap(), 0,
159 current_envdb
.cmd_line
);
160 LeaveCriticalSection( ¤t_envdb
.section
);
161 return current_envdb
.cmd_lineW
;
165 /***********************************************************************
166 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
168 LPSTR WINAPI
GetEnvironmentStringsA(void)
170 return current_envdb
.environ
;
174 /***********************************************************************
175 * GetEnvironmentStringsW (KERNEL32.321)
177 LPWSTR WINAPI
GetEnvironmentStringsW(void)
182 EnterCriticalSection( ¤t_envdb
.section
);
183 size
= HeapSize( GetProcessHeap(), 0, current_envdb
.environ
);
184 if ((ret
= HeapAlloc( GetProcessHeap(), 0, size
* sizeof(WCHAR
) )) != NULL
)
186 LPSTR pA
= current_envdb
.environ
;
188 while (size
--) *pW
++ = (WCHAR
)(BYTE
)*pA
++;
190 LeaveCriticalSection( ¤t_envdb
.section
);
195 /***********************************************************************
196 * FreeEnvironmentStringsA (KERNEL32.268)
198 BOOL WINAPI
FreeEnvironmentStringsA( LPSTR ptr
)
200 if (ptr
!= current_envdb
.environ
)
202 SetLastError( ERROR_INVALID_PARAMETER
);
209 /***********************************************************************
210 * FreeEnvironmentStringsW (KERNEL32.269)
212 BOOL WINAPI
FreeEnvironmentStringsW( LPWSTR ptr
)
214 return HeapFree( GetProcessHeap(), 0, ptr
);
218 /***********************************************************************
219 * GetEnvironmentVariableA (KERNEL32.322)
221 DWORD WINAPI
GetEnvironmentVariableA( LPCSTR name
, LPSTR value
, DWORD size
)
228 SetLastError( ERROR_INVALID_PARAMETER
);
231 EnterCriticalSection( ¤t_envdb
.section
);
232 if ((p
= ENV_FindVariable( current_envdb
.environ
, name
, strlen(name
) )))
237 /* If not enough room, include the terminating null
238 * in the returned size and return an empty string */
240 if (value
) *value
= '\0';
242 else if (value
) strcpy( value
, p
);
244 LeaveCriticalSection( ¤t_envdb
.section
);
246 SetLastError( ERROR_ENVVAR_NOT_FOUND
);
251 /***********************************************************************
252 * GetEnvironmentVariableW (KERNEL32.323)
254 DWORD WINAPI
GetEnvironmentVariableW( LPCWSTR nameW
, LPWSTR valW
, DWORD size
)
256 LPSTR name
= HEAP_strdupWtoA( GetProcessHeap(), 0, nameW
);
257 LPSTR val
= valW
? HeapAlloc( GetProcessHeap(), 0, size
) : NULL
;
258 DWORD res
= GetEnvironmentVariableA( name
, val
, size
);
259 HeapFree( GetProcessHeap(), 0, name
);
262 if (size
> 0 && !MultiByteToWideChar( CP_ACP
, 0, val
, -1, valW
, size
))
264 HeapFree( GetProcessHeap(), 0, val
);
270 /***********************************************************************
271 * SetEnvironmentVariableA (KERNEL32.641)
273 BOOL WINAPI
SetEnvironmentVariableA( LPCSTR name
, LPCSTR value
)
275 INT old_size
, len
, res
;
276 LPSTR p
, env
, new_env
;
279 EnterCriticalSection( ¤t_envdb
.section
);
280 env
= p
= current_envdb
.environ
;
282 /* Find a place to insert the string */
288 if (!strncasecmp( name
, p
, len
) && (p
[len
] == '=')) break;
291 if (!value
&& !*p
) goto done
; /* Value to remove doesn't exist */
293 /* Realloc the buffer */
295 len
= value
? strlen(name
) + strlen(value
) + 2 : 0;
296 if (*p
) len
-= strlen(p
) + 1; /* The name already exists */
297 old_size
= HeapSize( GetProcessHeap(), 0, env
);
300 LPSTR next
= p
+ strlen(p
) + 1; /* We know there is a next one */
301 memmove( next
+ len
, next
, old_size
- (next
- env
) );
303 if (!(new_env
= HeapReAlloc( GetProcessHeap(), 0, env
, old_size
+ len
)))
305 if (current_envdb
.env_sel
)
306 current_envdb
.env_sel
= SELECTOR_ReallocBlock( current_envdb
.env_sel
,
307 new_env
, old_size
+ len
);
308 p
= new_env
+ (p
- env
);
309 if (len
> 0) memmove( p
+ len
, p
, old_size
- (p
- new_env
) );
311 /* Set the new string */
319 current_envdb
.environ
= new_env
;
323 LeaveCriticalSection( ¤t_envdb
.section
);
328 /***********************************************************************
329 * SetEnvironmentVariableW (KERNEL32.642)
331 BOOL WINAPI
SetEnvironmentVariableW( LPCWSTR name
, LPCWSTR value
)
333 LPSTR nameA
= HEAP_strdupWtoA( GetProcessHeap(), 0, name
);
334 LPSTR valueA
= HEAP_strdupWtoA( GetProcessHeap(), 0, value
);
335 BOOL ret
= SetEnvironmentVariableA( nameA
, valueA
);
336 HeapFree( GetProcessHeap(), 0, nameA
);
337 HeapFree( GetProcessHeap(), 0, valueA
);
342 /***********************************************************************
343 * ExpandEnvironmentStringsA (KERNEL32.216)
345 * Note: overlapping buffers are not supported; this is how it should be.
347 DWORD WINAPI
ExpandEnvironmentStringsA( LPCSTR src
, LPSTR dst
, DWORD count
)
349 DWORD len
, total_size
= 1; /* 1 for terminating '\0' */
352 if (!count
) dst
= NULL
;
353 EnterCriticalSection( ¤t_envdb
.section
);
359 if ((p
= strchr( src
, '%' ))) len
= p
- src
;
360 else len
= strlen(src
);
364 else /* we are at the start of a variable */
366 if ((p
= strchr( src
+ 1, '%' )))
368 len
= p
- src
- 1; /* Length of the variable name */
369 if ((var
= ENV_FindVariable( current_envdb
.environ
,
372 src
+= len
+ 2; /* Skip the variable name */
377 var
= src
; /* Copy original name instead */
382 else /* unfinished variable name, ignore it */
385 len
= strlen(src
); /* Copy whole string */
392 if (count
< len
) len
= count
;
393 memcpy( dst
, var
, len
);
398 LeaveCriticalSection( ¤t_envdb
.section
);
400 /* Null-terminate the string */
410 /***********************************************************************
411 * ExpandEnvironmentStringsW (KERNEL32.217)
413 DWORD WINAPI
ExpandEnvironmentStringsW( LPCWSTR src
, LPWSTR dst
, DWORD len
)
415 LPSTR srcA
= HEAP_strdupWtoA( GetProcessHeap(), 0, src
);
416 LPSTR dstA
= dst
? HeapAlloc( GetProcessHeap(), 0, len
) : NULL
;
417 DWORD ret
= ExpandEnvironmentStringsA( srcA
, dstA
, len
);
420 ret
= MultiByteToWideChar( CP_ACP
, 0, dstA
, -1, dst
, len
);
421 HeapFree( GetProcessHeap(), 0, dstA
);
423 HeapFree( GetProcessHeap(), 0, srcA
);
428 /***********************************************************************
429 * GetStdHandle (KERNEL32.276)
431 HANDLE WINAPI
GetStdHandle( DWORD std_handle
)
435 case STD_INPUT_HANDLE
: return current_envdb
.hStdin
;
436 case STD_OUTPUT_HANDLE
: return current_envdb
.hStdout
;
437 case STD_ERROR_HANDLE
: return current_envdb
.hStderr
;
439 SetLastError( ERROR_INVALID_PARAMETER
);
440 return INVALID_HANDLE_VALUE
;
444 /***********************************************************************
445 * SetStdHandle (KERNEL32.506)
447 BOOL WINAPI
SetStdHandle( DWORD std_handle
, HANDLE handle
)
451 case STD_INPUT_HANDLE
: current_envdb
.hStdin
= handle
; return TRUE
;
452 case STD_OUTPUT_HANDLE
: current_envdb
.hStdout
= handle
; return TRUE
;
453 case STD_ERROR_HANDLE
: current_envdb
.hStderr
= handle
; return TRUE
;
455 SetLastError( ERROR_INVALID_PARAMETER
);
460 /***********************************************************************
461 * GetStartupInfoA (KERNEL32.273)
463 VOID WINAPI
GetStartupInfoA( LPSTARTUPINFOA info
)
465 *info
= current_startupinfo
;
469 /***********************************************************************
470 * GetStartupInfoW (KERNEL32.274)
472 VOID WINAPI
GetStartupInfoW( LPSTARTUPINFOW info
)
474 info
->cb
= sizeof(STARTUPINFOW
);
475 info
->dwX
= current_startupinfo
.dwX
;
476 info
->dwY
= current_startupinfo
.dwY
;
477 info
->dwXSize
= current_startupinfo
.dwXSize
;
478 info
->dwXCountChars
= current_startupinfo
.dwXCountChars
;
479 info
->dwYCountChars
= current_startupinfo
.dwYCountChars
;
480 info
->dwFillAttribute
= current_startupinfo
.dwFillAttribute
;
481 info
->dwFlags
= current_startupinfo
.dwFlags
;
482 info
->wShowWindow
= current_startupinfo
.wShowWindow
;
483 info
->cbReserved2
= current_startupinfo
.cbReserved2
;
484 info
->lpReserved2
= current_startupinfo
.lpReserved2
;
485 info
->hStdInput
= current_startupinfo
.hStdInput
;
486 info
->hStdOutput
= current_startupinfo
.hStdOutput
;
487 info
->hStdError
= current_startupinfo
.hStdError
;
488 info
->lpReserved
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpReserved
);
489 info
->lpDesktop
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpDesktop
);
490 info
->lpTitle
= HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo
.lpTitle
);