Winspool DocumentProperties and DeviceCapabilities should now work on
[wine.git] / memory / environ.c
blob2893cde405ce397f5af6ec78ee157c66d7faf038
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,
135 size + EXTRA_ENV_SIZE )))
136 return FALSE;
137 pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
138 0x10000, SEGMENT_DATA,
139 FALSE, FALSE );
140 src = env;
141 dst = pdb->env_db->environ;
142 while (*src)
144 if (pdb->flags & PDB32_WIN16_PROC)
145 lstrcpynA( dst, src, MAX_WIN16_LEN );
146 else
147 strcpy( dst, src );
148 src += strlen(src) + 1;
149 dst += strlen(dst) + 1;
151 FILL_EXTRA_ENV( dst );
152 return TRUE;
156 /***********************************************************************
157 * ENV_FreeEnvironment
159 * Free a process environment.
161 void ENV_FreeEnvironment( PDB *pdb )
163 if (!pdb->env_db) return;
164 if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
165 DeleteCriticalSection( &pdb->env_db->section );
166 HeapFree( pdb->heap, 0, pdb->env_db );
170 /***********************************************************************
171 * GetCommandLine32A (KERNEL32.289)
173 LPCSTR WINAPI GetCommandLineA(void)
175 return PROCESS_Current()->env_db->cmd_line;
178 /***********************************************************************
179 * GetCommandLine32W (KERNEL32.290)
181 LPCWSTR WINAPI GetCommandLineW(void)
183 PDB *pdb = PROCESS_Current();
184 EnterCriticalSection( &pdb->env_db->section );
185 if (!pdb->env_db->cmd_lineW)
186 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( pdb->heap, 0,
187 pdb->env_db->cmd_line );
188 LeaveCriticalSection( &pdb->env_db->section );
189 return pdb->env_db->cmd_lineW;
193 /***********************************************************************
194 * GetEnvironmentStrings32A (KERNEL32.319) (KERNEL32.320)
196 LPSTR WINAPI GetEnvironmentStringsA(void)
198 PDB *pdb = PROCESS_Current();
199 return pdb->env_db->environ;
203 /***********************************************************************
204 * GetEnvironmentStrings32W (KERNEL32.321)
206 LPWSTR WINAPI GetEnvironmentStringsW(void)
208 INT size;
209 LPWSTR ret;
210 PDB *pdb = PROCESS_Current();
212 EnterCriticalSection( &pdb->env_db->section );
213 size = HeapSize( pdb->heap, 0, pdb->env_db->environ );
214 if ((ret = HeapAlloc( pdb->heap, 0, size * sizeof(WCHAR) )) != NULL)
216 LPSTR pA = pdb->env_db->environ;
217 LPWSTR pW = ret;
218 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
220 LeaveCriticalSection( &pdb->env_db->section );
221 return ret;
225 /***********************************************************************
226 * FreeEnvironmentStrings32A (KERNEL32.268)
228 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
230 PDB *pdb = PROCESS_Current();
231 if (ptr != pdb->env_db->environ)
233 SetLastError( ERROR_INVALID_PARAMETER );
234 return FALSE;
236 return TRUE;
240 /***********************************************************************
241 * FreeEnvironmentStrings32W (KERNEL32.269)
243 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
245 return HeapFree( GetProcessHeap(), 0, ptr );
249 /***********************************************************************
250 * GetEnvironmentVariable32A (KERNEL32.322)
252 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
254 LPCSTR p;
255 INT ret = 0;
256 PDB *pdb = PROCESS_Current();
258 if (!name || !*name)
260 SetLastError( ERROR_INVALID_PARAMETER );
261 return 0;
263 EnterCriticalSection( &pdb->env_db->section );
264 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
266 ret = strlen(p);
267 if (size <= ret)
269 /* If not enough room, include the terminating null
270 * in the returned size and return an empty string */
271 ret++;
272 if (value) *value = '\0';
274 else if (value) strcpy( value, p );
276 LeaveCriticalSection( &pdb->env_db->section );
277 return ret; /* FIXME: SetLastError */
281 /***********************************************************************
282 * GetEnvironmentVariable32W (KERNEL32.323)
284 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
286 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
287 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
288 DWORD res = GetEnvironmentVariableA( name, val, size );
289 HeapFree( GetProcessHeap(), 0, name );
290 if (val)
292 lstrcpynAtoW( valW, val, size );
293 HeapFree( GetProcessHeap(), 0, val );
295 return res;
299 /***********************************************************************
300 * SetEnvironmentVariable32A (KERNEL32.641)
302 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
304 INT old_size, len, res;
305 LPSTR p, env, new_env;
306 BOOL ret = FALSE;
307 PDB *pdb = PROCESS_Current();
309 EnterCriticalSection( &pdb->env_db->section );
310 env = p = pdb->env_db->environ;
312 /* Find a place to insert the string */
314 res = -1;
315 len = strlen(name);
316 while (*p)
318 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
319 p += strlen(p) + 1;
321 if (!value && !*p) goto done; /* Value to remove doesn't exist */
323 /* Realloc the buffer */
325 len = value ? strlen(name) + strlen(value) + 2 : 0;
326 if (*p) len -= strlen(p) + 1; /* The name already exists */
327 old_size = HeapSize( pdb->heap, 0, env );
328 if (len < 0)
330 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
331 memmove( next + len, next, old_size - (next - env) );
333 if (!(new_env = HeapReAlloc( pdb->heap, 0, env, old_size + len )))
334 goto done;
335 if (pdb->env_db->env_sel)
336 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
337 p = new_env + (p - env);
338 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
340 /* Set the new string */
342 if (value)
344 strcpy( p, name );
345 strcat( p, "=" );
346 strcat( p, value );
348 pdb->env_db->environ = new_env;
349 ret = TRUE;
351 done:
352 LeaveCriticalSection( &pdb->env_db->section );
353 return ret;
357 /***********************************************************************
358 * SetEnvironmentVariable32W (KERNEL32.642)
360 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
362 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
363 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
364 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
365 HeapFree( GetProcessHeap(), 0, nameA );
366 HeapFree( GetProcessHeap(), 0, valueA );
367 return ret;
371 /***********************************************************************
372 * ExpandEnvironmentStrings32A (KERNEL32.216)
374 * Note: overlapping buffers are not supported; this is how it should be.
376 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
378 DWORD len, total_size = 1; /* 1 for terminating '\0' */
379 LPCSTR p, var;
380 PDB *pdb = PROCESS_Current();
382 if (!count) dst = NULL;
383 EnterCriticalSection( &pdb->env_db->section );
385 while (*src)
387 if (*src != '%')
389 if ((p = strchr( src, '%' ))) len = p - src;
390 else len = strlen(src);
391 var = src;
392 src += len;
394 else /* we are at the start of a variable */
396 if ((p = strchr( src + 1, '%' )))
398 len = p - src - 1; /* Length of the variable name */
399 if ((var = ENV_FindVariable( pdb->env_db->environ,
400 src + 1, len )))
402 src += len + 2; /* Skip the variable name */
403 len = strlen(var);
405 else
407 var = src; /* Copy original name instead */
408 len += 2;
409 src += len;
412 else /* unfinished variable name, ignore it */
414 var = src;
415 len = strlen(src); /* Copy whole string */
416 src += len;
419 total_size += len;
420 if (dst)
422 if (count < len) len = count;
423 memcpy( dst, var, len );
424 dst += len;
425 count -= len;
428 LeaveCriticalSection( &pdb->env_db->section );
430 /* Null-terminate the string */
431 if (dst)
433 if (!count) dst--;
434 *dst = '\0';
436 return total_size;
440 /***********************************************************************
441 * ExpandEnvironmentStrings32W (KERNEL32.217)
443 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
445 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
446 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
447 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
448 if (dstA)
450 lstrcpyAtoW( dst, dstA );
451 HeapFree( GetProcessHeap(), 0, dstA );
453 HeapFree( GetProcessHeap(), 0, srcA );
454 return ret;