Rewrote Unix process launching to allow passing startup information to
[wine.git] / memory / environ.c
blob2fa96b3206582d50d578219289f97b3548eae499
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 BOOL ENV_BuildEnvironment(void)
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 PROCESS_Current()->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( PDB *pdb, LPCSTR env )
109 DWORD size;
110 LPCSTR src;
111 LPSTR dst;
113 /* Compute the environment size */
115 src = env;
116 size = EXTRA_ENV_SIZE;
117 while (*src)
119 int len = strlen(src) + 1;
120 src += len;
121 if ((len > MAX_WIN16_LEN) && (pdb->flags & PDB32_WIN16_PROC))
122 len = MAX_WIN16_LEN;
123 size += len;
126 /* Copy the environment */
128 if (!(pdb->env_db->environ = HeapAlloc( GetProcessHeap(), 0, size )))
129 return FALSE;
130 pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
131 0x10000, SEGMENT_DATA,
132 FALSE, FALSE );
133 src = env;
134 dst = pdb->env_db->environ;
135 while (*src)
137 if (pdb->flags & PDB32_WIN16_PROC)
138 lstrcpynA( dst, src, MAX_WIN16_LEN );
139 else
140 strcpy( dst, src );
141 src += strlen(src) + 1;
142 dst += strlen(dst) + 1;
144 FILL_EXTRA_ENV( dst );
145 return TRUE;
149 /***********************************************************************
150 * ENV_FreeEnvironment
152 * Free a process environment.
154 void ENV_FreeEnvironment( PDB *pdb )
156 if (!pdb->env_db) return;
157 if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
158 DeleteCriticalSection( &pdb->env_db->section );
159 /* the storage will be deleted when the process heap is destroyed */
163 /***********************************************************************
164 * GetCommandLineA (KERNEL32.289)
166 LPCSTR WINAPI GetCommandLineA(void)
168 return PROCESS_Current()->env_db->cmd_line;
171 /***********************************************************************
172 * GetCommandLineW (KERNEL32.290)
174 LPCWSTR WINAPI GetCommandLineW(void)
176 PDB *pdb = PROCESS_Current();
177 EnterCriticalSection( &pdb->env_db->section );
178 if (!pdb->env_db->cmd_lineW)
179 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( GetProcessHeap(), 0,
180 pdb->env_db->cmd_line );
181 LeaveCriticalSection( &pdb->env_db->section );
182 return pdb->env_db->cmd_lineW;
186 /***********************************************************************
187 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
189 LPSTR WINAPI GetEnvironmentStringsA(void)
191 PDB *pdb = PROCESS_Current();
192 return pdb->env_db->environ;
196 /***********************************************************************
197 * GetEnvironmentStringsW (KERNEL32.321)
199 LPWSTR WINAPI GetEnvironmentStringsW(void)
201 INT size;
202 LPWSTR ret;
203 PDB *pdb = PROCESS_Current();
205 EnterCriticalSection( &pdb->env_db->section );
206 size = HeapSize( GetProcessHeap(), 0, pdb->env_db->environ );
207 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
209 LPSTR pA = pdb->env_db->environ;
210 LPWSTR pW = ret;
211 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
213 LeaveCriticalSection( &pdb->env_db->section );
214 return ret;
218 /***********************************************************************
219 * FreeEnvironmentStringsA (KERNEL32.268)
221 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
223 PDB *pdb = PROCESS_Current();
224 if (ptr != pdb->env_db->environ)
226 SetLastError( ERROR_INVALID_PARAMETER );
227 return FALSE;
229 return TRUE;
233 /***********************************************************************
234 * FreeEnvironmentStringsW (KERNEL32.269)
236 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
238 return HeapFree( GetProcessHeap(), 0, ptr );
242 /***********************************************************************
243 * GetEnvironmentVariableA (KERNEL32.322)
245 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
247 LPCSTR p;
248 INT ret = 0;
249 PDB *pdb = PROCESS_Current();
251 if (!name || !*name)
253 SetLastError( ERROR_INVALID_PARAMETER );
254 return 0;
256 EnterCriticalSection( &pdb->env_db->section );
257 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
259 ret = strlen(p);
260 if (size <= ret)
262 /* If not enough room, include the terminating null
263 * in the returned size and return an empty string */
264 ret++;
265 if (value) *value = '\0';
267 else if (value) strcpy( value, p );
269 LeaveCriticalSection( &pdb->env_db->section );
270 return ret; /* FIXME: SetLastError */
274 /***********************************************************************
275 * GetEnvironmentVariableW (KERNEL32.323)
277 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
279 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
280 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
281 DWORD res = GetEnvironmentVariableA( name, val, size );
282 HeapFree( GetProcessHeap(), 0, name );
283 if (val)
285 lstrcpynAtoW( valW, val, size );
286 HeapFree( GetProcessHeap(), 0, val );
288 return res;
292 /***********************************************************************
293 * SetEnvironmentVariableA (KERNEL32.641)
295 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
297 INT old_size, len, res;
298 LPSTR p, env, new_env;
299 BOOL ret = FALSE;
300 PDB *pdb = PROCESS_Current();
302 EnterCriticalSection( &pdb->env_db->section );
303 env = p = pdb->env_db->environ;
305 /* Find a place to insert the string */
307 res = -1;
308 len = strlen(name);
309 while (*p)
311 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
312 p += strlen(p) + 1;
314 if (!value && !*p) goto done; /* Value to remove doesn't exist */
316 /* Realloc the buffer */
318 len = value ? strlen(name) + strlen(value) + 2 : 0;
319 if (*p) len -= strlen(p) + 1; /* The name already exists */
320 old_size = HeapSize( GetProcessHeap(), 0, env );
321 if (len < 0)
323 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
324 memmove( next + len, next, old_size - (next - env) );
326 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
327 goto done;
328 if (pdb->env_db->env_sel)
329 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
330 p = new_env + (p - env);
331 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
333 /* Set the new string */
335 if (value)
337 strcpy( p, name );
338 strcat( p, "=" );
339 strcat( p, value );
341 pdb->env_db->environ = new_env;
342 ret = TRUE;
344 done:
345 LeaveCriticalSection( &pdb->env_db->section );
346 return ret;
350 /***********************************************************************
351 * SetEnvironmentVariableW (KERNEL32.642)
353 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
355 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
356 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
357 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
358 HeapFree( GetProcessHeap(), 0, nameA );
359 HeapFree( GetProcessHeap(), 0, valueA );
360 return ret;
364 /***********************************************************************
365 * ExpandEnvironmentStringsA (KERNEL32.216)
367 * Note: overlapping buffers are not supported; this is how it should be.
369 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
371 DWORD len, total_size = 1; /* 1 for terminating '\0' */
372 LPCSTR p, var;
373 PDB *pdb = PROCESS_Current();
375 if (!count) dst = NULL;
376 EnterCriticalSection( &pdb->env_db->section );
378 while (*src)
380 if (*src != '%')
382 if ((p = strchr( src, '%' ))) len = p - src;
383 else len = strlen(src);
384 var = src;
385 src += len;
387 else /* we are at the start of a variable */
389 if ((p = strchr( src + 1, '%' )))
391 len = p - src - 1; /* Length of the variable name */
392 if ((var = ENV_FindVariable( pdb->env_db->environ,
393 src + 1, len )))
395 src += len + 2; /* Skip the variable name */
396 len = strlen(var);
398 else
400 var = src; /* Copy original name instead */
401 len += 2;
402 src += len;
405 else /* unfinished variable name, ignore it */
407 var = src;
408 len = strlen(src); /* Copy whole string */
409 src += len;
412 total_size += len;
413 if (dst)
415 if (count < len) len = count;
416 memcpy( dst, var, len );
417 dst += len;
418 count -= len;
421 LeaveCriticalSection( &pdb->env_db->section );
423 /* Null-terminate the string */
424 if (dst)
426 if (!count) dst--;
427 *dst = '\0';
429 return total_size;
433 /***********************************************************************
434 * ExpandEnvironmentStringsW (KERNEL32.217)
436 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
438 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
439 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
440 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
441 if (dstA)
443 lstrcpyAtoW( dst, dstA );
444 HeapFree( GetProcessHeap(), 0, dstA );
446 HeapFree( GetProcessHeap(), 0, srcA );
447 return ret;