Fixed bad register constraint in InterlockedCompareExchange.
[wine/hacks.git] / memory / environ.c
blobff45b0dc140700a00eb2b0f361df3e870b4720a6
1 /*
2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include "winuser.h"
10 #include "wine/winestring.h"
11 #include "process.h"
12 #include "heap.h"
13 #include "selectors.h"
14 #include "winerror.h"
16 /* Format of an environment block:
17 * ASCIIZ string 1 (xx=yy format)
18 * ...
19 * ASCIIZ string n
20 * BYTE 0
21 * WORD 1
22 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
24 * Notes:
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) \
40 *(p) = '\0'; \
41 PUT_WORD( (p) + 1, 1 ); \
42 strcpy( (p) + 3, ENV_program_name );
45 /***********************************************************************
46 * ENV_FindVariable
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 )
53 while (*env)
55 if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
56 return env + len + 1;
57 env += strlen(env) + 1;
59 return NULL;
63 /***********************************************************************
64 * ENV_BuildEnvironment
66 * Build the environment for the initial process
68 static BOOL ENV_BuildEnvironment( PDB *pdb )
70 extern char **environ;
71 LPSTR p, *e;
72 int size;
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++)
88 strcpy( p, *e );
89 p += strlen(p) + 1;
92 /* Now add the program name */
94 FILL_EXTRA_ENV( p );
95 return TRUE;
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 )
107 DWORD size;
108 LPCSTR src;
109 LPSTR dst;
111 /* FIXME: should lock the parent environment */
112 if (!env)
114 if (!pdb->parent) /* initial process */
115 return ENV_BuildEnvironment( pdb );
116 env = pdb->parent->env_db->environ;
119 /* Compute the environment size */
121 src = env;
122 size = EXTRA_ENV_SIZE;
123 while (*src)
125 int len = strlen(src) + 1;
126 src += len;
127 if ((len > MAX_WIN16_LEN) && (pdb->flags & PDB32_WIN16_PROC))
128 len = MAX_WIN16_LEN;
129 size += len;
132 /* Copy the environment */
134 if (!(pdb->env_db->environ = HeapAlloc( pdb->heap, 0, size )))
135 return FALSE;
136 pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
137 0x10000, SEGMENT_DATA,
138 FALSE, FALSE );
139 src = env;
140 dst = pdb->env_db->environ;
141 while (*src)
143 if (pdb->flags & PDB32_WIN16_PROC)
144 lstrcpynA( dst, src, MAX_WIN16_LEN );
145 else
146 strcpy( dst, src );
147 src += strlen(src) + 1;
148 dst += strlen(dst) + 1;
150 FILL_EXTRA_ENV( dst );
151 return TRUE;
155 /***********************************************************************
156 * ENV_FreeEnvironment
158 * Free a process environment.
160 void ENV_FreeEnvironment( PDB *pdb )
162 if (!pdb->env_db) return;
163 if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
164 DeleteCriticalSection( &pdb->env_db->section );
165 HeapFree( pdb->heap, 0, pdb->env_db );
169 /***********************************************************************
170 * GetCommandLine32A (KERNEL32.289)
172 LPCSTR WINAPI GetCommandLineA(void)
174 return PROCESS_Current()->env_db->cmd_line;
177 /***********************************************************************
178 * GetCommandLine32W (KERNEL32.290)
180 LPCWSTR WINAPI GetCommandLineW(void)
182 PDB *pdb = PROCESS_Current();
183 EnterCriticalSection( &pdb->env_db->section );
184 if (!pdb->env_db->cmd_lineW)
185 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( pdb->heap, 0,
186 pdb->env_db->cmd_line );
187 LeaveCriticalSection( &pdb->env_db->section );
188 return pdb->env_db->cmd_lineW;
192 /***********************************************************************
193 * GetEnvironmentStrings32A (KERNEL32.319) (KERNEL32.320)
195 LPSTR WINAPI GetEnvironmentStringsA(void)
197 PDB *pdb = PROCESS_Current();
198 return pdb->env_db->environ;
202 /***********************************************************************
203 * GetEnvironmentStrings32W (KERNEL32.321)
205 LPWSTR WINAPI GetEnvironmentStringsW(void)
207 INT size;
208 LPWSTR ret;
209 PDB *pdb = PROCESS_Current();
211 EnterCriticalSection( &pdb->env_db->section );
212 size = HeapSize( pdb->heap, 0, pdb->env_db->environ );
213 if ((ret = HeapAlloc( pdb->heap, 0, size * sizeof(WCHAR) )) != NULL)
215 LPSTR pA = pdb->env_db->environ;
216 LPWSTR pW = ret;
217 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
219 LeaveCriticalSection( &pdb->env_db->section );
220 return ret;
224 /***********************************************************************
225 * FreeEnvironmentStrings32A (KERNEL32.268)
227 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
229 PDB *pdb = PROCESS_Current();
230 if (ptr != pdb->env_db->environ)
232 SetLastError( ERROR_INVALID_PARAMETER );
233 return FALSE;
235 return TRUE;
239 /***********************************************************************
240 * FreeEnvironmentStrings32W (KERNEL32.269)
242 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
244 return HeapFree( GetProcessHeap(), 0, ptr );
248 /***********************************************************************
249 * GetEnvironmentVariable32A (KERNEL32.322)
251 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
253 LPCSTR p;
254 INT ret = 0;
255 PDB *pdb = PROCESS_Current();
257 if (!name || !*name)
259 SetLastError( ERROR_INVALID_PARAMETER );
260 return 0;
262 EnterCriticalSection( &pdb->env_db->section );
263 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
265 ret = strlen(p);
266 if (size <= ret)
268 /* If not enough room, include the terminating null
269 * in the returned size and return an empty string */
270 ret++;
271 if (value) *value = '\0';
273 else if (value) strcpy( value, p );
275 LeaveCriticalSection( &pdb->env_db->section );
276 return ret; /* FIXME: SetLastError */
280 /***********************************************************************
281 * GetEnvironmentVariable32W (KERNEL32.323)
283 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
285 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
286 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
287 DWORD res = GetEnvironmentVariableA( name, val, size );
288 HeapFree( GetProcessHeap(), 0, name );
289 if (val)
291 lstrcpynAtoW( valW, val, size );
292 HeapFree( GetProcessHeap(), 0, val );
294 return res;
298 /***********************************************************************
299 * SetEnvironmentVariable32A (KERNEL32.641)
301 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
303 INT old_size, len, res;
304 LPSTR p, env, new_env;
305 BOOL ret = FALSE;
306 PDB *pdb = PROCESS_Current();
308 EnterCriticalSection( &pdb->env_db->section );
309 env = p = pdb->env_db->environ;
311 /* Find a place to insert the string */
313 res = -1;
314 len = strlen(name);
315 while (*p)
317 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
318 p += strlen(p) + 1;
320 if (!value && !*p) goto done; /* Value to remove doesn't exist */
322 /* Realloc the buffer */
324 len = value ? strlen(name) + strlen(value) + 2 : 0;
325 if (*p) len -= strlen(p) + 1; /* The name already exists */
326 old_size = HeapSize( pdb->heap, 0, env );
327 if (len < 0)
329 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
330 memmove( next + len, next, old_size - (next - env) );
332 if (!(new_env = HeapReAlloc( pdb->heap, 0, env, old_size + len )))
333 goto done;
334 if (pdb->env_db->env_sel)
335 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
336 p = new_env + (p - env);
337 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
339 /* Set the new string */
341 if (value)
343 strcpy( p, name );
344 strcat( p, "=" );
345 strcat( p, value );
347 pdb->env_db->environ = new_env;
348 ret = TRUE;
350 done:
351 LeaveCriticalSection( &pdb->env_db->section );
352 return ret;
356 /***********************************************************************
357 * SetEnvironmentVariable32W (KERNEL32.642)
359 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
361 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
362 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
363 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
364 HeapFree( GetProcessHeap(), 0, nameA );
365 HeapFree( GetProcessHeap(), 0, valueA );
366 return ret;
370 /***********************************************************************
371 * ExpandEnvironmentStrings32A (KERNEL32.216)
373 * Note: overlapping buffers are not supported; this is how it should be.
375 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
377 DWORD len, total_size = 1; /* 1 for terminating '\0' */
378 LPCSTR p, var;
379 PDB *pdb = PROCESS_Current();
381 if (!count) dst = NULL;
382 EnterCriticalSection( &pdb->env_db->section );
384 while (*src)
386 if (*src != '%')
388 if ((p = strchr( src, '%' ))) len = p - src;
389 else len = strlen(src);
390 var = src;
391 src += len;
393 else /* we are at the start of a variable */
395 if ((p = strchr( src + 1, '%' )))
397 len = p - src - 1; /* Length of the variable name */
398 if ((var = ENV_FindVariable( pdb->env_db->environ,
399 src + 1, len )))
401 src += len + 2; /* Skip the variable name */
402 len = strlen(var);
404 else
406 var = src; /* Copy original name instead */
407 len += 2;
408 src += len;
411 else /* unfinished variable name, ignore it */
413 var = src;
414 len = strlen(src); /* Copy whole string */
415 src += len;
418 total_size += len;
419 if (dst)
421 if (count < len) len = count;
422 memcpy( dst, var, len );
423 dst += len;
424 count -= len;
427 LeaveCriticalSection( &pdb->env_db->section );
429 /* Null-terminate the string */
430 if (dst)
432 if (!count) dst--;
433 *dst = '\0';
435 return total_size;
439 /***********************************************************************
440 * ExpandEnvironmentStrings32W (KERNEL32.217)
442 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
444 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
445 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
446 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
447 if (dstA)
449 lstrcpyAtoW( dst, dstA );
450 HeapFree( GetProcessHeap(), 0, dstA );
452 HeapFree( GetProcessHeap(), 0, srcA );
453 return ret;