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(void)
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( GetProcessHeap(), 0, size ))) return FALSE;
82 PROCESS_Current()->env_db->environ = p;
83 PROCESS_Current()->env_db->env_sel = SELECTOR_AllocBlock( p, 0x10000, SEGMENT_DATA,
86 /* And fill it with the Unix environment */
88 for (e = environ; *e; e++)
94 /* Now add the program name */
101 /***********************************************************************
102 * GetCommandLineA (KERNEL32.289)
104 LPSTR WINAPI GetCommandLineA(void)
106 return PROCESS_Current()->env_db->cmd_line;
109 /***********************************************************************
110 * GetCommandLineW (KERNEL32.290)
112 LPWSTR WINAPI GetCommandLineW(void)
114 PDB *pdb = PROCESS_Current();
115 EnterCriticalSection( &pdb->env_db->section );
116 if (!pdb->env_db->cmd_lineW)
117 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( GetProcessHeap(), 0,
118 pdb->env_db->cmd_line );
119 LeaveCriticalSection( &pdb->env_db->section );
120 return pdb->env_db->cmd_lineW;
124 /***********************************************************************
125 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
127 LPSTR WINAPI GetEnvironmentStringsA(void)
129 PDB *pdb = PROCESS_Current();
130 return pdb->env_db->environ;
134 /***********************************************************************
135 * GetEnvironmentStringsW (KERNEL32.321)
137 LPWSTR WINAPI GetEnvironmentStringsW(void)
141 PDB *pdb = PROCESS_Current();
143 EnterCriticalSection( &pdb->env_db->section );
144 size = HeapSize( GetProcessHeap(), 0, pdb->env_db->environ );
145 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
147 LPSTR pA = pdb->env_db->environ;
149 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
151 LeaveCriticalSection( &pdb->env_db->section );
156 /***********************************************************************
157 * FreeEnvironmentStringsA (KERNEL32.268)
159 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
161 PDB *pdb = PROCESS_Current();
162 if (ptr != pdb->env_db->environ)
164 SetLastError( ERROR_INVALID_PARAMETER );
171 /***********************************************************************
172 * FreeEnvironmentStringsW (KERNEL32.269)
174 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
176 return HeapFree( GetProcessHeap(), 0, ptr );
180 /***********************************************************************
181 * GetEnvironmentVariableA (KERNEL32.322)
183 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
187 PDB *pdb = PROCESS_Current();
191 SetLastError( ERROR_INVALID_PARAMETER );
194 EnterCriticalSection( &pdb->env_db->section );
195 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
200 /* If not enough room, include the terminating null
201 * in the returned size and return an empty string */
203 if (value) *value = '\0';
205 else if (value) strcpy( value, p );
207 LeaveCriticalSection( &pdb->env_db->section );
208 return ret; /* FIXME: SetLastError */
212 /***********************************************************************
213 * GetEnvironmentVariableW (KERNEL32.323)
215 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
217 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
218 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
219 DWORD res = GetEnvironmentVariableA( name, val, size );
220 HeapFree( GetProcessHeap(), 0, name );
223 lstrcpynAtoW( valW, val, size );
224 HeapFree( GetProcessHeap(), 0, val );
230 /***********************************************************************
231 * SetEnvironmentVariableA (KERNEL32.641)
233 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
235 INT old_size, len, res;
236 LPSTR p, env, new_env;
238 PDB *pdb = PROCESS_Current();
240 EnterCriticalSection( &pdb->env_db->section );
241 env = p = pdb->env_db->environ;
243 /* Find a place to insert the string */
249 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
252 if (!value && !*p) goto done; /* Value to remove doesn't exist */
254 /* Realloc the buffer */
256 len = value ? strlen(name) + strlen(value) + 2 : 0;
257 if (*p) len -= strlen(p) + 1; /* The name already exists */
258 old_size = HeapSize( GetProcessHeap(), 0, env );
261 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
262 memmove( next + len, next, old_size - (next - env) );
264 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
266 if (pdb->env_db->env_sel)
267 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
268 p = new_env + (p - env);
269 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
271 /* Set the new string */
279 pdb->env_db->environ = new_env;
283 LeaveCriticalSection( &pdb->env_db->section );
288 /***********************************************************************
289 * SetEnvironmentVariableW (KERNEL32.642)
291 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
293 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
294 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
295 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
296 HeapFree( GetProcessHeap(), 0, nameA );
297 HeapFree( GetProcessHeap(), 0, valueA );
302 /***********************************************************************
303 * ExpandEnvironmentStringsA (KERNEL32.216)
305 * Note: overlapping buffers are not supported; this is how it should be.
307 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
309 DWORD len, total_size = 1; /* 1 for terminating '\0' */
311 PDB *pdb = PROCESS_Current();
313 if (!count) dst = NULL;
314 EnterCriticalSection( &pdb->env_db->section );
320 if ((p = strchr( src, '%' ))) len = p - src;
321 else len = strlen(src);
325 else /* we are at the start of a variable */
327 if ((p = strchr( src + 1, '%' )))
329 len = p - src - 1; /* Length of the variable name */
330 if ((var = ENV_FindVariable( pdb->env_db->environ,
333 src += len + 2; /* Skip the variable name */
338 var = src; /* Copy original name instead */
343 else /* unfinished variable name, ignore it */
346 len = strlen(src); /* Copy whole string */
353 if (count < len) len = count;
354 memcpy( dst, var, len );
359 LeaveCriticalSection( &pdb->env_db->section );
361 /* Null-terminate the string */
371 /***********************************************************************
372 * ExpandEnvironmentStringsW (KERNEL32.217)
374 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
376 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
377 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
378 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
381 lstrcpyAtoW( dst, dstA );
382 HeapFree( GetProcessHeap(), 0, dstA );
384 HeapFree( GetProcessHeap(), 0, srcA );