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