Make USER and GDI separate dlls.
[wine.git] / memory / environ.c
blobe6adfa73c80084af9e5738bdfcaf87d9407b3342
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 "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 BOOL ENV_BuildEnvironment(void)
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( GetProcessHeap(), 0, size ))) return FALSE;
82 PROCESS_Current()->env_db->environ = p;
83 PROCESS_Current()->env_db->env_sel = SELECTOR_AllocBlock( p, 0x10000, SEGMENT_DATA,
84 FALSE, FALSE );
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 * GetCommandLineA (KERNEL32.289)
104 LPSTR WINAPI GetCommandLineA(void)
106 return PROCESS_Current()->env_db->cmd_line;
109 /***********************************************************************
110 * GetCommandLineW (KERNEL32.290)
112 LPWSTR WINAPI GetCommandLineW(void)
114 PDB *pdb = PROCESS_Current();
115 EnterCriticalSection( &pdb->env_db->section );
116 if (!pdb->env_db->cmd_lineW)
117 pdb->env_db->cmd_lineW = HEAP_strdupAtoW( GetProcessHeap(), 0,
118 pdb->env_db->cmd_line );
119 LeaveCriticalSection( &pdb->env_db->section );
120 return pdb->env_db->cmd_lineW;
124 /***********************************************************************
125 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
127 LPSTR WINAPI GetEnvironmentStringsA(void)
129 PDB *pdb = PROCESS_Current();
130 return pdb->env_db->environ;
134 /***********************************************************************
135 * GetEnvironmentStringsW (KERNEL32.321)
137 LPWSTR WINAPI GetEnvironmentStringsW(void)
139 INT size;
140 LPWSTR ret;
141 PDB *pdb = PROCESS_Current();
143 EnterCriticalSection( &pdb->env_db->section );
144 size = HeapSize( GetProcessHeap(), 0, pdb->env_db->environ );
145 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
147 LPSTR pA = pdb->env_db->environ;
148 LPWSTR pW = ret;
149 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
151 LeaveCriticalSection( &pdb->env_db->section );
152 return ret;
156 /***********************************************************************
157 * FreeEnvironmentStringsA (KERNEL32.268)
159 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
161 PDB *pdb = PROCESS_Current();
162 if (ptr != pdb->env_db->environ)
164 SetLastError( ERROR_INVALID_PARAMETER );
165 return FALSE;
167 return TRUE;
171 /***********************************************************************
172 * FreeEnvironmentStringsW (KERNEL32.269)
174 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
176 return HeapFree( GetProcessHeap(), 0, ptr );
180 /***********************************************************************
181 * GetEnvironmentVariableA (KERNEL32.322)
183 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
185 LPCSTR p;
186 INT ret = 0;
187 PDB *pdb = PROCESS_Current();
189 if (!name || !*name)
191 SetLastError( ERROR_INVALID_PARAMETER );
192 return 0;
194 EnterCriticalSection( &pdb->env_db->section );
195 if ((p = ENV_FindVariable( pdb->env_db->environ, name, strlen(name) )))
197 ret = strlen(p);
198 if (size <= ret)
200 /* If not enough room, include the terminating null
201 * in the returned size and return an empty string */
202 ret++;
203 if (value) *value = '\0';
205 else if (value) strcpy( value, p );
207 LeaveCriticalSection( &pdb->env_db->section );
208 return ret; /* FIXME: SetLastError */
212 /***********************************************************************
213 * GetEnvironmentVariableW (KERNEL32.323)
215 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
217 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
218 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
219 DWORD res = GetEnvironmentVariableA( name, val, size );
220 HeapFree( GetProcessHeap(), 0, name );
221 if (val)
223 lstrcpynAtoW( valW, val, size );
224 HeapFree( GetProcessHeap(), 0, val );
226 return res;
230 /***********************************************************************
231 * SetEnvironmentVariableA (KERNEL32.641)
233 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
235 INT old_size, len, res;
236 LPSTR p, env, new_env;
237 BOOL ret = FALSE;
238 PDB *pdb = PROCESS_Current();
240 EnterCriticalSection( &pdb->env_db->section );
241 env = p = pdb->env_db->environ;
243 /* Find a place to insert the string */
245 res = -1;
246 len = strlen(name);
247 while (*p)
249 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
250 p += strlen(p) + 1;
252 if (!value && !*p) goto done; /* Value to remove doesn't exist */
254 /* Realloc the buffer */
256 len = value ? strlen(name) + strlen(value) + 2 : 0;
257 if (*p) len -= strlen(p) + 1; /* The name already exists */
258 old_size = HeapSize( GetProcessHeap(), 0, env );
259 if (len < 0)
261 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
262 memmove( next + len, next, old_size - (next - env) );
264 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
265 goto done;
266 if (pdb->env_db->env_sel)
267 SELECTOR_MoveBlock( pdb->env_db->env_sel, new_env );
268 p = new_env + (p - env);
269 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
271 /* Set the new string */
273 if (value)
275 strcpy( p, name );
276 strcat( p, "=" );
277 strcat( p, value );
279 pdb->env_db->environ = new_env;
280 ret = TRUE;
282 done:
283 LeaveCriticalSection( &pdb->env_db->section );
284 return ret;
288 /***********************************************************************
289 * SetEnvironmentVariableW (KERNEL32.642)
291 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
293 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
294 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
295 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
296 HeapFree( GetProcessHeap(), 0, nameA );
297 HeapFree( GetProcessHeap(), 0, valueA );
298 return ret;
302 /***********************************************************************
303 * ExpandEnvironmentStringsA (KERNEL32.216)
305 * Note: overlapping buffers are not supported; this is how it should be.
307 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
309 DWORD len, total_size = 1; /* 1 for terminating '\0' */
310 LPCSTR p, var;
311 PDB *pdb = PROCESS_Current();
313 if (!count) dst = NULL;
314 EnterCriticalSection( &pdb->env_db->section );
316 while (*src)
318 if (*src != '%')
320 if ((p = strchr( src, '%' ))) len = p - src;
321 else len = strlen(src);
322 var = src;
323 src += len;
325 else /* we are at the start of a variable */
327 if ((p = strchr( src + 1, '%' )))
329 len = p - src - 1; /* Length of the variable name */
330 if ((var = ENV_FindVariable( pdb->env_db->environ,
331 src + 1, len )))
333 src += len + 2; /* Skip the variable name */
334 len = strlen(var);
336 else
338 var = src; /* Copy original name instead */
339 len += 2;
340 src += len;
343 else /* unfinished variable name, ignore it */
345 var = src;
346 len = strlen(src); /* Copy whole string */
347 src += len;
350 total_size += len;
351 if (dst)
353 if (count < len) len = count;
354 memcpy( dst, var, len );
355 dst += len;
356 count -= len;
359 LeaveCriticalSection( &pdb->env_db->section );
361 /* Null-terminate the string */
362 if (dst)
364 if (!count) dst--;
365 *dst = '\0';
367 return total_size;
371 /***********************************************************************
372 * ExpandEnvironmentStringsW (KERNEL32.217)
374 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
376 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
377 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
378 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
379 if (dstA)
381 lstrcpyAtoW( dst, dstA );
382 HeapFree( GetProcessHeap(), 0, dstA );
384 HeapFree( GetProcessHeap(), 0, srcA );
385 return ret;