2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
10 #include "wine/winestring.h"
13 #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 );
45 /***********************************************************************
48 * Find a variable in the environment and return a pointer to the value.
49 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
51 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
55 if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
57 env += strlen(env) + 1;
63 /***********************************************************************
64 * ENV_BuildEnvironment
66 * Build the environment for the initial process
68 BOOL ENV_BuildEnvironment( PDB *pdb )
70 extern char **environ;
74 /* Compute the total size of the Unix environment */
76 size = EXTRA_ENV_SIZE;
77 for (e = environ; *e; e++) size += strlen(*e) + 1;
79 /* Now allocate the environment */
81 if (!(p = HeapAlloc( SystemHeap, 0, size ))) return FALSE;
82 pdb->env_db->environ = p;
84 /* And fill it with the Unix environment */
86 for (e = environ; *e; e++)
92 /* Now add the program name */
99 /***********************************************************************
100 * ENV_InheritEnvironment
102 * Make a process inherit the environment from its parent or from an
103 * explicit environment.
105 BOOL ENV_InheritEnvironment( PDB *pdb, LPCSTR env )
111 /* FIXME: should lock the parent environment */
112 if (!env) env = pdb->parent->env_db->environ;
114 /* Compute the environment size */
117 size = EXTRA_ENV_SIZE;
120 int len = strlen(src) + 1;
122 if ((len > MAX_WIN16_LEN) && (pdb->flags & PDB32_WIN16_PROC))
127 /* Copy the environment */
129 if (!(pdb->env_db->environ = HeapAlloc( pdb->heap, 0,
130 size + EXTRA_ENV_SIZE )))
132 pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
133 0x10000, SEGMENT_DATA,
136 dst = pdb->env_db->environ;
139 if (pdb->flags & PDB32_WIN16_PROC)
140 lstrcpynA( dst, src, MAX_WIN16_LEN );
143 src += strlen(src) + 1;
144 dst += strlen(dst) + 1;
146 FILL_EXTRA_ENV( dst );
151 /***********************************************************************
152 * ENV_FreeEnvironment
154 * Free a process environment.
156 void ENV_FreeEnvironment( PDB *pdb )
158 if (!pdb->env_db) return;
159 if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
160 DeleteCriticalSection( &pdb->env_db->section );
161 HeapFree( pdb->heap, 0, pdb->env_db );
165 /***********************************************************************
166 * GetCommandLine32A (KERNEL32.289)
168 LPCSTR WINAPI GetCommandLineA(void)
170 return PROCESS_Current()->env_db->cmd_line;
173 /***********************************************************************
174 * GetCommandLine32W (KERNEL32.290)
176 LPCWSTR WINAPI GetCommandLineW(void)
178 PDB *pdb = PROCESS_Current();
179 EnterCriticalSection( &pdb->env_db->section );
180 if (!pdb->env_db->cmd_lineW)
181 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( pdb->heap, 0,
182 pdb->env_db->cmd_line );
183 LeaveCriticalSection( &pdb->env_db->section );
184 return pdb->env_db->cmd_lineW;
188 /***********************************************************************
189 * GetEnvironmentStrings32A (KERNEL32.319) (KERNEL32.320)
191 LPSTR WINAPI GetEnvironmentStringsA(void)
193 PDB *pdb = PROCESS_Current();
194 return pdb->env_db->environ;
198 /***********************************************************************
199 * GetEnvironmentStrings32W (KERNEL32.321)
201 LPWSTR WINAPI GetEnvironmentStringsW(void)
205 PDB *pdb = PROCESS_Current();
207 EnterCriticalSection( &pdb->env_db->section );
208 size = HeapSize( pdb->heap, 0, pdb->env_db->environ );
209 if ((ret = HeapAlloc( pdb->heap, 0, size * sizeof(WCHAR) )) != NULL)
211 LPSTR pA = pdb->env_db->environ;
213 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
215 LeaveCriticalSection( &pdb->env_db->section );
220 /***********************************************************************
221 * FreeEnvironmentStrings32A (KERNEL32.268)
223 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
225 PDB *pdb = PROCESS_Current();
226 if (ptr != pdb->env_db->environ)
228 SetLastError( ERROR_INVALID_PARAMETER );
235 /***********************************************************************
236 * FreeEnvironmentStrings32W (KERNEL32.269)
238 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
240 return HeapFree( GetProcessHeap(), 0, ptr );
244 /***********************************************************************
245 * GetEnvironmentVariable32A (KERNEL32.322)
247 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
251 PDB *pdb = PROCESS_Current();
255 SetLastError( ERROR_INVALID_PARAMETER );
258 EnterCriticalSection( &pdb->env_db->section );
259 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
264 /* If not enough room, include the terminating null
265 * in the returned size and return an empty string */
267 if (value) *value = '\0';
269 else if (value) strcpy( value, p );
271 LeaveCriticalSection( &pdb->env_db->section );
272 return ret; /* FIXME: SetLastError */
276 /***********************************************************************
277 * GetEnvironmentVariable32W (KERNEL32.323)
279 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
281 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
282 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
283 DWORD res = GetEnvironmentVariableA( name, val, size );
284 HeapFree( GetProcessHeap(), 0, name );
287 lstrcpynAtoW( valW, val, size );
288 HeapFree( GetProcessHeap(), 0, val );
294 /***********************************************************************
295 * SetEnvironmentVariable32A (KERNEL32.641)
297 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
299 INT old_size, len, res;
300 LPSTR p, env, new_env;
302 PDB *pdb = PROCESS_Current();
304 EnterCriticalSection( &pdb->env_db->section );
305 env = p = pdb->env_db->environ;
307 /* Find a place to insert the string */
313 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
316 if (!value && !*p) goto done; /* Value to remove doesn't exist */
318 /* Realloc the buffer */
320 len = value ? strlen(name) + strlen(value) + 2 : 0;
321 if (*p) len -= strlen(p) + 1; /* The name already exists */
322 old_size = HeapSize( pdb->heap, 0, env );
325 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
326 memmove( next + len, next, old_size - (next - env) );
328 if (!(new_env = HeapReAlloc( pdb->heap, 0, env, old_size + len )))
330 if (pdb->env_db->env_sel)
331 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
332 p = new_env + (p - env);
333 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
335 /* Set the new string */
343 pdb->env_db->environ = new_env;
347 LeaveCriticalSection( &pdb->env_db->section );
352 /***********************************************************************
353 * SetEnvironmentVariable32W (KERNEL32.642)
355 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
357 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
358 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
359 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
360 HeapFree( GetProcessHeap(), 0, nameA );
361 HeapFree( GetProcessHeap(), 0, valueA );
366 /***********************************************************************
367 * ExpandEnvironmentStrings32A (KERNEL32.216)
369 * Note: overlapping buffers are not supported; this is how it should be.
371 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
373 DWORD len, total_size = 1; /* 1 for terminating '\0' */
375 PDB *pdb = PROCESS_Current();
377 if (!count) dst = NULL;
378 EnterCriticalSection( &pdb->env_db->section );
384 if ((p = strchr( src, '%' ))) len = p - src;
385 else len = strlen(src);
389 else /* we are at the start of a variable */
391 if ((p = strchr( src + 1, '%' )))
393 len = p - src - 1; /* Length of the variable name */
394 if ((var = ENV_FindVariable( pdb->env_db->environ,
397 src += len + 2; /* Skip the variable name */
402 var = src; /* Copy original name instead */
407 else /* unfinished variable name, ignore it */
410 len = strlen(src); /* Copy whole string */
417 if (count < len) len = count;
418 memcpy( dst, var, len );
423 LeaveCriticalSection( &pdb->env_db->section );
425 /* Null-terminate the string */
435 /***********************************************************************
436 * ExpandEnvironmentStrings32W (KERNEL32.217)
438 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
440 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
441 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
442 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
445 lstrcpyAtoW( dst, dstA );
446 HeapFree( GetProcessHeap(), 0, dstA );
448 HeapFree( GetProcessHeap(), 0, srcA );