libtool 1.5 installs DLLs in the bin subdirectory, so get them from there.
[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 gchar *
110 g_win32_getlocale (void)
111 {
112   LCID lcid;
113   LANGID langid;
114   gchar *ev;
115   gint primary, sub;
116   gchar *l = NULL, *sl = NULL;
117   gchar bfr[20];
118
119   /* Let the user override the system settings through environment
120      variables, as on POSIX systems.  */
121   if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
122       || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
123       || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
124     return g_strdup (ev);
125
126   /* Use native Win32 API locale ID.  */
127   lcid = GetThreadLocale ();
128
129   /* Strip off the sorting rules, keep only the language part.  */
130   langid = LANGIDFROMLCID (lcid);
131
132   /* Split into language and territory part.  */
133   primary = PRIMARYLANGID (langid);
134   sub = SUBLANGID (langid);
135   switch (primary)
136     {
137     case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
138     case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
139     case LANG_ARABIC:
140       l = "ar";
141       switch (sub)
142         {
143         case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
144         case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
145         case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
146         case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
147         case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
148         case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
149         case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
150         case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
151         case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
152         case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
153         case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
154         case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
155         case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
156         case SUBLANG_ARABIC_UAE: sl = "AE"; break;
157         case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
158         case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
159         }
160       break;
161 #ifdef LANG_ARMENIAN
162     case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
163 #endif
164 #ifdef LANG_ASSAMESE
165     case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
166 #endif
167 #ifdef LANG_AZERI
168     case LANG_AZERI:
169       l = "az";
170 #if defined (SUBLANG_AZERI_LATIN) && defined (SUBLANG_AZERI_CYRILLIC)
171       switch (sub)
172         {
173         /* FIXME: Adjust this when Azerbaijani locales appear on Unix.  */
174         case SUBLANG_AZERI_LATIN: sl = "@latin"; break;
175         case SUBLANG_AZERI_CYRILLIC: sl = "@cyrillic"; break;
176         }
177 #endif
178       break;
179 #endif
180     case LANG_BASQUE:
181       l = "eu"; /* sl could be "ES" or "FR".  */
182       break;
183     case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
184 #ifdef LANG_BENGALI
185     case LANG_BENGALI: l = "bn"; sl = "IN"; break;
186 #endif
187     case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
188     case LANG_CATALAN: l = "ca"; sl = "ES"; break;
189     case LANG_CHINESE:
190       l = "zh";
191       switch (sub)
192         {
193         case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
194         case SUBLANG_CHINESE_SIMPLIFIED: sl = "CN"; break;
195         case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
196         case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
197 #ifdef SUBLANG_CHINESE_MACAU
198         case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
199 #endif
200         }
201       break;
202     case LANG_CROATIAN:         /* LANG_CROATIAN == LANG_SERBIAN */
203       switch (sub)
204         {
205         /* FIXME: How to distinguish Croatian and Latin Serbian locales?  */
206         case SUBLANG_SERBIAN_LATIN: l = "sr"; sl = "@Latn"; break;
207         case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
208         default: l = "hr"; sl = "HR";
209         }
210       break;
211     case LANG_CZECH: l = "cs"; sl = "CZ"; break;
212     case LANG_DANISH: l = "da"; sl = "DK"; break;
213 #ifdef LANG_DIVEHI
214     case LANG_DIVEHI: l = "div"; sl = "MV"; break;
215 #endif
216     case LANG_DUTCH:
217       l = "nl";
218       switch (sub)
219         {
220         case SUBLANG_DUTCH: sl = "NL"; break;
221         case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
222         }
223       break;
224     case LANG_ENGLISH:
225       l = "en";
226       switch (sub)
227         {
228         case SUBLANG_ENGLISH_US: sl = "US"; break;
229         case SUBLANG_ENGLISH_UK: sl = "GB"; break;
230         case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
231         case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
232         case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
233         case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
234         case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
235         case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
236         case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
237         case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
238         case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
239 #ifdef SUBLANG_ENGLISH_ZIMBABWE
240         case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
241 #endif
242 #ifdef SUBLANG_ENGLISH_PHILIPPINES
243         case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
244 #endif
245         }
246       break;
247     case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
248     case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
249     case LANG_FARSI: l = "fa"; sl = "IR"; break;
250     case LANG_FINNISH: l = "fi"; sl = "FI"; break;
251     case LANG_FRENCH:
252       l = "fr";
253       switch (sub)
254         {
255         case SUBLANG_FRENCH: sl = "FR"; break;
256         case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
257         case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
258         case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
259         case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
260 #ifdef SUBLANG_FRENCH_MONACO
261         case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
262 #endif
263         }
264       break;
265       /* FIXME: LANG_GALICIAN: What's the code for Galician? */
266 #ifdef LANG_GEORGIAN
267     case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
268 #endif
269     case LANG_GERMAN:
270       l = "de";
271       switch (sub)
272         {
273         case SUBLANG_GERMAN: sl = "DE"; break;
274         case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
275         case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
276         case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
277         case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
278         }
279       break;
280     case LANG_GREEK: l = "el"; sl = "GR"; break;
281 #ifdef LANG_GUJARATI
282     case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
283 #endif
284     case LANG_HEBREW: l = "he"; sl = "IL"; break;
285 #ifdef LANG_HINDI
286     case LANG_HINDI: l = "hi"; sl = "IN"; break;
287 #endif
288     case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
289     case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
290     case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
291     case LANG_ITALIAN:
292       l = "it";
293       switch (sub)
294         {
295         case SUBLANG_ITALIAN: sl = "IT"; break;
296         case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
297         }
298       break;
299     case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
300 #ifdef LANG_KANNADA
301     case LANG_KANNADA: l = "kn"; sl = "IN"; break;
302 #endif
303 #ifdef LANG_KASHMIRI
304     case LANG_KASHMIRI:
305       l = "ks";
306       switch (sub)
307         {
308         case SUBLANG_DEFAULT: sl = "PK"; break;
309 #ifdef SUBLANG_KASHMIRI_INDIA
310         case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
311 #endif
312         }
313       break;
314 #endif
315 #ifdef LANG_KAZAK
316     case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
317 #endif
318 #ifdef LANG_KONKANI
319     case LANG_KONKANI:
320       /* FIXME: Adjust this when such locales appear on Unix.  */
321       l = "kok"; sl = "IN";
322       break;
323 #endif
324     case LANG_KOREAN: l = "ko"; sl = "KR"; break;
325 #ifdef LANG_KYRGYZ
326     case LANG_KYRGYZ: l = "ky"; sl = "KG"; /* ??? */ break; 
327 #endif
328     case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
329     case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
330 #ifdef LANG_MACEDONIAN
331     case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
332 #endif
333 #ifdef LANG_MALAY
334     case LANG_MALAY:
335       l = "ms";
336       switch (sub)
337         {
338 #ifdef SUBLANG_MALAY_MALAYSIA
339         case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
340 #endif
341 #ifdef SUBLANG_MALAY_BRUNEI_DARUSSALAM
342         case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
343 #endif
344         }
345       break;
346 #endif
347 #ifdef LANG_MALAYALAM
348     case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
349 #endif
350 #ifdef LANG_MANIPURI
351     case LANG_MANIPURI:
352       /* FIXME: Adjust this when such locales appear on Unix.  */
353       l = "mni"; sl = "IN";
354       break;
355 #endif
356 #ifdef LANG_MARATHI
357     case LANG_MARATHI: l = "mr"; sl = "IN"; break;
358 #endif
359 #ifdef LANG_MONGOLIAN
360     case LANG_MONGOLIAN: l = "mn"; sl = "MN"; break;
361 #endif
362 #ifdef LANG_NEPALI
363     case LANG_NEPALI:
364       l = "ne";
365       switch (sub)
366         {
367         case SUBLANG_DEFAULT: sl = "NP"; break;
368 #ifdef SUBLANG_NEPALI_INDIA
369         case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
370 #endif
371         }
372       break;
373 #endif
374     case LANG_NORWEGIAN:
375       l = "no";
376       switch (sub)
377         {
378         case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
379         case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
380         }
381       break;
382 #ifdef LANG_ORIYA
383     case LANG_ORIYA: l = "or"; sl = "IN"; break;
384 #endif
385     case LANG_POLISH: l = "pl"; sl = "PL"; break;
386     case LANG_PORTUGUESE:
387       l = "pt";
388       switch (sub)
389         {
390         case SUBLANG_PORTUGUESE: sl = "PT"; break;
391         case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
392         }
393       break;
394 #ifdef LANG_PUNJABI
395     case LANG_PUNJABI: l = "pa"; sl = "IN"; break;
396 #endif
397     case LANG_ROMANIAN: l = "ro"; sl = "RO"; break;
398     case LANG_RUSSIAN:
399       l = "ru"; /* sl could be "RU" or "UA".  */
400       break;
401 #ifdef LANG_SANSKRIT
402     case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
403 #endif
404 #ifdef LANG_SINDHI
405     case LANG_SINDHI: l = "sd"; break;
406 #endif
407     case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
408     case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
409 #ifdef LANG_SORBIAN
410     case LANG_SORBIAN:
411       /* FIXME: Adjust this when such locales appear on Unix.  */
412       l = "wen"; sl = "DE";
413       break;
414 #endif
415     case LANG_SPANISH:
416       l = "es";
417       switch (sub)
418         {
419         case SUBLANG_SPANISH: sl = "ES"; break;
420         case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
421         case SUBLANG_SPANISH_MODERN:
422           sl = "ES@modern"; break;      /* not seen on Unix */
423         case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
424         case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
425         case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
426         case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
427         case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
428         case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
429         case SUBLANG_SPANISH_PERU: sl = "PE"; break;
430         case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
431         case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
432         case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
433         case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
434         case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
435         case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
436         case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
437         case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
438         case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
439         case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
440         }
441       break;
442 #ifdef LANG_SWAHILI
443     case LANG_SWAHILI: l = "sw"; break;
444 #endif
445     case LANG_SWEDISH:
446       l = "sv";
447       switch (sub)
448         {
449         case SUBLANG_DEFAULT: sl = "SE"; break;
450         case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
451         }
452       break;
453 #ifdef LANG_SYRIAC
454     case LANG_SYRIAC: l = "syr"; break;
455 #endif
456 #ifdef LANG_TAMIL
457     case LANG_TAMIL:
458       l = "ta"; /* sl could be "IN" or "LK".  */
459       break;
460 #endif
461 #ifdef LANG_TATAR
462     case LANG_TATAR: l = "tt"; break;
463 #endif
464 #ifdef LANG_TELUGU
465     case LANG_TELUGU: l = "te"; sl = "IN"; break;
466 #endif
467     case LANG_THAI: l = "th"; sl = "TH"; break;
468     case LANG_TURKISH: l = "tr"; sl = "TR"; break;
469     case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
470 #ifdef LANG_URDU
471     case LANG_URDU:
472       l = "ur";
473       switch (sub)
474         {
475 #ifdef SUBLANG_URDU_PAKISTAN
476         case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
477 #endif
478 #ifdef SUBLANG_URDU_INDIA
479         case SUBLANG_URDU_INDIA: sl = "IN"; break;
480 #endif
481         }
482       break;
483 #endif
484 #ifdef LANG_UZBEK
485     case LANG_UZBEK:
486       l = "uz";
487       switch (sub)
488         {
489         /* FIXME: Adjust this when Uzbek locales appear on Unix.  */
490 #ifdef SUBLANG_UZBEK_LATIN
491         case SUBLANG_UZBEK_LATIN: sl = "UZ@latin"; break;
492 #endif
493 #ifdef SUBLANG_UZBEK_CYRILLIC
494         case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
495 #endif
496         }
497       break;
498 #endif
499     case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
500     default: l = "xx"; break;
501     }
502   strcpy (bfr, l);
503   if (sl != NULL)
504     {
505       if (sl[0] != '@')
506         strcat (bfr, "_");
507       strcat (bfr, sl);
508     }
509
510   return g_strdup (bfr);
511 }
512
513 /**
514  * g_win32_error_message:
515  * @error: error code.
516  *
517  * Translate a Win32 error code (as returned by GetLastError()) into
518  * the corresponding message. The message is either language neutral,
519  * or in the thread's language, or the user's language, the system's
520  * language, or US English (see docs for <function>FormatMessage()</function>). *
521  * The returned string should be deallocated with g_free().
522  *
523  * Returns: newly-allocated error message
524  **/
525 gchar *
526 g_win32_error_message (gint error)
527 {
528   gchar *msg;
529   gchar *retval;
530   int nbytes;
531
532   FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
533                  |FORMAT_MESSAGE_IGNORE_INSERTS
534                  |FORMAT_MESSAGE_FROM_SYSTEM,
535                  NULL, error, 0,
536                  (LPTSTR) &msg, 0, NULL);
537   nbytes = strlen (msg);
538
539   if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
540     msg[nbytes-2] = '\0';
541   
542   retval = g_strdup (msg);
543
544   if (msg != NULL)
545     LocalFree (msg);
546
547   return retval;
548 }
549
550 static gchar *
551 get_package_directory_from_module (gchar *module_name)
552 {
553   static GHashTable *module_dirs = NULL;
554   G_LOCK_DEFINE_STATIC (module_dirs);
555   HMODULE hmodule = NULL;
556   gchar *fn;
557   gchar *p;
558   gchar *result;
559
560   G_LOCK (module_dirs);
561
562   if (module_dirs == NULL)
563     module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
564   
565   result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
566       
567   if (result)
568     {
569       G_UNLOCK (module_dirs);
570       return g_strdup (result);
571     }
572
573   if (module_name)
574     {
575       hmodule = GetModuleHandle (module_name);
576       if (!hmodule)
577         return NULL;
578     }
579
580   fn = g_malloc (MAX_PATH);
581   if (!GetModuleFileName (hmodule, fn, MAX_PATH))
582     {
583       G_UNLOCK (module_dirs);
584       g_free (fn);
585       return NULL;
586     }
587
588   if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
589     *p = '\0';
590
591   p = strrchr (fn, G_DIR_SEPARATOR);
592   if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
593             g_ascii_strcasecmp (p + 1, "lib") == 0))
594     *p = '\0';
595
596 #ifdef G_WITH_CYGWIN
597   /* In Cygwin we need to have POSIX paths */
598   {
599     gchar tmp[MAX_PATH];
600
601     cygwin_conv_to_posix_path(fn, tmp);
602     g_free(fn);
603     fn = g_strdup(tmp);
604   }
605 #endif
606
607   g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
608
609   G_UNLOCK (module_dirs);
610
611   return g_strdup (fn);
612 }
613
614 /**
615  * g_win32_get_package_installation_directory:
616  * @package: An identifier for a software package, or %NULL
617  * @dll_name: The name of a DLL that a package provides, or %NULL.
618  *
619  * Try to determine the installation directory for a software package.
620  * Typically used by GNU software packages.
621  *
622  * @package should be a short identifier for the package. Typically it
623  * is the same identifier as used for
624  * <literal>GETTEXT_PACKAGE</literal> in software configured according
625  * to GNU standards. The function first looks in the Windows Registry
626  * for the value <literal>&num;InstallationDirectory</literal> in the key
627  * <literal>&num;HKLM\Software\@package</literal>, and if that value
628  * exists and is a string, returns that.
629  *
630  * If @package is %NULL, or the above value isn't found in the
631  * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
632  * into the current process. Typically that would be the name of the
633  * DLL calling this function, looking for its installation
634  * directory. The function then asks Windows what directory that DLL
635  * was loaded from. If that directory's last component is "bin" or
636  * "lib", the parent directory is returned, otherwise the directory
637  * itself. If that DLL isn't loaded, the function proceeds as if
638  * @dll_name was %NULL.
639  *
640  * If both @package and @dll_name are %NULL, the directory from where
641  * the main executable of the process was loaded is uses instead in
642  * the same way as above.
643  *
644  * Returns: a string containing the installation directory for @package.
645  * The return value should be freed with g_free() when not needed any longer.
646  **/
647
648 gchar *
649 g_win32_get_package_installation_directory (gchar *package,
650                                             gchar *dll_name)
651 {
652   static GHashTable *package_dirs = NULL;
653   G_LOCK_DEFINE_STATIC (package_dirs);
654   gchar *result = NULL;
655   gchar *key;
656   HKEY reg_key = NULL;
657   DWORD type;
658   DWORD nbytes;
659
660   if (package != NULL)
661     {
662       G_LOCK (package_dirs);
663       
664       if (package_dirs == NULL)
665         package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
666       
667       result = g_hash_table_lookup (package_dirs, package);
668       
669       if (result && result[0])
670         {
671           G_UNLOCK (package_dirs);
672           return g_strdup (result);
673         }
674       
675       key = g_strconcat ("Software\\", package, NULL);
676       
677       nbytes = 0;
678       if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
679                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
680            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
681                                &type, NULL, &nbytes) == ERROR_SUCCESS)
682           ||
683           ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
684                          KEY_QUERY_VALUE, &reg_key) == ERROR_SUCCESS
685            && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
686                                &type, NULL, &nbytes) == ERROR_SUCCESS)
687            && type == REG_SZ))
688         {
689           result = g_malloc (nbytes + 1);
690           RegQueryValueEx (reg_key, "InstallationDirectory", 0,
691                            &type, result, &nbytes);
692           result[nbytes] = '\0';
693         }
694
695       if (reg_key != NULL)
696         RegCloseKey (reg_key);
697       
698       g_free (key);
699
700       if (result)
701         {
702           g_hash_table_insert (package_dirs, package, result);
703           G_UNLOCK (package_dirs);
704           return g_strdup (result);
705         }
706       G_UNLOCK (package_dirs);
707     }
708
709   if (dll_name != NULL)
710     result = get_package_directory_from_module (dll_name);
711
712   if (result == NULL)
713     result = get_package_directory_from_module (NULL);
714
715   return result;
716 }
717
718 /**
719  * g_win32_get_package_installation_subdirectory:
720  * @package: An identifier for a software package, or %NULL.
721  * @dll_name: The name of a DLL that a package provides, or %NULL.
722  * @subdir: A subdirectory of the package installation directory.
723  *
724  * Returns a newly-allocated string containing the path of the
725  * subdirectory @subdir in the return value from calling
726  * g_win32_get_package_installation_directory() with the @package and
727  * @dll_name parameters. 
728  *
729  * Returns: a string containing the complete path to @subdir inside the 
730  * installation directory of @package. The return value should be freed with
731  * g_free() when no longer needed.
732  **/
733
734 gchar *
735 g_win32_get_package_installation_subdirectory (gchar *package,
736                                                gchar *dll_name,
737                                                gchar *subdir)
738 {
739   gchar *prefix;
740
741   prefix = g_win32_get_package_installation_directory (package, dll_name);
742
743   return g_build_filename (prefix, subdir, NULL);
744 }