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