More doc fixups
[platform/upstream/glib.git] / glib / gwin32.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1998  Peter Mattis, Spencer Kimball and Josh MacDonald
3  * Copyright (C) 1998-1999  Tor Lillqvist
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GLib Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 /* 
29  * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
30  */
31
32 #include "config.h"
33
34 #include "glibconfig.h"
35
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <wchar.h>
40 #include <errno.h>
41
42 #define STRICT                  /* Strict typing, please */
43 #include <windows.h>
44 #undef STRICT
45 #ifndef G_WITH_CYGWIN
46 #include <direct.h>
47 #endif
48 #include <errno.h>
49 #include <ctype.h>
50 #ifdef _MSC_VER
51 #  include <io.h>
52 #endif /* _MSC_VER */
53
54 #include "glib.h"
55 #include "galias.h"
56
57 #ifdef G_WITH_CYGWIN
58 #include <sys/cygwin.h>
59 #endif
60
61 #ifndef G_WITH_CYGWIN
62
63 gint
64 g_win32_ftruncate (gint  fd,
65                    guint size)
66 {
67   return _chsize (fd, size);
68 }
69
70 #endif
71
72 /**
73  * g_win32_getlocale:
74  *
75  * The setlocale() function in the Microsoft C library uses locale
76  * names of the form "English_United States.1252" etc. We want the
77  * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
78  * current thread locale from Windows - without any encoding info -
79  * and returns it as a string of the above form for use in forming
80  * file names etc. The returned string should be deallocated with
81  * g_free().
82  *
83  * Returns: newly-allocated locale name.
84  **/
85
86 /* Borrowed from GNU gettext 0.13.1: */
87 /* Mingw headers don't have latest language and sublanguage codes.  */
88 #ifndef LANG_AFRIKAANS
89 #define LANG_AFRIKAANS 0x36
90 #endif
91 #ifndef LANG_ALBANIAN
92 #define LANG_ALBANIAN 0x1c
93 #endif
94 #ifndef LANG_AMHARIC
95 #define LANG_AMHARIC 0x5e
96 #endif
97 #ifndef LANG_ARABIC
98 #define LANG_ARABIC 0x01
99 #endif
100 #ifndef LANG_ARMENIAN
101 #define LANG_ARMENIAN 0x2b
102 #endif
103 #ifndef LANG_ASSAMESE
104 #define LANG_ASSAMESE 0x4d
105 #endif
106 #ifndef LANG_AZERI
107 #define LANG_AZERI 0x2c
108 #endif
109 #ifndef LANG_BASQUE
110 #define LANG_BASQUE 0x2d
111 #endif
112 #ifndef LANG_BELARUSIAN
113 #define LANG_BELARUSIAN 0x23
114 #endif
115 #ifndef LANG_BENGALI
116 #define LANG_BENGALI 0x45
117 #endif
118 #ifndef LANG_BURMESE
119 #define LANG_BURMESE 0x55
120 #endif
121 #ifndef LANG_CAMBODIAN
122 #define LANG_CAMBODIAN 0x53
123 #endif
124 #ifndef LANG_CATALAN
125 #define LANG_CATALAN 0x03
126 #endif
127 #ifndef LANG_CHEROKEE
128 #define LANG_CHEROKEE 0x5c
129 #endif
130 #ifndef LANG_DIVEHI
131 #define LANG_DIVEHI 0x65
132 #endif
133 #ifndef LANG_EDO
134 #define LANG_EDO 0x66
135 #endif
136 #ifndef LANG_ESTONIAN
137 #define LANG_ESTONIAN 0x25
138 #endif
139 #ifndef LANG_FAEROESE
140 #define LANG_FAEROESE 0x38
141 #endif
142 #ifndef LANG_FARSI
143 #define LANG_FARSI 0x29
144 #endif
145 #ifndef LANG_FRISIAN
146 #define LANG_FRISIAN 0x62
147 #endif
148 #ifndef LANG_FULFULDE
149 #define LANG_FULFULDE 0x67
150 #endif
151 #ifndef LANG_GAELIC
152 #define LANG_GAELIC 0x3c
153 #endif
154 #ifndef LANG_GALICIAN
155 #define LANG_GALICIAN 0x56
156 #endif
157 #ifndef LANG_GEORGIAN
158 #define LANG_GEORGIAN 0x37
159 #endif
160 #ifndef LANG_GUARANI
161 #define LANG_GUARANI 0x74
162 #endif
163 #ifndef LANG_GUJARATI
164 #define LANG_GUJARATI 0x47
165 #endif
166 #ifndef LANG_HAUSA
167 #define LANG_HAUSA 0x68
168 #endif
169 #ifndef LANG_HAWAIIAN
170 #define LANG_HAWAIIAN 0x75
171 #endif
172 #ifndef LANG_HEBREW
173 #define LANG_HEBREW 0x0d
174 #endif
175 #ifndef LANG_HINDI
176 #define LANG_HINDI 0x39
177 #endif
178 #ifndef LANG_IBIBIO
179 #define LANG_IBIBIO 0x69
180 #endif
181 #ifndef LANG_IGBO
182 #define LANG_IGBO 0x70
183 #endif
184 #ifndef LANG_INDONESIAN
185 #define LANG_INDONESIAN 0x21
186 #endif
187 #ifndef LANG_INUKTITUT
188 #define LANG_INUKTITUT 0x5d
189 #endif
190 #ifndef LANG_KANNADA
191 #define LANG_KANNADA 0x4b
192 #endif
193 #ifndef LANG_KANURI
194 #define LANG_KANURI 0x71
195 #endif
196 #ifndef LANG_KASHMIRI
197 #define LANG_KASHMIRI 0x60
198 #endif
199 #ifndef LANG_KAZAK
200 #define LANG_KAZAK 0x3f
201 #endif
202 #ifndef LANG_KONKANI
203 #define LANG_KONKANI 0x57
204 #endif
205 #ifndef LANG_KYRGYZ
206 #define LANG_KYRGYZ 0x40
207 #endif
208 #ifndef LANG_LAO
209 #define LANG_LAO 0x54
210 #endif
211 #ifndef LANG_LATIN
212 #define LANG_LATIN 0x76
213 #endif
214 #ifndef LANG_LATVIAN
215 #define LANG_LATVIAN 0x26
216 #endif
217 #ifndef LANG_LITHUANIAN
218 #define LANG_LITHUANIAN 0x27
219 #endif
220 #ifndef LANG_MACEDONIAN
221 #define LANG_MACEDONIAN 0x2f
222 #endif
223 #ifndef LANG_MALAY
224 #define LANG_MALAY 0x3e
225 #endif
226 #ifndef LANG_MALAYALAM
227 #define LANG_MALAYALAM 0x4c
228 #endif
229 #ifndef LANG_MALTESE
230 #define LANG_MALTESE 0x3a
231 #endif
232 #ifndef LANG_MANIPURI
233 #define LANG_MANIPURI 0x58
234 #endif
235 #ifndef LANG_MARATHI
236 #define LANG_MARATHI 0x4e
237 #endif
238 #ifndef LANG_MONGOLIAN
239 #define LANG_MONGOLIAN 0x50
240 #endif
241 #ifndef LANG_NEPALI
242 #define LANG_NEPALI 0x61
243 #endif
244 #ifndef LANG_ORIYA
245 #define LANG_ORIYA 0x48
246 #endif
247 #ifndef LANG_OROMO
248 #define LANG_OROMO 0x72
249 #endif
250 #ifndef LANG_PAPIAMENTU
251 #define LANG_PAPIAMENTU 0x79
252 #endif
253 #ifndef LANG_PASHTO
254 #define LANG_PASHTO 0x63
255 #endif
256 #ifndef LANG_PUNJABI
257 #define LANG_PUNJABI 0x46
258 #endif
259 #ifndef LANG_RHAETO_ROMANCE
260 #define LANG_RHAETO_ROMANCE 0x17
261 #endif
262 #ifndef LANG_SAAMI
263 #define LANG_SAAMI 0x3b
264 #endif
265 #ifndef LANG_SANSKRIT
266 #define LANG_SANSKRIT 0x4f
267 #endif
268 #ifndef LANG_SERBIAN
269 #define LANG_SERBIAN 0x1a
270 #endif
271 #ifndef LANG_SINDHI
272 #define LANG_SINDHI 0x59
273 #endif
274 #ifndef LANG_SINHALESE
275 #define LANG_SINHALESE 0x5b
276 #endif
277 #ifndef LANG_SLOVAK
278 #define LANG_SLOVAK 0x1b
279 #endif
280 #ifndef LANG_SOMALI
281 #define LANG_SOMALI 0x77
282 #endif
283 #ifndef LANG_SORBIAN
284 #define LANG_SORBIAN 0x2e
285 #endif
286 #ifndef LANG_SUTU
287 #define LANG_SUTU 0x30
288 #endif
289 #ifndef LANG_SWAHILI
290 #define LANG_SWAHILI 0x41
291 #endif
292 #ifndef LANG_SYRIAC
293 #define LANG_SYRIAC 0x5a
294 #endif
295 #ifndef LANG_TAGALOG
296 #define LANG_TAGALOG 0x64
297 #endif
298 #ifndef LANG_TAJIK
299 #define LANG_TAJIK 0x28
300 #endif
301 #ifndef LANG_TAMAZIGHT
302 #define LANG_TAMAZIGHT 0x5f
303 #endif
304 #ifndef LANG_TAMIL
305 #define LANG_TAMIL 0x49
306 #endif
307 #ifndef LANG_TATAR
308 #define LANG_TATAR 0x44
309 #endif
310 #ifndef LANG_TELUGU
311 #define LANG_TELUGU 0x4a
312 #endif
313 #ifndef LANG_THAI
314 #define LANG_THAI 0x1e
315 #endif
316 #ifndef LANG_TIBETAN
317 #define LANG_TIBETAN 0x51
318 #endif
319 #ifndef LANG_TIGRINYA
320 #define LANG_TIGRINYA 0x73
321 #endif
322 #ifndef LANG_TSONGA
323 #define LANG_TSONGA 0x31
324 #endif
325 #ifndef LANG_TSWANA
326 #define LANG_TSWANA 0x32
327 #endif
328 #ifndef LANG_TURKMEN
329 #define LANG_TURKMEN 0x42
330 #endif
331 #ifndef LANG_UKRAINIAN
332 #define LANG_UKRAINIAN 0x22
333 #endif
334 #ifndef LANG_URDU
335 #define LANG_URDU 0x20
336 #endif
337 #ifndef LANG_UZBEK
338 #define LANG_UZBEK 0x43
339 #endif
340 #ifndef LANG_VENDA
341 #define LANG_VENDA 0x33
342 #endif
343 #ifndef LANG_VIETNAMESE
344 #define LANG_VIETNAMESE 0x2a
345 #endif
346 #ifndef LANG_WELSH
347 #define LANG_WELSH 0x52
348 #endif
349 #ifndef LANG_XHOSA
350 #define LANG_XHOSA 0x34
351 #endif
352 #ifndef LANG_YI
353 #define LANG_YI 0x78
354 #endif
355 #ifndef LANG_YIDDISH
356 #define LANG_YIDDISH 0x3d
357 #endif
358 #ifndef LANG_YORUBA
359 #define LANG_YORUBA 0x6a
360 #endif
361 #ifndef LANG_ZULU
362 #define LANG_ZULU 0x35
363 #endif
364 #ifndef SUBLANG_ARABIC_SAUDI_ARABIA
365 #define SUBLANG_ARABIC_SAUDI_ARABIA 0x01
366 #endif
367 #ifndef SUBLANG_ARABIC_IRAQ
368 #define SUBLANG_ARABIC_IRAQ 0x02
369 #endif
370 #ifndef SUBLANG_ARABIC_EGYPT
371 #define SUBLANG_ARABIC_EGYPT 0x03
372 #endif
373 #ifndef SUBLANG_ARABIC_LIBYA
374 #define SUBLANG_ARABIC_LIBYA 0x04
375 #endif
376 #ifndef SUBLANG_ARABIC_ALGERIA
377 #define SUBLANG_ARABIC_ALGERIA 0x05
378 #endif
379 #ifndef SUBLANG_ARABIC_MOROCCO
380 #define SUBLANG_ARABIC_MOROCCO 0x06
381 #endif
382 #ifndef SUBLANG_ARABIC_TUNISIA
383 #define SUBLANG_ARABIC_TUNISIA 0x07
384 #endif
385 #ifndef SUBLANG_ARABIC_OMAN
386 #define SUBLANG_ARABIC_OMAN 0x08
387 #endif
388 #ifndef SUBLANG_ARABIC_YEMEN
389 #define SUBLANG_ARABIC_YEMEN 0x09
390 #endif
391 #ifndef SUBLANG_ARABIC_SYRIA
392 #define SUBLANG_ARABIC_SYRIA 0x0a
393 #endif
394 #ifndef SUBLANG_ARABIC_JORDAN
395 #define SUBLANG_ARABIC_JORDAN 0x0b
396 #endif
397 #ifndef SUBLANG_ARABIC_LEBANON
398 #define SUBLANG_ARABIC_LEBANON 0x0c
399 #endif
400 #ifndef SUBLANG_ARABIC_KUWAIT
401 #define SUBLANG_ARABIC_KUWAIT 0x0d
402 #endif
403 #ifndef SUBLANG_ARABIC_UAE
404 #define SUBLANG_ARABIC_UAE 0x0e
405 #endif
406 #ifndef SUBLANG_ARABIC_BAHRAIN
407 #define SUBLANG_ARABIC_BAHRAIN 0x0f
408 #endif
409 #ifndef SUBLANG_ARABIC_QATAR
410 #define SUBLANG_ARABIC_QATAR 0x10
411 #endif
412 #ifndef SUBLANG_AZERI_LATIN
413 #define SUBLANG_AZERI_LATIN 0x01
414 #endif
415 #ifndef SUBLANG_AZERI_CYRILLIC
416 #define SUBLANG_AZERI_CYRILLIC 0x02
417 #endif
418 #ifndef SUBLANG_BENGALI_INDIA
419 #define SUBLANG_BENGALI_INDIA 0x00
420 #endif
421 #ifndef SUBLANG_BENGALI_BANGLADESH
422 #define SUBLANG_BENGALI_BANGLADESH 0x01
423 #endif
424 #ifndef SUBLANG_CHINESE_MACAU
425 #define SUBLANG_CHINESE_MACAU 0x05
426 #endif
427 #ifndef SUBLANG_ENGLISH_SOUTH_AFRICA
428 #define SUBLANG_ENGLISH_SOUTH_AFRICA 0x07
429 #endif
430 #ifndef SUBLANG_ENGLISH_JAMAICA
431 #define SUBLANG_ENGLISH_JAMAICA 0x08
432 #endif
433 #ifndef SUBLANG_ENGLISH_CARIBBEAN
434 #define SUBLANG_ENGLISH_CARIBBEAN 0x09
435 #endif
436 #ifndef SUBLANG_ENGLISH_BELIZE
437 #define SUBLANG_ENGLISH_BELIZE 0x0a
438 #endif
439 #ifndef SUBLANG_ENGLISH_TRINIDAD
440 #define SUBLANG_ENGLISH_TRINIDAD 0x0b
441 #endif
442 #ifndef SUBLANG_ENGLISH_ZIMBABWE
443 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
444 #endif
445 #ifndef SUBLANG_ENGLISH_PHILIPPINES
446 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
447 #endif
448 #ifndef SUBLANG_ENGLISH_INDONESIA
449 #define SUBLANG_ENGLISH_INDONESIA 0x0e
450 #endif
451 #ifndef SUBLANG_ENGLISH_HONGKONG
452 #define SUBLANG_ENGLISH_HONGKONG 0x0f
453 #endif
454 #ifndef SUBLANG_ENGLISH_INDIA
455 #define SUBLANG_ENGLISH_INDIA 0x10
456 #endif
457 #ifndef SUBLANG_ENGLISH_MALAYSIA
458 #define SUBLANG_ENGLISH_MALAYSIA 0x11
459 #endif
460 #ifndef SUBLANG_ENGLISH_SINGAPORE
461 #define SUBLANG_ENGLISH_SINGAPORE 0x12
462 #endif
463 #ifndef SUBLANG_FRENCH_LUXEMBOURG
464 #define SUBLANG_FRENCH_LUXEMBOURG 0x05
465 #endif
466 #ifndef SUBLANG_FRENCH_MONACO
467 #define SUBLANG_FRENCH_MONACO 0x06
468 #endif
469 #ifndef SUBLANG_FRENCH_WESTINDIES
470 #define SUBLANG_FRENCH_WESTINDIES 0x07
471 #endif
472 #ifndef SUBLANG_FRENCH_REUNION
473 #define SUBLANG_FRENCH_REUNION 0x08
474 #endif
475 #ifndef SUBLANG_FRENCH_CONGO
476 #define SUBLANG_FRENCH_CONGO 0x09
477 #endif
478 #ifndef SUBLANG_FRENCH_SENEGAL
479 #define SUBLANG_FRENCH_SENEGAL 0x0a
480 #endif
481 #ifndef SUBLANG_FRENCH_CAMEROON
482 #define SUBLANG_FRENCH_CAMEROON 0x0b
483 #endif
484 #ifndef SUBLANG_FRENCH_COTEDIVOIRE
485 #define SUBLANG_FRENCH_COTEDIVOIRE 0x0c
486 #endif
487 #ifndef SUBLANG_FRENCH_MALI
488 #define SUBLANG_FRENCH_MALI 0x0d
489 #endif
490 #ifndef SUBLANG_FRENCH_MOROCCO
491 #define SUBLANG_FRENCH_MOROCCO 0x0e
492 #endif
493 #ifndef SUBLANG_FRENCH_HAITI
494 #define SUBLANG_FRENCH_HAITI 0x0f
495 #endif
496 #ifndef SUBLANG_GERMAN_LUXEMBOURG
497 #define SUBLANG_GERMAN_LUXEMBOURG 0x04
498 #endif
499 #ifndef SUBLANG_GERMAN_LIECHTENSTEIN
500 #define SUBLANG_GERMAN_LIECHTENSTEIN 0x05
501 #endif
502 #ifndef SUBLANG_KASHMIRI_INDIA
503 #define SUBLANG_KASHMIRI_INDIA 0x02
504 #endif
505 #ifndef SUBLANG_MALAY_MALAYSIA
506 #define SUBLANG_MALAY_MALAYSIA 0x01
507 #endif
508 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
509 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
510 #endif
511 #ifndef SUBLANG_NEPALI_INDIA
512 #define SUBLANG_NEPALI_INDIA 0x02
513 #endif
514 #ifndef SUBLANG_PUNJABI_INDIA
515 #define SUBLANG_PUNJABI_INDIA 0x00
516 #endif
517 #ifndef SUBLANG_PUNJABI_PAKISTAN
518 #define SUBLANG_PUNJABI_PAKISTAN 0x01
519 #endif
520 #ifndef SUBLANG_ROMANIAN_ROMANIA
521 #define SUBLANG_ROMANIAN_ROMANIA 0x00
522 #endif
523 #ifndef SUBLANG_ROMANIAN_MOLDOVA
524 #define SUBLANG_ROMANIAN_MOLDOVA 0x01
525 #endif
526 #ifndef SUBLANG_SERBIAN_LATIN
527 #define SUBLANG_SERBIAN_LATIN 0x02
528 #endif
529 #ifndef SUBLANG_SERBIAN_CYRILLIC
530 #define SUBLANG_SERBIAN_CYRILLIC 0x03
531 #endif
532 #ifndef SUBLANG_SINDHI_INDIA
533 #define SUBLANG_SINDHI_INDIA 0x00
534 #endif
535 #ifndef SUBLANG_SINDHI_PAKISTAN
536 #define SUBLANG_SINDHI_PAKISTAN 0x01
537 #endif
538 #ifndef SUBLANG_SPANISH_GUATEMALA
539 #define SUBLANG_SPANISH_GUATEMALA 0x04
540 #endif
541 #ifndef SUBLANG_SPANISH_COSTA_RICA
542 #define SUBLANG_SPANISH_COSTA_RICA 0x05
543 #endif
544 #ifndef SUBLANG_SPANISH_PANAMA
545 #define SUBLANG_SPANISH_PANAMA 0x06
546 #endif
547 #ifndef SUBLANG_SPANISH_DOMINICAN_REPUBLIC
548 #define SUBLANG_SPANISH_DOMINICAN_REPUBLIC 0x07
549 #endif
550 #ifndef SUBLANG_SPANISH_VENEZUELA
551 #define SUBLANG_SPANISH_VENEZUELA 0x08
552 #endif
553 #ifndef SUBLANG_SPANISH_COLOMBIA
554 #define SUBLANG_SPANISH_COLOMBIA 0x09
555 #endif
556 #ifndef SUBLANG_SPANISH_PERU
557 #define SUBLANG_SPANISH_PERU 0x0a
558 #endif
559 #ifndef SUBLANG_SPANISH_ARGENTINA
560 #define SUBLANG_SPANISH_ARGENTINA 0x0b
561 #endif
562 #ifndef SUBLANG_SPANISH_ECUADOR
563 #define SUBLANG_SPANISH_ECUADOR 0x0c
564 #endif
565 #ifndef SUBLANG_SPANISH_CHILE
566 #define SUBLANG_SPANISH_CHILE 0x0d
567 #endif
568 #ifndef SUBLANG_SPANISH_URUGUAY
569 #define SUBLANG_SPANISH_URUGUAY 0x0e
570 #endif
571 #ifndef SUBLANG_SPANISH_PARAGUAY
572 #define SUBLANG_SPANISH_PARAGUAY 0x0f
573 #endif
574 #ifndef SUBLANG_SPANISH_BOLIVIA
575 #define SUBLANG_SPANISH_BOLIVIA 0x10
576 #endif
577 #ifndef SUBLANG_SPANISH_EL_SALVADOR
578 #define SUBLANG_SPANISH_EL_SALVADOR 0x11
579 #endif
580 #ifndef SUBLANG_SPANISH_HONDURAS
581 #define SUBLANG_SPANISH_HONDURAS 0x12
582 #endif
583 #ifndef SUBLANG_SPANISH_NICARAGUA
584 #define SUBLANG_SPANISH_NICARAGUA 0x13
585 #endif
586 #ifndef SUBLANG_SPANISH_PUERTO_RICO
587 #define SUBLANG_SPANISH_PUERTO_RICO 0x14
588 #endif
589 #ifndef SUBLANG_SWEDISH_FINLAND
590 #define SUBLANG_SWEDISH_FINLAND 0x02
591 #endif
592 #ifndef SUBLANG_TAMAZIGHT_ARABIC
593 #define SUBLANG_TAMAZIGHT_ARABIC 0x01
594 #endif
595 #ifndef SUBLANG_TAMAZIGHT_LATIN
596 #define SUBLANG_TAMAZIGHT_LATIN 0x02
597 #endif
598 #ifndef SUBLANG_TIGRINYA_ETHIOPIA
599 #define SUBLANG_TIGRINYA_ETHIOPIA 0x00
600 #endif
601 #ifndef SUBLANG_TIGRINYA_ERITREA
602 #define SUBLANG_TIGRINYA_ERITREA 0x01
603 #endif
604 #ifndef SUBLANG_URDU_PAKISTAN
605 #define SUBLANG_URDU_PAKISTAN 0x01
606 #endif
607 #ifndef SUBLANG_URDU_INDIA
608 #define SUBLANG_URDU_INDIA 0x02
609 #endif
610 #ifndef SUBLANG_UZBEK_LATIN
611 #define SUBLANG_UZBEK_LATIN 0x01
612 #endif
613 #ifndef SUBLANG_UZBEK_CYRILLIC
614 #define SUBLANG_UZBEK_CYRILLIC 0x02
615 #endif
616
617 gchar *
618 g_win32_getlocale (void)
619 {
620   LCID lcid;
621   LANGID langid;
622   gchar *ev;
623   gint primary, sub;
624   gchar *l = "C", *sl = NULL;
625   gchar bfr[20];
626
627   /* Let the user override the system settings through environment
628      variables, as on POSIX systems.  */
629   if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
630       || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
631       || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
632     return g_strdup (ev);
633
634   /* Use native Win32 API locale ID.  */
635   lcid = GetThreadLocale ();
636
637   /* Strip off the sorting rules, keep only the language part.  */
638   langid = LANGIDFROMLCID (lcid);
639
640   /* Split into language and territory part.  */
641   primary = PRIMARYLANGID (langid);
642   sub = SUBLANGID (langid);
643   switch (primary)
644     {
645     case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
646     case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
647     case LANG_ARABIC:
648       l = "ar";
649       switch (sub)
650         {
651         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
652         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
653         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
654         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
655         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
656         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
657         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
658         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
659         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
660         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
661         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
662         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
663         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
664         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
665         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
666         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
667         }
668       break;
669     case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
670     case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
671     case LANG_AZERI:
672       l = "az";
673       switch (sub)
674         {
675         /* FIXME: Adjust this when Azerbaijani locales appear on Unix.  */
676         case SUBLANG_AZERI_LATIN: sl = "AZ@latin"; break;
677         case SUBLANG_AZERI_CYRILLIC: sl = "AZ@cyrillic"; break;
678         }
679       break;
680     case LANG_BASQUE:
681       l = "eu"; /* sl could be "ES" or "FR".  */
682       break;
683     case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
684     case LANG_BENGALI:
685       l = "bn";
686       switch (sub)
687         {
688         case SUBLANG_BENGALI_INDIA: sl = "IN"; break;
689         case SUBLANG_BENGALI_BANGLADESH: sl = "BD"; break;
690         }
691       break;
692     case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
693     case LANG_BURMESE: l = "my"; sl = "MM"; break;
694     case LANG_CAMBODIAN: l = "km"; sl = "KH"; break;
695     case LANG_CATALAN: l = "ca"; sl = "ES"; break;
696     case LANG_CHINESE:
697       l = "zh";
698       switch (sub)
699         {
700         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
701         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
702         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
703         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
704         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
705         }
706       break;
707     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN */
708       switch (sub)
709         {
710         /* FIXME: How to distinguish Croatian and Latin Serbian locales?  */
711         case SUBLANG_SERBIAN_LATIN: l = "sr"; sl = "@Latn"; break;
712         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
713         default: l = "hr"; sl = "HR";
714         }
715       break;
716     case LANG_CZECH: l = "cs"; sl = "CZ"; break;
717     case LANG_DANISH: l = "da"; sl = "DK"; break;
718     case LANG_DIVEHI: l = "div"; sl = "MV"; break;
719     case LANG_DUTCH:
720       l = "nl";
721       switch (sub)
722         {
723         case SUBLANG_DUTCH: sl = "NL"; break;
724         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
725         }
726       break;
727     case LANG_ENGLISH:
728       l = "en";
729       switch (sub)
730         {
731         case SUBLANG_ENGLISH_US: sl = "US"; break;
732         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
733         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
734         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
735         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
736         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
737         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
738         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
739         case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
740         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
741         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
742         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
743         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
744         case SUBLANG_ENGLISH_INDONESIA: sl = "ID"; break;
745         case SUBLANG_ENGLISH_HONGKONG: sl = "HK"; break;
746         case SUBLANG_ENGLISH_INDIA: sl = "IN"; break;
747         case SUBLANG_ENGLISH_MALAYSIA: sl = "MY"; break;
748         case SUBLANG_ENGLISH_SINGAPORE: sl = "SG"; break;
749         }
750       break;
751     case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
752     case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
753     case LANG_FARSI: l = "fa"; sl = "IR"; break;
754     case LANG_FINNISH: l = "fi"; sl = "FI"; break;
755     case LANG_FRENCH:
756       l = "fr";
757       switch (sub)
758         {
759         case SUBLANG_FRENCH: sl = "FR"; break;
760         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
761         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
762         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
763         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
764         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
765         case SUBLANG_FRENCH_WESTINDIES: break;
766         case SUBLANG_FRENCH_REUNION: sl = "RE"; break;
767         case SUBLANG_FRENCH_CONGO: sl = "CG"; break;
768         case SUBLANG_FRENCH_SENEGAL: sl = "SN"; break;
769         case SUBLANG_FRENCH_CAMEROON: sl = "CM"; break;
770         case SUBLANG_FRENCH_COTEDIVOIRE: sl = "CI"; break;
771         case SUBLANG_FRENCH_MALI: sl = "ML"; break;
772         case SUBLANG_FRENCH_MOROCCO: sl = "MA"; break;
773         case SUBLANG_FRENCH_HAITI: sl = "HT"; break;
774         }
775       break;
776     case LANG_FRISIAN: l = "fy"; sl ="NL"; break;
777     case LANG_FULFULDE: l = "ful"; sl = "NG"; break;
778     case LANG_GAELIC:
779       switch (sub)
780         {
781         case 0x01: /* SCOTTISH */ l = "gd"; sl = "GB"; break;
782         case 0x02: /* IRISH */ l = "ga"; sl = "IE"; break;
783         }
784       break;
785     case LANG_GALICIAN: l = "gl"; sl = "ES"; break;
786     case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
787     case LANG_GERMAN:
788       l = "de";
789       switch (sub)
790         {
791         case SUBLANG_GERMAN: sl = "DE"; break;
792         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
793         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
794         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
795         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
796         }
797       break;
798     case LANG_GREEK: l = "el"; sl = "GR"; break;
799     case LANG_GUARANI: l = "gn"; sl = "PY"; break;
800     case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
801     case LANG_HAUSA: l = "ha"; sl = "NG"; break;
802     case LANG_HAWAIIAN:
803       /* FIXME: Do they mean Hawaiian ("haw_US", 1000 speakers)
804        * or Hawaii Creole English ("cpe_US", 600000 speakers)?
805        */
806       l = "cpe";
807       sl = "US";
808       break;
809     case LANG_HEBREW: l = "he"; sl = "IL"; break;
810     case LANG_HINDI: l = "hi"; sl = "IN"; break;
811     case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
812     case LANG_IBIBIO: l = "nic"; sl = "NG"; break;
813     case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
814     case LANG_IGBO: l = "ibo"; sl = "NG"; break;
815     case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
816     case LANG_INUKTITUT: l = "iu"; sl = "CA"; break;
817     case LANG_ITALIAN:
818       l = "it";
819       switch (sub)
820         {
821         case SUBLANG_ITALIAN: sl = "IT"; break;
822         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
823         }
824       break;
825     case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
826     case LANG_KANNADA: l = "kn"; sl = "IN"; break;
827     case LANG_KANURI: l = "kau"; sl = "NG"; break;
828     case LANG_KASHMIRI:
829       l = "ks";
830       switch (sub)
831         {
832         case SUBLANG_DEFAULT: sl = "PK"; break;
833         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
834         }
835       break;
836     case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
837     case LANG_KONKANI:
838       /* FIXME: Adjust this when such locales appear on Unix.  */
839       l = "kok";
840       sl = "IN";
841       break;
842     case LANG_KOREAN: l = "ko"; sl = "KR"; break;
843     case LANG_KYRGYZ: l = "ky"; sl = "KG"; break; 
844     case LANG_LAO: l = "lo"; sl = "LA"; break;
845     case LANG_LATIN: l = "la"; sl = "VA"; break;
846     case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
847     case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
848     case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
849     case LANG_MALAY:
850       l = "ms";
851       switch (sub)
852         {
853         case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
854         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
855         }
856       break;
857     case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
858     case LANG_MANIPURI:
859       /* FIXME: Adjust this when such locales appear on Unix.  */
860       l = "mni";
861       sl = "IN";
862       break;
863     case LANG_MARATHI: l = "mr"; sl = "IN"; break;
864     case LANG_MONGOLIAN:
865       /* Ambiguous: could be "mn_CN" or "mn_MN".  */
866       l = "mn";
867       break;
868     case LANG_NEPALI:
869       l = "ne";
870       switch (sub)
871         {
872         case SUBLANG_DEFAULT: sl = "NP"; break;
873         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
874         }
875       break;
876     case LANG_NORWEGIAN:
877       l = "no";
878       switch (sub)
879         {
880         case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
881         case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
882         }
883       break;
884     case LANG_ORIYA: l = "or"; sl = "IN"; break;
885     case LANG_OROMO: l = "om"; sl = "ET"; break;
886     case LANG_PAPIAMENTU: l = "pap"; sl = "AN"; break;
887     case LANG_PASHTO:
888       /* Ambiguous: could be "ps_PK" or "ps_AF".  */
889       l = "ps";
890       break;
891     case LANG_POLISH: l = "pl"; sl = "PL"; break;
892     case LANG_PORTUGUESE:
893       l = "pt";
894       switch (sub)
895         {
896         case SUBLANG_PORTUGUESE: sl = "PT"; break;
897         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
898         }
899       break;
900     case LANG_PUNJABI:
901       l = "pa";
902       switch (sub)
903         {
904         case SUBLANG_PUNJABI_INDIA: sl = "IN"; break; /* Gurmukhi script */
905         case SUBLANG_PUNJABI_PAKISTAN: sl = "PK"; break; /* Arabic script */
906         }
907       break;
908     case LANG_RHAETO_ROMANCE: l = "rm"; sl = "CH"; break;
909     case LANG_ROMANIAN:
910       l = "ro";
911       switch (sub)
912         {
913         case SUBLANG_ROMANIAN_ROMANIA: sl = "RO"; break;
914         case SUBLANG_ROMANIAN_MOLDOVA: sl = "MD"; break;
915         }
916       break;
917     case LANG_RUSSIAN:
918       l = "ru";/* Ambiguous: could be "ru_RU" or "ru_UA" or "ru_MD". */
919       break;
920     case LANG_SAAMI: /* actually Northern Sami */ l = "se"; sl = "NO"; break;
921     case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
922     case LANG_SINDHI: l = "sd";
923       switch (sub)
924         {
925         case SUBLANG_SINDHI_INDIA: sl = "IN"; break;
926         case SUBLANG_SINDHI_PAKISTAN: sl = "PK"; break;
927         }
928       break;
929     case LANG_SINHALESE: l = "si"; sl = "LK"; break;
930     case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
931     case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
932     case LANG_SOMALI: l = "so"; sl = "SO"; break;
933     case LANG_SORBIAN:
934       /* FIXME: Adjust this when such locales appear on Unix.  */
935       l = "wen";
936       sl = "DE";
937       break;
938     case LANG_SPANISH:
939       l = "es";
940       switch (sub)
941         {
942         case SUBLANG_SPANISH: sl = "ES"; break;
943         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
944         case SUBLANG_SPANISH_MODERN:
945           sl = "ES@modern"; break;      /* not seen on Unix */
946         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
947         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
948         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
949         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
950         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
951         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
952         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
953         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
954         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
955         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
956         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
957         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
958         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
959         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
960         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
961         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
962         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
963         }
964       break;
965     case LANG_SUTU: l = "bnt"; sl = "TZ"; break; /* or "st_LS" or "nso_ZA"? */ 
966     case LANG_SWAHILI: l = "sw"; sl = "KE"; break;
967     case LANG_SWEDISH:
968       l = "sv";
969       switch (sub)
970         {
971         case SUBLANG_DEFAULT: sl = "SE"; break;
972         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
973         }
974       break;
975     case LANG_SYRIAC: l = "syr"; sl = "TR"; break; /* An extinct language. */
976     case LANG_TAGALOG: l = "tl"; sl = "PH"; break;
977     case LANG_TAJIK: l = "tg"; sl = "TJ"; break;
978     case LANG_TAMIL:
979       l = "ta"; /* Ambiguous: could be "ta_IN" or "ta_LK" or "ta_SG". */
980       break;
981     case LANG_TATAR: l = "tt"; sl = "RU"; break;
982     case LANG_TELUGU: l = "te"; sl = "IN"; break;
983     case LANG_THAI: l = "th"; sl = "TH"; break;
984     case LANG_TIBETAN: l = "bo"; sl = "CN"; break;
985     case LANG_TIGRINYA:
986       l = "ti";
987       switch (sub)
988         {
989         case SUBLANG_TIGRINYA_ETHIOPIA: sl = "ET"; break;
990         case SUBLANG_TIGRINYA_ERITREA: sl = "ER"; break;
991         }
992       break;
993     case LANG_TSONGA: l = "ts"; sl = "ZA"; break;
994     case LANG_TSWANA: l = "tn"; sl = "BW"; break;
995     case LANG_TURKISH: l = "tr"; sl = "TR"; break;
996     case LANG_TURKMEN: l = "tk"; sl = "TM"; break;
997     case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
998     case LANG_URDU:
999       l = "ur";
1000       switch (sub)
1001         {
1002         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
1003         case SUBLANG_URDU_INDIA: sl = "IN"; break;
1004         }
1005       break;
1006     case LANG_UZBEK:
1007       l = "uz";
1008       switch (sub)
1009         {
1010         case SUBLANG_UZBEK_LATIN: sl = "UZ"; break;
1011         case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
1012         }
1013       break;
1014     case LANG_VENDA:
1015       /* FIXME: It's not clear whether Venda has the ISO 639-2 two-letter code
1016          "ve" or not.
1017          http://www.loc.gov/standards/iso639-2/englangn.html has it, but
1018          http://lcweb.loc.gov/standards/iso639-2/codechanges.html doesn't,  */
1019       l = "ven"; /* or "ve"? */
1020       sl = "ZA";
1021       break;
1022     case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
1023     case LANG_WELSH: l = "cy"; sl = "GB"; break;
1024     case LANG_XHOSA: l = "xh"; sl = "ZA"; break;
1025     case LANG_YI: l = "sit"; sl = "CN"; break;
1026     case LANG_YIDDISH: l = "yi"; sl = "IL"; break;
1027     case LANG_YORUBA: l = "yo"; sl = "NG"; break;
1028     case LANG_ZULU: l = "zu"; sl = "ZA"; break;
1029     }
1030   strcpy (bfr, l);
1031   if (sl != NULL)
1032     {
1033       if (sl[0] != '@')
1034         strcat (bfr, "_");
1035       strcat (bfr, sl);
1036     }
1037
1038   return g_strdup (bfr);
1039 }
1040
1041 /**
1042  * g_win32_error_message:
1043  * @error: error code.
1044  *
1045  * Translate a Win32 error code (as returned by GetLastError()) into
1046  * the corresponding message. The message is either language neutral,
1047  * or in the thread's language, or the user's language, the system's
1048  * language, or US English (see docs for FormatMessage()). The
1049  * returned string is in UTF-8. It should be deallocated with
1050  * g_free().
1051  *
1052  * Returns: newly-allocated error message
1053  **/
1054 gchar *
1055 g_win32_error_message (gint error)
1056 {
1057   gchar *retval;
1058
1059   if (G_WIN32_HAVE_WIDECHAR_API ())
1060     {
1061       wchar_t *msg = NULL;
1062       int nchars;
1063
1064       FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
1065                       |FORMAT_MESSAGE_IGNORE_INSERTS
1066                       |FORMAT_MESSAGE_FROM_SYSTEM,
1067                       NULL, error, 0,
1068                       (LPWSTR) &msg, 0, NULL);
1069       if (msg != NULL)
1070         {
1071           nchars = wcslen (msg);
1072
1073           if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
1074             msg[nchars-2] = '\0';
1075           
1076           retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
1077           
1078           LocalFree (msg);
1079         }
1080       else
1081         retval = g_strdup ("");
1082     }
1083   else
1084     {
1085       gchar *msg = NULL;
1086       int nbytes;
1087
1088       FormatMessageA (FORMAT_MESSAGE_ALLOCATE_BUFFER
1089                       |FORMAT_MESSAGE_IGNORE_INSERTS
1090                       |FORMAT_MESSAGE_FROM_SYSTEM,
1091                       NULL, error, 0,
1092                       (LPTSTR) &msg, 0, NULL);
1093       if (msg != NULL)
1094         {
1095           nbytes = strlen (msg);
1096
1097           if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
1098             msg[nbytes-2] = '\0';
1099           
1100           retval = g_locale_to_utf8 (msg, -1, NULL, NULL, NULL);
1101           
1102           LocalFree (msg);
1103         }
1104       else
1105         retval = g_strdup ("");
1106     }
1107
1108   return retval;
1109 }
1110
1111 static gchar *
1112 get_package_directory_from_module (gchar *module_name)
1113 {
1114   static GHashTable *module_dirs = NULL;
1115   G_LOCK_DEFINE_STATIC (module_dirs);
1116   HMODULE hmodule = NULL;
1117   gchar *fn;
1118   gchar *p;
1119   gchar *result;
1120
1121   G_LOCK (module_dirs);
1122
1123   if (module_dirs == NULL)
1124     module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1125   
1126   result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
1127       
1128   if (result)
1129     {
1130       G_UNLOCK (module_dirs);
1131       return g_strdup (result);
1132     }
1133
1134   if (module_name)
1135     {
1136       if (G_WIN32_HAVE_WIDECHAR_API ())
1137         {
1138           wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
1139           hmodule = GetModuleHandleW (wc_module_name);
1140           g_free (wc_module_name);
1141         }
1142       else
1143         {
1144           char *cp_module_name = g_locale_from_utf8 (module_name, -1, NULL, NULL, NULL);
1145           hmodule = GetModuleHandleA (cp_module_name);
1146           g_free (cp_module_name);
1147         }
1148       if (!hmodule)
1149         return NULL;
1150     }
1151
1152   if (G_WIN32_HAVE_WIDECHAR_API ())
1153     {
1154       wchar_t wc_fn[MAX_PATH];
1155       if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
1156         {
1157           G_UNLOCK (module_dirs);
1158           return NULL;
1159         }
1160       fn = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
1161     }
1162   else
1163     {
1164       gchar cp_fn[MAX_PATH];
1165       if (!GetModuleFileNameA (hmodule, cp_fn, MAX_PATH))
1166         {
1167           G_UNLOCK (module_dirs);
1168           return NULL;
1169         }
1170       fn = g_locale_to_utf8 (cp_fn, -1, NULL, NULL, NULL);
1171     }
1172
1173   if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
1174     *p = '\0';
1175
1176   p = strrchr (fn, G_DIR_SEPARATOR);
1177   if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
1178             g_ascii_strcasecmp (p + 1, "lib") == 0))
1179     *p = '\0';
1180
1181 #ifdef G_WITH_CYGWIN
1182   /* In Cygwin we need to have POSIX paths */
1183   {
1184     gchar tmp[MAX_PATH];
1185
1186     cygwin_conv_to_posix_path(fn, tmp);
1187     g_free(fn);
1188     fn = g_strdup(tmp);
1189   }
1190 #endif
1191
1192   g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
1193
1194   G_UNLOCK (module_dirs);
1195
1196   return g_strdup (fn);
1197 }
1198
1199 /**
1200  * g_win32_get_package_installation_directory:
1201  * @package: An identifier for a software package, or %NULL, in UTF-8
1202  * @dll_name: The name of a DLL that a package provides, or %NULL, in UTF-8
1203  *
1204  * Try to determine the installation directory for a software package.
1205  * Typically used by GNU software packages.
1206  *
1207  * @package should be a short identifier for the package. Typically it
1208  * is the same identifier as used for
1209  * <literal>GETTEXT_PACKAGE</literal> in software configured according
1210  * to GNU standards. The function first looks in the Windows Registry
1211  * for the value <literal>&num;InstallationDirectory</literal> in the key
1212  * <literal>&num;HKLM\Software\@package</literal>, and if that value
1213  * exists and is a string, returns that.
1214  *
1215  * If @package is %NULL, or the above value isn't found in the
1216  * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
1217  * into the current process. Typically that would be the name of the
1218  * DLL calling this function, looking for its installation
1219  * directory. The function then asks Windows what directory that DLL
1220  * was loaded from. If that directory's last component is "bin" or
1221  * "lib", the parent directory is returned, otherwise the directory
1222  * itself. If that DLL isn't loaded, the function proceeds as if
1223  * @dll_name was %NULL.
1224  *
1225  * If both @package and @dll_name are %NULL, the directory from where
1226  * the main executable of the process was loaded is used instead in
1227  * the same way as above.
1228  *
1229  * Returns: a string containing the installation directory for
1230  * @package. The string is in the GLib file name encoding, i.e. UTF-8
1231  * on Windows. The return value should be freed with g_free() when not
1232  * needed any longer.
1233  **/
1234
1235 gchar *
1236 g_win32_get_package_installation_directory (gchar *package,
1237                                             gchar *dll_name)
1238 {
1239   static GHashTable *package_dirs = NULL;
1240   G_LOCK_DEFINE_STATIC (package_dirs);
1241   gchar *result = NULL;
1242   gchar *key;
1243   HKEY reg_key = NULL;
1244   DWORD type;
1245   DWORD nbytes;
1246
1247   if (package != NULL)
1248     {
1249       G_LOCK (package_dirs);
1250       
1251       if (package_dirs == NULL)
1252         package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
1253       
1254       result = g_hash_table_lookup (package_dirs, package);
1255       
1256       if (result && result[0])
1257         {
1258           G_UNLOCK (package_dirs);
1259           return g_strdup (result);
1260         }
1261       
1262       key = g_strconcat ("Software\\", package, NULL);
1263       
1264       nbytes = 0;
1265       if (G_WIN32_HAVE_WIDECHAR_API ())
1266         {
1267           wchar_t *wc_key = g_utf8_to_utf16 (key, -1, NULL, NULL, NULL);
1268           if (((RegOpenKeyExW (HKEY_CURRENT_USER, wc_key, 0,
1269                                KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
1270                 && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
1271                                      &type, NULL, &nbytes) == ERROR_SUCCESS)
1272                ||
1273                (RegOpenKeyExW (HKEY_LOCAL_MACHINE, wc_key, 0,
1274                                KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
1275                 && RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
1276                                      &type, NULL, &nbytes) == ERROR_SUCCESS))
1277               && type == REG_SZ)
1278             {
1279               wchar_t *wc_temp = g_new (wchar_t, (nbytes+1)/2 + 1);
1280               RegQueryValueExW (reg_key, L"InstallationDirectory", 0,
1281                                 &type, (LPBYTE) wc_temp, &nbytes);
1282               wc_temp[nbytes/2] = '\0';
1283               result = g_utf16_to_utf8 (wc_temp, -1, NULL, NULL, NULL);
1284               g_free (wc_temp);
1285             }
1286           g_free (wc_key);
1287         }
1288       else
1289         {
1290           char *cp_key = g_locale_from_utf8 (key, -1, NULL, NULL, NULL);
1291           if (((RegOpenKeyExA (HKEY_CURRENT_USER, cp_key, 0,
1292                                KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
1293                 && RegQueryValueExA (reg_key, "InstallationDirectory", 0,
1294                                      &type, NULL, &nbytes) == ERROR_SUCCESS)
1295                ||
1296                (RegOpenKeyExA (HKEY_LOCAL_MACHINE, cp_key, 0,
1297                                KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
1298                 && RegQueryValueExA (reg_key, "InstallationDirectory", 0,
1299                                      &type, NULL, &nbytes) == ERROR_SUCCESS))
1300               && type == REG_SZ)
1301             {
1302               char *cp_temp = g_malloc (nbytes + 1);
1303               RegQueryValueExA (reg_key, "InstallationDirectory", 0,
1304                                 &type, cp_temp, &nbytes);
1305               cp_temp[nbytes] = '\0';
1306               result = g_locale_to_utf8 (cp_temp, -1, NULL, NULL, NULL);
1307               g_free (cp_temp);
1308             }
1309           g_free (cp_key);
1310         }
1311
1312       if (reg_key != NULL)
1313         RegCloseKey (reg_key);
1314       
1315       g_free (key);
1316
1317       if (result)
1318         {
1319           g_hash_table_insert (package_dirs, package, result);
1320           G_UNLOCK (package_dirs);
1321           return result;
1322         }
1323       G_UNLOCK (package_dirs);
1324     }
1325
1326   if (dll_name != NULL)
1327     result = get_package_directory_from_module (dll_name);
1328
1329   if (result == NULL)
1330     result = get_package_directory_from_module (NULL);
1331
1332   return result;
1333 }
1334
1335 #undef g_win32_get_package_installation_directory
1336
1337 /* DLL ABI binary compatibility version that uses system codepage file names */
1338
1339 gchar *
1340 g_win32_get_package_installation_directory (gchar *package,
1341                                             gchar *dll_name)
1342 {
1343   gchar *utf8_package = NULL, *utf8_dll_name = NULL;
1344   gchar *utf8_retval, *retval;
1345
1346   if (package != NULL)
1347     utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);
1348
1349   if (dll_name != NULL)
1350     utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);
1351
1352   utf8_retval =
1353     g_win32_get_package_installation_directory_utf8 (utf8_package,
1354                                                      utf8_dll_name);
1355
1356   retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
1357
1358   g_free (utf8_package);
1359   g_free (utf8_dll_name);
1360   g_free (utf8_retval);
1361
1362   return retval;
1363 }
1364
1365 /**
1366  * g_win32_get_package_installation_subdirectory:
1367  * @package: An identifier for a software package, in UTF-8, or %NULL
1368  * @dll_name: The name of a DLL that a package provides, in UTF-8, or %NULL
1369  * @subdir: A subdirectory of the package installation directory, also in UTF-8
1370  *
1371  * Returns a newly-allocated string containing the path of the
1372  * subdirectory @subdir in the return value from calling
1373  * g_win32_get_package_installation_directory() with the @package and
1374  * @dll_name parameters. 
1375  *
1376  * Returns: a string containing the complete path to @subdir inside
1377  * the installation directory of @package. The returned string is in
1378  * the GLib file name encoding, i.e. UTF-8 on Windows. The return
1379  * value should be freed with g_free() when no longer needed.
1380  **/
1381
1382 gchar *
1383 g_win32_get_package_installation_subdirectory (gchar *package,
1384                                                gchar *dll_name,
1385                                                gchar *subdir)
1386 {
1387   gchar *prefix;
1388   gchar *dirname;
1389
1390   prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
1391
1392   dirname = g_build_filename (prefix, subdir, NULL);
1393   g_free (prefix);
1394
1395   return dirname;
1396 }
1397
1398 #undef g_win32_get_package_installation_subdirectory
1399
1400 /* DLL ABI binary compatibility version that uses system codepage file names */
1401
1402 gchar *
1403 g_win32_get_package_installation_subdirectory (gchar *package,
1404                                                gchar *dll_name,
1405                                                gchar *subdir)
1406 {
1407   gchar *prefix;
1408   gchar *dirname;
1409
1410   prefix = g_win32_get_package_installation_directory (package, dll_name);
1411
1412   dirname = g_build_filename (prefix, subdir, NULL);
1413   g_free (prefix);
1414
1415   return dirname;
1416 }
1417
1418 static guint windows_version;
1419
1420 static void 
1421 g_win32_windows_version_init (void)
1422 {
1423   static gboolean beenhere = FALSE;
1424
1425   if (!beenhere)
1426     {
1427       beenhere = TRUE;
1428       if (getenv ("G_WIN32_PRETEND_WIN9X"))
1429         windows_version = 0x80000004;
1430       else
1431         windows_version = GetVersion ();
1432     }
1433 }
1434
1435 void 
1436 _g_win32_thread_init (void)
1437 {
1438   g_win32_windows_version_init ();
1439 }
1440
1441 /**
1442  * g_win32_get_windows_version:
1443  *
1444  * Returns version information for the Windows operating system the
1445  * code is running on. See MSDN documentation for the GetVersion()
1446  * function. To summarize, the most significant bit is one on Win9x,
1447  * and zero on NT-based systems. The least significant byte is 4 on
1448  * Windows NT 4, 5 on Windows XP. Software that needs really detailled
1449  * version and feature information should use Win32 API like
1450  * GetVersionEx() and VerifyVersionInfo().
1451  *
1452  * If there is an environment variable <envar>G_WIN32_PRETEND_WIN9X</envar> 
1453  * defined (with any value), this function always returns a version 
1454  * code for Windows 9x. This is mainly an internal debugging aid for 
1455  * GTK+ and GLib developers, to be able to check the code paths for 
1456  * Windows 9x.
1457  *
1458  * Returns: The version information.
1459  * 
1460  * Since: 2.6
1461  **/
1462 guint
1463 g_win32_get_windows_version (void)
1464 {
1465   g_win32_windows_version_init ();
1466   
1467   return windows_version;
1468 }
1469
1470 /**
1471  * g_win32_locale_filename_from_utf8:
1472  * @utf8filename: a UTF-8 encoded filename.
1473  *
1474  * Converts a filename from UTF-8 to the system codepage.
1475  *
1476  * On NT-based Windows, on NTFS file systems, file names are in
1477  * Unicode. It is quite possible that Unicode file names contain
1478  * characters not representable in the system codepage. (For instance,
1479  * Greek or Cyrillic characters on Western European or US Windows
1480  * installations, or various less common CJK characters on CJK Windows
1481  * installations.)
1482  *
1483  * In such a case, and if the filename refers to an existing file, and
1484  * the file system stores alternate short (8.3) names for directory
1485  * entries, the short form of the filename is returned. Note that the
1486  * "short" name might in fact be longer than the Unicode name. If no
1487  * system codepage name for the file is possible, %NULL is returned.
1488  *
1489  * The return value is dynamically allocated and should be freed when
1490  * no longer used.
1491  *
1492  * Return value: The converted filename, or %NULL on conversion
1493  * failure and lack of short names.
1494  *
1495  * Since: 2.8
1496  */
1497 gchar *
1498 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
1499 {
1500   gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
1501
1502   if (retval == NULL && G_WIN32_HAVE_WIDECHAR_API ())
1503     {
1504       /* Conversion failed, so convert to wide chars, check if there
1505        * is a 8.3 version, and use that.
1506        */
1507       wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
1508       if (wname != NULL)
1509         {
1510           wchar_t wshortname[MAX_PATH + 1];
1511           if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
1512             {
1513               gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
1514               retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
1515               g_free (tem);
1516             }
1517           g_free (wname);
1518         }
1519     }
1520   return retval;
1521 }
1522
1523 #define __G_WIN32_C__
1524 #include "galiasdef.c"