comctl32: Update thumb position on autopage instead of deferring it.
[wine] / dlls / msvcrt / locale.c
1 /*
2  * msvcrt.dll locale functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <limits.h>
25 #include <locale.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winnls.h"
33
34 #include "msvcrt.h"
35 #include "mtdll.h"
36
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
40
41 #define MAX_ELEM_LEN 64 /* Max length of country/language/CP string */
42 #define MAX_LOCALE_LENGTH 256
43 MSVCRT__locale_t MSVCRT_locale = NULL;
44 unsigned short *MSVCRT__pctype = NULL;
45 unsigned int MSVCRT___lc_codepage = 0;
46 int MSVCRT___lc_collate_cp = 0;
47 LCID MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
48 int MSVCRT___mb_cur_max = 1;
49 static unsigned char charmax = CHAR_MAX;
50
51 /* MT */
52 #define LOCK_LOCALE   _mlock(_SETLOCALE_LOCK);
53 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
54
55 #define MSVCRT_LEADBYTE  0x8000
56 #define MSVCRT_C1_DEFINED 0x200
57
58 /* Friendly country strings & language names abbreviations. */
59 static const char * const _country_synonyms[] =
60 {
61     "american", "enu",
62     "american english", "enu",
63     "american-english", "enu",
64     "english-american", "enu",
65     "english-us", "enu",
66     "english-usa", "enu",
67     "us", "enu",
68     "usa", "enu",
69     "australian", "ena",
70     "english-aus", "ena",
71     "belgian", "nlb",
72     "french-belgian", "frb",
73     "canadian", "enc",
74     "english-can", "enc",
75     "french-canadian", "frc",
76     "chinese", "chs",
77     "chinese-simplified", "chs",
78     "chinese-traditional", "cht",
79     "dutch-belgian", "nlb",
80     "english-nz", "enz",
81     "uk", "eng",
82     "english-uk", "eng",
83     "french-swiss", "frs",
84     "swiss", "des",
85     "german-swiss", "des",
86     "italian-swiss", "its",
87     "german-austrian", "dea",
88     "portugese", "ptb",
89     "portuguese-brazil", "ptb",
90     "spanish-mexican", "esm",
91     "norwegian-bokmal", "nor",
92     "norwegian-nynorsk", "non",
93     "spanish-modern", "esn"
94 };
95
96 /* INTERNAL: Map a synonym to an ISO code */
97 static void remap_synonym(char *name)
98 {
99   unsigned int i;
100   for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
101   {
102     if (!strcasecmp(_country_synonyms[i],name))
103     {
104       TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
105       strcpy(name, _country_synonyms[i+1]);
106       return;
107     }
108   }
109 }
110
111 /* Note: Flags are weighted in order of matching importance */
112 #define FOUND_LANGUAGE         0x4
113 #define FOUND_COUNTRY          0x2
114 #define FOUND_CODEPAGE         0x1
115
116 typedef struct {
117   char search_language[MAX_ELEM_LEN];
118   char search_country[MAX_ELEM_LEN];
119   char search_codepage[MAX_ELEM_LEN];
120   char found_codepage[MAX_ELEM_LEN];
121   unsigned int match_flags;
122   LANGID found_lang_id;
123 } locale_search_t;
124
125 #define CONTINUE_LOOKING TRUE
126 #define STOP_LOOKING     FALSE
127
128 /* INTERNAL: Get and compare locale info with a given string */
129 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp, BOOL exact)
130 {
131   int len;
132
133   buff[0] = 0;
134   GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
135   if (!buff[0] || !cmp[0])
136     return 0;
137
138   /* Partial matches are only allowed on language/country names */
139   len = strlen(cmp);
140   if(exact || len<=3)
141     return !strcasecmp(cmp, buff);
142   else
143     return !strncasecmp(cmp, buff, len);
144 }
145
146 static BOOL CALLBACK
147 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
148 {
149   locale_search_t *res = (locale_search_t *)lParam;
150   const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
151   char buff[MAX_ELEM_LEN];
152   unsigned int flags = 0;
153
154   if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
155     return CONTINUE_LOOKING;
156
157   /* Check Language */
158   if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
159       compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
160       compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
161   {
162     TRACE(":Found language: %s->%s\n", res->search_language, buff);
163     flags |= FOUND_LANGUAGE;
164   }
165   else if (res->match_flags & FOUND_LANGUAGE)
166   {
167     return CONTINUE_LOOKING;
168   }
169
170   /* Check Country */
171   if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
172       compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
173       compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
174   {
175     TRACE("Found country:%s->%s\n", res->search_country, buff);
176     flags |= FOUND_COUNTRY;
177   }
178   else if (res->match_flags & FOUND_COUNTRY)
179   {
180     return CONTINUE_LOOKING;
181   }
182
183   /* Check codepage */
184   if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
185       (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
186   {
187     TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
188     flags |= FOUND_CODEPAGE;
189     memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
190   }
191   else if (res->match_flags & FOUND_CODEPAGE)
192   {
193     return CONTINUE_LOOKING;
194   }
195
196   if (flags > res->match_flags)
197   {
198     /* Found a better match than previously */
199     res->match_flags = flags;
200     res->found_lang_id = LangID;
201   }
202   if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
203         (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
204   {
205     TRACE(":found exact locale match\n");
206     return STOP_LOOKING;
207   }
208   return CONTINUE_LOOKING;
209 }
210
211 extern int atoi(const char *);
212
213 /* Internal: Find the LCID for a locale specification */
214 LCID MSVCRT_locale_to_LCID(const char *locale)
215 {
216     LCID lcid;
217     locale_search_t search;
218     const char *cp, *region;
219
220     memset(&search, 0, sizeof(locale_search_t));
221
222     cp = strchr(locale, '.');
223     region = strchr(locale, '_');
224
225     lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
226     if(region) {
227         lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
228         if(region-locale < MAX_ELEM_LEN)
229             search.search_language[region-locale] = '\0';
230     } else
231         search.search_country[0] = '\0';
232
233     if(cp) {
234         lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
235         if(region && cp-region-1<MAX_ELEM_LEN)
236           search.search_country[cp-region-1] = '\0';
237         if(cp-locale < MAX_ELEM_LEN)
238             search.search_language[cp-locale] = '\0';
239     } else
240         search.search_codepage[0] = '\0';
241
242     if(!search.search_country[0] && !search.search_codepage[0])
243         remap_synonym(search.search_language);
244
245     EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
246             (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
247             (LONG_PTR)&search);
248
249     if (!search.match_flags)
250         return -1;
251
252     /* If we were given something that didn't match, fail */
253     if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
254         return -1;
255
256     lcid =  MAKELCID(search.found_lang_id, SORT_DEFAULT);
257
258     /* Populate partial locale, translating LCID to locale string elements */
259     if (!search.found_codepage[0]) {
260         /* Even if a codepage is not enumerated for a locale
261          * it can be set if valid */
262         if (search.search_codepage[0]) {
263             if (IsValidCodePage(atoi(search.search_codepage)))
264                 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
265             else {
266                 /* Special codepage values: OEM & ANSI */
267                 if (strcasecmp(search.search_codepage,"OCP")) {
268                     GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
269                             search.found_codepage, MAX_ELEM_LEN);
270                 } else if (strcasecmp(search.search_codepage,"ACP")) {
271                     GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
272                             search.found_codepage, MAX_ELEM_LEN);
273                 } else
274                     return -1;
275
276                 if (!atoi(search.found_codepage))
277                     return -1;
278             }
279         } else {
280             /* Prefer ANSI codepages if present */
281             GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
282                     search.found_codepage, MAX_ELEM_LEN);
283             if (!search.found_codepage[0] || !atoi(search.found_codepage))
284                 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
285                         search.found_codepage, MAX_ELEM_LEN);
286         }
287     }
288
289     return lcid;
290 }
291
292 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
293 static BOOL update_threadlocinfo_category(LCID lcid, MSVCRT__locale_t loc, int category)
294 {
295     char buf[256], *p;
296     int len;
297
298     if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
299         p = buf;
300
301         loc->locinfo->lc_id[category].wLanguage = 0;
302         while(*p) {
303             loc->locinfo->lc_id[category].wLanguage *= 16;
304
305             if(*p <= '9')
306                 loc->locinfo->lc_id[category].wLanguage += *p-'0';
307             else
308                 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
309
310             p++;
311         }
312
313         loc->locinfo->lc_id[category].wCountry =
314             loc->locinfo->lc_id[category].wLanguage;
315     }
316
317     if(GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
318                 |LOCALE_NOUSEROVERRIDE, buf, 256))
319         loc->locinfo->lc_id[category].wCodePage = atoi(buf);
320
321     loc->locinfo->lc_handle[category] = lcid;
322
323     len = 0;
324     len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
325             |LOCALE_NOUSEROVERRIDE, buf, 256);
326     buf[len-1] = '_';
327     len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
328             |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
329     buf[len-1] = '.';
330     len += GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
331             |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
332
333     loc->locinfo->lc_category[category].locale = MSVCRT_malloc(len);
334     loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
335     if(!loc->locinfo->lc_category[category].locale
336             || !loc->locinfo->lc_category[category].refcount) {
337         MSVCRT_free(loc->locinfo->lc_category[category].locale);
338         MSVCRT_free(loc->locinfo->lc_category[category].refcount);
339         loc->locinfo->lc_category[category].locale = NULL;
340         loc->locinfo->lc_category[category].refcount = NULL;
341         return TRUE;
342     }
343     memcpy(loc->locinfo->lc_category[category].locale, buf, len);
344     *loc->locinfo->lc_category[category].refcount = 1;
345
346     return FALSE;
347 }
348
349 /* INTERNAL: swap pointers values */
350 static inline void swap_pointers(void **p1, void **p2) {
351     void *hlp;
352
353     hlp = *p1;
354     *p1 = *p2;
355     *p2 = hlp;
356 }
357
358 /* INTERNAL: returns pthreadlocinfo struct */
359 MSVCRT_pthreadlocinfo get_locinfo(void) {
360     thread_data_t *data = msvcrt_get_thread_data();
361
362     if(!data || !data->have_locale)
363         return MSVCRT_locale->locinfo;
364
365     return data->locinfo;
366 }
367
368 /* INTERNAL: returns pthreadlocinfo struct */
369 MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
370     thread_data_t *data = msvcrt_get_thread_data();
371
372     if(!data || !data->have_locale)
373         return MSVCRT_locale->mbcinfo;
374
375     return data->mbcinfo;
376 }
377
378 /* INTERNAL: constructs string returned by setlocale */
379 static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
380     static char current_lc_all[MAX_LOCALE_LENGTH];
381
382     int i;
383
384     for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
385         if(strcmp(locinfo->lc_category[i].locale,
386                     locinfo->lc_category[i+1].locale))
387             break;
388     }
389
390     if(i==MSVCRT_LC_MAX)
391         return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
392
393     sprintf(current_lc_all,
394             "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
395             locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
396             locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
397             locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
398             locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
399             locinfo->lc_category[MSVCRT_LC_TIME].locale);
400
401     return current_lc_all;
402 }
403
404
405 /*********************************************************************
406  *              wsetlocale (MSVCRT.@)
407  */
408 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
409 {
410   static MSVCRT_wchar_t fake[] = {
411     'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
412     'S','t','a','t','e','s','.','1','2','5','2',0 };
413
414   FIXME("%d %s\n", category, debugstr_w(locale));
415
416   return fake;
417 }
418
419 /*********************************************************************
420  *              _Getdays (MSVCRT.@)
421  */
422 char* CDECL _Getdays(void)
423 {
424     MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
425     int i, len, size;
426     char *out;
427
428     TRACE("\n");
429
430     size = cur->str[2*7]-cur->str[0];
431     out = MSVCRT_malloc(size+1);
432     if(!out)
433         return NULL;
434
435     size = 0;
436     for(i=0; i<7; i++) {
437         out[size++] = ':';
438         len = strlen(cur->str[i]);
439         memcpy(&out[size], cur->str[i], len);
440         size += len;
441
442         out[size++] = ':';
443         len = strlen(cur->str[7+i]);
444         memcpy(&out[size], cur->str[7+i], len);
445         size += len;
446     }
447     out[size] = '\0';
448
449     return out;
450 }
451
452 /*********************************************************************
453  *              _Getmonths (MSVCRT.@)
454  */
455 char* CDECL _Getmonths(void)
456 {
457     static const int months_offset = 14;
458
459     MSVCRT___lc_time_data *cur = get_locinfo()->lc_time_curr;
460     int i, len, size;
461     char *out;
462
463     TRACE("\n");
464
465     size = cur->str[months_offset+2*12]-cur->str[months_offset];
466     out = MSVCRT_malloc(size+1);
467     if(!out)
468         return NULL;
469
470     size = 0;
471     for(i=0; i<12; i++) {
472         out[size++] = ':';
473         len = strlen(cur->str[months_offset+i]);
474         memcpy(&out[size], cur->str[months_offset+i], len);
475         size += len;
476
477         out[size++] = ':';
478         len = strlen(cur->str[months_offset+12+i]);
479         memcpy(&out[size], cur->str[months_offset+12+i], len);
480         size += len;
481     }
482     out[size] = '\0';
483
484     return out;
485 }
486
487 /*********************************************************************
488  *              _Gettnames (MSVCRT.@)
489  */
490 void* CDECL _Gettnames(void)
491 {
492     MSVCRT___lc_time_data *ret, *cur = get_locinfo()->lc_time_curr;
493     int i, size = sizeof(MSVCRT___lc_time_data);
494
495     TRACE("\n");
496
497     for(i=0; i<sizeof(cur->str)/sizeof(cur->str[0]); i++)
498         size += strlen(cur->str[i])+1;
499
500     ret = MSVCRT_malloc(size);
501     if(!ret)
502         return NULL;
503     memcpy(ret, cur, size);
504
505     size = 0;
506     for(i=0; i<sizeof(cur->str)/sizeof(cur->str[0]); i++) {
507         ret->str[i] = &ret->data[size];
508         size += strlen(&ret->data[size])+1;
509     }
510
511     return ret;
512 }
513
514 /*********************************************************************
515  *              _Strftime (MSVCRT.@)
516  */
517 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
518                             const void *tm, void *foo)
519 {
520   /* FIXME: */
521   TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
522   return "";
523 }
524
525 /*********************************************************************
526  *              __crtLCMapStringA (MSVCRT.@)
527  */
528 int CDECL __crtLCMapStringA(
529   LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
530   int dstlen, unsigned int codepage, int xflag
531 ) {
532   FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
533         lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
534   /* FIXME: A bit incorrect. But msvcrt itself just converts its
535    * arguments to wide strings and then calls LCMapStringW
536    */
537   return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
538 }
539
540 /*********************************************************************
541  *              __crtLCMapStringW (MSVCRT.@)
542  */
543 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
544         int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
545 {
546     FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
547             lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
548
549     return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
550 }
551
552 /*********************************************************************
553  *              __crtCompareStringA (MSVCRT.@)
554  */
555 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
556                                const char *src2, int len2 )
557 {
558     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
559           lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
560     /* FIXME: probably not entirely right */
561     return CompareStringA( lcid, flags, src1, len1, src2, len2 );
562 }
563
564 /*********************************************************************
565  *              __crtCompareStringW (MSVCRT.@)
566  */
567 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
568                                const MSVCRT_wchar_t *src2, int len2 )
569 {
570     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
571           lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
572     /* FIXME: probably not entirely right */
573     return CompareStringW( lcid, flags, src1, len1, src2, len2 );
574 }
575
576 /*********************************************************************
577  *              __crtGetLocaleInfoW (MSVCRT.@)
578  */
579 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
580 {
581     FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
582     /* FIXME: probably not entirely right */
583     return GetLocaleInfoW( lcid, type, buffer, len );
584 }
585
586 /*********************************************************************
587  *              btowc(MSVCRT.@)
588  */
589 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
590 {
591     unsigned char letter = c;
592     MSVCRT_wchar_t ret;
593
594     if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
595                 0, (LPCSTR)&letter, 1, &ret, 1))
596         return 0;
597
598     return ret;
599 }
600
601 /*********************************************************************
602  *              __crtGetStringTypeW(MSVCRT.@)
603  *
604  * This function was accepting different number of arguments in older
605  * versions of msvcrt.
606  */
607 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
608         MSVCRT_wchar_t *buffer, int len, WORD *out)
609 {
610     FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
611             unk, type, buffer, len, out);
612
613     return GetStringTypeW(type, buffer, len, out);
614 }
615
616 /*********************************************************************
617  *              localeconv (MSVCRT.@)
618  */
619 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
620 {
621     return get_locinfo()->lconv;
622 }
623
624 /*********************************************************************
625  *              __lconv_init (MSVCRT.@)
626  */
627 int CDECL __lconv_init(void)
628 {
629     /* this is used to make chars unsigned */
630     charmax = 255;
631     return 0;
632 }
633
634 /*********************************************************************
635  *      ___lc_handle_func (MSVCRT.@)
636  */
637 LCID* CDECL ___lc_handle_func(void)
638 {
639     return MSVCRT___lc_handle;
640 }
641
642 /*********************************************************************
643  *      ___lc_codepage_func (MSVCRT.@)
644  */
645 unsigned int CDECL ___lc_codepage_func(void)
646 {
647     return MSVCRT___lc_codepage;
648 }
649
650 /*********************************************************************
651  *      ___lc_collate_cp_func (MSVCRT.@)
652  */
653 int CDECL ___lc_collate_cp_func(void)
654 {
655     return get_locinfo()->lc_collate_cp;
656 }
657
658 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
659 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
660 {
661     int i;
662
663     if(!locinfo)
664         return;
665
666     if(InterlockedDecrement(&locinfo->refcount))
667         return;
668
669     for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
670         MSVCRT_free(locinfo->lc_category[i].locale);
671         MSVCRT_free(locinfo->lc_category[i].refcount);
672     }
673
674     if(locinfo->lconv) {
675         MSVCRT_free(locinfo->lconv->decimal_point);
676         MSVCRT_free(locinfo->lconv->thousands_sep);
677         MSVCRT_free(locinfo->lconv->grouping);
678         MSVCRT_free(locinfo->lconv->int_curr_symbol);
679         MSVCRT_free(locinfo->lconv->currency_symbol);
680         MSVCRT_free(locinfo->lconv->mon_decimal_point);
681         MSVCRT_free(locinfo->lconv->mon_thousands_sep);
682         MSVCRT_free(locinfo->lconv->mon_grouping);
683         MSVCRT_free(locinfo->lconv->positive_sign);
684         MSVCRT_free(locinfo->lconv->negative_sign);
685     }
686     MSVCRT_free(locinfo->lconv_intl_refcount);
687     MSVCRT_free(locinfo->lconv_num_refcount);
688     MSVCRT_free(locinfo->lconv_mon_refcount);
689     MSVCRT_free(locinfo->lconv);
690
691     MSVCRT_free(locinfo->ctype1_refcount);
692     MSVCRT_free(locinfo->ctype1);
693
694     MSVCRT_free(locinfo->pclmap);
695     MSVCRT_free(locinfo->pcumap);
696
697     MSVCRT_free(locinfo->lc_time_curr);
698
699     MSVCRT_free(locinfo);
700 }
701
702 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
703 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
704 {
705     if(!mbcinfo)
706         return;
707
708     if(InterlockedDecrement(&mbcinfo->refcount))
709         return;
710
711     MSVCRT_free(mbcinfo);
712 }
713
714 /* _get_current_locale - not exported in native msvcrt */
715 MSVCRT__locale_t CDECL MSVCRT__get_current_locale(void)
716 {
717     MSVCRT__locale_t loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
718     if(!loc)
719         return NULL;
720
721     loc->locinfo = get_locinfo();
722     loc->mbcinfo = get_mbcinfo();
723     InterlockedIncrement(&loc->locinfo->refcount);
724     InterlockedIncrement(&loc->mbcinfo->refcount);
725     return loc;
726 }
727
728 /* _free_locale - not exported in native msvcrt */
729 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
730 {
731     if (!locale)
732         return;
733
734     free_locinfo(locale->locinfo);
735     free_mbcinfo(locale->mbcinfo);
736     MSVCRT_free(locale);
737 }
738
739 /* _create_locale - not exported in native msvcrt */
740 MSVCRT__locale_t CDECL MSVCRT__create_locale(int category, const char *locale)
741 {
742     static const DWORD time_data[] = {
743         LOCALE_SABBREVDAYNAME7, LOCALE_SABBREVDAYNAME1, LOCALE_SABBREVDAYNAME2,
744         LOCALE_SABBREVDAYNAME3, LOCALE_SABBREVDAYNAME4, LOCALE_SABBREVDAYNAME5,
745         LOCALE_SABBREVDAYNAME6,
746         LOCALE_SDAYNAME7, LOCALE_SDAYNAME1, LOCALE_SDAYNAME2, LOCALE_SDAYNAME3,
747         LOCALE_SDAYNAME4, LOCALE_SDAYNAME5, LOCALE_SDAYNAME6,
748         LOCALE_SABBREVMONTHNAME1, LOCALE_SABBREVMONTHNAME2, LOCALE_SABBREVMONTHNAME3,
749         LOCALE_SABBREVMONTHNAME4, LOCALE_SABBREVMONTHNAME5, LOCALE_SABBREVMONTHNAME6,
750         LOCALE_SABBREVMONTHNAME7, LOCALE_SABBREVMONTHNAME8, LOCALE_SABBREVMONTHNAME9,
751         LOCALE_SABBREVMONTHNAME10, LOCALE_SABBREVMONTHNAME11, LOCALE_SABBREVMONTHNAME12,
752         LOCALE_SMONTHNAME1, LOCALE_SMONTHNAME2, LOCALE_SMONTHNAME3, LOCALE_SMONTHNAME4,
753         LOCALE_SMONTHNAME5, LOCALE_SMONTHNAME6, LOCALE_SMONTHNAME7, LOCALE_SMONTHNAME8,
754         LOCALE_SMONTHNAME9, LOCALE_SMONTHNAME10, LOCALE_SMONTHNAME11, LOCALE_SMONTHNAME12,
755         LOCALE_S1159, LOCALE_S2359,
756         LOCALE_SSHORTDATE, LOCALE_SLONGDATE,
757         LOCALE_STIMEFORMAT
758     };
759     static const char collate[] = "COLLATE=";
760     static const char ctype[] = "CTYPE=";
761     static const char monetary[] = "MONETARY=";
762     static const char numeric[] = "NUMERIC=";
763     static const char time[] = "TIME=";
764
765     MSVCRT__locale_t loc;
766     LCID lcid[6] = { 0 };
767     char buf[256];
768     int i, ret, size;
769
770     TRACE("(%d %s)\n", category, locale);
771
772     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
773         return NULL;
774
775     if(locale[0]=='C' && !locale[1])
776         lcid[0] = CP_ACP;
777     else if(!locale[0])
778         lcid[0] = GetSystemDefaultLCID();
779     else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
780         const char *p;
781
782         while(1) {
783             locale += 3; /* LC_ */
784             if(!memcmp(locale, collate, sizeof(collate)-1)) {
785                 i = MSVCRT_LC_COLLATE;
786                 locale += sizeof(collate)-1;
787             } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
788                 i = MSVCRT_LC_CTYPE;
789                 locale += sizeof(ctype)-1;
790             } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
791                 i = MSVCRT_LC_MONETARY;
792                 locale += sizeof(monetary)-1;
793             } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
794                 i = MSVCRT_LC_NUMERIC;
795                 locale += sizeof(numeric)-1;
796             } else if(!memcmp(locale, time, sizeof(time)-1)) {
797                 i = MSVCRT_LC_TIME;
798                 locale += sizeof(time)-1;
799             } else
800                 return NULL;
801
802             p = strchr(locale, ';');
803             if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0'))
804                 lcid[i] = 0;
805             else if(p) {
806                 memcpy(buf, locale, p-locale);
807                 buf[p-locale] = '\0';
808                 lcid[i] = MSVCRT_locale_to_LCID(buf);
809             } else
810                 lcid[i] = MSVCRT_locale_to_LCID(locale);
811
812             if(lcid[i] == -1)
813                 return NULL;
814
815             if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
816                 break;
817
818             locale = p+1;
819         }
820     } else {
821         lcid[0] = MSVCRT_locale_to_LCID(locale);
822         if(lcid[0] == -1)
823             return NULL;
824     }
825
826     for(i=1; i<6; i++) {
827         if(!lcid[i])
828             lcid[i] = lcid[0];
829     }
830
831     loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
832     if(!loc)
833         return NULL;
834
835     loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
836     if(!loc->locinfo) {
837         MSVCRT_free(loc);
838         return NULL;
839     }
840
841     loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
842     if(!loc->mbcinfo) {
843         MSVCRT_free(loc->locinfo);
844         MSVCRT_free(loc);
845         return NULL;
846     }
847
848     memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
849     loc->locinfo->refcount = 1;
850     loc->mbcinfo->refcount = 1;
851
852     loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
853     if(!loc->locinfo->lconv) {
854         MSVCRT__free_locale(loc);
855         return NULL;
856     }
857     memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
858
859     loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
860     loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
861     if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
862         MSVCRT__free_locale(loc);
863         return NULL;
864     }
865
866     if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
867         if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
868             MSVCRT__free_locale(loc);
869             return NULL;
870         }
871
872         loc->locinfo->lc_collate_cp = loc->locinfo->lc_id[MSVCRT_LC_COLLATE].wCodePage;
873     } else
874         loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
875
876     if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
877         CPINFO cp;
878         int j;
879
880         if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
881             MSVCRT__free_locale(loc);
882             return NULL;
883         }
884
885         loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
886         loc->locinfo->lc_clike = 1;
887         if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
888             MSVCRT__free_locale(loc);
889             return NULL;
890         }
891         loc->locinfo->mb_cur_max = cp.MaxCharSize;
892
893         loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
894         loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
895         if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
896             MSVCRT__free_locale(loc);
897             return NULL;
898         }
899
900         *loc->locinfo->ctype1_refcount = 1;
901         loc->locinfo->ctype1[0] = 0;
902         loc->locinfo->pctype = loc->locinfo->ctype1+1;
903
904         buf[1] = buf[2] = '\0';
905         for(i=1; i<257; i++) {
906             buf[0] = i-1;
907
908             /* builtin GetStringTypeA doesn't set output to 0 on invalid input */
909             loc->locinfo->ctype1[i] = 0;
910
911             GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
912                     1, loc->locinfo->ctype1+i);
913         }
914
915         for(i=0; cp.LeadByte[i+1]!=0; i+=2)
916             for(j=cp.LeadByte[i]; j<=cp.LeadByte[i+1]; j++)
917                 loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
918     } else {
919         loc->locinfo->lc_clike = 1;
920         loc->locinfo->mb_cur_max = 1;
921         loc->locinfo->pctype = MSVCRT__ctype+1;
922         loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
923     }
924
925     for(i=0; i<256; i++) {
926         if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
927             buf[i] = ' ';
928         else
929             buf[i] = i;
930
931     }
932
933     if(lcid[MSVCRT_LC_CTYPE]) {
934         LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
935                 (char*)loc->locinfo->pclmap, 256);
936         LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
937                 (char*)loc->locinfo->pcumap, 256);
938     } else {
939         for(i=0; i<256; i++) {
940             loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
941             loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
942         }
943     }
944
945     _setmbcp_l(loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage, lcid[MSVCRT_LC_CTYPE], loc->mbcinfo);
946
947     if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
948         if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
949             MSVCRT__free_locale(loc);
950             return NULL;
951         }
952
953         loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
954         loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
955         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
956             MSVCRT__free_locale(loc);
957             return NULL;
958         }
959
960         *loc->locinfo->lconv_intl_refcount = 1;
961         *loc->locinfo->lconv_mon_refcount = 1;
962
963         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
964                 |LOCALE_NOUSEROVERRIDE, buf, 256);
965         if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(i)))
966             memcpy(loc->locinfo->lconv->int_curr_symbol, buf, i);
967         else {
968             MSVCRT__free_locale(loc);
969             return NULL;
970         }
971
972         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
973                 |LOCALE_NOUSEROVERRIDE, buf, 256);
974         if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(i)))
975             memcpy(loc->locinfo->lconv->currency_symbol, buf, i);
976         else {
977             MSVCRT__free_locale(loc);
978             return NULL;
979         }
980
981         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
982                 |LOCALE_NOUSEROVERRIDE, buf, 256);
983         if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(i)))
984             memcpy(loc->locinfo->lconv->mon_decimal_point, buf, i);
985         else {
986             MSVCRT__free_locale(loc);
987             return NULL;
988         }
989
990         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
991                 |LOCALE_NOUSEROVERRIDE, buf, 256);
992         if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(i)))
993             memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, i);
994         else {
995             MSVCRT__free_locale(loc);
996             return NULL;
997         }
998
999         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
1000                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1001         if(i>1)
1002             i = i/2 + (buf[i-2]=='0'?0:1);
1003         if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(i))) {
1004             for(i=0; buf[i+1]==';'; i+=2)
1005                 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1006             loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
1007             if(buf[i] != '0')
1008                 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
1009         } else {
1010             MSVCRT__free_locale(loc);
1011             return NULL;
1012         }
1013
1014         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
1015                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1016         if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(i)))
1017             memcpy(loc->locinfo->lconv->positive_sign, buf, i);
1018         else {
1019             MSVCRT__free_locale(loc);
1020             return NULL;
1021         }
1022
1023         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
1024                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1025         if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(i)))
1026             memcpy(loc->locinfo->lconv->negative_sign, buf, i);
1027         else {
1028             MSVCRT__free_locale(loc);
1029             return NULL;
1030         }
1031
1032         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
1033                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1034             loc->locinfo->lconv->int_frac_digits = atoi(buf);
1035         else {
1036             MSVCRT__free_locale(loc);
1037             return NULL;
1038         }
1039
1040         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
1041                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1042             loc->locinfo->lconv->frac_digits = atoi(buf);
1043         else {
1044             MSVCRT__free_locale(loc);
1045             return NULL;
1046         }
1047
1048         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
1049                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1050             loc->locinfo->lconv->p_cs_precedes = atoi(buf);
1051         else {
1052             MSVCRT__free_locale(loc);
1053             return NULL;
1054         }
1055
1056         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
1057                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1058             loc->locinfo->lconv->p_sep_by_space = atoi(buf);
1059         else {
1060             MSVCRT__free_locale(loc);
1061             return NULL;
1062         }
1063
1064         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
1065                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1066             loc->locinfo->lconv->n_cs_precedes = atoi(buf);
1067         else {
1068             MSVCRT__free_locale(loc);
1069             return NULL;
1070         }
1071
1072         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
1073                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1074             loc->locinfo->lconv->n_sep_by_space = atoi(buf);
1075         else {
1076             MSVCRT__free_locale(loc);
1077             return NULL;
1078         }
1079
1080         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
1081                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1082             loc->locinfo->lconv->p_sign_posn = atoi(buf);
1083         else {
1084             MSVCRT__free_locale(loc);
1085             return NULL;
1086         }
1087
1088         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
1089                     |LOCALE_NOUSEROVERRIDE, buf, 256))
1090             loc->locinfo->lconv->n_sign_posn = atoi(buf);
1091         else {
1092             MSVCRT__free_locale(loc);
1093             return NULL;
1094         }
1095     } else {
1096         loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1097         loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1098         loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1099         loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1100         loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1101         loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1102         loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1103
1104         if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1105                 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1106                 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1107                 || !loc->locinfo->lconv->negative_sign) {
1108             MSVCRT__free_locale(loc);
1109             return NULL;
1110         }
1111
1112         loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1113         loc->locinfo->lconv->currency_symbol[0] = '\0';
1114         loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1115         loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1116         loc->locinfo->lconv->mon_grouping[0] = '\0';
1117         loc->locinfo->lconv->positive_sign[0] = '\0';
1118         loc->locinfo->lconv->negative_sign[0] = '\0';
1119         loc->locinfo->lconv->int_frac_digits = 127;
1120         loc->locinfo->lconv->frac_digits = 127;
1121         loc->locinfo->lconv->p_cs_precedes = 127;
1122         loc->locinfo->lconv->p_sep_by_space = 127;
1123         loc->locinfo->lconv->n_cs_precedes = 127;
1124         loc->locinfo->lconv->n_sep_by_space = 127;
1125         loc->locinfo->lconv->p_sign_posn = 127;
1126         loc->locinfo->lconv->n_sign_posn = 127;
1127
1128         loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1129     }
1130
1131     if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1132         if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1133             MSVCRT__free_locale(loc);
1134             return NULL;
1135         }
1136
1137         if(!loc->locinfo->lconv_intl_refcount)
1138             loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1139         loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1140         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1141             MSVCRT__free_locale(loc);
1142             return NULL;
1143         }
1144
1145         *loc->locinfo->lconv_intl_refcount = 1;
1146         *loc->locinfo->lconv_num_refcount = 1;
1147
1148         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1149                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1150         if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(i)))
1151             memcpy(loc->locinfo->lconv->decimal_point, buf, i);
1152         else {
1153             MSVCRT__free_locale(loc);
1154             return NULL;
1155         }
1156
1157         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1158                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1159         if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(i)))
1160             memcpy(loc->locinfo->lconv->thousands_sep, buf, i);
1161         else {
1162             MSVCRT__free_locale(loc);
1163             return NULL;
1164         }
1165
1166         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1167                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1168         if(i>1)
1169             i = i/2 + (buf[i-2]=='0'?0:1);
1170         if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(i))) {
1171             for(i=0; buf[i+1]==';'; i+=2)
1172                 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1173             loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1174             if(buf[i] != '0')
1175                 loc->locinfo->lconv->grouping[i/2+1] = 127;
1176         } else {
1177             MSVCRT__free_locale(loc);
1178             return NULL;
1179         }
1180     } else {
1181         loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1182         loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1183         loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1184         if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1185                 || !loc->locinfo->lconv->grouping) {
1186             MSVCRT__free_locale(loc);
1187             return NULL;
1188         }
1189
1190         loc->locinfo->lconv->decimal_point[0] = '.';
1191         loc->locinfo->lconv->decimal_point[1] = '\0';
1192         loc->locinfo->lconv->thousands_sep[0] = '\0';
1193         loc->locinfo->lconv->grouping[0] = '\0';
1194
1195         loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1196     }
1197
1198     if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1199         if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1200             MSVCRT__free_locale(loc);
1201             return NULL;
1202         }
1203     } else
1204         loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1205
1206     size = sizeof(MSVCRT___lc_time_data);
1207     for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1208         ret = GetLocaleInfoA(lcid[MSVCRT_LC_TIME], time_data[i]
1209                 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1210         if(!ret) {
1211             MSVCRT__free_locale(loc);
1212             return NULL;
1213         }
1214         size += ret;
1215
1216         ret = GetLocaleInfoW(lcid[MSVCRT_LC_TIME], time_data[i]
1217                 |LOCALE_NOUSEROVERRIDE, NULL, 0);
1218         if(!ret) {
1219             MSVCRT__free_locale(loc);
1220             return NULL;
1221         }
1222         size += ret*sizeof(MSVCRT_wchar_t);
1223     }
1224
1225     loc->locinfo->lc_time_curr = MSVCRT_malloc(size);
1226     if(!loc->locinfo->lc_time_curr) {
1227         MSVCRT__free_locale(loc);
1228         return NULL;
1229     }
1230
1231     ret = 0;
1232     for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1233         loc->locinfo->lc_time_curr->str[i] = &loc->locinfo->lc_time_curr->data[ret];
1234         ret += GetLocaleInfoA(lcid[MSVCRT_LC_TIME], time_data[i]|LOCALE_NOUSEROVERRIDE,
1235                 &loc->locinfo->lc_time_curr->data[ret], size-ret);
1236     }
1237     for(i=0; i<sizeof(time_data)/sizeof(time_data[0]); i++) {
1238         loc->locinfo->lc_time_curr->wstr[i] = (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret];
1239         ret += GetLocaleInfoW(lcid[MSVCRT_LC_TIME], time_data[i]|LOCALE_NOUSEROVERRIDE,
1240                 (MSVCRT_wchar_t*)&loc->locinfo->lc_time_curr->data[ret], size-ret)*sizeof(MSVCRT_wchar_t);
1241     }
1242     loc->locinfo->lc_time_curr->lcid = lcid[MSVCRT_LC_TIME];
1243
1244     return loc;
1245 }
1246
1247 /*********************************************************************
1248  *             setlocale (MSVCRT.@)
1249  */
1250 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1251 {
1252     MSVCRT__locale_t loc;
1253     MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1254
1255     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1256         return NULL;
1257
1258     if(!locale) {
1259         if(category == MSVCRT_LC_ALL)
1260             return construct_lc_all(locinfo);
1261
1262         return locinfo->lc_category[category].locale;
1263     }
1264
1265     loc = MSVCRT__create_locale(category, locale);
1266     if(!loc) {
1267         WARN("%d %s failed\n", category, locale);
1268         return NULL;
1269     }
1270
1271     LOCK_LOCALE;
1272
1273     switch(category) {
1274         case MSVCRT_LC_ALL:
1275         case MSVCRT_LC_COLLATE:
1276             locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1277             locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1278                 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1279             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1280                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1281             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1282                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1283
1284             if(category != MSVCRT_LC_ALL)
1285                 break;
1286             /* fall through */
1287         case MSVCRT_LC_CTYPE:
1288             locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1289                 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1290             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1291                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1292             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1293                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1294
1295             locinfo->lc_codepage = loc->locinfo->lc_codepage;
1296             locinfo->lc_clike = loc->locinfo->lc_clike;
1297             locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1298
1299             swap_pointers((void**)&locinfo->ctype1_refcount,
1300                     (void**)&loc->locinfo->ctype1_refcount);
1301             swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1302             swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1303             swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1304             swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1305
1306             if(category != MSVCRT_LC_ALL)
1307                 break;
1308             /* fall through */
1309         case MSVCRT_LC_MONETARY:
1310             locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1311                 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1312             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1313                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1314             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1315                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1316
1317             swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1318                     (void**)&loc->locinfo->lconv->int_curr_symbol);
1319             swap_pointers((void**)&locinfo->lconv->currency_symbol,
1320                     (void**)&loc->locinfo->lconv->currency_symbol);
1321             swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1322                     (void**)&loc->locinfo->lconv->mon_decimal_point);
1323             swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1324                     (void**)&loc->locinfo->lconv->mon_thousands_sep);
1325             swap_pointers((void**)&locinfo->lconv->mon_grouping,
1326                     (void**)&loc->locinfo->lconv->mon_grouping);
1327             swap_pointers((void**)&locinfo->lconv->positive_sign,
1328                     (void**)&loc->locinfo->lconv->positive_sign);
1329             swap_pointers((void**)&locinfo->lconv->negative_sign,
1330                     (void**)&loc->locinfo->lconv->negative_sign);
1331             locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1332             locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1333             locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1334             locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1335             locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1336             locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1337             locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1338             locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1339
1340             if(category != MSVCRT_LC_ALL)
1341                 break;
1342             /* fall through */
1343         case MSVCRT_LC_NUMERIC:
1344             locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1345                 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1346             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1347                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1348             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1349                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1350
1351             swap_pointers((void**)&locinfo->lconv->decimal_point,
1352                     (void**)&loc->locinfo->lconv->decimal_point);
1353             swap_pointers((void**)&locinfo->lconv->thousands_sep,
1354                     (void**)&loc->locinfo->lconv->thousands_sep);
1355             swap_pointers((void**)&locinfo->lconv->grouping,
1356                     (void**)&loc->locinfo->lconv->grouping);
1357
1358             if(category != MSVCRT_LC_ALL)
1359                 break;
1360             /* fall through */
1361         case MSVCRT_LC_TIME:
1362             locinfo->lc_handle[MSVCRT_LC_TIME] =
1363                 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1364             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1365                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1366             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1367                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1368             swap_pointers((void**)&locinfo->lc_time_curr,
1369                     (void**)&loc->locinfo->lc_time_curr);
1370
1371             if(category != MSVCRT_LC_ALL)
1372                 break;
1373     }
1374
1375     MSVCRT__free_locale(loc);
1376     UNLOCK_LOCALE;
1377
1378     if(locinfo == MSVCRT_locale->locinfo) {
1379         int i;
1380
1381         MSVCRT___lc_codepage = locinfo->lc_codepage;
1382         MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1383         MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1384         MSVCRT__pctype = locinfo->pctype;
1385         for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1386             MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1387     }
1388
1389     if(category == MSVCRT_LC_ALL)
1390         return construct_lc_all(locinfo);
1391
1392     return locinfo->lc_category[category].locale;
1393 }
1394
1395 /* _configthreadlocale - not exported in native msvcrt */
1396 int CDECL _configthreadlocale(int type)
1397 {
1398     thread_data_t *data = msvcrt_get_thread_data();
1399     MSVCRT__locale_t locale;
1400     int ret;
1401
1402     if(!data)
1403         return -1;
1404
1405     ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1406
1407     if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1408         if(!data->have_locale) {
1409             /* Copy current global locale */
1410             locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1411             if(!locale)
1412                 return -1;
1413
1414             data->locinfo = locale->locinfo;
1415             data->mbcinfo = locale->mbcinfo;
1416             data->have_locale = TRUE;
1417             MSVCRT_free(locale);
1418         }
1419
1420         return ret;
1421     }
1422
1423     if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1424         if(data->have_locale) {
1425             free_locinfo(data->locinfo);
1426             free_mbcinfo(data->mbcinfo);
1427             data->locinfo = MSVCRT_locale->locinfo;
1428             data->mbcinfo = MSVCRT_locale->mbcinfo;
1429             data->have_locale = FALSE;
1430         }
1431
1432         return ret;
1433     }
1434
1435     if(!type)
1436         return ret;
1437
1438     return -1;
1439 }
1440
1441 BOOL msvcrt_init_locale(void)
1442 {
1443     int i;
1444
1445     LOCK_LOCALE;
1446     MSVCRT_locale = MSVCRT__create_locale(0, "C");
1447     UNLOCK_LOCALE;
1448     if(!MSVCRT_locale)
1449         return FALSE;
1450
1451     MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1452     MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1453     MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1454     MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1455     for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1456         MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1457     _setmbcp(_MB_CP_ANSI);
1458     return TRUE;
1459 }