wined3d: Make the device parameter to wined3d_device_get_ps_consts_f() const.
[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 LCID MSVCRT___lc_handle[MSVCRT_LC_MAX - MSVCRT_LC_MIN + 1] = { 0 };
47 static 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     const 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 pthreadlocinfo struct */
357 MSVCRT_pthreadlocinfo get_locinfo(void) {
358     thread_data_t *data = msvcrt_get_thread_data();
359
360     if(!data || !data->have_locale)
361         return MSVCRT_locale->locinfo;
362
363     return data->locinfo;
364 }
365
366 /* INTERNAL: returns pthreadlocinfo struct */
367 static MSVCRT_pthreadmbcinfo get_mbcinfo(void) {
368     thread_data_t *data = msvcrt_get_thread_data();
369
370     if(!data || !data->have_locale)
371         return MSVCRT_locale->mbcinfo;
372
373     return data->mbcinfo;
374 }
375
376 /* INTERNAL: constructs string returned by setlocale */
377 static inline char* construct_lc_all(MSVCRT_pthreadlocinfo locinfo) {
378     static char current_lc_all[MAX_LOCALE_LENGTH];
379
380     int i;
381
382     for(i=MSVCRT_LC_MIN+1; i<MSVCRT_LC_MAX; i++) {
383         if(strcmp(locinfo->lc_category[i].locale,
384                     locinfo->lc_category[i+1].locale))
385             break;
386     }
387
388     if(i==MSVCRT_LC_MAX)
389         return locinfo->lc_category[MSVCRT_LC_COLLATE].locale;
390
391     sprintf(current_lc_all,
392             "LC_COLLATE=%s;LC_CTYPE=%s;LC_MONETARY=%s;LC_NUMERIC=%s;LC_TIME=%s",
393             locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
394             locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
395             locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
396             locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
397             locinfo->lc_category[MSVCRT_LC_TIME].locale);
398
399     return current_lc_all;
400 }
401
402
403 /*********************************************************************
404  *              wsetlocale (MSVCRT.@)
405  */
406 MSVCRT_wchar_t* CDECL MSVCRT__wsetlocale(int category, const MSVCRT_wchar_t* locale)
407 {
408   static MSVCRT_wchar_t fake[] = {
409     'E','n','g','l','i','s','h','_','U','n','i','t','e','d',' ',
410     'S','t','a','t','e','s','.','1','2','5','2',0 };
411
412   FIXME("%d %s\n", category, debugstr_w(locale));
413
414   return fake;
415 }
416
417 /*********************************************************************
418  *              _Getdays (MSVCRT.@)
419  */
420 const char* CDECL _Getdays(void)
421 {
422   static const char MSVCRT_days[] = ":Sun:Sunday:Mon:Monday:Tue:Tuesday:Wed:"
423                             "Wednesday:Thu:Thursday:Fri:Friday:Sat:Saturday";
424   /* FIXME: Use locale */
425   TRACE("(void) semi-stub\n");
426   return MSVCRT_days;
427 }
428
429 /*********************************************************************
430  *              _Getmonths (MSVCRT.@)
431  */
432 const char* CDECL _Getmonths(void)
433 {
434   static const char MSVCRT_months[] = ":Jan:January:Feb:February:Mar:March:Apr:"
435                 "April:May:May:Jun:June:Jul:July:Aug:August:Sep:September:Oct:"
436                 "October:Nov:November:Dec:December";
437   /* FIXME: Use locale */
438   TRACE("(void) semi-stub\n");
439   return MSVCRT_months;
440 }
441
442 /*********************************************************************
443  *              _Gettnames (MSVCRT.@)
444  */
445 const char* CDECL _Gettnames(void)
446 {
447   /* FIXME: */
448   TRACE("(void) stub\n");
449   return "";
450 }
451
452 /*********************************************************************
453  *              _Strftime (MSVCRT.@)
454  */
455 const char* CDECL _Strftime(char *out, unsigned int len, const char *fmt,
456                             const void *tm, void *foo)
457 {
458   /* FIXME: */
459   TRACE("(%p %d %s %p %p) stub\n", out, len, fmt, tm, foo);
460   return "";
461 }
462
463 /*********************************************************************
464  *              __crtLCMapStringA (MSVCRT.@)
465  */
466 int CDECL __crtLCMapStringA(
467   LCID lcid, DWORD mapflags, const char* src, int srclen, char* dst,
468   int dstlen, unsigned int codepage, int xflag
469 ) {
470   FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
471         lcid,mapflags,src,srclen,dst,dstlen,codepage,xflag);
472   /* FIXME: A bit incorrect. But msvcrt itself just converts its
473    * arguments to wide strings and then calls LCMapStringW
474    */
475   return LCMapStringA(lcid,mapflags,src,srclen,dst,dstlen);
476 }
477
478 /*********************************************************************
479  *              __crtLCMapStringW (MSVCRT.@)
480  */
481 int CDECL __crtLCMapStringW(LCID lcid, DWORD mapflags, const MSVCRT_wchar_t *src,
482         int srclen, MSVCRT_wchar_t *dst, int dstlen, unsigned int codepage, int xflag)
483 {
484     FIXME("(lcid %x, flags %x, %s(%d), %p(%d), %x, %d), partial stub!\n",
485             lcid, mapflags, debugstr_w(src), srclen, dst, dstlen, codepage, xflag);
486
487     return LCMapStringW(lcid, mapflags, src, srclen, dst, dstlen);
488 }
489
490 /*********************************************************************
491  *              __crtCompareStringA (MSVCRT.@)
492  */
493 int CDECL __crtCompareStringA( LCID lcid, DWORD flags, const char *src1, int len1,
494                                const char *src2, int len2 )
495 {
496     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
497           lcid, flags, debugstr_a(src1), len1, debugstr_a(src2), len2 );
498     /* FIXME: probably not entirely right */
499     return CompareStringA( lcid, flags, src1, len1, src2, len2 );
500 }
501
502 /*********************************************************************
503  *              __crtCompareStringW (MSVCRT.@)
504  */
505 int CDECL __crtCompareStringW( LCID lcid, DWORD flags, const MSVCRT_wchar_t *src1, int len1,
506                                const MSVCRT_wchar_t *src2, int len2 )
507 {
508     FIXME("(lcid %x, flags %x, %s(%d), %s(%d), partial stub\n",
509           lcid, flags, debugstr_w(src1), len1, debugstr_w(src2), len2 );
510     /* FIXME: probably not entirely right */
511     return CompareStringW( lcid, flags, src1, len1, src2, len2 );
512 }
513
514 /*********************************************************************
515  *              __crtGetLocaleInfoW (MSVCRT.@)
516  */
517 int CDECL __crtGetLocaleInfoW( LCID lcid, LCTYPE type, MSVCRT_wchar_t *buffer, int len )
518 {
519     FIXME("(lcid %x, type %x, %p(%d), partial stub\n", lcid, type, buffer, len );
520     /* FIXME: probably not entirely right */
521     return GetLocaleInfoW( lcid, type, buffer, len );
522 }
523
524 /*********************************************************************
525  *              btowc(MSVCRT.@)
526  */
527 MSVCRT_wint_t CDECL MSVCRT_btowc(int c)
528 {
529     unsigned char letter = c;
530     MSVCRT_wchar_t ret;
531
532     if(!MultiByteToWideChar(get_locinfo()->lc_handle[MSVCRT_LC_CTYPE],
533                 0, (LPCSTR)&letter, 1, &ret, 1))
534         return 0;
535
536     return ret;
537 }
538
539 /*********************************************************************
540  *              __crtGetStringTypeW(MSVCRT.@)
541  *
542  * This function was accepting different number of arguments in older
543  * versions of msvcrt.
544  */
545 BOOL CDECL __crtGetStringTypeW(DWORD unk, DWORD type,
546         MSVCRT_wchar_t *buffer, int len, WORD *out)
547 {
548     FIXME("(unk %x, type %x, wstr %p(%d), %p) partial stub\n",
549             unk, type, buffer, len, out);
550
551     return GetStringTypeW(type, buffer, len, out);
552 }
553
554 /*********************************************************************
555  *              localeconv (MSVCRT.@)
556  */
557 struct MSVCRT_lconv * CDECL MSVCRT_localeconv(void)
558 {
559     return get_locinfo()->lconv;
560 }
561
562 /*********************************************************************
563  *              __lconv_init (MSVCRT.@)
564  */
565 int CDECL __lconv_init(void)
566 {
567     /* this is used to make chars unsigned */
568     charmax = 255;
569     return 0;
570 }
571
572 /*********************************************************************
573  *      ___lc_handle_func (MSVCRT.@)
574  */
575 LCID* CDECL ___lc_handle_func(void)
576 {
577     return MSVCRT___lc_handle;
578 }
579
580 /*********************************************************************
581  *      ___lc_codepage_func (MSVCRT.@)
582  */
583 int CDECL ___lc_codepage_func(void)
584 {
585     return MSVCRT___lc_codepage;
586 }
587
588 /*********************************************************************
589  *      ___lc_collate_cp_func (MSVCRT.@)
590  */
591 int CDECL ___lc_collate_cp_func(void)
592 {
593     return get_locinfo()->lc_collate_cp;
594 }
595
596 /* INTERNAL: frees MSVCRT_pthreadlocinfo struct */
597 void free_locinfo(MSVCRT_pthreadlocinfo locinfo)
598 {
599     int i;
600
601     if(!locinfo)
602         return;
603
604     for(i=MSVCRT_LC_MIN+1; i<=MSVCRT_LC_MAX; i++) {
605         MSVCRT_free(locinfo->lc_category[i].locale);
606         MSVCRT_free(locinfo->lc_category[i].refcount);
607     }
608
609     if(locinfo->lconv) {
610         MSVCRT_free(locinfo->lconv->decimal_point);
611         MSVCRT_free(locinfo->lconv->thousands_sep);
612         MSVCRT_free(locinfo->lconv->grouping);
613         MSVCRT_free(locinfo->lconv->int_curr_symbol);
614         MSVCRT_free(locinfo->lconv->currency_symbol);
615         MSVCRT_free(locinfo->lconv->mon_decimal_point);
616         MSVCRT_free(locinfo->lconv->mon_thousands_sep);
617         MSVCRT_free(locinfo->lconv->mon_grouping);
618         MSVCRT_free(locinfo->lconv->positive_sign);
619         MSVCRT_free(locinfo->lconv->negative_sign);
620     }
621     MSVCRT_free(locinfo->lconv_intl_refcount);
622     MSVCRT_free(locinfo->lconv_num_refcount);
623     MSVCRT_free(locinfo->lconv_mon_refcount);
624     MSVCRT_free(locinfo->lconv);
625
626     MSVCRT_free(locinfo->ctype1_refcount);
627     MSVCRT_free(locinfo->ctype1);
628
629     MSVCRT_free(locinfo->pclmap);
630     MSVCRT_free(locinfo->pcumap);
631
632     MSVCRT_free(locinfo);
633 }
634
635 /* INTERNAL: frees MSVCRT_pthreadmbcinfo struct */
636 void free_mbcinfo(MSVCRT_pthreadmbcinfo mbcinfo)
637 {
638     if(!mbcinfo)
639         return;
640
641     MSVCRT_free(mbcinfo);
642 }
643
644 /* _free_locale - not exported in native msvcrt */
645 void CDECL MSVCRT__free_locale(MSVCRT__locale_t locale)
646 {
647     if (!locale)
648         return;
649
650     free_locinfo(locale->locinfo);
651     free_mbcinfo(locale->mbcinfo);
652     MSVCRT_free(locale);
653 }
654
655 /* _create_locale - not exported in native msvcrt */
656 MSVCRT__locale_t MSVCRT__create_locale(int category, const char *locale)
657 {
658     static const char collate[] = "COLLATE=";
659     static const char ctype[] = "CTYPE=";
660     static const char monetary[] = "MONETARY=";
661     static const char numeric[] = "NUMERIC=";
662     static const char time[] = "TIME=";
663
664     MSVCRT__locale_t loc;
665     LCID lcid[6] = { 0 };
666     char buf[256];
667     int i;
668
669     TRACE("(%d %s)\n", category, locale);
670
671     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX || !locale)
672         return NULL;
673
674     if(locale[0]=='C' && !locale[1])
675         lcid[0] = CP_ACP;
676     else if(!locale[0])
677         lcid[0] = GetSystemDefaultLCID();
678     else if (locale[0] == 'L' && locale[1] == 'C' && locale[2] == '_') {
679         const char *p;
680
681         while(1) {
682             locale += 3; /* LC_ */
683             if(!memcmp(locale, collate, sizeof(collate)-1)) {
684                 i = MSVCRT_LC_COLLATE;
685                 locale += sizeof(collate)-1;
686             } else if(!memcmp(locale, ctype, sizeof(ctype)-1)) {
687                 i = MSVCRT_LC_CTYPE;
688                 locale += sizeof(ctype)-1;
689             } else if(!memcmp(locale, monetary, sizeof(monetary)-1)) {
690                 i = MSVCRT_LC_MONETARY;
691                 locale += sizeof(monetary)-1;
692             } else if(!memcmp(locale, numeric, sizeof(numeric)-1)) {
693                 i = MSVCRT_LC_NUMERIC;
694                 locale += sizeof(numeric)-1;
695             } else if(!memcmp(locale, time, sizeof(time)-1)) {
696                 i = MSVCRT_LC_TIME;
697                 locale += sizeof(time)-1;
698             } else
699                 return NULL;
700
701             p = strchr(locale, ';');
702             if(locale[0]=='C' && (locale[1]==';' || locale[1]=='\0'))
703                 lcid[i] = 0;
704             else if(p) {
705                 memcpy(buf, locale, p-locale);
706                 buf[p-locale] = '\0';
707                 lcid[i] = MSVCRT_locale_to_LCID(buf);
708             } else
709                 lcid[i] = MSVCRT_locale_to_LCID(locale);
710
711             if(lcid[i] == -1)
712                 return NULL;
713
714             if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
715                 break;
716
717             locale = p+1;
718         }
719     } else {
720         lcid[0] = MSVCRT_locale_to_LCID(locale);
721         if(lcid[0] == -1)
722             return NULL;
723     }
724
725     for(i=1; i<6; i++) {
726         if(!lcid[i])
727             lcid[i] = lcid[0];
728     }
729
730     loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
731     if(!loc)
732         return NULL;
733
734     loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
735     if(!loc->locinfo) {
736         MSVCRT_free(loc);
737         return NULL;
738     }
739
740     loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
741     if(!loc->mbcinfo) {
742         MSVCRT_free(loc->locinfo);
743         MSVCRT_free(loc);
744         return NULL;
745     }
746
747     memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
748     memset(loc->mbcinfo, 0, sizeof(MSVCRT_threadmbcinfo));
749
750     loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
751     if(!loc->locinfo->lconv) {
752         MSVCRT__free_locale(loc);
753         return NULL;
754     }
755     memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
756
757     loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
758     loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
759     if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
760         MSVCRT__free_locale(loc);
761         return NULL;
762     }
763
764     loc->locinfo->refcount = 1;
765
766     if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
767         if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
768             MSVCRT__free_locale(loc);
769             return NULL;
770         }
771     } else
772         loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = MSVCRT__strdup("C");
773
774     if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
775         CPINFO cp;
776         int j;
777
778         if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
779             MSVCRT__free_locale(loc);
780             return NULL;
781         }
782
783         loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
784         loc->locinfo->lc_collate_cp = loc->locinfo->lc_codepage;
785         loc->locinfo->lc_clike = 1;
786         if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
787             MSVCRT__free_locale(loc);
788             return NULL;
789         }
790         loc->locinfo->mb_cur_max = cp.MaxCharSize;
791
792         loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
793         loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
794         if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
795             MSVCRT__free_locale(loc);
796             return NULL;
797         }
798
799         *loc->locinfo->ctype1_refcount = 1;
800         loc->locinfo->ctype1[0] = 0;
801         loc->locinfo->pctype = loc->locinfo->ctype1+1;
802
803         buf[1] = buf[2] = '\0';
804         for(i=1; i<257; i++) {
805             buf[0] = i-1;
806
807             GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
808                     1, loc->locinfo->ctype1+i);
809         }
810
811         for(i=0; cp.LeadByte[i+1]!=0; i+=2)
812             for(j=cp.LeadByte[i]; j<=cp.LeadByte[i+1]; j++)
813                 loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
814     } else {
815         loc->locinfo->lc_clike = 1;
816         loc->locinfo->mb_cur_max = 1;
817         loc->locinfo->pctype = MSVCRT__ctype+1;
818         loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = MSVCRT__strdup("C");
819     }
820
821     for(i=0; i<256; i++) {
822         if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
823             buf[i] = ' ';
824         else
825             buf[i] = i;
826
827     }
828
829     if(lcid[MSVCRT_LC_CTYPE]) {
830         LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
831                 (char*)loc->locinfo->pclmap, 256);
832         LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
833                 (char*)loc->locinfo->pcumap, 256);
834     } else {
835         for(i=0; i<256; i++) {
836             loc->locinfo->pclmap[i] = (i>='A' && i<='Z' ? i-'A'+'a' : i);
837             loc->locinfo->pcumap[i] = (i>='a' && i<='z' ? i-'a'+'A' : i);
838         }
839     }
840
841     loc->mbcinfo->refcount = 1;
842     loc->mbcinfo->mbcodepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
843
844     for(i=0; i<256; i++) {
845         if(loc->locinfo->pclmap[i] != i) {
846             loc->mbcinfo->mbctype[i+1] |= 0x10;
847             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pclmap[i];
848         } else if(loc->locinfo->pcumap[i] != i) {
849             loc->mbcinfo->mbctype[i+1] |= 0x20;
850             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pcumap[i];
851         }
852     }
853
854     if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
855         if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
856             MSVCRT__free_locale(loc);
857             return NULL;
858         }
859
860         loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
861         loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
862         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
863             MSVCRT__free_locale(loc);
864             return NULL;
865         }
866
867         *loc->locinfo->lconv_intl_refcount = 1;
868         *loc->locinfo->lconv_mon_refcount = 1;
869
870         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
871                 |LOCALE_NOUSEROVERRIDE, buf, 256);
872         if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char[i]))))
873             memcpy(loc->locinfo->lconv->int_curr_symbol, buf, sizeof(char[i]));
874         else {
875             MSVCRT__free_locale(loc);
876             return NULL;
877         }
878
879         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
880                 |LOCALE_NOUSEROVERRIDE, buf, 256);
881         if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char[i]))))
882             memcpy(loc->locinfo->lconv->currency_symbol, buf, sizeof(char[i]));
883         else {
884             MSVCRT__free_locale(loc);
885             return NULL;
886         }
887
888         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
889                 |LOCALE_NOUSEROVERRIDE, buf, 256);
890         if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char[i]))))
891             memcpy(loc->locinfo->lconv->mon_decimal_point, buf, sizeof(char[i]));
892         else {
893             MSVCRT__free_locale(loc);
894             return NULL;
895         }
896
897         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
898                 |LOCALE_NOUSEROVERRIDE, buf, 256);
899         if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
900             memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, sizeof(char[i]));
901         else {
902             MSVCRT__free_locale(loc);
903             return NULL;
904         }
905
906         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
907                 |LOCALE_NOUSEROVERRIDE, buf, 256);
908         if(i>1)
909             i = i/2 + (buf[i-2]=='0'?0:1);
910         if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char[i])))) {
911             for(i=0; buf[i+1]==';'; i+=2)
912                 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
913             loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
914             if(buf[i] != '0')
915                 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
916         } else {
917             MSVCRT__free_locale(loc);
918             return NULL;
919         }
920
921         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
922                 |LOCALE_NOUSEROVERRIDE, buf, 256);
923         if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char[i]))))
924             memcpy(loc->locinfo->lconv->positive_sign, buf, sizeof(char[i]));
925         else {
926             MSVCRT__free_locale(loc);
927             return NULL;
928         }
929
930         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
931                 |LOCALE_NOUSEROVERRIDE, buf, 256);
932         if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char[i]))))
933             memcpy(loc->locinfo->lconv->negative_sign, buf, sizeof(char[i]));
934         else {
935             MSVCRT__free_locale(loc);
936             return NULL;
937         }
938
939         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
940                     |LOCALE_NOUSEROVERRIDE, buf, 256))
941             loc->locinfo->lconv->int_frac_digits = atoi(buf);
942         else {
943             MSVCRT__free_locale(loc);
944             return NULL;
945         }
946
947         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
948                     |LOCALE_NOUSEROVERRIDE, buf, 256))
949             loc->locinfo->lconv->frac_digits = atoi(buf);
950         else {
951             MSVCRT__free_locale(loc);
952             return NULL;
953         }
954
955         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
956                     |LOCALE_NOUSEROVERRIDE, buf, 256))
957             loc->locinfo->lconv->p_cs_precedes = atoi(buf);
958         else {
959             MSVCRT__free_locale(loc);
960             return NULL;
961         }
962
963         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
964                     |LOCALE_NOUSEROVERRIDE, buf, 256))
965             loc->locinfo->lconv->p_sep_by_space = atoi(buf);
966         else {
967             MSVCRT__free_locale(loc);
968             return NULL;
969         }
970
971         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
972                     |LOCALE_NOUSEROVERRIDE, buf, 256))
973             loc->locinfo->lconv->n_cs_precedes = atoi(buf);
974         else {
975             MSVCRT__free_locale(loc);
976             return NULL;
977         }
978
979         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
980                     |LOCALE_NOUSEROVERRIDE, buf, 256))
981             loc->locinfo->lconv->n_sep_by_space = atoi(buf);
982         else {
983             MSVCRT__free_locale(loc);
984             return NULL;
985         }
986
987         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
988                     |LOCALE_NOUSEROVERRIDE, buf, 256))
989             loc->locinfo->lconv->p_sign_posn = atoi(buf);
990         else {
991             MSVCRT__free_locale(loc);
992             return NULL;
993         }
994
995         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
996                     |LOCALE_NOUSEROVERRIDE, buf, 256))
997             loc->locinfo->lconv->n_sign_posn = atoi(buf);
998         else {
999             MSVCRT__free_locale(loc);
1000             return NULL;
1001         }
1002     } else {
1003         loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
1004         loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
1005         loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
1006         loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
1007         loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
1008         loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
1009         loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
1010
1011         if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
1012                 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
1013                 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
1014                 || !loc->locinfo->lconv->negative_sign) {
1015             MSVCRT__free_locale(loc);
1016             return NULL;
1017         }
1018
1019         loc->locinfo->lconv->int_curr_symbol[0] = '\0';
1020         loc->locinfo->lconv->currency_symbol[0] = '\0';
1021         loc->locinfo->lconv->mon_decimal_point[0] = '\0';
1022         loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
1023         loc->locinfo->lconv->mon_grouping[0] = '\0';
1024         loc->locinfo->lconv->positive_sign[0] = '\0';
1025         loc->locinfo->lconv->negative_sign[0] = '\0';
1026         loc->locinfo->lconv->int_frac_digits = 127;
1027         loc->locinfo->lconv->frac_digits = 127;
1028         loc->locinfo->lconv->p_cs_precedes = 127;
1029         loc->locinfo->lconv->p_sep_by_space = 127;
1030         loc->locinfo->lconv->n_cs_precedes = 127;
1031         loc->locinfo->lconv->n_sep_by_space = 127;
1032         loc->locinfo->lconv->p_sign_posn = 127;
1033         loc->locinfo->lconv->n_sign_posn = 127;
1034
1035         loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = MSVCRT__strdup("C");
1036     }
1037
1038     if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1039         if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1040             MSVCRT__free_locale(loc);
1041             return NULL;
1042         }
1043
1044         if(!loc->locinfo->lconv_intl_refcount)
1045             loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1046         loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1047         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1048             MSVCRT__free_locale(loc);
1049             return NULL;
1050         }
1051
1052         *loc->locinfo->lconv_intl_refcount = 1;
1053         *loc->locinfo->lconv_num_refcount = 1;
1054
1055         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1056                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1057         if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[i]))))
1058             memcpy(loc->locinfo->lconv->decimal_point, buf, sizeof(char[i]));
1059         else {
1060             MSVCRT__free_locale(loc);
1061             return NULL;
1062         }
1063
1064         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1065                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1066         if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
1067             memcpy(loc->locinfo->lconv->thousands_sep, buf, sizeof(char[i]));
1068         else {
1069             MSVCRT__free_locale(loc);
1070             return NULL;
1071         }
1072
1073         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1074                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1075         if(i>1)
1076             i = i/2 + (buf[i-2]=='0'?0:1);
1077         if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char[i])))) {
1078             for(i=0; buf[i+1]==';'; i+=2)
1079                 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1080             loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1081             if(buf[i] != '0')
1082                 loc->locinfo->lconv->grouping[i/2+1] = 127;
1083         } else {
1084             MSVCRT__free_locale(loc);
1085             return NULL;
1086         }
1087     } else {
1088         loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1089         loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1090         loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1091         if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1092                 || !loc->locinfo->lconv->grouping) {
1093             MSVCRT__free_locale(loc);
1094             return NULL;
1095         }
1096
1097         loc->locinfo->lconv->decimal_point[0] = '.';
1098         loc->locinfo->lconv->decimal_point[1] = '\0';
1099         loc->locinfo->lconv->thousands_sep[0] = '\0';
1100         loc->locinfo->lconv->grouping[0] = '\0';
1101
1102         loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = MSVCRT__strdup("C");
1103     }
1104
1105     if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1106         if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1107             MSVCRT__free_locale(loc);
1108             return NULL;
1109         }
1110     } else
1111         loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = MSVCRT__strdup("C");
1112
1113     return loc;
1114 }
1115
1116 /*********************************************************************
1117  *             setlocale (MSVCRT.@)
1118  */
1119 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1120 {
1121     MSVCRT__locale_t loc;
1122     MSVCRT_pthreadlocinfo locinfo = get_locinfo();
1123
1124     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1125         return NULL;
1126
1127     if(!locale) {
1128         if(category == MSVCRT_LC_ALL)
1129             return construct_lc_all(locinfo);
1130
1131         return locinfo->lc_category[category].locale;
1132     }
1133
1134     loc = MSVCRT__create_locale(category, locale);
1135     if(!loc) {
1136         WARN("%d %s failed\n", category, locale);
1137         return NULL;
1138     }
1139
1140     LOCK_LOCALE;
1141
1142     switch(category) {
1143         case MSVCRT_LC_ALL:
1144         case MSVCRT_LC_COLLATE:
1145             locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1146                 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1147             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1148                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1149             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1150                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1151
1152             if(category != MSVCRT_LC_ALL)
1153                 break;
1154         case MSVCRT_LC_CTYPE:
1155             locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1156                 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1157             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1158                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1159             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1160                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1161
1162             locinfo->lc_codepage = loc->locinfo->lc_codepage;
1163             locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1164             locinfo->lc_clike = loc->locinfo->lc_clike;
1165             locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1166
1167             swap_pointers((void**)&locinfo->ctype1_refcount,
1168                     (void**)&loc->locinfo->ctype1_refcount);
1169             swap_pointers((void**)&locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1170             swap_pointers((void**)&locinfo->pctype, (void**)&loc->locinfo->pctype);
1171             swap_pointers((void**)&locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1172             swap_pointers((void**)&locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1173
1174             memcpy(get_mbcinfo(), loc->mbcinfo, sizeof(MSVCRT_threadmbcinfo));
1175
1176             if(category != MSVCRT_LC_ALL)
1177                 break;
1178         case MSVCRT_LC_MONETARY:
1179             locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1180                 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1181             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1182                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1183             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1184                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1185
1186             swap_pointers((void**)&locinfo->lconv->int_curr_symbol,
1187                     (void**)&loc->locinfo->lconv->int_curr_symbol);
1188             swap_pointers((void**)&locinfo->lconv->currency_symbol,
1189                     (void**)&loc->locinfo->lconv->currency_symbol);
1190             swap_pointers((void**)&locinfo->lconv->mon_decimal_point,
1191                     (void**)&loc->locinfo->lconv->mon_decimal_point);
1192             swap_pointers((void**)&locinfo->lconv->mon_thousands_sep,
1193                     (void**)&loc->locinfo->lconv->mon_thousands_sep);
1194             swap_pointers((void**)&locinfo->lconv->mon_grouping,
1195                     (void**)&loc->locinfo->lconv->mon_grouping);
1196             swap_pointers((void**)&locinfo->lconv->positive_sign,
1197                     (void**)&loc->locinfo->lconv->positive_sign);
1198             swap_pointers((void**)&locinfo->lconv->negative_sign,
1199                     (void**)&loc->locinfo->lconv->negative_sign);
1200             locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1201             locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1202             locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1203             locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1204             locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1205             locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1206             locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1207             locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1208
1209             if(category != MSVCRT_LC_ALL)
1210                 break;
1211         case MSVCRT_LC_NUMERIC:
1212             locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1213                 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1214             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1215                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1216             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1217                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1218
1219             swap_pointers((void**)&locinfo->lconv->decimal_point,
1220                     (void**)&loc->locinfo->lconv->decimal_point);
1221             swap_pointers((void**)&locinfo->lconv->thousands_sep,
1222                     (void**)&loc->locinfo->lconv->thousands_sep);
1223             swap_pointers((void**)&locinfo->lconv->grouping,
1224                     (void**)&loc->locinfo->lconv->grouping);
1225
1226             if(category != MSVCRT_LC_ALL)
1227                 break;
1228         case MSVCRT_LC_TIME:
1229             locinfo->lc_handle[MSVCRT_LC_TIME] =
1230                 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1231             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].locale,
1232                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1233             swap_pointers((void**)&locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1234                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1235
1236             if(category != MSVCRT_LC_ALL)
1237                 break;
1238     }
1239
1240     MSVCRT__free_locale(loc);
1241     UNLOCK_LOCALE;
1242
1243     if(locinfo == MSVCRT_locale->locinfo) {
1244         int i;
1245
1246         MSVCRT___lc_codepage = locinfo->lc_codepage;
1247         MSVCRT___lc_collate_cp = locinfo->lc_collate_cp;
1248         MSVCRT___mb_cur_max = locinfo->mb_cur_max;
1249         MSVCRT__pctype = locinfo->pctype;
1250         for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1251             MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1252     }
1253
1254     if(category == MSVCRT_LC_ALL)
1255         return construct_lc_all(locinfo);
1256
1257     return locinfo->lc_category[category].locale;
1258 }
1259
1260 /* _configthreadlocale - not exported in native msvcrt */
1261 int CDECL _configthreadlocale(int type)
1262 {
1263     thread_data_t *data = msvcrt_get_thread_data();
1264     MSVCRT__locale_t locale;
1265     int ret;
1266
1267     if(!data)
1268         return -1;
1269
1270     ret = (data->have_locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1271
1272     if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1273         if(!data->have_locale) {
1274             /* Copy current global locale */
1275             locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1276             if(!locale)
1277                 return -1;
1278
1279             data->locinfo = locale->locinfo;
1280             data->mbcinfo = locale->mbcinfo;
1281             data->have_locale = TRUE;
1282             MSVCRT_free(locale);
1283         }
1284
1285         return ret;
1286     }
1287
1288     if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1289         if(data->have_locale) {
1290             free_locinfo(data->locinfo);
1291             free_mbcinfo(data->mbcinfo);
1292             data->locinfo = MSVCRT_locale->locinfo;
1293             data->mbcinfo = MSVCRT_locale->mbcinfo;
1294             data->have_locale = FALSE;
1295         }
1296
1297         return ret;
1298     }
1299
1300     if(!type)
1301         return ret;
1302
1303     return -1;
1304 }
1305
1306 BOOL msvcrt_init_locale(void)
1307 {
1308     int i;
1309
1310     LOCK_LOCALE;
1311     MSVCRT_locale = MSVCRT__create_locale(0, "C");
1312     UNLOCK_LOCALE;
1313     if(!MSVCRT_locale)
1314         return FALSE;
1315
1316     MSVCRT___lc_codepage = MSVCRT_locale->locinfo->lc_codepage;
1317     MSVCRT___lc_collate_cp = MSVCRT_locale->locinfo->lc_collate_cp;
1318     MSVCRT___mb_cur_max = MSVCRT_locale->locinfo->mb_cur_max;
1319     MSVCRT__pctype = MSVCRT_locale->locinfo->pctype;
1320     for(i=MSVCRT_LC_MIN; i<=MSVCRT_LC_MAX; i++)
1321         MSVCRT___lc_handle[i] = MSVCRT_locale->locinfo->lc_handle[i];
1322     return TRUE;
1323 }