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
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.
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.
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.
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/.
29 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
34 #include "glibconfig.h"
41 #define STRICT /* Strict typing, please */
56 #include <sys/cygwin.h>
62 g_win32_ftruncate (gint fd,
68 g_return_val_if_fail (fd >= 0, -1);
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))
76 gint error = GetLastError ();
80 case ERROR_INVALID_HANDLE:
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().
106 * Returns: newly-allocated locale name.
110 g_win32_getlocale (void)
116 gchar *l = NULL, *sl = NULL;
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);
126 /* Use native Win32 API locale ID. */
127 lcid = GetThreadLocale ();
129 /* Strip off the sorting rules, keep only the language part. */
130 langid = LANGIDFROMLCID (lcid);
132 /* Split into language and territory part. */
133 primary = PRIMARYLANGID (langid);
134 sub = SUBLANGID (langid);
137 case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
138 case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
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;
162 case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
165 case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
170 #if defined (SUBLANG_AZERI_LATIN) && defined (SUBLANG_AZERI_CYRILLIC)
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;
181 l = "eu"; /* sl could be "ES" or "FR". */
183 case LANG_BELARUSIAN: l = "be"; sl = "BY"; break;
185 case LANG_BENGALI: l = "bn"; sl = "IN"; break;
187 case LANG_BULGARIAN: l = "bg"; sl = "BG"; break;
188 case LANG_CATALAN: l = "ca"; sl = "ES"; break;
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;
202 case LANG_CROATIAN: /* LANG_CROATIAN == LANG_SERBIAN
203 * What used to be called Serbo-Croatian
204 * should really now be two separate
205 * languages because of political reasons.
206 * (Says tml, who knows nothing about Serbian
208 * (I can feel those flames coming already.)
212 /* FIXME: How to distinguish Croatian and Latin Serbian locales? */
213 case SUBLANG_SERBIAN_LATIN: l = "sr"; break;
214 case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; sl = "@cyrillic"; break;
215 default: l = "hr"; sl = "HR";
218 case LANG_CZECH: l = "cs"; sl = "CZ"; break;
219 case LANG_DANISH: l = "da"; sl = "DK"; break;
221 case LANG_DIVEHI: l = "div"; sl = "MV"; break;
227 case SUBLANG_DUTCH: sl = "NL"; break;
228 case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
235 case SUBLANG_ENGLISH_US: sl = "US"; break;
236 case SUBLANG_ENGLISH_UK: sl = "GB"; break;
237 case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
238 case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
239 case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
240 case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
241 case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
242 case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
243 case SUBLANG_ENGLISH_CARIBBEAN: sl = "GD"; break; /* Grenada? */
244 case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
245 case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
246 #ifdef SUBLANG_ENGLISH_ZIMBABWE
247 case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
249 #ifdef SUBLANG_ENGLISH_PHILIPPINES
250 case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
254 case LANG_ESTONIAN: l = "et"; sl = "EE"; break;
255 case LANG_FAEROESE: l = "fo"; sl = "FO"; break;
256 case LANG_FARSI: l = "fa"; sl = "IR"; break;
257 case LANG_FINNISH: l = "fi"; sl = "FI"; break;
262 case SUBLANG_FRENCH: sl = "FR"; break;
263 case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
264 case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
265 case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
266 case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
267 #ifdef SUBLANG_FRENCH_MONACO
268 case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
272 /* FIXME: LANG_GALICIAN: What's the code for Galician? */
274 case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
280 case SUBLANG_GERMAN: sl = "DE"; break;
281 case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
282 case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
283 case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
284 case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
287 case LANG_GREEK: l = "el"; sl = "GR"; break;
289 case LANG_GUJARATI: l = "gu"; sl = "IN"; break;
291 case LANG_HEBREW: l = "he"; sl = "IL"; break;
293 case LANG_HINDI: l = "hi"; sl = "IN"; break;
295 case LANG_HUNGARIAN: l = "hu"; sl = "HU"; break;
296 case LANG_ICELANDIC: l = "is"; sl = "IS"; break;
297 case LANG_INDONESIAN: l = "id"; sl = "ID"; break;
302 case SUBLANG_ITALIAN: sl = "IT"; break;
303 case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
306 case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
308 case LANG_KANNADA: l = "kn"; sl = "IN"; break;
315 case SUBLANG_DEFAULT: sl = "PK"; break;
316 #ifdef SUBLANG_KASHMIRI_INDIA
317 case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
323 case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
327 /* FIXME: Adjust this when such locales appear on Unix. */
328 l = "kok"; sl = "IN";
331 case LANG_KOREAN: l = "ko"; sl = "KR"; break;
333 case LANG_KYRGYZ: l = "ky"; sl = "KG"; /* ??? */ break;
335 case LANG_LATVIAN: l = "lv"; sl = "LV"; break;
336 case LANG_LITHUANIAN: l = "lt"; sl = "LT"; break;
337 #ifdef LANG_MACEDONIAN
338 case LANG_MACEDONIAN: l = "mk"; sl = "MK"; break;
345 #ifdef SUBLANG_MALAY_MALAYSIA
346 case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
348 #ifdef SUBLANG_MALAY_BRUNEI_DARUSSALAM
349 case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
354 #ifdef LANG_MALAYALAM
355 case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
359 /* FIXME: Adjust this when such locales appear on Unix. */
360 l = "mni"; sl = "IN";
364 case LANG_MARATHI: l = "mr"; sl = "IN"; break;
366 #ifdef LANG_MONGOLIAN
367 case LANG_MONGOLIAN: l = "mn"; sl = "MN"; break;
374 case SUBLANG_DEFAULT: sl = "NP"; break;
375 #ifdef SUBLANG_NEPALI_INDIA
376 case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
385 case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
386 case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
390 case LANG_ORIYA: l = "or"; sl = "IN"; break;
392 case LANG_POLISH: l = "pl"; sl = "PL"; break;
393 case LANG_PORTUGUESE:
397 case SUBLANG_PORTUGUESE: sl = "PT"; break;
398 case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
402 case LANG_PUNJABI: l = "pa"; sl = "IN"; break;
404 case LANG_ROMANIAN: l = "ro"; sl = "RO"; break;
406 l = "ru"; /* sl could be "RU" or "UA". */
409 case LANG_SANSKRIT: l = "sa"; sl = "IN"; break;
412 case LANG_SINDHI: l = "sd"; break;
414 case LANG_SLOVAK: l = "sk"; sl = "SK"; break;
415 case LANG_SLOVENIAN: l = "sl"; sl = "SI"; break;
418 /* FIXME: Adjust this when such locales appear on Unix. */
419 l = "wen"; sl = "DE";
426 case SUBLANG_SPANISH: sl = "ES"; break;
427 case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
428 case SUBLANG_SPANISH_MODERN:
429 sl = "ES@modern"; break; /* not seen on Unix */
430 case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
431 case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
432 case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
433 case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
434 case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
435 case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
436 case SUBLANG_SPANISH_PERU: sl = "PE"; break;
437 case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
438 case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
439 case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
440 case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
441 case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
442 case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
443 case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
444 case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
445 case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
446 case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
450 case LANG_SWAHILI: l = "sw"; break;
456 case SUBLANG_DEFAULT: sl = "SE"; break;
457 case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
461 case LANG_SYRIAC: l = "syr"; break;
465 l = "ta"; /* sl could be "IN" or "LK". */
469 case LANG_TATAR: l = "tt"; break;
472 case LANG_TELUGU: l = "te"; sl = "IN"; break;
474 case LANG_THAI: l = "th"; sl = "TH"; break;
475 case LANG_TURKISH: l = "tr"; sl = "TR"; break;
476 case LANG_UKRAINIAN: l = "uk"; sl = "UA"; break;
482 #ifdef SUBLANG_URDU_PAKISTAN
483 case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
485 #ifdef SUBLANG_URDU_INDIA
486 case SUBLANG_URDU_INDIA: sl = "IN"; break;
496 /* FIXME: Adjust this when Uzbek locales appear on Unix. */
497 #ifdef SUBLANG_UZBEK_LATIN
498 case SUBLANG_UZBEK_LATIN: sl = "UZ@latin"; break;
500 #ifdef SUBLANG_UZBEK_CYRILLIC
501 case SUBLANG_UZBEK_CYRILLIC: sl = "UZ@cyrillic"; break;
506 case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
507 default: l = "xx"; break;
517 return g_strdup (bfr);
521 * g_win32_error_message:
522 * @error: error code.
524 * Translate a Win32 error code (as returned by GetLastError()) into
525 * the corresponding message. The message is either language neutral,
526 * or in the thread's language, or the user's language, the system's
527 * language, or US English (see docs for <function>FormatMessage()</function>). *
528 * The returned string should be deallocated with g_free().
530 * Returns: newly-allocated error message
533 g_win32_error_message (gint error)
539 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
540 |FORMAT_MESSAGE_IGNORE_INSERTS
541 |FORMAT_MESSAGE_FROM_SYSTEM,
543 (LPTSTR) &msg, 0, NULL);
544 nbytes = strlen (msg);
546 if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
547 msg[nbytes-2] = '\0';
549 retval = g_strdup (msg);
558 get_package_directory_from_module (gchar *module_name)
560 static GHashTable *module_dirs = NULL;
561 G_LOCK_DEFINE_STATIC (module_dirs);
562 HMODULE hmodule = NULL;
567 G_LOCK (module_dirs);
569 if (module_dirs == NULL)
570 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
572 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
576 G_UNLOCK (module_dirs);
577 return g_strdup (result);
582 hmodule = GetModuleHandle (module_name);
587 fn = g_malloc (MAX_PATH);
588 if (!GetModuleFileName (hmodule, fn, MAX_PATH))
590 G_UNLOCK (module_dirs);
595 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
598 p = strrchr (fn, G_DIR_SEPARATOR);
599 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
600 g_ascii_strcasecmp (p + 1, "lib") == 0))
604 /* In Cygwin we need to have POSIX paths */
608 cygwin_conv_to_posix_path(fn, tmp);
614 g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
616 G_UNLOCK (module_dirs);
618 return g_strdup (fn);
622 * g_win32_get_package_installation_directory:
623 * @package: An identifier for a software package, or %NULL
624 * @dll_name: The name of a DLL that a package provides, or %NULL.
626 * Try to determine the installation directory for a software package.
627 * Typically used by GNU software packages.
629 * @package should be a short identifier for the package. Typically it
630 * is the same identifier as used for
631 * <literal>GETTEXT_PACKAGE</literal> in software configured according
632 * to GNU standards. The function first looks in the Windows Registry
633 * for the value <literal>#InstallationDirectory</literal> in the key
634 * <literal>#HKLM\Software\@package</literal>, and if that value
635 * exists and is a string, returns that.
637 * If @package is %NULL, or the above value isn't found in the
638 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
639 * into the current process. Typically that would be the name of the
640 * DLL calling this function, looking for its installation
641 * directory. The function then asks Windows what directory that DLL
642 * was loaded from. If that directory's last component is "bin" or
643 * "lib", the parent directory is returned, otherwise the directory
644 * itself. If that DLL isn't loaded, the function proceeds as if
645 * @dll_name was %NULL.
647 * If both @package and @dll_name are %NULL, the directory from where
648 * the main executable of the process was loaded is uses instead in
649 * the same way as above.
651 * Returns: a string containing the installation directory for @package.
652 * The return value should be freed with g_free() when not needed any longer.
656 g_win32_get_package_installation_directory (gchar *package,
659 static GHashTable *package_dirs = NULL;
660 G_LOCK_DEFINE_STATIC (package_dirs);
661 gchar *result = NULL;
669 G_LOCK (package_dirs);
671 if (package_dirs == NULL)
672 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
674 result = g_hash_table_lookup (package_dirs, package);
676 if (result && result[0])
678 G_UNLOCK (package_dirs);
679 return g_strdup (result);
682 key = g_strconcat ("Software\\", package, NULL);
685 if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
686 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
687 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
688 &type, NULL, &nbytes) == ERROR_SUCCESS)
690 ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
691 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
692 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
693 &type, NULL, &nbytes) == ERROR_SUCCESS)
696 result = g_malloc (nbytes + 1);
697 RegQueryValueEx (reg_key, "InstallationDirectory", 0,
698 &type, result, &nbytes);
699 result[nbytes] = '\0';
703 RegCloseKey (reg_key);
709 g_hash_table_insert (package_dirs, package, result);
710 G_UNLOCK (package_dirs);
711 return g_strdup (result);
713 G_UNLOCK (package_dirs);
716 if (dll_name != NULL)
717 result = get_package_directory_from_module (dll_name);
720 result = get_package_directory_from_module (NULL);
726 * g_win32_get_package_installation_subdirectory:
727 * @package: An identifier for a software package, or %NULL.
728 * @dll_name: The name of a DLL that a package provides, or %NULL.
729 * @subdir: A subdirectory of the package installation directory.
731 * Returns a newly-allocated string containing the path of the
732 * subdirectory @subdir in the return value from calling
733 * g_win32_get_package_installation_directory() with the @package and
734 * @dll_name parameters.
736 * Returns: a string containing the complete path to @subdir inside the
737 * installation directory of @package. The return value should be freed with
738 * g_free() when no longer needed.
742 g_win32_get_package_installation_subdirectory (gchar *package,
748 prefix = g_win32_get_package_installation_directory (package, dll_name);
750 return g_build_filename (prefix, subdir, NULL);