2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
11 #include "wine/winestring.h"
14 #include "selectors.h"
17 /* Format of an environment block:
18 * ASCIIZ string 1 (xx=yy format)
23 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
26 * - contrary to Microsoft docs, the environment strings do not appear
27 * to be sorted on Win95 (although they are on NT); so we don't bother
28 * to sort them either.
31 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
33 /* Maximum length of a Win16 environment string (including NULL) */
34 #define MAX_WIN16_LEN 128
36 /* Extra bytes to reserve at the end of an environment */
37 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
39 /* Fill the extra bytes with the program name and stuff */
40 #define FILL_EXTRA_ENV(p) \
42 PUT_WORD( (p) + 1, 1 ); \
43 strcpy( (p) + 3, ENV_program_name );
46 /***********************************************************************
49 * Find a variable in the environment and return a pointer to the value.
50 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
52 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT32 len )
56 if (!lstrncmpi32A( name, env, len ) && (env[len] == '='))
58 env += strlen(env) + 1;
64 /***********************************************************************
65 * ENV_BuildEnvironment
67 * Build the environment for the initial process
69 BOOL32 ENV_BuildEnvironment( PDB32 *pdb )
71 extern char **environ;
75 /* Compute the total size of the Unix environment */
77 size = EXTRA_ENV_SIZE;
78 for (e = environ; *e; e++) size += strlen(*e) + 1;
80 /* Now allocate the environment */
82 if (!(p = HeapAlloc( SystemHeap, 0, size ))) return FALSE;
83 pdb->env_db->environ = p;
85 /* And fill it with the Unix environment */
87 for (e = environ; *e; e++)
93 /* Now add the program name */
100 /***********************************************************************
101 * ENV_InheritEnvironment
103 * Make a process inherit the environment from its parent or from an
104 * explicit environment.
106 BOOL32 ENV_InheritEnvironment( PDB32 *pdb, LPCSTR env )
112 /* FIXME: should lock the parent environment */
113 if (!env) env = pdb->parent->env_db->environ;
115 /* Compute the environment size */
118 size = EXTRA_ENV_SIZE;
121 int len = strlen(src) + 1;
123 if ((len > MAX_WIN16_LEN) && (pdb->flags & PDB32_WIN16_PROC))
128 /* Copy the environment */
130 if (!(pdb->env_db->environ = HeapAlloc( pdb->heap, 0,
131 size + EXTRA_ENV_SIZE )))
133 pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
134 0x10000, SEGMENT_DATA,
137 dst = pdb->env_db->environ;
140 if (pdb->flags & PDB32_WIN16_PROC)
141 lstrcpyn32A( dst, src, MAX_WIN16_LEN );
144 src += strlen(src) + 1;
145 dst += strlen(dst) + 1;
147 FILL_EXTRA_ENV( dst );
152 /***********************************************************************
153 * ENV_FreeEnvironment
155 * Free a process environment.
157 void ENV_FreeEnvironment( PDB32 *pdb )
159 if (!pdb->env_db) return;
160 if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
161 DeleteCriticalSection( &pdb->env_db->section );
162 HeapFree( pdb->heap, 0, pdb->env_db );
166 /***********************************************************************
167 * GetCommandLine32A (KERNEL32.289)
169 LPCSTR WINAPI GetCommandLine32A(void)
171 return PROCESS_Current()->env_db->cmd_line;
174 /***********************************************************************
175 * GetCommandLine32W (KERNEL32.290)
177 LPCWSTR WINAPI GetCommandLine32W(void)
179 PDB32 *pdb = PROCESS_Current();
180 EnterCriticalSection( &pdb->env_db->section );
181 if (!pdb->env_db->cmd_lineW)
182 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( pdb->heap, 0,
183 pdb->env_db->cmd_line );
184 LeaveCriticalSection( &pdb->env_db->section );
185 return pdb->env_db->cmd_lineW;
189 /***********************************************************************
190 * GetEnvironmentStrings32A (KERNEL32.319) (KERNEL32.320)
192 LPSTR WINAPI GetEnvironmentStrings32A(void)
194 PDB32 *pdb = PROCESS_Current();
195 return pdb->env_db->environ;
199 /***********************************************************************
200 * GetEnvironmentStrings32W (KERNEL32.321)
202 LPWSTR WINAPI GetEnvironmentStrings32W(void)
206 PDB32 *pdb = PROCESS_Current();
208 EnterCriticalSection( &pdb->env_db->section );
209 size = HeapSize( pdb->heap, 0, pdb->env_db->environ );
210 if ((ret = HeapAlloc( pdb->heap, 0, size * sizeof(WCHAR) )) != NULL)
212 LPSTR pA = pdb->env_db->environ;
214 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
216 LeaveCriticalSection( &pdb->env_db->section );
221 /***********************************************************************
222 * FreeEnvironmentStrings32A (KERNEL32.268)
224 BOOL32 WINAPI FreeEnvironmentStrings32A( LPSTR ptr )
226 PDB32 *pdb = PROCESS_Current();
227 if (ptr != pdb->env_db->environ)
229 SetLastError( ERROR_INVALID_PARAMETER );
236 /***********************************************************************
237 * FreeEnvironmentStrings32W (KERNEL32.269)
239 BOOL32 WINAPI FreeEnvironmentStrings32W( LPWSTR ptr )
241 return HeapFree( GetProcessHeap(), 0, ptr );
245 /***********************************************************************
246 * GetEnvironmentVariable32A (KERNEL32.322)
248 DWORD WINAPI GetEnvironmentVariable32A( LPCSTR name, LPSTR value, DWORD size )
252 PDB32 *pdb = PROCESS_Current();
256 SetLastError( ERROR_INVALID_PARAMETER );
259 EnterCriticalSection( &pdb->env_db->section );
260 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
265 /* If not enough room, include the terminating null
266 * in the returned size and return an empty string */
268 if (value) *value = '\0';
270 else if (value) strcpy( value, p );
272 LeaveCriticalSection( &pdb->env_db->section );
273 return ret; /* FIXME: SetLastError */
277 /***********************************************************************
278 * GetEnvironmentVariable32W (KERNEL32.323)
280 DWORD WINAPI GetEnvironmentVariable32W( LPCWSTR nameW, LPWSTR valW, DWORD size)
282 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
283 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
284 DWORD res = GetEnvironmentVariable32A( name, val, size );
285 HeapFree( GetProcessHeap(), 0, name );
288 lstrcpynAtoW( valW, val, size );
289 HeapFree( GetProcessHeap(), 0, val );
295 /***********************************************************************
296 * SetEnvironmentVariable32A (KERNEL32.641)
298 BOOL32 WINAPI SetEnvironmentVariable32A( LPCSTR name, LPCSTR value )
300 INT32 old_size, len, res;
301 LPSTR p, env, new_env;
303 PDB32 *pdb = PROCESS_Current();
305 EnterCriticalSection( &pdb->env_db->section );
306 env = p = pdb->env_db->environ;
308 /* Find a place to insert the string */
314 if (!lstrncmpi32A( name, p, len ) && (p[len] == '=')) break;
317 if (!value && !*p) goto done; /* Value to remove doesn't exist */
319 /* Realloc the buffer */
321 len = value ? strlen(name) + strlen(value) + 2 : 0;
322 if (*p) len -= strlen(p) + 1; /* The name already exists */
323 old_size = HeapSize( pdb->heap, 0, env );
326 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
327 memmove( next + len, next, old_size - (next - env) );
329 if (!(new_env = HeapReAlloc( pdb->heap, 0, env, old_size + len )))
331 if (pdb->env_db->env_sel)
332 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
333 p = new_env + (p - env);
334 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
336 /* Set the new string */
344 pdb->env_db->environ = new_env;
348 LeaveCriticalSection( &pdb->env_db->section );
353 /***********************************************************************
354 * SetEnvironmentVariable32W (KERNEL32.642)
356 BOOL32 WINAPI SetEnvironmentVariable32W( LPCWSTR name, LPCWSTR value )
358 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
359 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
360 BOOL32 ret = SetEnvironmentVariable32A( nameA, valueA );
361 HeapFree( GetProcessHeap(), 0, nameA );
362 HeapFree( GetProcessHeap(), 0, valueA );
367 /***********************************************************************
368 * ExpandEnvironmentStrings32A (KERNEL32.216)
370 * Note: overlapping buffers are not supported; this is how it should be.
372 DWORD WINAPI ExpandEnvironmentStrings32A( LPCSTR src, LPSTR dst, DWORD count )
374 DWORD len, total_size = 1; /* 1 for terminating '\0' */
376 PDB32 *pdb = PROCESS_Current();
378 if (!count) dst = NULL;
379 EnterCriticalSection( &pdb->env_db->section );
385 if ((p = strchr( src, '%' ))) len = p - src;
386 else len = strlen(src);
390 else /* we are at the start of a variable */
392 if ((p = strchr( src + 1, '%' )))
394 len = p - src - 1; /* Length of the variable name */
395 if ((var = ENV_FindVariable( pdb->env_db->environ,
398 src += len + 2; /* Skip the variable name */
403 var = src; /* Copy original name instead */
408 else /* unfinished variable name, ignore it */
411 len = strlen(src); /* Copy whole string */
418 if (count < len) len = count;
419 memcpy( dst, var, len );
424 LeaveCriticalSection( &pdb->env_db->section );
426 /* Null-terminate the string */
436 /***********************************************************************
437 * ExpandEnvironmentStrings32W (KERNEL32.217)
439 DWORD WINAPI ExpandEnvironmentStrings32W( LPCWSTR src, LPWSTR dst, DWORD len )
441 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
442 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
443 DWORD ret = ExpandEnvironmentStrings32A( srcA, dstA, len );
446 lstrcpyAtoW( dst, dstA );
447 HeapFree( GetProcessHeap(), 0, dstA );
449 HeapFree( GetProcessHeap(), 0, srcA );