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