- Return modified rectangle after layouting header control
[wine.git] / memory / environ.c
bloba41f0e51150eb751b9eb92b88426b76517150e19
1 /*
2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include "windef.h"
10 #include "wingdi.h"
11 #include "winuser.h"
12 #include "wine/winestring.h"
13 #include "process.h"
14 #include "heap.h"
15 #include "selectors.h"
16 #include "winerror.h"
18 /* Format of an environment block:
19 * ASCIIZ string 1 (xx=yy format)
20 * ...
21 * ASCIIZ string n
22 * BYTE 0
23 * WORD 1
24 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
26 * Notes:
27 * - contrary to Microsoft docs, the environment strings do not appear
28 * to be sorted on Win95 (although they are on NT); so we don't bother
29 * to sort them either.
32 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
34 /* Maximum length of a Win16 environment string (including NULL) */
35 #define MAX_WIN16_LEN 128
37 /* Extra bytes to reserve at the end of an environment */
38 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
40 /* Fill the extra bytes with the program name and stuff */
41 #define FILL_EXTRA_ENV(p) \
42 *(p) = '\0'; \
43 PUT_WORD( (p) + 1, 1 ); \
44 strcpy( (p) + 3, ENV_program_name );
47 /***********************************************************************
48 * ENV_FindVariable
50 * Find a variable in the environment and return a pointer to the value.
51 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
53 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
55 while (*env)
57 if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
58 return env + len + 1;
59 env += strlen(env) + 1;
61 return NULL;
65 /***********************************************************************
66 * ENV_BuildEnvironment
68 * Build the environment for the initial process
70 static BOOL ENV_BuildEnvironment( PDB *pdb )
72 extern char **environ;
73 LPSTR p, *e;
74 int size;
76 /* Compute the total size of the Unix environment */
78 size = EXTRA_ENV_SIZE;
79 for (e = environ; *e; e++) size += strlen(*e) + 1;
81 /* Now allocate the environment */
83 if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
84 pdb->env_db->environ = p;
86 /* And fill it with the Unix environment */
88 for (e = environ; *e; e++)
90 strcpy( p, *e );
91 p += strlen(p) + 1;
94 /* Now add the program name */
96 FILL_EXTRA_ENV( p );
97 return TRUE;
101 /***********************************************************************
102 * ENV_InheritEnvironment
104 * Make a process inherit the environment from its parent or from an
105 * explicit environment.
107 BOOL ENV_InheritEnvironment( LPCSTR env )
109 DWORD size;
110 LPCSTR src;
111 LPSTR dst;
112 PDB *pdb = PROCESS_Current();
114 /* FIXME: should lock the parent environment */
115 if (!env)
117 if (!pdb->parent) /* initial process */
118 return ENV_BuildEnvironment( pdb );
119 env = pdb->parent->env_db->environ;
122 /* Compute the environment size */
124 src = env;
125 size = EXTRA_ENV_SIZE;
126 while (*src)
128 int len = strlen(src) + 1;
129 src += len;
130 if ((len > MAX_WIN16_LEN) && (pdb->flags & PDB32_WIN16_PROC))
131 len = MAX_WIN16_LEN;
132 size += len;
135 /* Copy the environment */
137 if (!(pdb->env_db->environ = HeapAlloc( GetProcessHeap(), 0, size )))
138 return FALSE;
139 pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
140 0x10000, SEGMENT_DATA,
141 FALSE, FALSE );
142 src = env;
143 dst = pdb->env_db->environ;
144 while (*src)
146 if (pdb->flags & PDB32_WIN16_PROC)
147 lstrcpynA( dst, src, MAX_WIN16_LEN );
148 else
149 strcpy( dst, src );
150 src += strlen(src) + 1;
151 dst += strlen(dst) + 1;
153 FILL_EXTRA_ENV( dst );
154 return TRUE;
158 /***********************************************************************
159 * ENV_FreeEnvironment
161 * Free a process environment.
163 void ENV_FreeEnvironment( PDB *pdb )
165 if (!pdb->env_db) return;
166 if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
167 DeleteCriticalSection( &pdb->env_db->section );
168 /* the storage will be deleted when the process heap is destroyed */
172 /***********************************************************************
173 * GetCommandLineA (KERNEL32.289)
175 LPCSTR WINAPI GetCommandLineA(void)
177 return PROCESS_Current()->env_db->cmd_line;
180 /***********************************************************************
181 * GetCommandLineW (KERNEL32.290)
183 LPCWSTR WINAPI GetCommandLineW(void)
185 PDB *pdb = PROCESS_Current();
186 EnterCriticalSection( &pdb->env_db->section );
187 if (!pdb->env_db->cmd_lineW)
188 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( GetProcessHeap(), 0,
189 pdb->env_db->cmd_line );
190 LeaveCriticalSection( &pdb->env_db->section );
191 return pdb->env_db->cmd_lineW;
195 /***********************************************************************
196 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
198 LPSTR WINAPI GetEnvironmentStringsA(void)
200 PDB *pdb = PROCESS_Current();
201 return pdb->env_db->environ;
205 /***********************************************************************
206 * GetEnvironmentStringsW (KERNEL32.321)
208 LPWSTR WINAPI GetEnvironmentStringsW(void)
210 INT size;
211 LPWSTR ret;
212 PDB *pdb = PROCESS_Current();
214 EnterCriticalSection( &pdb->env_db->section );
215 size = HeapSize( GetProcessHeap(), 0, pdb->env_db->environ );
216 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
218 LPSTR pA = pdb->env_db->environ;
219 LPWSTR pW = ret;
220 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
222 LeaveCriticalSection( &pdb->env_db->section );
223 return ret;
227 /***********************************************************************
228 * FreeEnvironmentStringsA (KERNEL32.268)
230 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
232 PDB *pdb = PROCESS_Current();
233 if (ptr != pdb->env_db->environ)
235 SetLastError( ERROR_INVALID_PARAMETER );
236 return FALSE;
238 return TRUE;
242 /***********************************************************************
243 * FreeEnvironmentStringsW (KERNEL32.269)
245 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
247 return HeapFree( GetProcessHeap(), 0, ptr );
251 /***********************************************************************
252 * GetEnvironmentVariableA (KERNEL32.322)
254 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
256 LPCSTR p;
257 INT ret = 0;
258 PDB *pdb = PROCESS_Current();
260 if (!name || !*name)
262 SetLastError( ERROR_INVALID_PARAMETER );
263 return 0;
265 EnterCriticalSection( &pdb->env_db->section );
266 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
268 ret = strlen(p);
269 if (size <= ret)
271 /* If not enough room, include the terminating null
272 * in the returned size and return an empty string */
273 ret++;
274 if (value) *value = '\0';
276 else if (value) strcpy( value, p );
278 LeaveCriticalSection( &pdb->env_db->section );
279 return ret; /* FIXME: SetLastError */
283 /***********************************************************************
284 * GetEnvironmentVariableW (KERNEL32.323)
286 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
288 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
289 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
290 DWORD res = GetEnvironmentVariableA( name, val, size );
291 HeapFree( GetProcessHeap(), 0, name );
292 if (val)
294 lstrcpynAtoW( valW, val, size );
295 HeapFree( GetProcessHeap(), 0, val );
297 return res;
301 /***********************************************************************
302 * SetEnvironmentVariableA (KERNEL32.641)
304 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
306 INT old_size, len, res;
307 LPSTR p, env, new_env;
308 BOOL ret = FALSE;
309 PDB *pdb = PROCESS_Current();
311 EnterCriticalSection( &pdb->env_db->section );
312 env = p = pdb->env_db->environ;
314 /* Find a place to insert the string */
316 res = -1;
317 len = strlen(name);
318 while (*p)
320 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
321 p += strlen(p) + 1;
323 if (!value && !*p) goto done; /* Value to remove doesn't exist */
325 /* Realloc the buffer */
327 len = value ? strlen(name) + strlen(value) + 2 : 0;
328 if (*p) len -= strlen(p) + 1; /* The name already exists */
329 old_size = HeapSize( GetProcessHeap(), 0, env );
330 if (len < 0)
332 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
333 memmove( next + len, next, old_size - (next - env) );
335 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
336 goto done;
337 if (pdb->env_db->env_sel)
338 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
339 p = new_env + (p - env);
340 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
342 /* Set the new string */
344 if (value)
346 strcpy( p, name );
347 strcat( p, "=" );
348 strcat( p, value );
350 pdb->env_db->environ = new_env;
351 ret = TRUE;
353 done:
354 LeaveCriticalSection( &pdb->env_db->section );
355 return ret;
359 /***********************************************************************
360 * SetEnvironmentVariableW (KERNEL32.642)
362 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
364 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
365 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
366 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
367 HeapFree( GetProcessHeap(), 0, nameA );
368 HeapFree( GetProcessHeap(), 0, valueA );
369 return ret;
373 /***********************************************************************
374 * ExpandEnvironmentStringsA (KERNEL32.216)
376 * Note: overlapping buffers are not supported; this is how it should be.
378 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
380 DWORD len, total_size = 1; /* 1 for terminating '\0' */
381 LPCSTR p, var;
382 PDB *pdb = PROCESS_Current();
384 if (!count) dst = NULL;
385 EnterCriticalSection( &pdb->env_db->section );
387 while (*src)
389 if (*src != '%')
391 if ((p = strchr( src, '%' ))) len = p - src;
392 else len = strlen(src);
393 var = src;
394 src += len;
396 else /* we are at the start of a variable */
398 if ((p = strchr( src + 1, '%' )))
400 len = p - src - 1; /* Length of the variable name */
401 if ((var = ENV_FindVariable( pdb->env_db->environ,
402 src + 1, len )))
404 src += len + 2; /* Skip the variable name */
405 len = strlen(var);
407 else
409 var = src; /* Copy original name instead */
410 len += 2;
411 src += len;
414 else /* unfinished variable name, ignore it */
416 var = src;
417 len = strlen(src); /* Copy whole string */
418 src += len;
421 total_size += len;
422 if (dst)
424 if (count < len) len = count;
425 memcpy( dst, var, len );
426 dst += len;
427 count -= len;
430 LeaveCriticalSection( &pdb->env_db->section );
432 /* Null-terminate the string */
433 if (dst)
435 if (!count) dst--;
436 *dst = '\0';
438 return total_size;
442 /***********************************************************************
443 * ExpandEnvironmentStringsW (KERNEL32.217)
445 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
447 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
448 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
449 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
450 if (dstA)
452 lstrcpyAtoW( dst, dstA );
453 HeapFree( GetProcessHeap(), 0, dstA );
455 HeapFree( GetProcessHeap(), 0, srcA );
456 return ret;