Shell parameters (config.sys) and ANSI support stubs.
[wine/multimedia.git] / memory / environ.c
blob7fa10244d27913fa61f380e6d59e07d13bd6ba83
1 /*
2 * Process environment management
4 * Copyright 1996, 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "process.h"
11 #include "heap.h"
12 #include "selectors.h"
13 #include "winerror.h"
15 /* Format of an environment block:
16 * ASCIIZ string 1 (xx=yy format)
17 * ...
18 * ASCIIZ string n
19 * BYTE 0
20 * WORD 1
21 * ASCIIZ program name (e.g. C:\WINDOWS\SYSTEM\KRNL386.EXE)
23 * Notes:
24 * - contrary to Microsoft docs, the environment strings do not appear
25 * to be sorted on Win95 (although they are on NT); so we don't bother
26 * to sort them either.
29 static const char ENV_program_name[] = "C:\\WINDOWS\\SYSTEM\\KRNL386.EXE";
31 /* Maximum length of a Win16 environment string (including NULL) */
32 #define MAX_WIN16_LEN 128
34 /* Extra bytes to reserve at the end of an environment */
35 #define EXTRA_ENV_SIZE (sizeof(BYTE) + sizeof(WORD) + sizeof(ENV_program_name))
37 /* Fill the extra bytes with the program name and stuff */
38 #define FILL_EXTRA_ENV(p) \
39 *(p) = '\0'; \
40 PUT_WORD( (p) + 1, 1 ); \
41 strcpy( (p) + 3, ENV_program_name );
44 /***********************************************************************
45 * ENV_FindVariable
47 * Find a variable in the environment and return a pointer to the value.
48 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
50 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT32 len )
52 while (*env)
54 if (!lstrncmpi32A( name, env, len ) && (env[len] == '='))
55 return env + len + 1;
56 env += strlen(env) + 1;
58 return NULL;
62 /***********************************************************************
63 * ENV_BuildEnvironment
65 * Build the environment for the initial process
67 BOOL32 ENV_BuildEnvironment( PDB32 *pdb )
69 extern char **environ;
70 LPSTR p, *e;
71 int size;
73 /* Compute the total size of the Unix environment */
75 size = EXTRA_ENV_SIZE;
76 for (e = environ; *e; e++) size += strlen(*e) + 1;
78 /* Now allocate the environment */
80 if (!(p = HeapAlloc( SystemHeap, 0, size ))) return FALSE;
81 pdb->env_db->environ = p;
83 /* And fill it with the Unix environment */
85 for (e = environ; *e; e++)
87 strcpy( p, *e );
88 p += strlen(p) + 1;
91 /* Now add the program name */
93 FILL_EXTRA_ENV( p );
94 return TRUE;
98 /***********************************************************************
99 * ENV_InheritEnvironment
101 * Make a process inherit the environment from its parent or from an
102 * explicit environment.
104 BOOL32 ENV_InheritEnvironment( PDB32 *pdb, LPCSTR env )
106 DWORD size;
107 LPCSTR src;
108 LPSTR dst;
110 /* FIXME: should lock the parent environment */
111 if (!env) env = pdb->parent->env_db->environ;
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( pdb->heap, 0,
129 size + EXTRA_ENV_SIZE )))
130 return FALSE;
131 pdb->env_db->env_sel = SELECTOR_AllocBlock( pdb->env_db->environ,
132 0x10000, SEGMENT_DATA,
133 FALSE, FALSE );
134 src = env;
135 dst = pdb->env_db->environ;
136 while (*src)
138 if (pdb->flags & PDB32_WIN16_PROC)
139 lstrcpyn32A( dst, src, MAX_WIN16_LEN );
140 else
141 strcpy( dst, src );
142 src += strlen(src) + 1;
143 dst += strlen(dst) + 1;
145 FILL_EXTRA_ENV( dst );
146 return TRUE;
150 /***********************************************************************
151 * ENV_FreeEnvironment
153 * Free a process environment.
155 void ENV_FreeEnvironment( PDB32 *pdb )
157 if (!pdb->env_db) return;
158 if (pdb->env_db->env_sel) SELECTOR_FreeBlock( pdb->env_db->env_sel, 1 );
159 DeleteCriticalSection( &pdb->env_db->section );
160 HeapFree( pdb->heap, 0, pdb->env_db );
164 /***********************************************************************
165 * GetCommandLine32A (KERNEL32.289)
167 LPCSTR WINAPI GetCommandLine32A(void)
169 return PROCESS_Current()->env_db->cmd_line;
172 /***********************************************************************
173 * GetCommandLine32W (KERNEL32.290)
175 LPCWSTR WINAPI GetCommandLine32W(void)
177 PDB32 *pdb = PROCESS_Current();
178 EnterCriticalSection( &pdb->env_db->section );
179 if (!pdb->env_db->cmd_lineW)
180 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( pdb->heap, 0,
181 pdb->env_db->cmd_line );
182 LeaveCriticalSection( &pdb->env_db->section );
183 return pdb->env_db->cmd_lineW;
187 /***********************************************************************
188 * GetEnvironmentStrings32A (KERNEL32.319) (KERNEL32.320)
190 LPSTR WINAPI GetEnvironmentStrings32A(void)
192 PDB32 *pdb = PROCESS_Current();
193 return pdb->env_db->environ;
197 /***********************************************************************
198 * GetEnvironmentStrings32W (KERNEL32.321)
200 LPWSTR WINAPI GetEnvironmentStrings32W(void)
202 INT32 size;
203 LPWSTR ret;
204 PDB32 *pdb = PROCESS_Current();
206 EnterCriticalSection( &pdb->env_db->section );
207 size = HeapSize( pdb->heap, 0, pdb->env_db->environ );
208 if ((ret = HeapAlloc( pdb->heap, 0, size * sizeof(WCHAR) )) != NULL)
210 LPSTR pA = pdb->env_db->environ;
211 LPWSTR pW = ret;
212 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
214 LeaveCriticalSection( &pdb->env_db->section );
215 return ret;
219 /***********************************************************************
220 * FreeEnvironmentStrings32A (KERNEL32.268)
222 BOOL32 WINAPI FreeEnvironmentStrings32A( LPSTR ptr )
224 PDB32 *pdb = PROCESS_Current();
225 if (ptr != pdb->env_db->environ)
227 SetLastError( ERROR_INVALID_PARAMETER );
228 return FALSE;
230 return TRUE;
234 /***********************************************************************
235 * FreeEnvironmentStrings32W (KERNEL32.269)
237 BOOL32 WINAPI FreeEnvironmentStrings32W( LPWSTR ptr )
239 return HeapFree( GetProcessHeap(), 0, ptr );
243 /***********************************************************************
244 * GetEnvironmentVariable32A (KERNEL32.322)
246 DWORD WINAPI GetEnvironmentVariable32A( LPCSTR name, LPSTR value, DWORD size )
248 LPCSTR p;
249 INT32 ret = 0;
250 PDB32 *pdb = PROCESS_Current();
252 if (!name || !*name)
254 SetLastError( ERROR_INVALID_PARAMETER );
255 return 0;
257 EnterCriticalSection( &pdb->env_db->section );
258 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
260 ret = strlen(p);
261 if (size <= ret)
263 /* If not enough room, include the terminating null
264 * in the returned size and return an empty string */
265 ret++;
266 if (value) *value = '\0';
268 else if (value) strcpy( value, p );
270 LeaveCriticalSection( &pdb->env_db->section );
271 return ret; /* FIXME: SetLastError */
275 /***********************************************************************
276 * GetEnvironmentVariable32W (KERNEL32.323)
278 DWORD WINAPI GetEnvironmentVariable32W( LPCWSTR nameW, LPWSTR valW, DWORD size)
280 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
281 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
282 DWORD res = GetEnvironmentVariable32A( name, val, size );
283 HeapFree( GetProcessHeap(), 0, name );
284 if (val)
286 lstrcpynAtoW( valW, val, size );
287 HeapFree( GetProcessHeap(), 0, val );
289 return res;
293 /***********************************************************************
294 * SetEnvironmentVariable32A (KERNEL32.641)
296 BOOL32 WINAPI SetEnvironmentVariable32A( LPCSTR name, LPCSTR value )
298 INT32 old_size, len, res;
299 LPSTR p, env, new_env;
300 BOOL32 ret = FALSE;
301 PDB32 *pdb = PROCESS_Current();
303 EnterCriticalSection( &pdb->env_db->section );
304 env = p = pdb->env_db->environ;
306 /* Find a place to insert the string */
308 res = -1;
309 len = strlen(name);
310 while (*p)
312 if (!lstrncmpi32A( name, p, len ) && (p[len] == '=')) break;
313 p += strlen(p) + 1;
315 if (!value && !*p) goto done; /* Value to remove doesn't exist */
317 /* Realloc the buffer */
319 len = value ? strlen(name) + strlen(value) + 2 : 0;
320 if (*p) len -= strlen(p) + 1; /* The name already exists */
321 old_size = HeapSize( pdb->heap, 0, env );
322 if (len < 0)
324 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
325 memmove( next + len, next, old_size - (next - env) );
327 if (!(new_env = HeapReAlloc( pdb->heap, 0, env, old_size + len )))
328 goto done;
329 if (pdb->env_db->env_sel)
330 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
331 p = new_env + (p - env);
332 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
334 /* Set the new string */
336 if (value)
338 strcpy( p, name );
339 strcat( p, "=" );
340 strcat( p, value );
342 pdb->env_db->environ = new_env;
343 ret = TRUE;
345 done:
346 LeaveCriticalSection( &pdb->env_db->section );
347 return ret;
351 /***********************************************************************
352 * SetEnvironmentVariable32W (KERNEL32.642)
354 BOOL32 WINAPI SetEnvironmentVariable32W( LPCWSTR name, LPCWSTR value )
356 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
357 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
358 BOOL32 ret = SetEnvironmentVariable32A( nameA, valueA );
359 HeapFree( GetProcessHeap(), 0, nameA );
360 HeapFree( GetProcessHeap(), 0, valueA );
361 return ret;
365 /***********************************************************************
366 * ExpandEnvironmentStrings32A (KERNEL32.216)
368 * Note: overlapping buffers are not supported; this is how it should be.
370 DWORD WINAPI ExpandEnvironmentStrings32A( LPCSTR src, LPSTR dst, DWORD count )
372 DWORD len, total_size = 1; /* 1 for terminating '\0' */
373 LPCSTR p, var;
374 PDB32 *pdb = PROCESS_Current();
376 if (!count) dst = NULL;
377 EnterCriticalSection( &pdb->env_db->section );
379 while (*src)
381 if (*src != '%')
383 if ((p = strchr( src, '%' ))) len = p - src;
384 else len = strlen(src);
385 var = src;
386 src += len;
388 else /* we are at the start of a variable */
390 if ((p = strchr( src + 1, '%' )))
392 len = p - src - 1; /* Length of the variable name */
393 if ((var = ENV_FindVariable( pdb->env_db->environ,
394 src + 1, len )))
396 src += len + 2; /* Skip the variable name */
397 len = strlen(var);
399 else
401 var = src; /* Copy original name instead */
402 len += 2;
403 src += len;
406 else /* unfinished variable name, ignore it */
408 var = src;
409 len = strlen(src); /* Copy whole string */
410 src += len;
413 total_size += len;
414 if (dst)
416 if (count < len) len = count;
417 memcpy( dst, var, len );
418 dst += len;
419 count -= len;
422 LeaveCriticalSection( &pdb->env_db->section );
424 /* Null-terminate the string */
425 if (dst)
427 if (!count) dst--;
428 *dst = '\0';
430 return total_size;
434 /***********************************************************************
435 * ExpandEnvironmentStrings32W (KERNEL32.217)
437 DWORD WINAPI ExpandEnvironmentStrings32W( LPCWSTR src, LPWSTR dst, DWORD len )
439 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
440 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
441 DWORD ret = ExpandEnvironmentStrings32A( srcA, dstA, len );
442 if (dstA)
444 lstrcpyAtoW( dst, dstA );
445 HeapFree( GetProcessHeap(), 0, dstA );
447 HeapFree( GetProcessHeap(), 0, srcA );
448 return ret;