urlmon: Skip more tests on too old IEs.
[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 int MSVCRT___lc_codepage = 0;
45 int MSVCRT___lc_collate_cp = 0;
46 HANDLE MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
47 unsigned char charmax = CHAR_MAX;
48
49 /* MT */
50 #define LOCK_LOCALE   _mlock(_SETLOCALE_LOCK);
51 #define UNLOCK_LOCALE _munlock(_SETLOCALE_LOCK);
52
53 #define MSVCRT_LEADBYTE  0x8000
54 #define MSVCRT_C1_DEFINED 0x200
55
56 /* Friendly country strings & language names abbreviations. */
57 static const char * const _country_synonyms[] =
58 {
59     "american", "enu",
60     "american english", "enu",
61     "american-english", "enu",
62     "english-american", "enu",
63     "english-us", "enu",
64     "english-usa", "enu",
65     "us", "enu",
66     "usa", "enu",
67     "australian", "ena",
68     "english-aus", "ena",
69     "belgian", "nlb",
70     "french-belgian", "frb",
71     "canadian", "enc",
72     "english-can", "enc",
73     "french-canadian", "frc",
74     "chinese", "chs",
75     "chinese-simplified", "chs",
76     "chinese-traditional", "cht",
77     "dutch-belgian", "nlb",
78     "english-nz", "enz",
79     "uk", "eng",
80     "english-uk", "eng",
81     "french-swiss", "frs",
82     "swiss", "des",
83     "german-swiss", "des",
84     "italian-swiss", "its",
85     "german-austrian", "dea",
86     "portugese", "ptb",
87     "portuguese-brazil", "ptb",
88     "spanish-mexican", "esm",
89     "norwegian-bokmal", "nor",
90     "norwegian-nynorsk", "non",
91     "spanish-modern", "esn"
92 };
93
94 /* INTERNAL: Map a synonym to an ISO code */
95 static void remap_synonym(char *name)
96 {
97   unsigned int i;
98   for (i = 0; i < sizeof(_country_synonyms)/sizeof(char*); i += 2 )
99   {
100     if (!strcasecmp(_country_synonyms[i],name))
101     {
102       TRACE(":Mapping synonym %s to %s\n",name,_country_synonyms[i+1]);
103       memcpy(name, _country_synonyms[i+1], sizeof(_country_synonyms[i+1]));
104       return;
105     }
106   }
107 }
108
109 /* Note: Flags are weighted in order of matching importance */
110 #define FOUND_LANGUAGE         0x4
111 #define FOUND_COUNTRY          0x2
112 #define FOUND_CODEPAGE         0x1
113
114 typedef struct {
115   char search_language[MAX_ELEM_LEN];
116   char search_country[MAX_ELEM_LEN];
117   char search_codepage[MAX_ELEM_LEN];
118   char found_codepage[MAX_ELEM_LEN];
119   unsigned int match_flags;
120   LANGID found_lang_id;
121 } locale_search_t;
122
123 #define CONTINUE_LOOKING TRUE
124 #define STOP_LOOKING     FALSE
125
126 /* INTERNAL: Get and compare locale info with a given string */
127 static int compare_info(LCID lcid, DWORD flags, char* buff, const char* cmp, BOOL exact)
128 {
129   int len;
130
131   buff[0] = 0;
132   GetLocaleInfoA(lcid, flags|LOCALE_NOUSEROVERRIDE, buff, MAX_ELEM_LEN);
133   if (!buff[0] || !cmp[0])
134     return 0;
135
136   /* Partial matches are only allowed on language/country names */
137   len = strlen(cmp);
138   if(exact || len<=3)
139     return !strcasecmp(cmp, buff);
140   else
141     return !strncasecmp(cmp, buff, len);
142 }
143
144 static BOOL CALLBACK
145 find_best_locale_proc(HMODULE hModule, LPCSTR type, LPCSTR name, WORD LangID, LONG_PTR lParam)
146 {
147   locale_search_t *res = (locale_search_t *)lParam;
148   const LCID lcid = MAKELCID(LangID, SORT_DEFAULT);
149   char buff[MAX_ELEM_LEN];
150   unsigned int flags = 0;
151
152   if(PRIMARYLANGID(LangID) == LANG_NEUTRAL)
153     return CONTINUE_LOOKING;
154
155   /* Check Language */
156   if (compare_info(lcid,LOCALE_SISO639LANGNAME,buff,res->search_language, TRUE) ||
157       compare_info(lcid,LOCALE_SABBREVLANGNAME,buff,res->search_language, TRUE) ||
158       compare_info(lcid,LOCALE_SENGLANGUAGE,buff,res->search_language, FALSE))
159   {
160     TRACE(":Found language: %s->%s\n", res->search_language, buff);
161     flags |= FOUND_LANGUAGE;
162   }
163   else if (res->match_flags & FOUND_LANGUAGE)
164   {
165     return CONTINUE_LOOKING;
166   }
167
168   /* Check Country */
169   if (compare_info(lcid,LOCALE_SISO3166CTRYNAME,buff,res->search_country, TRUE) ||
170       compare_info(lcid,LOCALE_SABBREVCTRYNAME,buff,res->search_country, TRUE) ||
171       compare_info(lcid,LOCALE_SENGCOUNTRY,buff,res->search_country, FALSE))
172   {
173     TRACE("Found country:%s->%s\n", res->search_country, buff);
174     flags |= FOUND_COUNTRY;
175   }
176   else if (res->match_flags & FOUND_COUNTRY)
177   {
178     return CONTINUE_LOOKING;
179   }
180
181   /* Check codepage */
182   if (compare_info(lcid,LOCALE_IDEFAULTCODEPAGE,buff,res->search_codepage, TRUE) ||
183       (compare_info(lcid,LOCALE_IDEFAULTANSICODEPAGE,buff,res->search_codepage, TRUE)))
184   {
185     TRACE("Found codepage:%s->%s\n", res->search_codepage, buff);
186     flags |= FOUND_CODEPAGE;
187     memcpy(res->found_codepage,res->search_codepage,MAX_ELEM_LEN);
188   }
189   else if (res->match_flags & FOUND_CODEPAGE)
190   {
191     return CONTINUE_LOOKING;
192   }
193
194   if (flags > res->match_flags)
195   {
196     /* Found a better match than previously */
197     res->match_flags = flags;
198     res->found_lang_id = LangID;
199   }
200   if ((flags & (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE)) ==
201         (FOUND_LANGUAGE | FOUND_COUNTRY | FOUND_CODEPAGE))
202   {
203     TRACE(":found exact locale match\n");
204     return STOP_LOOKING;
205   }
206   return CONTINUE_LOOKING;
207 }
208
209 extern int atoi(const char *);
210
211 /* Internal: Find the LCID for a locale specification */
212 static LCID MSVCRT_locale_to_LCID(const char *locale)
213 {
214     LCID lcid;
215     locale_search_t search;
216     char *cp, *region;
217
218     memset(&search, 0, sizeof(locale_search_t));
219
220     cp = strchr(locale, '.');
221     region = strchr(locale, '_');
222
223     lstrcpynA(search.search_language, locale, MAX_ELEM_LEN);
224     if(region) {
225         lstrcpynA(search.search_country, region+1, MAX_ELEM_LEN);
226         if(region-locale < MAX_ELEM_LEN)
227             search.search_language[region-locale] = '\0';
228     } else
229         search.search_country[0] = '\0';
230
231     if(cp) {
232         lstrcpynA(search.search_codepage, cp+1, MAX_ELEM_LEN);
233         if(cp-region-1 < MAX_ELEM_LEN)
234           search.search_country[cp-region-1] = '\0';
235         if(cp-locale < MAX_ELEM_LEN)
236             search.search_language[cp-locale] = '\0';
237     } else
238         search.search_codepage[0] = '\0';
239
240     if(!search.search_country[0] && !search.search_codepage[0])
241         remap_synonym(search.search_language);
242
243     EnumResourceLanguagesA(GetModuleHandleA("KERNEL32"), (LPSTR)RT_STRING,
244             (LPCSTR)LOCALE_ILANGUAGE,find_best_locale_proc,
245             (LONG_PTR)&search);
246
247     if (!search.match_flags)
248         return -1;
249
250     /* If we were given something that didn't match, fail */
251     if (search.search_country[0] && !(search.match_flags & FOUND_COUNTRY))
252         return -1;
253
254     lcid =  MAKELCID(search.found_lang_id, SORT_DEFAULT);
255
256     /* Populate partial locale, translating LCID to locale string elements */
257     if (!search.found_codepage[0]) {
258         /* Even if a codepage is not enumerated for a locale
259          * it can be set if valid */
260         if (search.search_codepage[0]) {
261             if (IsValidCodePage(atoi(search.search_codepage)))
262                 memcpy(search.found_codepage,search.search_codepage,MAX_ELEM_LEN);
263             else {
264                 /* Special codepage values: OEM & ANSI */
265                 if (strcasecmp(search.search_codepage,"OCP")) {
266                     GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
267                             search.found_codepage, MAX_ELEM_LEN);
268                 } else if (strcasecmp(search.search_codepage,"ACP")) {
269                     GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
270                             search.found_codepage, MAX_ELEM_LEN);
271                 } else
272                     return -1;
273
274                 if (!atoi(search.found_codepage))
275                     return -1;
276             }
277         } else {
278             /* Prefer ANSI codepages if present */
279             GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE,
280                     search.found_codepage, MAX_ELEM_LEN);
281             if (!search.found_codepage[0] || !atoi(search.found_codepage))
282                 GetLocaleInfoA(lcid, LOCALE_IDEFAULTCODEPAGE,
283                         search.found_codepage, MAX_ELEM_LEN);
284         }
285     }
286
287     return lcid;
288 }
289
290 /* INTERNAL: Set lc_handle, lc_id and lc_category in threadlocinfo struct */
291 static BOOL update_threadlocinfo_category(LCID lcid, MSVCRT__locale_t loc, int category)
292 {
293     char buf[256], *p;
294     int len;
295
296     if(GetLocaleInfoA(lcid, LOCALE_ILANGUAGE|LOCALE_NOUSEROVERRIDE, buf, 256)) {
297         p = buf;
298
299         loc->locinfo->lc_id[category].wLanguage = 0;
300         while(*p) {
301             loc->locinfo->lc_id[category].wLanguage *= 16;
302
303             if(*p <= '9')
304                 loc->locinfo->lc_id[category].wLanguage += *p-'0';
305             else
306                 loc->locinfo->lc_id[category].wLanguage += *p-'a'+10;
307
308             p++;
309         }
310
311         loc->locinfo->lc_id[category].wCountry =
312             loc->locinfo->lc_id[category].wLanguage;
313     }
314
315     if(GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
316                 |LOCALE_NOUSEROVERRIDE, buf, 256))
317         loc->locinfo->lc_id[category].wCodePage = atoi(buf);
318
319     loc->locinfo->lc_handle[category] = lcid;
320
321     len = 0;
322     len += GetLocaleInfoA(lcid, LOCALE_SENGLANGUAGE
323             |LOCALE_NOUSEROVERRIDE, buf, 256);
324     buf[len-1] = '_';
325     len += GetLocaleInfoA(lcid, LOCALE_SENGCOUNTRY
326             |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
327     buf[len-1] = '.';
328     len += GetLocaleInfoA(lcid, LOCALE_IDEFAULTANSICODEPAGE
329             |LOCALE_NOUSEROVERRIDE, &buf[len], 256-len);
330
331     loc->locinfo->lc_category[category].locale = MSVCRT_malloc(sizeof(char[len]));
332     loc->locinfo->lc_category[category].refcount = MSVCRT_malloc(sizeof(int));
333     if(!loc->locinfo->lc_category[category].locale
334             || !loc->locinfo->lc_category[category].refcount) {
335         MSVCRT_free(loc->locinfo->lc_category[category].locale);
336         MSVCRT_free(loc->locinfo->lc_category[category].refcount);
337         loc->locinfo->lc_category[category].locale = NULL;
338         loc->locinfo->lc_category[category].refcount = NULL;
339         return TRUE;
340     }
341     memcpy(loc->locinfo->lc_category[category].locale, buf, sizeof(char[len]));
342     *loc->locinfo->lc_category[category].refcount = 1;
343
344     return FALSE;
345 }
346
347 /* INTERNAL: swap pointers values */
348 static inline void swap_pointers(void **p1, void **p2) {
349     void *hlp;
350
351     hlp = *p1;
352     *p1 = *p2;
353     *p2 = hlp;
354 }
355
356 /* INTERNAL: returns _locale_t struct for current locale */
357 MSVCRT__locale_t get_locale(void) {
358     thread_data_t *data = msvcrt_get_thread_data();
359
360     if(!data || !data->locale)
361         return MSVCRT_locale;
362
363     return data->locale;
364 }
365
366 /* INTERNAL: constructs string returned by setlocale */
367 static inline char* construct_lc_all(MSVCRT__locale_t cur) {
368     static char current_lc_all[MAX_LOCALE_LENGTH];
369
370     int i;
371
372     for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
373         if(strcmp(cur->locinfo->lc_category[i].locale,
374                     cur->locinfo->lc_category[i+1].locale))
375             break;
376     }
377
378     if(i==MSVCRT_LC_MAX)
379         return cur->locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
380
381     sprintf(current_lc_all,
382             "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
383             cur->locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
384             cur->locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
385             cur->locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
386             cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
387             cur->locinfo->lc_category[MSVCRT_LC_TIME].locale);
388
389     return current_lc_all;
390 }
391
392
393 /*********************************************************************
394  *              wsetlocale (MSVCRT.@)
395  */
396 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
397 {
398   static MSVCRT_wchar_t fake[] = {
399     'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
400     'S','t','a','t','e','s','.','1','2','5','2',0 };
401
402   FIXME("%d %s\n", category, debugstr_w(locale));
403
404   return fake;
405 }
406
407 /*********************************************************************
408  *              _Getdays (MSVCRT.@)
409  */
410 const char* CDECL _Getdays(void)
411 {
412   static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
413                             "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
414   /* FIXME: Use locale */
415   TRACE("(void) semi-stub\n");
416   return MSVCRT_days;
417 }
418
419 /*********************************************************************
420  *              _Getmonths (MSVCRT.@)
421  */
422 const char* CDECL _Getmonths(void)
423 {
424   static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
425                 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
426                 "October:Nov:November:Dec:December";
427   /* FIXME: Use locale */
428   TRACE("(void) semi-stub\n");
429   return MSVCRT_months;
430 }
431
432 /*********************************************************************
433  *              _Gettnames (MSVCRT.@)
434  */
435 const char* CDECL _Gettnames(void)
436 {
437   /* FIXME: */
438   TRACE("(void) stub\n");
439   return "";
440 }
441
442 /*********************************************************************
443  *              _Strftime (MSVCRT.@)
444  */
445 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
446                             const void *tm, void *foo)
447 {
448   /* FIXME: */
449   TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
450   return "";
451 }
452
453 /*********************************************************************
454  *              __crtLCMapStringA (MSVCRT.@)
455  */
456 int CDECL __crtLCMapStringA(
457   LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
458   int dstlen, unsigned int codepage, int xflag
459 ) {
460   FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
461         lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
462   /* FIXME: A bit incorrect. But msvcrt itself just converts its
463    * arguments to wide strings and then calls LCMapStringW
464    */
465   return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
466 }
467
468 /*********************************************************************
469  *              __crtLCMapStringW (MSVCRT.@)
470  */
471 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
472         int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
473 {
474     FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
475             lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
476
477     return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
478 }
479
480 /*********************************************************************
481  *              __crtCompareStringA (MSVCRT.@)
482  */
483 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
484                                const char *src2, int len2 )
485 {
486     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
487           lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
488     /* FIXME: probably not entirely right */
489     return CompareStringA( lcid, flags, src1, len1, src2, len2 );
490 }
491
492 /*********************************************************************
493  *              __crtCompareStringW (MSVCRT.@)
494  */
495 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
496                                const MSVCRT_wchar_t *src2, int len2 )
497 {
498     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
499           lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
500     /* FIXME: probably not entirely right */
501     return CompareStringW( lcid, flags, src1, len1, src2, len2 );
502 }
503
504 /*********************************************************************
505  *              __crtGetLocaleInfoW (MSVCRT.@)
506  */
507 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
508 {
509     FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
510     /* FIXME: probably not entirely right */
511     return GetLocaleInfoW( lcid, type, buffer, len );
512 }
513
514 /*********************************************************************
515  *              btowc(MSVCRT.@)
516  */
517 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
518 {
519     MSVCRT__locale_t locale = get_locale();
520     unsigned char letter = c;
521     MSVCRT_wchar_t ret;
522
523     if(!MultiByteToWideChar(locale->locinfo->lc_handle[MSVCRT_LC_CTYPE],
524                 0, (LPCSTR)&letter, 1, &ret, 1))
525         return 0;
526
527     return ret;
528 }
529
530 /*********************************************************************
531  *              __crtGetStringTypeW(MSVCRT.@)
532  *
533  * This function was accepting different number of arguments in older
534  * versions of msvcrt.
535  */
536 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
537         MSVCRT_wchar_t *buffer, int len, WORD *out)
538 {
539     FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
540             unk, type, buffer, len, out);
541
542     return GetStringTypeW(type, buffer, len, out);
543 }
544
545 /*********************************************************************
546  *              localeconv (MSVCRT.@)
547  */
548 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
549 {
550     return get_locale()->locinfo->lconv;
551 }
552
553 /*********************************************************************
554  *              __lconv_init (MSVCRT.@)
555  */
556 int CDECL __lconv_init(void)
557 {
558     /* this is used to make chars unsigned */
559     charmax = 255;
560     return 0;
561 }
562
563 /*********************************************************************
564  *      ___lc_handle_func (MSVCRT.@)
565  */
566 HANDLE * CDECL ___lc_handle_func(void)
567 {
568     return MSVCRT___lc_handle;
569 }
570
571 /*********************************************************************
572  *      ___lc_codepage_func (MSVCRT.@)
573  */
574 int CDECL ___lc_codepage_func(void)
575 {
576     return MSVCRT___lc_codepage;
577 }
578
579 /*********************************************************************
580  *      ___lc_collate_cp_func (MSVCRT.@)
581  */
582 int CDECL ___lc_collate_cp_func(void)
583 {
584     return get_locale()->locinfo->lc_collate_cp;
585 }
586
587 /* _free_locale - not exported in native msvcrt */
588 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
589 {
590     int i;
591
592     if (!locale)
593         return;
594
595     for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
596         MSVCRT_free(locale->locinfo->lc_category[i].locale);
597         MSVCRT_free(locale->locinfo->lc_category[i].refcount);
598     }
599
600     if(locale->locinfo->lconv) {
601         MSVCRT_free(locale->locinfo->lconv->decimal_point);
602         MSVCRT_free(locale->locinfo->lconv->thousands_sep);
603         MSVCRT_free(locale->locinfo->lconv->grouping);
604         MSVCRT_free(locale->locinfo->lconv->int_curr_symbol);
605         MSVCRT_free(locale->locinfo->lconv->currency_symbol);
606         MSVCRT_free(locale->locinfo->lconv->mon_decimal_point);
607         MSVCRT_free(locale->locinfo->lconv->mon_thousands_sep);
608         MSVCRT_free(locale->locinfo->lconv->mon_grouping);
609         MSVCRT_free(locale->locinfo->lconv->positive_sign);
610         MSVCRT_free(locale->locinfo->lconv->negative_sign);
611     }
612     MSVCRT_free(locale->locinfo->lconv_intl_refcount);
613     MSVCRT_free(locale->locinfo->lconv_num_refcount);
614     MSVCRT_free(locale->locinfo->lconv_mon_refcount);
615     MSVCRT_free(locale->locinfo->lconv);
616
617     MSVCRT_free(locale->locinfo->ctype1_refcount);
618     MSVCRT_free(locale->locinfo->ctype1);
619
620     MSVCRT_free(locale->locinfo->pclmap);
621     MSVCRT_free(locale->locinfo->pcumap);
622
623     MSVCRT_free(locale->locinfo);
624     MSVCRT_free(locale->mbcinfo);
625     MSVCRT_free(locale);
626 }
627
628 /* _create_locale - not exported in native msvcrt */
629 MSVCRT__locale_t MSVCRT__create_locale(int category, const char *locale)
630 {
631     static const char collate[] = "COLLATE=";
632     static const char ctype[] = "CTYPE=";
633     static const char monetary[] = "MONETARY=";
634     static const char numeric[] = "NUMERIC=";
635     static const char time[] = "TIME=";
636
637     MSVCRT__locale_t loc;
638     LCID lcid[6] = { 0 };
639     char buf[256];
640     int i;
641
642     TRACE("(%d %s)\n", category, locale);
643
644     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
645         return NULL;
646
647     if(locale[0]=='C' && !locale[1])
648         lcid[0] = CP_ACP;
649     else if(!locale[0])
650         lcid[0] = GetSystemDefaultLCID();
651     else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
652         char *p;
653
654         while(1) {
655             locale += 3; /* LC_ */
656             if(!memcmp(locale, collate, sizeof(collate)-1)) {
657                 i = MSVCRT_LC_COLLATE;
658                 locale += sizeof(collate)-1;
659             } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
660                 i = MSVCRT_LC_CTYPE;
661                 locale += sizeof(ctype)-1;
662             } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
663                 i = MSVCRT_LC_MONETARY;
664                 locale += sizeof(monetary)-1;
665             } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
666                 i = MSVCRT_LC_NUMERIC;
667                 locale += sizeof(numeric)-1;
668             } else if(!memcmp(locale, time, sizeof(time)-1)) {
669                 i = MSVCRT_LC_TIME;
670                 locale += sizeof(time)-1;
671             } else
672                 return NULL;
673
674             p = strchr(locale, ';');
675             if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0'))
676                 lcid[i] = 0;
677             else if(p) {
678                 memcpy(buf, locale, p-locale);
679                 lcid[i] = MSVCRT_locale_to_LCID(buf);
680             } else
681                 lcid[i] = MSVCRT_locale_to_LCID(locale);
682
683             if(lcid[i] == -1)
684                 return NULL;
685
686             if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
687                 break;
688
689             locale = p+1;
690         }
691     } else {
692         lcid[0] = MSVCRT_locale_to_LCID(locale);
693         if(lcid[0] == -1)
694             return NULL;
695     }
696
697     for(i=1; i<6; i++) {
698         if(!lcid[i])
699             lcid[i] = lcid[0];
700     }
701
702     loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
703     if(!loc)
704         return NULL;
705
706     loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
707     if(!loc->locinfo) {
708         MSVCRT_free(loc);
709         return NULL;
710     }
711
712     loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
713     if(!loc->mbcinfo) {
714         MSVCRT_free(loc->locinfo);
715         MSVCRT_free(loc);
716         return NULL;
717     }
718
719     memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
720     memset(loc->mbcinfo, 0, sizeof(MSVCRT_threadmbcinfo));
721
722     loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
723     if(!loc->locinfo->lconv) {
724         MSVCRT__free_locale(loc);
725         return NULL;
726     }
727     memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
728
729     loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
730     loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
731     if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
732         MSVCRT__free_locale(loc);
733         return NULL;
734     }
735
736     loc->locinfo->refcount = 1;
737
738     if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
739         if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
740             MSVCRT__free_locale(loc);
741             return NULL;
742         }
743     } else
744         loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = _strdup("C");
745
746     if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
747         CPINFO cp;
748
749         if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
750             MSVCRT__free_locale(loc);
751             return NULL;
752         }
753
754         loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
755         loc->locinfo->lc_collate_cp = loc->locinfo->lc_codepage;
756         loc->locinfo->lc_clike = 1;
757         if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
758             MSVCRT__free_locale(loc);
759             return NULL;
760         }
761         loc->locinfo->mb_cur_max = cp.MaxCharSize;
762
763         loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
764         loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
765         if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
766             MSVCRT__free_locale(loc);
767             return NULL;
768         }
769
770         *loc->locinfo->ctype1_refcount = 1;
771         loc->locinfo->ctype1[0] = 0;
772         loc->locinfo->pctype = loc->locinfo->ctype1+1;
773
774         buf[1] = buf[2] = '\0';
775         for(i=1; i<257; i++) {
776             buf[0] = i-1;
777
778             GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
779                     1, loc->locinfo->ctype1+i);
780             loc->locinfo->ctype1[i] |= 0x200;
781         }
782     } else {
783         loc->locinfo->lc_clike = 1;
784         loc->locinfo->mb_cur_max = 1;
785         loc->locinfo->pctype = MSVCRT__ctype+1;
786         loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = _strdup("C");
787     }
788
789     for(i=0; i<256; i++)
790         buf[i] = i;
791
792     LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
793             (char*)loc->locinfo->pclmap, 256);
794     LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
795             (char*)loc->locinfo->pcumap, 256);
796
797     loc->mbcinfo->refcount = 1;
798     loc->mbcinfo->mbcodepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
799
800     for(i=0; i<256; i++) {
801         if(loc->locinfo->pclmap[i] != i) {
802             loc->mbcinfo->mbctype[i+1] |= 0x10;
803             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pclmap[i];
804         } else if(loc->locinfo->pcumap[i] != i) {
805             loc->mbcinfo->mbctype[i+1] |= 0x20;
806             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pcumap[i];
807         }
808     }
809
810     if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
811         if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
812             MSVCRT__free_locale(loc);
813             return NULL;
814         }
815
816         loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
817         loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
818         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
819             MSVCRT__free_locale(loc);
820             return NULL;
821         }
822
823         *loc->locinfo->lconv_intl_refcount = 1;
824         *loc->locinfo->lconv_mon_refcount = 1;
825
826         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
827                 |LOCALE_NOUSEROVERRIDE, buf, 256);
828         if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char[i]))))
829             memcpy(loc->locinfo->lconv->int_curr_symbol, buf, sizeof(char[i]));
830         else {
831             MSVCRT__free_locale(loc);
832             return NULL;
833         }
834
835         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
836                 |LOCALE_NOUSEROVERRIDE, buf, 256);
837         if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char[i]))))
838             memcpy(loc->locinfo->lconv->currency_symbol, buf, sizeof(char[i]));
839         else {
840             MSVCRT__free_locale(loc);
841             return NULL;
842         }
843
844         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
845                 |LOCALE_NOUSEROVERRIDE, buf, 256);
846         if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char[i]))))
847             memcpy(loc->locinfo->lconv->mon_decimal_point, buf, sizeof(char[i]));
848         else {
849             MSVCRT__free_locale(loc);
850             return NULL;
851         }
852
853         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
854                 |LOCALE_NOUSEROVERRIDE, buf, 256);
855         if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
856             memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, sizeof(char[i]));
857         else {
858             MSVCRT__free_locale(loc);
859             return NULL;
860         }
861
862         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
863                 |LOCALE_NOUSEROVERRIDE, buf, 256);
864         if(i>1)
865             i = i/2 + (buf[i-2]=='0'?0:1);
866         if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char[i])))) {
867             for(i=0; buf[i+1]==';'; i+=2)
868                 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
869             loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
870             if(buf[i] != '0')
871                 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
872         } else {
873             MSVCRT__free_locale(loc);
874             return NULL;
875         }
876
877         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
878                 |LOCALE_NOUSEROVERRIDE, buf, 256);
879         if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char[i]))))
880             memcpy(loc->locinfo->lconv->positive_sign, buf, sizeof(char[i]));
881         else {
882             MSVCRT__free_locale(loc);
883             return NULL;
884         }
885
886         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
887                 |LOCALE_NOUSEROVERRIDE, buf, 256);
888         if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char[i]))))
889             memcpy(loc->locinfo->lconv->negative_sign, buf, sizeof(char[i]));
890         else {
891             MSVCRT__free_locale(loc);
892             return NULL;
893         }
894
895         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
896                     |LOCALE_NOUSEROVERRIDE, buf, 256))
897             loc->locinfo->lconv->int_frac_digits = atoi(buf);
898         else {
899             MSVCRT__free_locale(loc);
900             return NULL;
901         }
902
903         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
904                     |LOCALE_NOUSEROVERRIDE, buf, 256))
905             loc->locinfo->lconv->frac_digits = atoi(buf);
906         else {
907             MSVCRT__free_locale(loc);
908             return NULL;
909         }
910
911         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
912                     |LOCALE_NOUSEROVERRIDE, buf, 256))
913             loc->locinfo->lconv->p_cs_precedes = atoi(buf);
914         else {
915             MSVCRT__free_locale(loc);
916             return NULL;
917         }
918
919         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
920                     |LOCALE_NOUSEROVERRIDE, buf, 256))
921             loc->locinfo->lconv->p_sep_by_space = atoi(buf);
922         else {
923             MSVCRT__free_locale(loc);
924             return NULL;
925         }
926
927         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
928                     |LOCALE_NOUSEROVERRIDE, buf, 256))
929             loc->locinfo->lconv->n_cs_precedes = atoi(buf);
930         else {
931             MSVCRT__free_locale(loc);
932             return NULL;
933         }
934
935         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
936                     |LOCALE_NOUSEROVERRIDE, buf, 256))
937             loc->locinfo->lconv->n_sep_by_space = atoi(buf);
938         else {
939             MSVCRT__free_locale(loc);
940             return NULL;
941         }
942
943         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
944                     |LOCALE_NOUSEROVERRIDE, buf, 256))
945             loc->locinfo->lconv->p_sign_posn = atoi(buf);
946         else {
947             MSVCRT__free_locale(loc);
948             return NULL;
949         }
950
951         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
952                     |LOCALE_NOUSEROVERRIDE, buf, 256))
953             loc->locinfo->lconv->n_sign_posn = atoi(buf);
954         else {
955             MSVCRT__free_locale(loc);
956             return NULL;
957         }
958     } else {
959         loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
960         loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
961         loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
962         loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
963         loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
964         loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
965         loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
966
967         if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
968                 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
969                 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
970                 || !loc->locinfo->lconv->negative_sign) {
971             MSVCRT__free_locale(loc);
972             return NULL;
973         }
974
975         loc->locinfo->lconv->int_curr_symbol[0] = '\0';
976         loc->locinfo->lconv->currency_symbol[0] = '\0';
977         loc->locinfo->lconv->mon_decimal_point[0] = '\0';
978         loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
979         loc->locinfo->lconv->mon_grouping[0] = '\0';
980         loc->locinfo->lconv->positive_sign[0] = '\0';
981         loc->locinfo->lconv->negative_sign[0] = '\0';
982         loc->locinfo->lconv->int_frac_digits = 127;
983         loc->locinfo->lconv->frac_digits = 127;
984         loc->locinfo->lconv->p_cs_precedes = 127;
985         loc->locinfo->lconv->p_sep_by_space = 127;
986         loc->locinfo->lconv->n_cs_precedes = 127;
987         loc->locinfo->lconv->n_sep_by_space = 127;
988         loc->locinfo->lconv->p_sign_posn = 127;
989         loc->locinfo->lconv->n_sign_posn = 127;
990
991         loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = _strdup("C");
992     }
993
994     if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
995         if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
996             MSVCRT__free_locale(loc);
997             return NULL;
998         }
999
1000         if(!loc->locinfo->lconv_intl_refcount)
1001             loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1002         loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1003         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1004             MSVCRT__free_locale(loc);
1005             return NULL;
1006         }
1007
1008         *loc->locinfo->lconv_intl_refcount = 1;
1009         *loc->locinfo->lconv_num_refcount = 1;
1010
1011         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1012                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1013         if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[i]))))
1014             memcpy(loc->locinfo->lconv->decimal_point, buf, sizeof(char[i]));
1015         else {
1016             MSVCRT__free_locale(loc);
1017             return NULL;
1018         }
1019
1020         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1021                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1022         if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
1023             memcpy(loc->locinfo->lconv->thousands_sep, buf, sizeof(char[i]));
1024         else {
1025             MSVCRT__free_locale(loc);
1026             return NULL;
1027         }
1028
1029         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1030                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1031         if(i>1)
1032             i = i/2 + (buf[i-2]=='0'?0:1);
1033         if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char[i])))) {
1034             for(i=0; buf[i+1]==';'; i+=2)
1035                 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1036             loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1037             if(buf[i] != '0')
1038                 loc->locinfo->lconv->grouping[i/2+1] = 127;
1039         } else {
1040             MSVCRT__free_locale(loc);
1041             return NULL;
1042         }
1043     } else {
1044         loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1045         loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1046         loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1047         if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1048                 || !loc->locinfo->lconv->grouping) {
1049             MSVCRT__free_locale(loc);
1050             return NULL;
1051         }
1052
1053         loc->locinfo->lconv->decimal_point[0] = '.';
1054         loc->locinfo->lconv->decimal_point[1] = '\0';
1055         loc->locinfo->lconv->thousands_sep[0] = '\0';
1056         loc->locinfo->lconv->grouping[0] = '\0';
1057
1058         loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = _strdup("C");
1059     }
1060
1061     if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1062         if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1063             MSVCRT__free_locale(loc);
1064             return NULL;
1065         }
1066     } else
1067         loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = _strdup("C");
1068
1069     return loc;
1070 }
1071
1072 /* _configthreadlocale - not exported in native msvcrt */
1073 int CDECL _configthreadlocale(int type)
1074 {
1075     thread_data_t *data = msvcrt_get_thread_data();
1076     int ret;
1077
1078     if(!data)
1079         return -1;
1080
1081     ret = (data->locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1082
1083     if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1084         if(!data->locale) {
1085             /* Copy current global locale */
1086             data->locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1087             if(!data->locale)
1088                 return -1;
1089         }
1090
1091         return ret;
1092     }
1093
1094     if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1095         if(data->locale) {
1096             MSVCRT__free_locale(data->locale);
1097             data->locale = NULL;
1098         }
1099
1100         return ret;
1101     }
1102
1103     if(!type)
1104         return ret;
1105
1106     return -1;
1107 }
1108
1109 /*********************************************************************
1110  *             setlocale (MSVCRT.@)
1111  */
1112 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1113 {
1114     MSVCRT__locale_t loc, cur;
1115
1116     cur = get_locale();
1117
1118     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1119         return NULL;
1120
1121     if(!locale) {
1122         if(category == MSVCRT_LC_ALL)
1123             return construct_lc_all(cur);
1124
1125         return cur->locinfo->lc_category[category].locale;
1126     }
1127
1128     loc = MSVCRT__create_locale(category, locale);
1129     if(!loc) {
1130         WARN("%d %s failed\n", category, locale);
1131         return NULL;
1132     }
1133
1134     LOCK_LOCALE;
1135
1136     switch(category) {
1137         case MSVCRT_LC_ALL:
1138             if(!cur)
1139                 break;
1140         case MSVCRT_LC_COLLATE:
1141             cur->locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1142                 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1143             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1144                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1145             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1146                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1147
1148             if(category != MSVCRT_LC_ALL)
1149                 break;
1150         case MSVCRT_LC_CTYPE:
1151             cur->locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1152                 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1153             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1154                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1155             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1156                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1157
1158             cur->locinfo->lc_codepage = loc->locinfo->lc_codepage;
1159             cur->locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1160             cur->locinfo->lc_clike = loc->locinfo->lc_clike;
1161             cur->locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1162
1163             swap_pointers((void**)&cur->locinfo->ctype1_refcount,
1164                     (void**)&loc->locinfo->ctype1_refcount);
1165             swap_pointers((void**)&cur->locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1166             swap_pointers((void**)&cur->locinfo->pctype, (void**)&loc->locinfo->pctype);
1167             swap_pointers((void**)&cur->locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1168             swap_pointers((void**)&cur->locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1169
1170             memcpy(cur->mbcinfo, loc->mbcinfo, sizeof(MSVCRT_threadmbcinfo));
1171
1172             if(category != MSVCRT_LC_ALL)
1173                 break;
1174         case MSVCRT_LC_MONETARY:
1175             cur->locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1176                 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1177             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1178                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1179             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1180                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1181
1182             swap_pointers((void**)&cur->locinfo->lconv->int_curr_symbol,
1183                     (void**)&loc->locinfo->lconv->int_curr_symbol);
1184             swap_pointers((void**)&cur->locinfo->lconv->currency_symbol,
1185                     (void**)&loc->locinfo->lconv->currency_symbol);
1186             swap_pointers((void**)&cur->locinfo->lconv->mon_decimal_point,
1187                     (void**)&loc->locinfo->lconv->mon_decimal_point);
1188             swap_pointers((void**)&cur->locinfo->lconv->mon_thousands_sep,
1189                     (void**)&loc->locinfo->lconv->mon_thousands_sep);
1190             swap_pointers((void**)&cur->locinfo->lconv->mon_grouping,
1191                     (void**)&loc->locinfo->lconv->mon_grouping);
1192             swap_pointers((void**)&cur->locinfo->lconv->positive_sign,
1193                     (void**)&loc->locinfo->lconv->positive_sign);
1194             swap_pointers((void**)&cur->locinfo->lconv->negative_sign,
1195                     (void**)&loc->locinfo->lconv->negative_sign);
1196             cur->locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1197             cur->locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1198             cur->locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1199             cur->locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1200             cur->locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1201             cur->locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1202             cur->locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1203             cur->locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1204
1205             if(category != MSVCRT_LC_ALL)
1206                 break;
1207         case MSVCRT_LC_NUMERIC:
1208             cur->locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1209                 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1210             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1211                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1212             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1213                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1214
1215             swap_pointers((void**)&cur->locinfo->lconv->decimal_point,
1216                     (void**)&loc->locinfo->lconv->decimal_point);
1217             swap_pointers((void**)&cur->locinfo->lconv->thousands_sep,
1218                     (void**)&loc->locinfo->lconv->thousands_sep);
1219             swap_pointers((void**)&cur->locinfo->lconv->grouping,
1220                     (void**)&loc->locinfo->lconv->grouping);
1221
1222             if(category != MSVCRT_LC_ALL)
1223                 break;
1224         case MSVCRT_LC_TIME:
1225             cur->locinfo->lc_handle[MSVCRT_LC_TIME] =
1226                 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1227             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_TIME].locale,
1228                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1229             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1230                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1231
1232             if(category != MSVCRT_LC_ALL)
1233                 break;
1234     }
1235
1236     if(!cur)
1237         MSVCRT_locale = cur = loc;
1238     else
1239         MSVCRT__free_locale(loc);
1240
1241     UNLOCK_LOCALE;
1242
1243     if(cur == MSVCRT_locale) {
1244         MSVCRT___lc_codepage = cur->locinfo->lc_codepage;
1245         MSVCRT___lc_collate_cp = cur->locinfo->lc_collate_cp;
1246         MSVCRT___mb_cur_max = cur->locinfo->mb_cur_max;
1247         MSVCRT__pctype = cur->locinfo->pctype;
1248     }
1249
1250     if(category == MSVCRT_LC_ALL)
1251         return construct_lc_all(cur);
1252
1253     return cur->locinfo->lc_category[category].locale;
1254 }