Fixed DGA / DGA 2 palette creation.
[wine/hacks.git] / memory / environ.c
blob2b91ac56da23612848285b5f36ec5170653f50e2
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 );
44 STARTUPINFOA current_startupinfo =
46 sizeof(STARTUPINFOA), /* cb */
47 0, /* lpReserved */
48 0, /* lpDesktop */
49 0, /* lpTitle */
50 0, /* dwX */
51 0, /* dwY */
52 0, /* dwXSize */
53 0, /* dwYSize */
54 0, /* dwXCountChars */
55 0, /* dwYCountChars */
56 0, /* dwFillAttribute */
57 0, /* dwFlags */
58 0, /* wShowWindow */
59 0, /* cbReserved2 */
60 0, /* lpReserved2 */
61 0, /* hStdInput */
62 0, /* hStdOutput */
63 0 /* hStdError */
66 ENVDB current_envdb =
68 0, /* environ */
69 0, /* unknown1 */
70 0, /* cmd_line */
71 0, /* cur_dir */
72 &current_startupinfo, /* startup_info */
73 0, /* hStdin */
74 0, /* hStdout */
75 0, /* hStderr */
76 0, /* unknown2 */
77 0, /* inherit_console */
78 0, /* break_type */
79 0, /* break_sem */
80 0, /* break_event */
81 0, /* break_thread */
82 0, /* break_handlers */
83 CRITICAL_SECTION_INIT, /* section */
84 0, /* cmd_lineW */
85 0 /* env_sel */
88 /***********************************************************************
89 * ENV_FindVariable
91 * Find a variable in the environment and return a pointer to the value.
92 * Helper function for GetEnvironmentVariable and ExpandEnvironmentStrings.
94 static LPCSTR ENV_FindVariable( LPCSTR env, LPCSTR name, INT len )
96 while (*env)
98 if (!lstrncmpiA( name, env, len ) && (env[len] == '='))
99 return env + len + 1;
100 env += strlen(env) + 1;
102 return NULL;
106 /***********************************************************************
107 * ENV_BuildEnvironment
109 * Build the environment for the initial process
111 BOOL ENV_BuildEnvironment(void)
113 extern char **environ;
114 LPSTR p, *e;
115 int size;
117 /* Compute the total size of the Unix environment */
119 size = EXTRA_ENV_SIZE;
120 for (e = environ; *e; e++) size += strlen(*e) + 1;
122 /* Now allocate the environment */
124 if (!(p = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
125 current_envdb.environ = p;
126 current_envdb.env_sel = SELECTOR_AllocBlock( p, 0x10000, SEGMENT_DATA, FALSE, FALSE );
128 /* And fill it with the Unix environment */
130 for (e = environ; *e; e++)
132 strcpy( p, *e );
133 p += strlen(p) + 1;
136 /* Now add the program name */
138 FILL_EXTRA_ENV( p );
139 return TRUE;
143 /***********************************************************************
144 * GetCommandLineA (KERNEL32.289)
146 LPSTR WINAPI GetCommandLineA(void)
148 return current_envdb.cmd_line;
151 /***********************************************************************
152 * GetCommandLineW (KERNEL32.290)
154 LPWSTR WINAPI GetCommandLineW(void)
156 EnterCriticalSection( &current_envdb.section );
157 if (!current_envdb.cmd_lineW)
158 current_envdb.cmd_lineW = HEAP_strdupAtoW( GetProcessHeap(), 0,
159 current_envdb.cmd_line );
160 LeaveCriticalSection( &current_envdb.section );
161 return current_envdb.cmd_lineW;
165 /***********************************************************************
166 * GetEnvironmentStringsA (KERNEL32.319) (KERNEL32.320)
168 LPSTR WINAPI GetEnvironmentStringsA(void)
170 return current_envdb.environ;
174 /***********************************************************************
175 * GetEnvironmentStringsW (KERNEL32.321)
177 LPWSTR WINAPI GetEnvironmentStringsW(void)
179 INT size;
180 LPWSTR ret;
182 EnterCriticalSection( &current_envdb.section );
183 size = HeapSize( GetProcessHeap(), 0, current_envdb.environ );
184 if ((ret = HeapAlloc( GetProcessHeap(), 0, size * sizeof(WCHAR) )) != NULL)
186 LPSTR pA = current_envdb.environ;
187 LPWSTR pW = ret;
188 while (size--) *pW++ = (WCHAR)(BYTE)*pA++;
190 LeaveCriticalSection( &current_envdb.section );
191 return ret;
195 /***********************************************************************
196 * FreeEnvironmentStringsA (KERNEL32.268)
198 BOOL WINAPI FreeEnvironmentStringsA( LPSTR ptr )
200 if (ptr != current_envdb.environ)
202 SetLastError( ERROR_INVALID_PARAMETER );
203 return FALSE;
205 return TRUE;
209 /***********************************************************************
210 * FreeEnvironmentStringsW (KERNEL32.269)
212 BOOL WINAPI FreeEnvironmentStringsW( LPWSTR ptr )
214 return HeapFree( GetProcessHeap(), 0, ptr );
218 /***********************************************************************
219 * GetEnvironmentVariableA (KERNEL32.322)
221 DWORD WINAPI GetEnvironmentVariableA( LPCSTR name, LPSTR value, DWORD size )
223 LPCSTR p;
224 INT ret = 0;
226 if (!name || !*name)
228 SetLastError( ERROR_INVALID_PARAMETER );
229 return 0;
231 EnterCriticalSection( &current_envdb.section );
232 if ((p = ENV_FindVariable( current_envdb.environ, name, strlen(name) )))
234 ret = strlen(p);
235 if (size <= ret)
237 /* If not enough room, include the terminating null
238 * in the returned size and return an empty string */
239 ret++;
240 if (value) *value = '\0';
242 else if (value) strcpy( value, p );
244 LeaveCriticalSection( &current_envdb.section );
245 return ret; /* FIXME: SetLastError */
249 /***********************************************************************
250 * GetEnvironmentVariableW (KERNEL32.323)
252 DWORD WINAPI GetEnvironmentVariableW( LPCWSTR nameW, LPWSTR valW, DWORD size)
254 LPSTR name = HEAP_strdupWtoA( GetProcessHeap(), 0, nameW );
255 LPSTR val = valW ? HeapAlloc( GetProcessHeap(), 0, size ) : NULL;
256 DWORD res = GetEnvironmentVariableA( name, val, size );
257 HeapFree( GetProcessHeap(), 0, name );
258 if (val)
260 lstrcpynAtoW( valW, val, size );
261 HeapFree( GetProcessHeap(), 0, val );
263 return res;
267 /***********************************************************************
268 * SetEnvironmentVariableA (KERNEL32.641)
270 BOOL WINAPI SetEnvironmentVariableA( LPCSTR name, LPCSTR value )
272 INT old_size, len, res;
273 LPSTR p, env, new_env;
274 BOOL ret = FALSE;
276 EnterCriticalSection( &current_envdb.section );
277 env = p = current_envdb.environ;
279 /* Find a place to insert the string */
281 res = -1;
282 len = strlen(name);
283 while (*p)
285 if (!lstrncmpiA( name, p, len ) && (p[len] == '=')) break;
286 p += strlen(p) + 1;
288 if (!value && !*p) goto done; /* Value to remove doesn't exist */
290 /* Realloc the buffer */
292 len = value ? strlen(name) + strlen(value) + 2 : 0;
293 if (*p) len -= strlen(p) + 1; /* The name already exists */
294 old_size = HeapSize( GetProcessHeap(), 0, env );
295 if (len < 0)
297 LPSTR next = p + strlen(p) + 1; /* We know there is a next one */
298 memmove( next + len, next, old_size - (next - env) );
300 if (!(new_env = HeapReAlloc( GetProcessHeap(), 0, env, old_size + len )))
301 goto done;
302 if (current_envdb.env_sel)
303 SELECTOR_MoveBlock( current_envdb.env_sel, new_env );
304 p = new_env + (p - env);
305 if (len > 0) memmove( p + len, p, old_size - (p - new_env) );
307 /* Set the new string */
309 if (value)
311 strcpy( p, name );
312 strcat( p, "=" );
313 strcat( p, value );
315 current_envdb.environ = new_env;
316 ret = TRUE;
318 done:
319 LeaveCriticalSection( &current_envdb.section );
320 return ret;
324 /***********************************************************************
325 * SetEnvironmentVariableW (KERNEL32.642)
327 BOOL WINAPI SetEnvironmentVariableW( LPCWSTR name, LPCWSTR value )
329 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
330 LPSTR valueA = HEAP_strdupWtoA( GetProcessHeap(), 0, value );
331 BOOL ret = SetEnvironmentVariableA( nameA, valueA );
332 HeapFree( GetProcessHeap(), 0, nameA );
333 HeapFree( GetProcessHeap(), 0, valueA );
334 return ret;
338 /***********************************************************************
339 * ExpandEnvironmentStringsA (KERNEL32.216)
341 * Note: overlapping buffers are not supported; this is how it should be.
343 DWORD WINAPI ExpandEnvironmentStringsA( LPCSTR src, LPSTR dst, DWORD count )
345 DWORD len, total_size = 1; /* 1 for terminating '\0' */
346 LPCSTR p, var;
348 if (!count) dst = NULL;
349 EnterCriticalSection( &current_envdb.section );
351 while (*src)
353 if (*src != '%')
355 if ((p = strchr( src, '%' ))) len = p - src;
356 else len = strlen(src);
357 var = src;
358 src += len;
360 else /* we are at the start of a variable */
362 if ((p = strchr( src + 1, '%' )))
364 len = p - src - 1; /* Length of the variable name */
365 if ((var = ENV_FindVariable( current_envdb.environ,
366 src + 1, len )))
368 src += len + 2; /* Skip the variable name */
369 len = strlen(var);
371 else
373 var = src; /* Copy original name instead */
374 len += 2;
375 src += len;
378 else /* unfinished variable name, ignore it */
380 var = src;
381 len = strlen(src); /* Copy whole string */
382 src += len;
385 total_size += len;
386 if (dst)
388 if (count < len) len = count;
389 memcpy( dst, var, len );
390 dst += len;
391 count -= len;
394 LeaveCriticalSection( &current_envdb.section );
396 /* Null-terminate the string */
397 if (dst)
399 if (!count) dst--;
400 *dst = '\0';
402 return total_size;
406 /***********************************************************************
407 * ExpandEnvironmentStringsW (KERNEL32.217)
409 DWORD WINAPI ExpandEnvironmentStringsW( LPCWSTR src, LPWSTR dst, DWORD len )
411 LPSTR srcA = HEAP_strdupWtoA( GetProcessHeap(), 0, src );
412 LPSTR dstA = dst ? HeapAlloc( GetProcessHeap(), 0, len ) : NULL;
413 DWORD ret = ExpandEnvironmentStringsA( srcA, dstA, len );
414 if (dstA)
416 lstrcpyAtoW( dst, dstA );
417 HeapFree( GetProcessHeap(), 0, dstA );
419 HeapFree( GetProcessHeap(), 0, srcA );
420 return ret;
424 /***********************************************************************
425 * GetStdHandle (KERNEL32.276)
427 HANDLE WINAPI GetStdHandle( DWORD std_handle )
429 switch(std_handle)
431 case STD_INPUT_HANDLE: return current_envdb.hStdin;
432 case STD_OUTPUT_HANDLE: return current_envdb.hStdout;
433 case STD_ERROR_HANDLE: return current_envdb.hStderr;
435 SetLastError( ERROR_INVALID_PARAMETER );
436 return INVALID_HANDLE_VALUE;
440 /***********************************************************************
441 * SetStdHandle (KERNEL32.506)
443 BOOL WINAPI SetStdHandle( DWORD std_handle, HANDLE handle )
445 switch(std_handle)
447 case STD_INPUT_HANDLE: current_envdb.hStdin = handle; return TRUE;
448 case STD_OUTPUT_HANDLE: current_envdb.hStdout = handle; return TRUE;
449 case STD_ERROR_HANDLE: current_envdb.hStderr = handle; return TRUE;
451 SetLastError( ERROR_INVALID_PARAMETER );
452 return FALSE;
456 /***********************************************************************
457 * GetStartupInfoA (KERNEL32.273)
459 VOID WINAPI GetStartupInfoA( LPSTARTUPINFOA info )
461 *info = current_startupinfo;
465 /***********************************************************************
466 * GetStartupInfoW (KERNEL32.274)
468 VOID WINAPI GetStartupInfoW( LPSTARTUPINFOW info )
470 info->cb = sizeof(STARTUPINFOW);
471 info->dwX = current_startupinfo.dwX;
472 info->dwY = current_startupinfo.dwY;
473 info->dwXSize = current_startupinfo.dwXSize;
474 info->dwXCountChars = current_startupinfo.dwXCountChars;
475 info->dwYCountChars = current_startupinfo.dwYCountChars;
476 info->dwFillAttribute = current_startupinfo.dwFillAttribute;
477 info->dwFlags = current_startupinfo.dwFlags;
478 info->wShowWindow = current_startupinfo.wShowWindow;
479 info->cbReserved2 = current_startupinfo.cbReserved2;
480 info->lpReserved2 = current_startupinfo.lpReserved2;
481 info->hStdInput = current_startupinfo.hStdInput;
482 info->hStdOutput = current_startupinfo.hStdOutput;
483 info->hStdError = current_startupinfo.hStdError;
484 info->lpReserved = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpReserved );
485 info->lpDesktop = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpDesktop );
486 info->lpTitle = HEAP_strdupAtoW (GetProcessHeap(), 0, current_startupinfo.lpTitle );