Fixed header dependencies to be fully compatible with the Windows
[wine/multimedia.git] / dlls / ntdll / env.c
blob6486440c00a10786c211275183dcb5002cff3110
1 /*
2 * Ntdll environment functions
4 * Copyright 1996, 1998 Alexandre Julliard
5 * Copyright 2003 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winreg.h"
29 #include "winternl.h"
30 #include "wine/unicode.h"
31 #include "wine/debug.h"
32 #include "ntdll_misc.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(environ);
36 /******************************************************************************
37 * RtlCreateEnvironment [NTDLL.@]
39 NTSTATUS WINAPI RtlCreateEnvironment(BOOLEAN inherit, PWSTR* env)
41 NTSTATUS nts;
43 TRACE("(%u,%p)!\n", inherit, env);
45 if (inherit)
47 MEMORY_BASIC_INFORMATION mbi;
49 RtlAcquirePebLock();
51 nts = NtQueryVirtualMemory(NtCurrentProcess(), ntdll_get_process_pmts()->Environment,
52 0, &mbi, sizeof(mbi), NULL);
53 if (nts == STATUS_SUCCESS)
55 *env = NULL;
56 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &mbi.RegionSize,
57 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
58 if (nts == STATUS_SUCCESS)
59 memcpy(*env, ntdll_get_process_pmts()->Environment, mbi.RegionSize);
60 else *env = NULL;
62 RtlReleasePebLock();
64 else
66 ULONG size = 1;
67 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)env, 0, &size,
68 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
69 if (nts == STATUS_SUCCESS)
70 memset(*env, 0, size);
73 return nts;
76 /******************************************************************************
77 * RtlDestroyEnvironment [NTDLL.@]
79 NTSTATUS WINAPI RtlDestroyEnvironment(PWSTR env)
81 ULONG size = 0;
83 TRACE("(%p)!\n", env);
85 return NtFreeVirtualMemory(NtCurrentProcess(), (void**)&env, &size, MEM_RELEASE);
88 static LPCWSTR ENV_FindVariable(PCWSTR var, PCWSTR name, unsigned namelen)
90 for (; *var; var += strlenW(var) + 1)
92 /* match var names, but avoid setting a var with a name including a '='
93 * (a starting '=' is valid though)
95 if (strncmpiW(var, name, namelen) == 0 && var[namelen] == '=' &&
96 strchrW(var + 1, '=') == var + namelen)
98 return var + namelen + 1;
101 return NULL;
104 /******************************************************************
105 * RtlQueryEnvironmentVariable_U [NTDLL.@]
107 * NOTES: when the buffer is too small, the string is not written, but if the
108 * terminating null char is the only char that cannot be written, then
109 * all chars (except the null) are written and success is returned
110 * (behavior of Win2k at least)
112 NTSTATUS WINAPI RtlQueryEnvironmentVariable_U(PWSTR env,
113 PUNICODE_STRING name,
114 PUNICODE_STRING value)
116 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
117 PCWSTR var;
118 unsigned namelen;
120 TRACE("%s %s %p\n", debugstr_w(env), debugstr_w(name->Buffer), value);
122 value->Length = 0;
123 namelen = name->Length / sizeof(WCHAR);
124 if (!namelen) return nts;
126 if (!env)
128 RtlAcquirePebLock();
129 var = ntdll_get_process_pmts()->Environment;
131 else var = env;
133 var = ENV_FindVariable(var, name->Buffer, namelen);
134 if (var != NULL)
136 value->Length = strlenW(var) * sizeof(WCHAR);
138 if (value->Length <= value->MaximumLength)
140 memmove(value->Buffer, var, min(value->Length + sizeof(WCHAR), value->MaximumLength));
141 nts = STATUS_SUCCESS;
143 else nts = STATUS_BUFFER_TOO_SMALL;
146 if (!env) RtlReleasePebLock();
148 return nts;
151 /******************************************************************
152 * RtlSetCurrentEnvironment [NTDLL.@]
155 void WINAPI RtlSetCurrentEnvironment(PWSTR new_env, PWSTR* old_env)
157 TRACE("(%p %p)\n", new_env, old_env);
159 RtlAcquirePebLock();
161 if (old_env) *old_env = ntdll_get_process_pmts()->Environment;
162 ntdll_get_process_pmts()->Environment = new_env;
164 RtlReleasePebLock();
168 /******************************************************************************
169 * RtlSetEnvironmentVariable [NTDLL.@]
171 NTSTATUS WINAPI RtlSetEnvironmentVariable(PWSTR* penv, PUNICODE_STRING name,
172 PUNICODE_STRING value)
174 INT len, old_size;
175 LPWSTR p, env;
176 NTSTATUS nts = STATUS_VARIABLE_NOT_FOUND;
177 MEMORY_BASIC_INFORMATION mbi;
179 TRACE("(%p,%s,%s)\n",
180 penv, debugstr_w(name->Buffer),
181 value ? debugstr_w(value->Buffer) : "--nil--");
183 if (!name || !name->Buffer || !name->Buffer[0])
184 return STATUS_INVALID_PARAMETER_1;
185 /* variable names can't contain a '=' except as a first character */
186 if (strchrW(name->Buffer + 1, '=')) return STATUS_INVALID_PARAMETER;
188 if (!penv)
190 RtlAcquirePebLock();
191 env = ntdll_get_process_pmts()->Environment;
192 } else env = *penv;
194 len = name->Length / sizeof(WCHAR);
196 /* compute current size of environment */
197 for (p = env; *p; p += strlenW(p) + 1);
198 old_size = p + 1 - env;
200 /* Find a place to insert the string */
201 for (p = env; *p; p += strlenW(p) + 1)
203 if (!strncmpiW(name->Buffer, p, len) && (p[len] == '=')) break;
205 if (!value && !*p) goto done; /* Value to remove doesn't exist */
207 /* Realloc the buffer */
208 len = value ? len + value->Length / sizeof(WCHAR) + 2 : 0;
209 if (*p) len -= strlenW(p) + 1; /* The name already exists */
211 if (len < 0)
213 LPWSTR next = p + strlenW(p) + 1; /* We know there is a next one */
214 memmove(next + len, next, (old_size - (next - env)) * sizeof(WCHAR));
217 nts = NtQueryVirtualMemory(NtCurrentProcess(), env, 0,
218 &mbi, sizeof(mbi), NULL);
219 if (nts != STATUS_SUCCESS) goto done;
221 if ((old_size + len) * sizeof(WCHAR) > mbi.RegionSize)
223 LPWSTR new_env;
224 ULONG new_size = (old_size + len) * sizeof(WCHAR);
226 new_env = NULL;
227 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&new_env, 0,
228 &new_size, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
229 if (nts != STATUS_SUCCESS) goto done;
231 memmove(new_env, env, (p - env) * sizeof(WCHAR));
232 assert(len > 0);
233 memmove(new_env + (p - env) + len, p, (old_size - (p - env)) * sizeof(WCHAR));
234 p = new_env + (p - env);
236 RtlDestroyEnvironment(env);
237 if (!penv) ntdll_get_process_pmts()->Environment = new_env;
238 else *penv = new_env;
239 env = new_env;
241 else
243 if (len > 0) memmove(p + len, p, (old_size - (p - env)) * sizeof(WCHAR));
246 /* Set the new string */
247 if (value)
249 static const WCHAR equalW[] = {'=',0};
251 strcpyW(p, name->Buffer);
252 strcatW(p, equalW);
253 strcatW(p, value->Buffer);
255 done:
256 if (!penv) RtlReleasePebLock();
258 return nts;
261 /******************************************************************
262 * RtlExpandEnvironmentStrings_U (NTDLL.@)
265 NTSTATUS WINAPI RtlExpandEnvironmentStrings_U(PWSTR renv, const UNICODE_STRING* us_src,
266 PUNICODE_STRING us_dst, PULONG plen)
268 DWORD len, count, total_size = 1; /* 1 for terminating '\0' */
269 LPCWSTR env, src, p, var;
270 LPWSTR dst;
272 src = us_src->Buffer;
273 count = us_dst->MaximumLength / sizeof(WCHAR);
274 dst = count ? us_dst->Buffer : NULL;
276 if (!renv)
278 RtlAcquirePebLock();
279 env = ntdll_get_process_pmts()->Environment;
281 else env = renv;
283 while (*src)
285 if (*src != '%')
287 if ((p = strchrW( src, '%' ))) len = p - src;
288 else len = strlenW(src);
289 var = src;
290 src += len;
292 else /* we are at the start of a variable */
294 if ((p = strchrW( src + 1, '%' )))
296 len = p - src - 1; /* Length of the variable name */
297 if ((var = ENV_FindVariable( env, src + 1, len )))
299 src += len + 2; /* Skip the variable name */
300 len = strlenW(var);
302 else
304 var = src; /* Copy original name instead */
305 len += 2;
306 src += len;
309 else /* unfinished variable name, ignore it */
311 var = src;
312 len = strlenW(src); /* Copy whole string */
313 src += len;
316 total_size += len;
317 if (dst)
319 if (count < len) len = count;
320 memcpy(dst, var, len * sizeof(WCHAR));
321 count -= len;
322 dst += len;
326 if (!renv) RtlReleasePebLock();
328 /* Null-terminate the string */
329 if (dst && count) *dst = '\0';
331 us_dst->Length = (dst) ? (dst - us_dst->Buffer) * sizeof(WCHAR) : 0;
332 if (plen) *plen = total_size * sizeof(WCHAR);
334 return (count) ? STATUS_SUCCESS : STATUS_BUFFER_TOO_SMALL;
337 /***********************************************************************
338 * build_environment
340 * Build the Win32 environment from the Unix environment
342 static NTSTATUS build_initial_environment(void)
344 extern char **environ;
345 LPSTR* e, te;
346 LPWSTR p;
347 ULONG size;
348 NTSTATUS nts;
349 int len;
351 /* Compute the total size of the Unix environment */
352 size = sizeof(BYTE);
353 for (e = environ; *e; e++)
355 if (!memcmp(*e, "PATH=", 5)) continue;
356 size += strlen(*e) + 1;
358 size *= sizeof(WCHAR);
360 /* Now allocate the environment */
361 nts = NtAllocateVirtualMemory(NtCurrentProcess(), (void**)&p, 0, &size,
362 MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
363 if (nts != STATUS_SUCCESS) return nts;
365 ntdll_get_process_pmts()->Environment = p;
367 /* And fill it with the Unix environment */
368 for (e = environ; *e; e++)
370 /* skip Unix PATH and store WINEPATH as PATH */
371 if (!memcmp(*e, "PATH=", 5)) continue;
372 if (!memcmp(*e, "WINEPATH=", 9 )) te = *e + 4; else te = *e;
373 len = strlen(te);
374 RtlMultiByteToUnicodeN(p, len * sizeof(WCHAR), NULL, te, len);
375 p[len] = 0;
376 p += len + 1;
378 *p = 0;
380 return STATUS_SUCCESS;
383 static void init_unicode( UNICODE_STRING* us, const char** src, size_t len)
385 if (len)
387 STRING ansi;
388 ansi.Buffer = (char*)*src;
389 ansi.Length = len;
390 ansi.MaximumLength = len;
391 /* FIXME: should check value returned */
392 RtlAnsiStringToUnicodeString( us, &ansi, TRUE );
393 *src += len;
397 /***********************************************************************
398 * init_user_process_pmts
400 * Fill the RTL_USER_PROCESS_PARAMETERS structure from the server.
402 BOOL init_user_process_pmts( size_t info_size, char *main_exe_name, size_t main_exe_size )
404 startup_info_t info;
405 void *data;
406 const char *src;
407 size_t len;
408 RTL_USER_PROCESS_PARAMETERS *rupp;
410 if (build_initial_environment() != STATUS_SUCCESS) return FALSE;
411 if (!info_size) return TRUE;
412 if (!(data = RtlAllocateHeap( ntdll_get_process_heap(), 0, info_size )))
413 return FALSE;
415 SERVER_START_REQ( get_startup_info )
417 wine_server_set_reply( req, data, info_size );
418 wine_server_call( req );
419 info_size = wine_server_reply_size( reply );
421 SERVER_END_REQ;
423 if (info_size < sizeof(info.size)) goto done;
424 len = min( info_size, ((startup_info_t *)data)->size );
425 memset( &info, 0, sizeof(info) );
426 memcpy( &info, data, len );
427 src = (char *)data + len;
428 info_size -= len;
430 /* fixup the lengths */
431 if (info.filename_len > info_size) info.filename_len = info_size;
432 info_size -= info.filename_len;
433 if (info.cmdline_len > info_size) info.cmdline_len = info_size;
434 info_size -= info.cmdline_len;
435 if (info.desktop_len > info_size) info.desktop_len = info_size;
436 info_size -= info.desktop_len;
437 if (info.title_len > info_size) info.title_len = info_size;
439 /* store the filename */
440 len = min( info.filename_len, main_exe_size-1 );
441 memcpy( main_exe_name, src, len );
442 main_exe_name[len] = 0;
444 rupp = NtCurrentTeb()->Peb->ProcessParameters;
446 init_unicode( &rupp->ImagePathName, &src, info.filename_len );
447 init_unicode( &rupp->CommandLine, &src, info.cmdline_len );
448 init_unicode( &rupp->Desktop, &src, info.desktop_len );
449 init_unicode( &rupp->WindowTitle, &src, info.title_len );
451 rupp->dwX = info.x;
452 rupp->dwY = info.y;
453 rupp->dwXSize = info.cx;
454 rupp->dwYSize = info.cy;
455 rupp->dwXCountChars = info.x_chars;
456 rupp->dwYCountChars = info.y_chars;
457 rupp->dwFillAttribute = info.attribute;
458 rupp->wShowWindow = info.cmd_show;
459 rupp->dwFlags = info.flags;
461 done:
462 RtlFreeHeap( ntdll_get_process_heap(), 0, data );
463 return TRUE;
466 /***********************************************************************
467 * set_library_argv
469 * Set the Wine library argc/argv global variables.
471 static void set_library_argv( char **argv )
473 int argc;
474 WCHAR *p;
475 WCHAR **wargv;
476 DWORD total = 0, len, reslen;
478 for (argc = 0; argv[argc]; argc++)
480 len = strlen(argv[argc]) + 1;
481 RtlMultiByteToUnicodeN(NULL, 0, &reslen, argv[argc], len);
482 total += reslen;
484 wargv = RtlAllocateHeap( ntdll_get_process_heap(), 0,
485 total + (argc + 1) * sizeof(*wargv) );
486 p = (WCHAR *)(wargv + argc + 1);
487 for (argc = 0; argv[argc]; argc++)
489 len = strlen(argv[argc]) + 1;
490 RtlMultiByteToUnicodeN(p, total, &reslen, argv[argc], len);
491 wargv[argc] = p;
492 p += reslen / sizeof(WCHAR);
493 total -= reslen;
495 wargv[argc] = NULL;
497 __wine_main_argc = argc;
498 __wine_main_argv = argv;
499 __wine_main_wargv = wargv;
502 /***********************************************************************
503 * build_command_line
505 * Build the command line of a process from the argv array.
507 * Note that it does NOT necessarily include the file name.
508 * Sometimes we don't even have any command line options at all.
510 * We must quote and escape characters so that the argv array can be rebuilt
511 * from the command line:
512 * - spaces and tabs must be quoted
513 * 'a b' -> '"a b"'
514 * - quotes must be escaped
515 * '"' -> '\"'
516 * - if '\'s are followed by a '"', they must be doubled and followed by '\"',
517 * resulting in an odd number of '\' followed by a '"'
518 * '\"' -> '\\\"'
519 * '\\"' -> '\\\\\"'
520 * - '\'s that are not followed by a '"' can be left as is
521 * 'a\b' == 'a\b'
522 * 'a\\b' == 'a\\b'
524 BOOL build_command_line( char **argv )
526 int len;
527 char **arg;
528 LPWSTR p;
529 RTL_USER_PROCESS_PARAMETERS* rupp;
531 set_library_argv( argv );
533 rupp = ntdll_get_process_pmts();
534 if (rupp->CommandLine.Buffer) return TRUE; /* already got it from the server */
536 len = 0;
537 for (arg = argv; *arg; arg++)
539 int has_space,bcount;
540 char* a;
542 has_space=0;
543 bcount=0;
544 a=*arg;
545 if( !*a ) has_space=1;
546 while (*a!='\0') {
547 if (*a=='\\') {
548 bcount++;
549 } else {
550 if (*a==' ' || *a=='\t') {
551 has_space=1;
552 } else if (*a=='"') {
553 /* doubling of '\' preceeding a '"',
554 * plus escaping of said '"'
556 len+=2*bcount+1;
558 bcount=0;
560 a++;
562 len+=(a-*arg)+1 /* for the separating space */;
563 if (has_space)
564 len+=2; /* for the quotes */
567 if (!(rupp->CommandLine.Buffer = RtlAllocateHeap( ntdll_get_process_heap(), 0, len * sizeof(WCHAR))))
568 return FALSE;
570 p = rupp->CommandLine.Buffer;
571 rupp->CommandLine.Length = (len - 1) * sizeof(WCHAR);
572 rupp->CommandLine.MaximumLength = len * sizeof(WCHAR);
573 for (arg = argv; *arg; arg++)
575 int has_space,has_quote;
576 char* a;
578 /* Check for quotes and spaces in this argument */
579 has_space=has_quote=0;
580 a=*arg;
581 if( !*a ) has_space=1;
582 while (*a!='\0') {
583 if (*a==' ' || *a=='\t') {
584 has_space=1;
585 if (has_quote)
586 break;
587 } else if (*a=='"') {
588 has_quote=1;
589 if (has_space)
590 break;
592 a++;
595 /* Now transfer it to the command line */
596 if (has_space)
597 *p++='"';
598 if (has_quote) {
599 int bcount;
600 char* a;
602 bcount=0;
603 a=*arg;
604 while (*a!='\0') {
605 if (*a=='\\') {
606 *p++=*a;
607 bcount++;
608 } else {
609 if (*a=='"') {
610 int i;
612 /* Double all the '\\' preceeding this '"', plus one */
613 for (i=0;i<=bcount;i++)
614 *p++='\\';
615 *p++='"';
616 } else {
617 *p++=*a;
619 bcount=0;
621 a++;
623 } else {
624 char* x = *arg;
625 while ((*p=*x++)) p++;
627 if (has_space)
628 *p++='"';
629 *p++=' ';
631 if (p > rupp->CommandLine.Buffer)
632 p--; /* remove last space */
633 *p = '\0';
635 return TRUE;