user32: Pass a NULL rectangle in the ClipCursor entry point when clipping is reset...
[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                 buf[p-locale] = '\0';
680                 lcid[i] = MSVCRT_locale_to_LCID(buf);
681             } else
682                 lcid[i] = MSVCRT_locale_to_LCID(locale);
683
684             if(lcid[i] == -1)
685                 return NULL;
686
687             if(!p || *(p+1)!='L' || *(p+2)!='C' || *(p+3)!='_')
688                 break;
689
690             locale = p+1;
691         }
692     } else {
693         lcid[0] = MSVCRT_locale_to_LCID(locale);
694         if(lcid[0] == -1)
695             return NULL;
696     }
697
698     for(i=1; i<6; i++) {
699         if(!lcid[i])
700             lcid[i] = lcid[0];
701     }
702
703     loc = MSVCRT_malloc(sizeof(MSVCRT__locale_tstruct));
704     if(!loc)
705         return NULL;
706
707     loc->locinfo = MSVCRT_malloc(sizeof(MSVCRT_threadlocinfo));
708     if(!loc->locinfo) {
709         MSVCRT_free(loc);
710         return NULL;
711     }
712
713     loc->mbcinfo = MSVCRT_malloc(sizeof(MSVCRT_threadmbcinfo));
714     if(!loc->mbcinfo) {
715         MSVCRT_free(loc->locinfo);
716         MSVCRT_free(loc);
717         return NULL;
718     }
719
720     memset(loc->locinfo, 0, sizeof(MSVCRT_threadlocinfo));
721     memset(loc->mbcinfo, 0, sizeof(MSVCRT_threadmbcinfo));
722
723     loc->locinfo->lconv = MSVCRT_malloc(sizeof(struct MSVCRT_lconv));
724     if(!loc->locinfo->lconv) {
725         MSVCRT__free_locale(loc);
726         return NULL;
727     }
728     memset(loc->locinfo->lconv, 0, sizeof(struct MSVCRT_lconv));
729
730     loc->locinfo->pclmap = MSVCRT_malloc(sizeof(char[256]));
731     loc->locinfo->pcumap = MSVCRT_malloc(sizeof(char[256]));
732     if(!loc->locinfo->pclmap || !loc->locinfo->pcumap) {
733         MSVCRT__free_locale(loc);
734         return NULL;
735     }
736
737     loc->locinfo->refcount = 1;
738
739     if(lcid[MSVCRT_LC_COLLATE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_COLLATE)) {
740         if(update_threadlocinfo_category(lcid[MSVCRT_LC_COLLATE], loc, MSVCRT_LC_COLLATE)) {
741             MSVCRT__free_locale(loc);
742             return NULL;
743         }
744     } else
745         loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale = _strdup("C");
746
747     if(lcid[MSVCRT_LC_CTYPE] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_CTYPE)) {
748         CPINFO cp;
749         int j;
750
751         if(update_threadlocinfo_category(lcid[MSVCRT_LC_CTYPE], loc, MSVCRT_LC_CTYPE)) {
752             MSVCRT__free_locale(loc);
753             return NULL;
754         }
755
756         loc->locinfo->lc_codepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
757         loc->locinfo->lc_collate_cp = loc->locinfo->lc_codepage;
758         loc->locinfo->lc_clike = 1;
759         if(!GetCPInfo(loc->locinfo->lc_codepage, &cp)) {
760             MSVCRT__free_locale(loc);
761             return NULL;
762         }
763         loc->locinfo->mb_cur_max = cp.MaxCharSize;
764
765         loc->locinfo->ctype1_refcount = MSVCRT_malloc(sizeof(int));
766         loc->locinfo->ctype1 = MSVCRT_malloc(sizeof(short[257]));
767         if(!loc->locinfo->ctype1_refcount || !loc->locinfo->ctype1) {
768             MSVCRT__free_locale(loc);
769             return NULL;
770         }
771
772         *loc->locinfo->ctype1_refcount = 1;
773         loc->locinfo->ctype1[0] = 0;
774         loc->locinfo->pctype = loc->locinfo->ctype1+1;
775
776         buf[1] = buf[2] = '\0';
777         for(i=1; i<257; i++) {
778             buf[0] = i-1;
779
780             GetStringTypeA(lcid[MSVCRT_LC_CTYPE], CT_CTYPE1, buf,
781                     1, loc->locinfo->ctype1+i);
782         }
783
784         for(i=0; cp.LeadByte[i+1]!=0; i+=2)
785             for(j=cp.LeadByte[i]; j<=cp.LeadByte[i+1]; j++)
786                 loc->locinfo->ctype1[j+1] |= MSVCRT__LEADBYTE;
787     } else {
788         loc->locinfo->lc_clike = 1;
789         loc->locinfo->mb_cur_max = 1;
790         loc->locinfo->pctype = MSVCRT__ctype+1;
791         loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale = _strdup("C");
792     }
793
794     for(i=0; i<256; i++) {
795         if(loc->locinfo->pctype[i] & MSVCRT__LEADBYTE)
796             buf[i] = ' ';
797         else
798             buf[i] = i;
799
800     }
801
802     LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_LOWERCASE, buf, 256,
803             (char*)loc->locinfo->pclmap, 256);
804     LCMapStringA(lcid[MSVCRT_LC_CTYPE], LCMAP_UPPERCASE, buf, 256,
805             (char*)loc->locinfo->pcumap, 256);
806
807     loc->mbcinfo->refcount = 1;
808     loc->mbcinfo->mbcodepage = loc->locinfo->lc_id[MSVCRT_LC_CTYPE].wCodePage;
809
810     for(i=0; i<256; i++) {
811         if(loc->locinfo->pclmap[i] != i) {
812             loc->mbcinfo->mbctype[i+1] |= 0x10;
813             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pclmap[i];
814         } else if(loc->locinfo->pcumap[i] != i) {
815             loc->mbcinfo->mbctype[i+1] |= 0x20;
816             loc->mbcinfo->mbcasemap[i] = loc->locinfo->pcumap[i];
817         }
818     }
819
820     if(lcid[MSVCRT_LC_MONETARY] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_MONETARY)) {
821         if(update_threadlocinfo_category(lcid[MSVCRT_LC_MONETARY], loc, MSVCRT_LC_MONETARY)) {
822             MSVCRT__free_locale(loc);
823             return NULL;
824         }
825
826         loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
827         loc->locinfo->lconv_mon_refcount = MSVCRT_malloc(sizeof(int));
828         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_mon_refcount) {
829             MSVCRT__free_locale(loc);
830             return NULL;
831         }
832
833         *loc->locinfo->lconv_intl_refcount = 1;
834         *loc->locinfo->lconv_mon_refcount = 1;
835
836         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SINTLSYMBOL
837                 |LOCALE_NOUSEROVERRIDE, buf, 256);
838         if(i && (loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char[i]))))
839             memcpy(loc->locinfo->lconv->int_curr_symbol, buf, sizeof(char[i]));
840         else {
841             MSVCRT__free_locale(loc);
842             return NULL;
843         }
844
845         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SCURRENCY
846                 |LOCALE_NOUSEROVERRIDE, buf, 256);
847         if(i && (loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char[i]))))
848             memcpy(loc->locinfo->lconv->currency_symbol, buf, sizeof(char[i]));
849         else {
850             MSVCRT__free_locale(loc);
851             return NULL;
852         }
853
854         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONDECIMALSEP
855                 |LOCALE_NOUSEROVERRIDE, buf, 256);
856         if(i && (loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char[i]))))
857             memcpy(loc->locinfo->lconv->mon_decimal_point, buf, sizeof(char[i]));
858         else {
859             MSVCRT__free_locale(loc);
860             return NULL;
861         }
862
863         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONTHOUSANDSEP
864                 |LOCALE_NOUSEROVERRIDE, buf, 256);
865         if(i && (loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
866             memcpy(loc->locinfo->lconv->mon_thousands_sep, buf, sizeof(char[i]));
867         else {
868             MSVCRT__free_locale(loc);
869             return NULL;
870         }
871
872         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SMONGROUPING
873                 |LOCALE_NOUSEROVERRIDE, buf, 256);
874         if(i>1)
875             i = i/2 + (buf[i-2]=='0'?0:1);
876         if(i && (loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char[i])))) {
877             for(i=0; buf[i+1]==';'; i+=2)
878                 loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
879             loc->locinfo->lconv->mon_grouping[i/2] = buf[i]-'0';
880             if(buf[i] != '0')
881                 loc->locinfo->lconv->mon_grouping[i/2+1] = 127;
882         } else {
883             MSVCRT__free_locale(loc);
884             return NULL;
885         }
886
887         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SPOSITIVESIGN
888                 |LOCALE_NOUSEROVERRIDE, buf, 256);
889         if(i && (loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char[i]))))
890             memcpy(loc->locinfo->lconv->positive_sign, buf, sizeof(char[i]));
891         else {
892             MSVCRT__free_locale(loc);
893             return NULL;
894         }
895
896         i = GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_SNEGATIVESIGN
897                 |LOCALE_NOUSEROVERRIDE, buf, 256);
898         if(i && (loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char[i]))))
899             memcpy(loc->locinfo->lconv->negative_sign, buf, sizeof(char[i]));
900         else {
901             MSVCRT__free_locale(loc);
902             return NULL;
903         }
904
905         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IINTLCURRDIGITS
906                     |LOCALE_NOUSEROVERRIDE, buf, 256))
907             loc->locinfo->lconv->int_frac_digits = atoi(buf);
908         else {
909             MSVCRT__free_locale(loc);
910             return NULL;
911         }
912
913         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_ICURRDIGITS
914                     |LOCALE_NOUSEROVERRIDE, buf, 256))
915             loc->locinfo->lconv->frac_digits = atoi(buf);
916         else {
917             MSVCRT__free_locale(loc);
918             return NULL;
919         }
920
921         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSYMPRECEDES
922                     |LOCALE_NOUSEROVERRIDE, buf, 256))
923             loc->locinfo->lconv->p_cs_precedes = atoi(buf);
924         else {
925             MSVCRT__free_locale(loc);
926             return NULL;
927         }
928
929         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSEPBYSPACE
930                     |LOCALE_NOUSEROVERRIDE, buf, 256))
931             loc->locinfo->lconv->p_sep_by_space = atoi(buf);
932         else {
933             MSVCRT__free_locale(loc);
934             return NULL;
935         }
936
937         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSYMPRECEDES
938                     |LOCALE_NOUSEROVERRIDE, buf, 256))
939             loc->locinfo->lconv->n_cs_precedes = atoi(buf);
940         else {
941             MSVCRT__free_locale(loc);
942             return NULL;
943         }
944
945         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSEPBYSPACE
946                     |LOCALE_NOUSEROVERRIDE, buf, 256))
947             loc->locinfo->lconv->n_sep_by_space = atoi(buf);
948         else {
949             MSVCRT__free_locale(loc);
950             return NULL;
951         }
952
953         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_IPOSSIGNPOSN
954                     |LOCALE_NOUSEROVERRIDE, buf, 256))
955             loc->locinfo->lconv->p_sign_posn = atoi(buf);
956         else {
957             MSVCRT__free_locale(loc);
958             return NULL;
959         }
960
961         if(GetLocaleInfoA(lcid[MSVCRT_LC_MONETARY], LOCALE_INEGSIGNPOSN
962                     |LOCALE_NOUSEROVERRIDE, buf, 256))
963             loc->locinfo->lconv->n_sign_posn = atoi(buf);
964         else {
965             MSVCRT__free_locale(loc);
966             return NULL;
967         }
968     } else {
969         loc->locinfo->lconv->int_curr_symbol = MSVCRT_malloc(sizeof(char));
970         loc->locinfo->lconv->currency_symbol = MSVCRT_malloc(sizeof(char));
971         loc->locinfo->lconv->mon_decimal_point = MSVCRT_malloc(sizeof(char));
972         loc->locinfo->lconv->mon_thousands_sep = MSVCRT_malloc(sizeof(char));
973         loc->locinfo->lconv->mon_grouping = MSVCRT_malloc(sizeof(char));
974         loc->locinfo->lconv->positive_sign = MSVCRT_malloc(sizeof(char));
975         loc->locinfo->lconv->negative_sign = MSVCRT_malloc(sizeof(char));
976
977         if(!loc->locinfo->lconv->int_curr_symbol || !loc->locinfo->lconv->currency_symbol
978                 || !loc->locinfo->lconv->mon_decimal_point || !loc->locinfo->lconv->mon_thousands_sep
979                 || !loc->locinfo->lconv->mon_grouping || !loc->locinfo->lconv->positive_sign
980                 || !loc->locinfo->lconv->negative_sign) {
981             MSVCRT__free_locale(loc);
982             return NULL;
983         }
984
985         loc->locinfo->lconv->int_curr_symbol[0] = '\0';
986         loc->locinfo->lconv->currency_symbol[0] = '\0';
987         loc->locinfo->lconv->mon_decimal_point[0] = '\0';
988         loc->locinfo->lconv->mon_thousands_sep[0] = '\0';
989         loc->locinfo->lconv->mon_grouping[0] = '\0';
990         loc->locinfo->lconv->positive_sign[0] = '\0';
991         loc->locinfo->lconv->negative_sign[0] = '\0';
992         loc->locinfo->lconv->int_frac_digits = 127;
993         loc->locinfo->lconv->frac_digits = 127;
994         loc->locinfo->lconv->p_cs_precedes = 127;
995         loc->locinfo->lconv->p_sep_by_space = 127;
996         loc->locinfo->lconv->n_cs_precedes = 127;
997         loc->locinfo->lconv->n_sep_by_space = 127;
998         loc->locinfo->lconv->p_sign_posn = 127;
999         loc->locinfo->lconv->n_sign_posn = 127;
1000
1001         loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale = _strdup("C");
1002     }
1003
1004     if(lcid[MSVCRT_LC_NUMERIC] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_NUMERIC)) {
1005         if(update_threadlocinfo_category(lcid[MSVCRT_LC_NUMERIC], loc, MSVCRT_LC_NUMERIC)) {
1006             MSVCRT__free_locale(loc);
1007             return NULL;
1008         }
1009
1010         if(!loc->locinfo->lconv_intl_refcount)
1011             loc->locinfo->lconv_intl_refcount = MSVCRT_malloc(sizeof(int));
1012         loc->locinfo->lconv_num_refcount = MSVCRT_malloc(sizeof(int));
1013         if(!loc->locinfo->lconv_intl_refcount || !loc->locinfo->lconv_num_refcount) {
1014             MSVCRT__free_locale(loc);
1015             return NULL;
1016         }
1017
1018         *loc->locinfo->lconv_intl_refcount = 1;
1019         *loc->locinfo->lconv_num_refcount = 1;
1020
1021         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SDECIMAL
1022                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1023         if(i && (loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[i]))))
1024             memcpy(loc->locinfo->lconv->decimal_point, buf, sizeof(char[i]));
1025         else {
1026             MSVCRT__free_locale(loc);
1027             return NULL;
1028         }
1029
1030         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_STHOUSAND
1031                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1032         if(i && (loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char[i]))))
1033             memcpy(loc->locinfo->lconv->thousands_sep, buf, sizeof(char[i]));
1034         else {
1035             MSVCRT__free_locale(loc);
1036             return NULL;
1037         }
1038
1039         i = GetLocaleInfoA(lcid[MSVCRT_LC_NUMERIC], LOCALE_SGROUPING
1040                 |LOCALE_NOUSEROVERRIDE, buf, 256);
1041         if(i>1)
1042             i = i/2 + (buf[i-2]=='0'?0:1);
1043         if(i && (loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char[i])))) {
1044             for(i=0; buf[i+1]==';'; i+=2)
1045                 loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1046             loc->locinfo->lconv->grouping[i/2] = buf[i]-'0';
1047             if(buf[i] != '0')
1048                 loc->locinfo->lconv->grouping[i/2+1] = 127;
1049         } else {
1050             MSVCRT__free_locale(loc);
1051             return NULL;
1052         }
1053     } else {
1054         loc->locinfo->lconv->decimal_point = MSVCRT_malloc(sizeof(char[2]));
1055         loc->locinfo->lconv->thousands_sep = MSVCRT_malloc(sizeof(char));
1056         loc->locinfo->lconv->grouping = MSVCRT_malloc(sizeof(char));
1057         if(!loc->locinfo->lconv->decimal_point || !loc->locinfo->lconv->thousands_sep
1058                 || !loc->locinfo->lconv->grouping) {
1059             MSVCRT__free_locale(loc);
1060             return NULL;
1061         }
1062
1063         loc->locinfo->lconv->decimal_point[0] = '.';
1064         loc->locinfo->lconv->decimal_point[1] = '\0';
1065         loc->locinfo->lconv->thousands_sep[0] = '\0';
1066         loc->locinfo->lconv->grouping[0] = '\0';
1067
1068         loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale = _strdup("C");
1069     }
1070
1071     if(lcid[MSVCRT_LC_TIME] && (category==MSVCRT_LC_ALL || category==MSVCRT_LC_TIME)) {
1072         if(update_threadlocinfo_category(lcid[MSVCRT_LC_TIME], loc, MSVCRT_LC_TIME)) {
1073             MSVCRT__free_locale(loc);
1074             return NULL;
1075         }
1076     } else
1077         loc->locinfo->lc_category[MSVCRT_LC_TIME].locale = _strdup("C");
1078
1079     return loc;
1080 }
1081
1082 /* _configthreadlocale - not exported in native msvcrt */
1083 int CDECL _configthreadlocale(int type)
1084 {
1085     thread_data_t *data = msvcrt_get_thread_data();
1086     int ret;
1087
1088     if(!data)
1089         return -1;
1090
1091     ret = (data->locale ? MSVCRT__ENABLE_PER_THREAD_LOCALE : MSVCRT__DISABLE_PER_THREAD_LOCALE);
1092
1093     if(type == MSVCRT__ENABLE_PER_THREAD_LOCALE) {
1094         if(!data->locale) {
1095             /* Copy current global locale */
1096             data->locale = MSVCRT__create_locale(MSVCRT_LC_ALL, MSVCRT_setlocale(MSVCRT_LC_ALL, NULL));
1097             if(!data->locale)
1098                 return -1;
1099         }
1100
1101         return ret;
1102     }
1103
1104     if(type == MSVCRT__DISABLE_PER_THREAD_LOCALE) {
1105         if(data->locale) {
1106             MSVCRT__free_locale(data->locale);
1107             data->locale = NULL;
1108         }
1109
1110         return ret;
1111     }
1112
1113     if(!type)
1114         return ret;
1115
1116     return -1;
1117 }
1118
1119 /*********************************************************************
1120  *             setlocale (MSVCRT.@)
1121  */
1122 char* CDECL MSVCRT_setlocale(int category, const char* locale)
1123 {
1124     MSVCRT__locale_t loc, cur;
1125
1126     cur = get_locale();
1127
1128     if(category<MSVCRT_LC_MIN || category>MSVCRT_LC_MAX)
1129         return NULL;
1130
1131     if(!locale) {
1132         if(category == MSVCRT_LC_ALL)
1133             return construct_lc_all(cur);
1134
1135         return cur->locinfo->lc_category[category].locale;
1136     }
1137
1138     loc = MSVCRT__create_locale(category, locale);
1139     if(!loc) {
1140         WARN("%d %s failed\n", category, locale);
1141         return NULL;
1142     }
1143
1144     LOCK_LOCALE;
1145
1146     switch(category) {
1147         case MSVCRT_LC_ALL:
1148             if(!cur)
1149                 break;
1150         case MSVCRT_LC_COLLATE:
1151             cur->locinfo->lc_handle[MSVCRT_LC_COLLATE] =
1152                 loc->locinfo->lc_handle[MSVCRT_LC_COLLATE];
1153             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_COLLATE].locale,
1154                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].locale);
1155             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount,
1156                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_COLLATE].refcount);
1157
1158             if(category != MSVCRT_LC_ALL)
1159                 break;
1160         case MSVCRT_LC_CTYPE:
1161             cur->locinfo->lc_handle[MSVCRT_LC_CTYPE] =
1162                 loc->locinfo->lc_handle[MSVCRT_LC_CTYPE];
1163             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_CTYPE].locale,
1164                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].locale);
1165             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount,
1166                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_CTYPE].refcount);
1167
1168             cur->locinfo->lc_codepage = loc->locinfo->lc_codepage;
1169             cur->locinfo->lc_collate_cp = loc->locinfo->lc_collate_cp;
1170             cur->locinfo->lc_clike = loc->locinfo->lc_clike;
1171             cur->locinfo->mb_cur_max = loc->locinfo->mb_cur_max;
1172
1173             swap_pointers((void**)&cur->locinfo->ctype1_refcount,
1174                     (void**)&loc->locinfo->ctype1_refcount);
1175             swap_pointers((void**)&cur->locinfo->ctype1, (void**)&loc->locinfo->ctype1);
1176             swap_pointers((void**)&cur->locinfo->pctype, (void**)&loc->locinfo->pctype);
1177             swap_pointers((void**)&cur->locinfo->pclmap, (void**)&loc->locinfo->pclmap);
1178             swap_pointers((void**)&cur->locinfo->pcumap, (void**)&loc->locinfo->pcumap);
1179
1180             memcpy(cur->mbcinfo, loc->mbcinfo, sizeof(MSVCRT_threadmbcinfo));
1181
1182             if(category != MSVCRT_LC_ALL)
1183                 break;
1184         case MSVCRT_LC_MONETARY:
1185             cur->locinfo->lc_handle[MSVCRT_LC_MONETARY] =
1186                 loc->locinfo->lc_handle[MSVCRT_LC_MONETARY];
1187             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_MONETARY].locale,
1188                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].locale);
1189             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount,
1190                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_MONETARY].refcount);
1191
1192             swap_pointers((void**)&cur->locinfo->lconv->int_curr_symbol,
1193                     (void**)&loc->locinfo->lconv->int_curr_symbol);
1194             swap_pointers((void**)&cur->locinfo->lconv->currency_symbol,
1195                     (void**)&loc->locinfo->lconv->currency_symbol);
1196             swap_pointers((void**)&cur->locinfo->lconv->mon_decimal_point,
1197                     (void**)&loc->locinfo->lconv->mon_decimal_point);
1198             swap_pointers((void**)&cur->locinfo->lconv->mon_thousands_sep,
1199                     (void**)&loc->locinfo->lconv->mon_thousands_sep);
1200             swap_pointers((void**)&cur->locinfo->lconv->mon_grouping,
1201                     (void**)&loc->locinfo->lconv->mon_grouping);
1202             swap_pointers((void**)&cur->locinfo->lconv->positive_sign,
1203                     (void**)&loc->locinfo->lconv->positive_sign);
1204             swap_pointers((void**)&cur->locinfo->lconv->negative_sign,
1205                     (void**)&loc->locinfo->lconv->negative_sign);
1206             cur->locinfo->lconv->int_frac_digits = loc->locinfo->lconv->int_frac_digits;
1207             cur->locinfo->lconv->frac_digits = loc->locinfo->lconv->frac_digits;
1208             cur->locinfo->lconv->p_cs_precedes = loc->locinfo->lconv->p_cs_precedes;
1209             cur->locinfo->lconv->p_sep_by_space = loc->locinfo->lconv->p_sep_by_space;
1210             cur->locinfo->lconv->n_cs_precedes = loc->locinfo->lconv->n_cs_precedes;
1211             cur->locinfo->lconv->n_sep_by_space = loc->locinfo->lconv->n_sep_by_space;
1212             cur->locinfo->lconv->p_sign_posn = loc->locinfo->lconv->p_sign_posn;
1213             cur->locinfo->lconv->n_sign_posn = loc->locinfo->lconv->n_sign_posn;
1214
1215             if(category != MSVCRT_LC_ALL)
1216                 break;
1217         case MSVCRT_LC_NUMERIC:
1218             cur->locinfo->lc_handle[MSVCRT_LC_NUMERIC] =
1219                 loc->locinfo->lc_handle[MSVCRT_LC_NUMERIC];
1220             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale,
1221                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].locale);
1222             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount,
1223                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_NUMERIC].refcount);
1224
1225             swap_pointers((void**)&cur->locinfo->lconv->decimal_point,
1226                     (void**)&loc->locinfo->lconv->decimal_point);
1227             swap_pointers((void**)&cur->locinfo->lconv->thousands_sep,
1228                     (void**)&loc->locinfo->lconv->thousands_sep);
1229             swap_pointers((void**)&cur->locinfo->lconv->grouping,
1230                     (void**)&loc->locinfo->lconv->grouping);
1231
1232             if(category != MSVCRT_LC_ALL)
1233                 break;
1234         case MSVCRT_LC_TIME:
1235             cur->locinfo->lc_handle[MSVCRT_LC_TIME] =
1236                 loc->locinfo->lc_handle[MSVCRT_LC_TIME];
1237             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_TIME].locale,
1238                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].locale);
1239             swap_pointers((void**)&cur->locinfo->lc_category[MSVCRT_LC_TIME].refcount,
1240                     (void**)&loc->locinfo->lc_category[MSVCRT_LC_TIME].refcount);
1241
1242             if(category != MSVCRT_LC_ALL)
1243                 break;
1244     }
1245
1246     if(!cur)
1247         MSVCRT_locale = cur = loc;
1248     else
1249         MSVCRT__free_locale(loc);
1250
1251     UNLOCK_LOCALE;
1252
1253     if(cur == MSVCRT_locale) {
1254         MSVCRT___lc_codepage = cur->locinfo->lc_codepage;
1255         MSVCRT___lc_collate_cp = cur->locinfo->lc_collate_cp;
1256         MSVCRT___mb_cur_max = cur->locinfo->mb_cur_max;
1257         MSVCRT__pctype = cur->locinfo->pctype;
1258     }
1259
1260     if(category == MSVCRT_LC_ALL)
1261         return construct_lc_all(cur);
1262
1263     return cur->locinfo->lc_category[category].locale;
1264 }