mirrored from git://source.winehq.org/git/wine.git
/
env.c
2408 lines (2123 loc) · 81.6 KB
/
env.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Ntdll environment functions
*
* Copyright 1996, 1998 Alexandre Julliard
* Copyright 2003 Eric Pouech
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#if 0
#pragma makedep unix
#endif
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <locale.h>
#include <langinfo.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef HAVE_SYS_PRCTL_H
# include <sys/prctl.h>
#endif
#ifdef HAVE_PWD_H
# include <pwd.h>
#endif
#ifdef __APPLE__
# include <CoreFoundation/CFLocale.h>
# include <CoreFoundation/CFString.h>
#endif
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winternl.h"
#include "winbase.h"
#include "winnls.h"
#include "wine/condrv.h"
#include "wine/debug.h"
#include "unix_private.h"
#include "locale_private.h"
#include "error.h"
WINE_DEFAULT_DEBUG_CHANNEL(environ);
PEB *peb = NULL;
WOW_PEB *wow_peb = NULL;
USHORT *uctable = NULL, *lctable = NULL;
SIZE_T startup_info_size = 0;
BOOL is_prefix_bootstrap = FALSE;
static const WCHAR bootstrapW[] = {'W','I','N','E','B','O','O','T','S','T','R','A','P','M','O','D','E'};
int main_argc = 0;
char **main_argv = NULL;
char **main_envp = NULL;
WCHAR **main_wargv = NULL;
static LCID user_lcid, system_lcid;
static LANGID user_ui_language;
static char system_locale[LOCALE_NAME_MAX_LENGTH];
static char user_locale[LOCALE_NAME_MAX_LENGTH];
/* system directory with trailing backslash */
const WCHAR system_dir[] = {'\\','?','?','\\','C',':','\\','w','i','n','d','o','w','s','\\',
's','y','s','t','e','m','3','2','\\',0};
static CPTABLEINFO unix_cp = { CP_UTF8, 4, '?', 0xfffd, '?', '?' };
static char *get_nls_file_path( ULONG type, ULONG id )
{
const char *dir = build_dir ? build_dir : data_dir;
const char *name = NULL;
char *path, tmp[16];
switch (type)
{
case NLS_SECTION_SORTKEYS: name = "sortdefault"; break;
case NLS_SECTION_CASEMAP: name = "l_intl"; break;
case NLS_SECTION_CODEPAGE: name = tmp; sprintf( tmp, "c_%03u", id ); break;
case NLS_SECTION_NORMALIZE:
switch (id)
{
case NormalizationC: name = "normnfc"; break;
case NormalizationD: name = "normnfd"; break;
case NormalizationKC: name = "normnfkc"; break;
case NormalizationKD: name = "normnfkd"; break;
case 13: name = "normidna"; break;
}
break;
}
if (!name) return NULL;
if (!(path = malloc( strlen(dir) + strlen(name) + 10 ))) return NULL;
sprintf( path, "%s/nls/%s.nls", dir, name );
return path;
}
static void *read_nls_file( const char *name )
{
const char *dir = build_dir ? build_dir : data_dir;
char *path;
struct stat st;
void *data, *ret = NULL;
int fd;
if (!(path = malloc( strlen(dir) + strlen(name) + 10 ))) return NULL;
sprintf( path, "%s/nls/%s", dir, name );
if ((fd = open( path, O_RDONLY )) != -1)
{
fstat( fd, &st );
if ((data = malloc( st.st_size )) && st.st_size > 0x1000 &&
read( fd, data, st.st_size ) == st.st_size)
{
ret = data;
}
else
{
free( data );
data = NULL;
}
close( fd );
}
else ERR( "failed to load %s\n", path );
free( path );
return ret;
}
static NTSTATUS open_nls_data_file( const char *path, const WCHAR *sysdir, HANDLE *file )
{
NTSTATUS status = STATUS_OBJECT_NAME_NOT_FOUND;
OBJECT_ATTRIBUTES attr;
UNICODE_STRING valueW;
WCHAR buffer[64];
char *p, *ntpath;
wcscpy( buffer, system_dir );
p = strrchr( path, '/' ) + 1;
ascii_to_unicode( buffer + wcslen(buffer), p, strlen(p) + 1 );
init_unicode_string( &valueW, buffer );
InitializeObjectAttributes( &attr, &valueW, 0, 0, NULL );
status = open_unix_file( file, path, GENERIC_READ, &attr, 0, FILE_SHARE_READ,
FILE_OPEN, FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
if (status != STATUS_NO_SUCH_FILE) return status;
if ((status = nt_to_unix_file_name( &attr, &ntpath, FILE_OPEN ))) return status;
status = open_unix_file( file, ntpath, GENERIC_READ, &attr, 0, FILE_SHARE_READ,
FILE_OPEN, FILE_SYNCHRONOUS_IO_ALERT, NULL, 0 );
free( ntpath );
return status;
}
static NTSTATUS get_nls_section_name( ULONG type, ULONG id, WCHAR name[32] )
{
char buffer[32];
switch (type)
{
case NLS_SECTION_SORTKEYS:
if (id) return STATUS_INVALID_PARAMETER_1;
strcpy( buffer, "\\NLS\\NlsSectionSORTDEFAULT" );
break;
case NLS_SECTION_CASEMAP:
if (id) return STATUS_UNSUCCESSFUL;
strcpy( buffer, "\\NLS\\NlsSectionLANG_INTL" );
break;
case NLS_SECTION_CODEPAGE:
sprintf( buffer, "\\NLS\\NlsSectionCP%03u", id);
break;
case NLS_SECTION_NORMALIZE:
sprintf( buffer, "\\NLS\\NlsSectionNORM%08x", id);
break;
default:
return STATUS_INVALID_PARAMETER_1;
}
ascii_to_unicode( name, buffer, strlen(buffer) + 1 );
return STATUS_SUCCESS;
}
#ifdef __APPLE__
/* The Apple filesystem enforces NFD so we need the compose tables to put it back into NFC */
static struct norm_table *nfc_table;
static void init_unix_codepage(void)
{
nfc_table = read_nls_file( "normnfc.nls" );
}
#elif defined(__ANDROID__) /* Android always uses UTF-8 */
static void init_unix_codepage(void) { }
#else /* __APPLE__ || __ANDROID__ */
/* charset to codepage map, sorted by name */
static const struct { const char *name; UINT cp; } charset_names[] =
{
{ "ANSIX341968", 20127 },
{ "BIG5", 950 },
{ "BIG5HKSCS", 950 },
{ "CP1250", 1250 },
{ "CP1251", 1251 },
{ "CP1252", 1252 },
{ "CP1253", 1253 },
{ "CP1254", 1254 },
{ "CP1255", 1255 },
{ "CP1256", 1256 },
{ "CP1257", 1257 },
{ "CP1258", 1258 },
{ "CP932", 932 },
{ "CP936", 936 },
{ "CP949", 949 },
{ "CP950", 950 },
{ "EUCJP", 20932 },
{ "EUCKR", 949 },
{ "GB18030", 936 /* 54936 */ },
{ "GB2312", 936 },
{ "GBK", 936 },
{ "IBM037", 37 },
{ "IBM1026", 1026 },
{ "IBM424", 20424 },
{ "IBM437", 437 },
{ "IBM500", 500 },
{ "IBM850", 850 },
{ "IBM852", 852 },
{ "IBM855", 855 },
{ "IBM857", 857 },
{ "IBM860", 860 },
{ "IBM861", 861 },
{ "IBM862", 862 },
{ "IBM863", 863 },
{ "IBM864", 864 },
{ "IBM865", 865 },
{ "IBM866", 866 },
{ "IBM869", 869 },
{ "IBM874", 874 },
{ "IBM875", 875 },
{ "ISO88591", 28591 },
{ "ISO885913", 28603 },
{ "ISO885915", 28605 },
{ "ISO88592", 28592 },
{ "ISO88593", 28593 },
{ "ISO88594", 28594 },
{ "ISO88595", 28595 },
{ "ISO88596", 28596 },
{ "ISO88597", 28597 },
{ "ISO88598", 28598 },
{ "ISO88599", 28599 },
{ "KOI8R", 20866 },
{ "KOI8U", 21866 },
{ "SJIS", 932 },
{ "TIS620", 28601 },
{ "UTF8", CP_UTF8 }
};
static void init_unix_codepage(void)
{
char charset_name[16];
const char *name;
size_t i, j;
int min = 0, max = ARRAY_SIZE(charset_names) - 1;
setlocale( LC_CTYPE, "" );
if (!(name = nl_langinfo( CODESET ))) return;
/* remove punctuation characters from charset name */
for (i = j = 0; name[i] && j < sizeof(charset_name)-1; i++)
{
if (name[i] >= '0' && name[i] <= '9') charset_name[j++] = name[i];
else if (name[i] >= 'A' && name[i] <= 'Z') charset_name[j++] = name[i];
else if (name[i] >= 'a' && name[i] <= 'z') charset_name[j++] = name[i] + ('A' - 'a');
}
charset_name[j] = 0;
while (min <= max)
{
int pos = (min + max) / 2;
int res = strcmp( charset_names[pos].name, charset_name );
if (!res)
{
if (charset_names[pos].cp != CP_UTF8)
{
char buffer[16];
void *data;
sprintf( buffer, "c_%03u.nls", charset_names[pos].cp );
if ((data = read_nls_file( buffer ))) init_codepage_table( data, &unix_cp );
}
return;
}
if (res > 0) max = pos - 1;
else min = pos + 1;
}
ERR( "unrecognized charset '%s'\n", name );
}
#endif /* __APPLE__ || __ANDROID__ */
static inline SIZE_T get_env_length( const WCHAR *env )
{
const WCHAR *end = env;
while (*end) end += wcslen(end) + 1;
return end + 1 - env;
}
#define STARTS_WITH(var,str) (!strncmp( var, str, sizeof(str) - 1 ))
/***********************************************************************
* is_special_env_var
*
* Check if an environment variable needs to be handled specially when
* passed through the Unix environment (i.e. prefixed with "WINE").
*/
static BOOL is_special_env_var( const char *var )
{
return (STARTS_WITH( var, "PATH=" ) ||
STARTS_WITH( var, "PWD=" ) ||
STARTS_WITH( var, "HOME=" ) ||
STARTS_WITH( var, "TEMP=" ) ||
STARTS_WITH( var, "TMP=" ) ||
STARTS_WITH( var, "QT_" ) ||
STARTS_WITH( var, "VK_" ));
}
/* check if an environment variable changes dynamically in every new process */
static BOOL is_dynamic_env_var( const char *var )
{
return (STARTS_WITH( var, "WINEDLLOVERRIDES=" ) ||
STARTS_WITH( var, "WINEDATADIR=" ) ||
STARTS_WITH( var, "WINEHOMEDIR=" ) ||
STARTS_WITH( var, "WINEBUILDDIR=" ) ||
STARTS_WITH( var, "WINECONFIGDIR=" ) ||
STARTS_WITH( var, "WINEDLLDIR" ) ||
STARTS_WITH( var, "WINEUNIXCP=" ) ||
STARTS_WITH( var, "WINEUSERLOCALE=" ) ||
STARTS_WITH( var, "WINEUSERNAME=" ) ||
STARTS_WITH( var, "WINEPRELOADRESERVE=" ) ||
STARTS_WITH( var, "WINELOADERNOEXEC=" ) ||
STARTS_WITH( var, "WINESERVERSOCKET=" ));
}
/******************************************************************
* ntdll_umbstowcs (ntdll.so)
*
* Convert a multi-byte string in the Unix code page to UTF-16. Returns the
* number of characters converted, which may be less than the entire source
* string. The destination string must not be NULL.
*
* The size of the output buffer, and the return value, are both given in
* characters, not bytes.
*/
DWORD ntdll_umbstowcs( const char *src, DWORD srclen, WCHAR *dst, DWORD dstlen )
{
unsigned int reslen;
if (unix_cp.CodePage != CP_UTF8) return cp_mbstowcs( &unix_cp, dst, dstlen, src, srclen );
utf8_mbstowcs( dst, dstlen, &reslen, src, srclen );
#ifdef __APPLE__ /* work around broken Mac OS X filesystem that enforces NFD */
if (reslen && nfc_table) reslen = compose_string( nfc_table, dst, reslen );
#endif
return reslen;
}
/******************************************************************
* ntdll_wcstoumbs (ntdll.so)
*
* Convert a UTF-16 string to a multi-byte string in the Unix code page.
* The destination string must not be NULL.
*
* The size of the source string is given in characters, not bytes.
*/
int ntdll_wcstoumbs( const WCHAR *src, DWORD srclen, char *dst, DWORD dstlen, BOOL strict )
{
unsigned int i, reslen;
if (unix_cp.CodePage != CP_UTF8)
{
if (strict)
{
if (unix_cp.DBCSCodePage)
{
const WCHAR *uni2cp = unix_cp.WideCharTable;
for (i = 0; i < srclen; i++)
{
WCHAR ch = uni2cp[src[i]];
if (ch >> 8)
{
if (unix_cp.DBCSOffsets[unix_cp.DBCSOffsets[ch >> 8] + (ch & 0xff)] != src[i])
return -1;
}
else if (unix_cp.MultiByteTable[(unsigned char)ch] != src[i]) return -1;
}
}
else
{
const char *uni2cp = unix_cp.WideCharTable;
for (i = 0; i < srclen; i++)
if (unix_cp.MultiByteTable[(unsigned char)uni2cp[src[i]]] != src[i])
return -1;
}
}
reslen = cp_wcstombs( &unix_cp, dst, dstlen, src, srclen );
}
else
{
NTSTATUS status = utf8_wcstombs( dst, dstlen, &reslen, src, srclen );
if (strict && status == STATUS_SOME_NOT_MAPPED) return -1;
}
return reslen;
}
/**********************************************************************
* ntdll_wcsicmp (ntdll.so)
*/
int ntdll_wcsicmp( const WCHAR *str1, const WCHAR *str2 )
{
int ret;
for (;;)
{
if ((ret = ntdll_towupper( *str1 ) - ntdll_towupper( *str2 )) || !*str1) return ret;
str1++;
str2++;
}
}
/**********************************************************************
* ntdll_wcsnicmp (ntdll.so)
*/
int ntdll_wcsnicmp( const WCHAR *str1, const WCHAR *str2, int n )
{
int ret;
for (ret = 0; n > 0; n--, str1++, str2++)
if ((ret = ntdll_towupper(*str1) - ntdll_towupper(*str2)) || !*str1) break;
return ret;
}
/***********************************************************************
* ntdll_get_build_dir (ntdll.so)
*/
const char *ntdll_get_build_dir(void)
{
return build_dir;
}
/***********************************************************************
* ntdll_get_data_dir (ntdll.so)
*/
const char *ntdll_get_data_dir(void)
{
return data_dir;
}
/***********************************************************************
* build_envp
*
* Build the environment of a new child process.
*/
char **build_envp( const WCHAR *envW )
{
static const char * const unix_vars[] = { "PATH", "TEMP", "TMP", "HOME" };
char **envp;
char *env, *p;
int count = 1, length, lenW;
unsigned int i;
lenW = get_env_length( envW );
if (!(env = malloc( lenW * 3 ))) return NULL;
length = ntdll_wcstoumbs( envW, lenW, env, lenW * 3, FALSE );
for (p = env; *p; p += strlen(p) + 1, count++)
{
if (is_dynamic_env_var( p )) continue;
if (is_special_env_var( p )) length += 4; /* prefix it with "WINE" */
}
for (i = 0; i < ARRAY_SIZE( unix_vars ); i++)
{
if (!(p = getenv(unix_vars[i]))) continue;
length += strlen(unix_vars[i]) + strlen(p) + 2;
count++;
}
if ((envp = malloc( count * sizeof(*envp) + length )))
{
char **envptr = envp;
char *dst = (char *)(envp + count);
/* some variables must not be modified, so we get them directly from the unix env */
for (i = 0; i < ARRAY_SIZE( unix_vars ); i++)
{
if (!(p = getenv( unix_vars[i] ))) continue;
*envptr++ = strcpy( dst, unix_vars[i] );
strcat( dst, "=" );
strcat( dst, p );
dst += strlen(dst) + 1;
}
/* now put the Windows environment strings */
for (p = env; *p; p += strlen(p) + 1)
{
if (*p == '=') continue; /* skip drive curdirs, this crashes some unix apps */
if (is_dynamic_env_var( p )) continue;
if (is_special_env_var( p )) /* prefix it with "WINE" */
{
*envptr++ = strcpy( dst, "WINE" );
strcat( dst, p );
}
else
{
*envptr++ = strcpy( dst, p );
}
dst += strlen(dst) + 1;
}
*envptr = 0;
}
free( env );
return envp;
}
/***********************************************************************
* set_process_name
*
* Change the process name in the ps output.
*/
static void set_process_name( const char *name )
{
char *p;
#ifdef HAVE_SETPROCTITLE
setproctitle("-%s", name );
#endif
if ((p = strrchr( name, '\\' ))) name = p + 1;
if ((p = strrchr( name, '/' ))) name = p + 1;
#ifdef HAVE_SETPROGNAME
setprogname( name );
#endif
#ifdef HAVE_PRCTL
#ifndef PR_SET_NAME
# define PR_SET_NAME 15
#endif
prctl( PR_SET_NAME, name );
#endif
}
/***********************************************************************
* rebuild_argv
*
* Build the main argv by removing argv[0].
*/
static void rebuild_argv(void)
{
BOOL shift_strings = FALSE;
int i;
#ifndef HAVE_SETPROCTITLE
for (i = 1; i < main_argc; i++)
if (main_argv[i - 1] + strlen( main_argv[i - 1] ) + 1 != main_argv[i]) break;
shift_strings = (i == main_argc);
#endif
if (shift_strings)
{
int offset = main_argv[1] - main_argv[0];
char *end = main_argv[main_argc - 1] + strlen(main_argv[main_argc - 1]) + 1;
memmove( main_argv[0], main_argv[1], end - main_argv[1] );
memset( end - offset, 0, offset );
for (i = 1; i < main_argc; i++) main_argv[i - 1] = main_argv[i] - offset;
}
else memmove( main_argv, main_argv + 1, (main_argc - 1) * sizeof(main_argv[0]) );
main_argv[--main_argc] = NULL;
set_process_name( main_argv[0] );
}
/***********************************************************************
* prepend_argv
*
* Prepend values to the main argv, removing the original argv[0].
*/
static void prepend_argv( const char **args, int count )
{
char **new_argv;
char *p, *end;
BOOL write_strings = FALSE;
int i, total = 0, new_argc = main_argc + count - 1;
for (i = 0; i < count; i++) total += strlen(args[i]) + 1;
for (i = 1; i < main_argc; i++) total += strlen(main_argv[i]) + 1;
new_argv = malloc( (new_argc + 1) * sizeof(*new_argv) + total );
p = (char *)(new_argv + new_argc + 1);
for (i = 0; i < count; i++)
{
new_argv[i] = p;
strcpy( p, args[i] );
p += strlen(p) + 1;
}
for (i = 1; i < main_argc; i++)
{
new_argv[count + i - 1] = p;
strcpy( p, main_argv[i] );
p += strlen(p) + 1;
}
new_argv[new_argc] = NULL;
/* copy what we can over the original argv */
#ifndef HAVE_SETPROCTITLE
for (i = 1; i < main_argc; i++)
if (main_argv[i - 1] + strlen(main_argv[i - 1]) + 1 != main_argv[i]) break;
write_strings = (i == main_argc);
#endif
if (write_strings)
{
p = main_argv[0];
end = main_argv[main_argc - 1] + strlen(main_argv[main_argc - 1]) + 1;
for (i = 0; i < min( new_argc, main_argc ) && p < end; i++)
{
int len = min( end - p - 1, strlen(new_argv[i]) );
memcpy( p, new_argv[i], len );
main_argv[i] = p;
p += len;
*p++ = 0;
}
memset( p, 0, end - p );
main_argv[i] = NULL;
}
else
{
memcpy( main_argv, new_argv, min( new_argc, main_argc ) * sizeof(*new_argv) );
main_argv[min( new_argc, main_argc )] = NULL;
}
main_argc = new_argc;
main_argv = new_argv;
set_process_name( main_argv[0] );
}
/***********************************************************************
* build_wargv
*
* Build the Unicode argv array, replacing argv[0] by the image name.
*/
static WCHAR **build_wargv( const WCHAR *image )
{
int argc;
WCHAR *p, **wargv;
DWORD total = wcslen(image) + 1;
for (argc = 1; main_argv[argc]; argc++) total += strlen(main_argv[argc]) + 1;
wargv = malloc( total * sizeof(WCHAR) + (argc + 1) * sizeof(*wargv) );
p = (WCHAR *)(wargv + argc + 1);
wargv[0] = p;
wcscpy( p, image );
total -= wcslen( p ) + 1;
p += wcslen( p ) + 1;
for (argc = 1; main_argv[argc]; argc++)
{
DWORD reslen = ntdll_umbstowcs( main_argv[argc], strlen(main_argv[argc]) + 1, p, total );
wargv[argc] = p;
p += reslen;
total -= reslen;
}
wargv[argc] = NULL;
return wargv;
}
/* Unix format is: lang[_country][.charset][@modifier]
* Windows format is: lang[-script][-country][_modifier] */
static BOOL unix_to_win_locale( const char *unix_name, char *win_name )
{
static const char sep[] = "_.@";
const char *extra = NULL;
char buffer[LOCALE_NAME_MAX_LENGTH];
char *p, *country = NULL, *modifier = NULL;
if (!unix_name || !unix_name[0] || !strcmp( unix_name, "C" ))
{
unix_name = getenv( "LC_ALL" );
if (!unix_name || !unix_name[0]) return FALSE;
}
if (strlen( unix_name ) >= LOCALE_NAME_MAX_LENGTH) return FALSE;
strcpy( buffer, unix_name );
if (!(p = strpbrk( buffer, sep )))
{
if (!strcmp( buffer, "POSIX" ) || !strcmp( buffer, "C" ))
strcpy( win_name, "en-US" );
else
strcpy( win_name, buffer );
return TRUE;
}
if (*p == '_')
{
*p++ = 0;
country = p;
p = strpbrk( p, sep + 1 );
}
if (p && *p == '.')
{
*p++ = 0;
/* charset, ignore */
p = strchr( p, '@' );
}
if (p)
{
*p++ = 0;
modifier = p;
}
/* rebuild a Windows name */
strcpy( win_name, buffer );
if (modifier)
{
if (!strcmp( modifier, "arabic" )) strcat( win_name, "-Arab" );
else if (!strcmp( modifier, "chakma" )) strcat( win_name, "-Cakm" );
else if (!strcmp( modifier, "cherokee" )) strcat( win_name, "-Cher" );
else if (!strcmp( modifier, "cyrillic" )) strcat( win_name, "-Cyrl" );
else if (!strcmp( modifier, "devanagari" )) strcat( win_name, "-Deva" );
else if (!strcmp( modifier, "gurmukhi" )) strcat( win_name, "-Guru" );
else if (!strcmp( modifier, "javanese" )) strcat( win_name, "-Java" );
else if (!strcmp( modifier, "latin" )) strcat( win_name, "-Latn" );
else if (!strcmp( modifier, "mongolian" )) strcat( win_name, "-Mong" );
else if (!strcmp( modifier, "syriac" )) strcat( win_name, "-Syrc" );
else if (!strcmp( modifier, "tifinagh" )) strcat( win_name, "-Tfng" );
else if (!strcmp( modifier, "tibetan" )) strcat( win_name, "-Tibt" );
else if (!strcmp( modifier, "vai" )) strcat( win_name, "-Vaii" );
else if (!strcmp( modifier, "yi" )) strcat( win_name, "-Yiii" );
else if (!strcmp( modifier, "saaho" )) strcpy( win_name, "ssy" );
else if (!strcmp( modifier, "valencia" )) extra = "-valencia";
/* ignore unknown modifiers */
}
if (country)
{
p = win_name + strlen(win_name);
*p++ = '-';
strcpy( p, country );
}
if (extra) strcat( win_name, extra );
return TRUE;
}
static const NLS_LOCALE_DATA *get_win_locale( const NLS_LOCALE_HEADER *header, const char *win_name )
{
WCHAR name[LOCALE_NAME_MAX_LENGTH];
const NLS_LOCALE_LCNAME_INDEX *entry;
ascii_to_unicode( name, win_name, strlen(win_name) + 1 );
if (!(entry = find_lcname_entry( header, name ))) return NULL;
return get_locale_data( header, entry->idx );
}
/******************************************************************
* init_locale
*/
static void init_locale(void)
{
struct locale_nls_header *header;
const NLS_LOCALE_HEADER *locale_table;
const NLS_LOCALE_DATA *locale;
setlocale( LC_ALL, "" );
if (!unix_to_win_locale( setlocale( LC_CTYPE, NULL ), system_locale )) system_locale[0] = 0;
if (!unix_to_win_locale( setlocale( LC_MESSAGES, NULL ), user_locale )) user_locale[0] = 0;
#ifdef __APPLE__
if (!system_locale[0])
{
CFLocaleRef locale = CFLocaleCopyCurrent();
CFStringRef lang = CFLocaleGetValue( locale, kCFLocaleLanguageCode );
CFStringRef country = CFLocaleGetValue( locale, kCFLocaleCountryCode );
CFStringRef locale_string;
if (country)
locale_string = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@-%@"), lang, country);
else
locale_string = CFStringCreateCopy(NULL, lang);
CFStringGetCString(locale_string, system_locale, sizeof(system_locale), kCFStringEncodingUTF8);
CFRelease(locale);
CFRelease(locale_string);
}
if (!user_locale[0])
{
/* Retrieve the preferred language as chosen in System Preferences. */
CFArrayRef preferred_langs = CFLocaleCopyPreferredLanguages();
if (preferred_langs && CFArrayGetCount( preferred_langs ))
{
CFStringRef preferred_lang = CFArrayGetValueAtIndex( preferred_langs, 0 );
CFDictionaryRef components = CFLocaleCreateComponentsFromLocaleIdentifier( NULL, preferred_lang );
if (components)
{
CFStringRef lang = CFDictionaryGetValue( components, kCFLocaleLanguageCode );
CFStringRef country = CFDictionaryGetValue( components, kCFLocaleCountryCode );
CFLocaleRef locale = NULL;
CFStringRef locale_string;
if (!country)
{
locale = CFLocaleCopyCurrent();
country = CFLocaleGetValue( locale, kCFLocaleCountryCode );
}
if (country)
locale_string = CFStringCreateWithFormat( NULL, NULL, CFSTR("%@-%@"), lang, country );
else
locale_string = CFStringCreateCopy( NULL, lang );
CFStringGetCString( locale_string, user_locale, sizeof(user_locale), kCFStringEncodingUTF8 );
CFRelease( locale_string );
if (locale) CFRelease( locale );
CFRelease( components );
}
}
if (preferred_langs) CFRelease( preferred_langs );
}
#endif
if ((header = read_nls_file( "locale.nls" )))
{
locale_table = (const NLS_LOCALE_HEADER *)((char *)header + header->locales);
if ((locale = get_win_locale( locale_table, system_locale )))
system_lcid = locale->idefaultlanguage;
if ((locale = get_win_locale( locale_table, user_locale )))
user_lcid = locale->idefaultlanguage;
free( header );
}
if (!system_lcid) system_lcid = MAKELANGID( LANG_ENGLISH, SUBLANG_DEFAULT );
if (!user_lcid) user_lcid = system_lcid;
user_ui_language = user_lcid;
setlocale( LC_NUMERIC, "C" ); /* FIXME: oleaut32 depends on this */
}
/***********************************************************************
* init_environment
*/
void init_environment( int argc, char *argv[], char *envp[] )
{
USHORT *case_table;
init_unix_codepage();
init_locale();
if ((case_table = read_nls_file( "l_intl.nls" )))
{
uctable = case_table + 2;
lctable = case_table + case_table[1] + 2;
}
main_argc = argc;
main_argv = argv;
main_envp = envp;
}
static const char overrides_help_message[] =
"Syntax:\n"
" WINEDLLOVERRIDES=\"entry;entry;entry...\"\n"
" where each entry is of the form:\n"
" module[,module...]={native|builtin}[,{b|n}]\n"
"\n"
" Only the first letter of the override (native or builtin)\n"
" is significant.\n\n"
"Example:\n"
" WINEDLLOVERRIDES=\"comdlg32=n,b;shell32,shlwapi=b\"\n";
/*************************************************************************
* get_initial_environment
*
* Return the initial environment.
*/
static WCHAR *get_initial_environment( SIZE_T *pos, SIZE_T *size )
{
char **e;
WCHAR *env, *ptr, *end;
/* estimate needed size */
*size = 1;
for (e = main_envp; *e; e++) *size += strlen(*e) + 1;
if (!(env = malloc( *size * sizeof(WCHAR) ))) return NULL;
ptr = env;
end = env + *size - 1;
for (e = main_envp; *e && ptr < end; e++)
{
char *str = *e;
/* skip Unix special variables and use the Wine variants instead */
if (!strncmp( str, "WINE", 4 ))
{
if (is_special_env_var( str + 4 )) str += 4;
else if (!strcmp( str, "WINEDLLOVERRIDES=help" ))
{
MESSAGE( overrides_help_message );
exit(0);
}
}
else if (is_special_env_var( str )) continue; /* skip it */
if (is_dynamic_env_var( str )) continue;
ptr += ntdll_umbstowcs( str, strlen(str) + 1, ptr, end - ptr );
}
*pos = ptr - env;
return env;
}
static WCHAR *find_env_var( WCHAR *env, SIZE_T size, const WCHAR *name, SIZE_T namelen )
{
WCHAR *p = env;
while (p < env + size)
{
if (!wcsnicmp( p, name, namelen ) && p[namelen] == '=') return p;
p += wcslen(p) + 1;
}
return NULL;
}
static WCHAR *get_env_var( WCHAR *env, SIZE_T size, const WCHAR *name, SIZE_T namelen )
{
WCHAR *ret = NULL, *var = find_env_var( env, size, name, namelen );
if (var)
{
var += namelen + 1; /* skip name */
if ((ret = malloc( (wcslen(var) + 1) * sizeof(WCHAR) ))) wcscpy( ret, var );
}
return ret;
}
/* set an environment variable, replacing it if it exists */
static void set_env_var( WCHAR **env, SIZE_T *pos, SIZE_T *size,
const WCHAR *name, SIZE_T namelen, const WCHAR *value )
{
WCHAR *p;
SIZE_T len;
/* remove existing string */
if ((p = find_env_var( *env, *pos, name, namelen )))
{
len = wcslen(p) + 1;
memmove( p, p + len, (*pos - (p + len - *env)) * sizeof(WCHAR) );
*pos -= len;
}
if (!value) return;
len = wcslen( value );
if (*pos + namelen + len + 3 > *size)
{
*size = max( *size * 2, *pos + namelen + len + 3 );
*env = realloc( *env, *size * sizeof(WCHAR) );
}
memcpy( *env + *pos, name, namelen * sizeof(WCHAR) );
(*env)[*pos + namelen] = '=';
memcpy( *env + *pos + namelen + 1, value, (len + 1) * sizeof(WCHAR) );
*pos += namelen + len + 2;
}