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