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.
36 #include "glibconfig.h"
43 #define STRICT /* Strict typing, please */
58 #include <sys/cygwin.h>
64 g_win32_ftruncate (gint fd,
70 g_return_val_if_fail (fd >= 0, -1);
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))
78 gint error = GetLastError ();
82 case ERROR_INVALID_HANDLE:
98 /* MSVC 5.0 headers don't have latest language and sublanguage codes */
100 #define LANG_ARMENIAN 0x2b
102 #ifndef LANG_ASSAMESE
103 #define LANG_ASSAMESE 0x4d
106 #define LANG_AZERI 0x2c
109 #define LANG_BENGALI 0x45
111 #ifndef LANG_GEORGIAN
112 #define LANG_GEORGIAN 0x37
114 #ifndef LANG_GUJARATI
115 #define LANG_GUJARATI 0x47
118 #define LANG_HINDI 0x39
121 #define LANG_KANNADA 0x4b
123 #ifndef LANG_KASHMIRI
124 #define LANG_KASHMIRI 0x60
127 #define LANG_KAZAK 0x3f
130 #define LANG_KONKANI 0x57
132 #ifndef LANG_MACEDONIAN
133 #define LANG_MACEDONIAN 0x2f
136 #define LANG_MALAY 0x3e
138 #ifndef LANG_MALAYALAM
139 #define LANG_MALAYALAM 0x4c
141 #ifndef LANG_MANIPURI
142 #define LANG_MANIPURI 0x58
145 #define LANG_MARATHI 0x4e
148 #define LANG_NEPALI 0x61
151 #define LANG_ORIYA 0x48
154 #define LANG_PUNJABI 0x46
156 #ifndef LANG_SANSKRIT
157 #define LANG_SANSKRIT 0x4f
160 #define LANG_SINDHI 0x59
163 #define LANG_SWAHILI 0x41
166 #define LANG_TAMIL 0x49
169 #define LANG_TATAR 0x44
172 #define LANG_TELUGU 0x4a
175 #define LANG_URDU 0x20
178 #define LANG_UZBEK 0x43
181 #ifndef SUBLANG_CHINESE_MACAU
182 #define SUBLANG_CHINESE_MACAU 0x05
184 #ifndef SUBLANG_ENGLISH_ZIMBABWE
185 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
187 #ifndef SUBLANG_ENGLISH_PHILIPPINES
188 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
190 #ifndef SUBLANG_FRENCH_MONACO
191 #define SUBLANG_FRENCH_MONACO 0x06
193 #ifndef SUBLANG_KASHMIRI_INDIA
194 #define SUBLANG_KASHMIRI_INDIA 0x02
196 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
197 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
199 #ifndef SUBLANG_NEPALI_INDIA
200 #define SUBLANG_NEPALI_INDIA 0x02
202 #ifndef SUBLANG_URDU_PAKISTAN
203 #define SUBLANG_URDU_PAKISTAN 0x01
205 #ifndef SUBLANG_URDU_INDIA
206 #define SUBLANG_URDU_INDIA 0x02
208 #ifndef SUBLANG_UZBEK_CYRILLIC
209 #define SUBLANG_UZBEK_CYRILLIC 0x02
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().
222 * Returns: newly-allocated locale name.
226 g_win32_getlocale (void)
232 gchar *l = NULL, *sl = NULL;
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);
242 /* Use native Win32 API locale ID. */
243 lcid = GetThreadLocale ();
245 /* Strip off the sorting rules, keep only the language part. */
246 langid = LANGIDFROMLCID (lcid);
248 /* Split into language and territory part. */
249 primary = PRIMARYLANGID (langid);
250 sub = SUBLANGID (langid);
253 case LANG_AFRIKAANS: l = "af"; sl = "ZA"; break;
254 case LANG_ALBANIAN: l = "sq"; sl = "AL"; break;
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;
277 case LANG_ARMENIAN: l = "hy"; sl = "AM"; break;
278 case LANG_ASSAMESE: l = "as"; sl = "IN"; break;
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;
289 l = "eu"; /* sl could be "ES" or "FR". */
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;
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;
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
312 * (I can feel those flames coming already.)
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";
322 case LANG_CZECH: l = "cs"; sl = "CZ"; break;
323 case LANG_DANISH: l = "da"; sl = "DK"; break;
328 case SUBLANG_DUTCH: sl = "NL"; break;
329 case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
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;
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;
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;
367 case LANG_GEORGIAN: l = "ka"; sl = "GE"; break;
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;
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;
390 case SUBLANG_ITALIAN: sl = "IT"; break;
391 case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
394 case LANG_JAPANESE: l = "ja"; sl = "JP"; break;
395 case LANG_KANNADA: l = "kn"; sl = "IN"; break;
400 case SUBLANG_DEFAULT: sl = "PK"; break;
401 case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
404 case LANG_KAZAK: l = "kk"; sl = "KZ"; break;
406 /* FIXME: Adjust this when such locales appear on Unix. */
407 l = "kok"; sl = "IN";
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;
417 case SUBLANG_MALAY_MALAYSIA: sl = "MY"; break;
418 case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
421 case LANG_MALAYALAM: l = "ml"; sl = "IN"; break;
423 /* FIXME: Adjust this when such locales appear on Unix. */
424 l = "mni"; sl = "IN";
426 case LANG_MARATHI: l = "mr"; sl = "IN"; break;
431 case SUBLANG_DEFAULT: sl = "NP"; break;
432 case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
439 case SUBLANG_NORWEGIAN_BOKMAL: sl = "NO"; break;
440 case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; sl = "NO"; break;
443 case LANG_ORIYA: l = "or"; sl = "IN"; break;
444 case LANG_POLISH: l = "pl"; sl = "PL"; break;
445 case LANG_PORTUGUESE:
449 case SUBLANG_PORTUGUESE: sl = "PT"; break;
450 case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
453 case LANG_PUNJABI: l = "pa"; sl = "IN"; break;
454 case LANG_ROMANIAN: l = "ro"; sl = "RO"; break;
456 l = "ru"; /* sl could be "RU" or "UA". */
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;
464 /* FIXME: Adjust this when such locales appear on Unix. */
465 l = "wen"; sl = "DE";
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;
495 case LANG_SWAHILI: l = "sw"; break;
500 case SUBLANG_DEFAULT: sl = "SE"; break;
501 case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
505 l = "ta"; /* sl could be "IN" or "LK". */
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;
516 case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
517 case SUBLANG_URDU_INDIA: sl = "IN"; break;
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;
529 case LANG_VIETNAMESE: l = "vi"; sl = "VN"; break;
530 default: l = "xx"; break;
540 return g_strdup (bfr);
544 * g_win32_error_message:
545 * @error: error code.
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().
553 * Returns: newly-allocated error message
556 g_win32_error_message (gint error)
562 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
563 |FORMAT_MESSAGE_IGNORE_INSERTS
564 |FORMAT_MESSAGE_FROM_SYSTEM,
566 (LPTSTR) &msg, 0, NULL);
567 nbytes = strlen (msg);
569 if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
570 msg[nbytes-2] = '\0';
572 retval = g_strdup (msg);
581 get_package_directory_from_module (gchar *module_name)
583 static GHashTable *module_dirs = NULL;
584 G_LOCK_DEFINE_STATIC (module_dirs);
585 HMODULE hmodule = NULL;
590 G_LOCK (module_dirs);
592 if (module_dirs == NULL)
593 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
595 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
599 G_UNLOCK (module_dirs);
600 return g_strdup (result);
605 hmodule = GetModuleHandle (module_name);
610 fn = g_malloc (MAX_PATH);
611 if (!GetModuleFileName (hmodule, fn, MAX_PATH))
613 G_UNLOCK (module_dirs);
618 /* In Cygwin we need to have POSIX paths */
622 cygwin_conv_to_posix_path(fn, tmp);
628 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
631 p = strrchr (fn, G_DIR_SEPARATOR);
632 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
633 g_ascii_strcasecmp (p + 1, "lib") == 0))
636 g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
638 G_UNLOCK (module_dirs);
640 return g_strdup (fn);
644 * g_win32_get_package_installation_directory:
645 * @package: An identifier for a software package, or %NULL
646 * @dll_name: The name of a DLL that a package provides, or %NULL.
648 * Try to determine the installation directory for a software package.
649 * Typically used by GNU software packages.
651 * @package should be a short identifier for the package. Typically it
652 * is the same identifier as used for
653 * <literal>GETTEXT_PACKAGE</literal> in software configured according
654 * to GNU standards. The function first looks in the Windows Registry
655 * for the value <literal>#InstallationDirectory</literal> in the key
656 * <literal>#HKLM\Software\@package</literal>, and if that value
657 * exists and is a string, returns that.
659 * If @package is %NULL, or the above value isn't found in the
660 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
661 * into the current process. Typically that would be the name of the
662 * DLL calling this function, looking for its installation
663 * directory. The function then asks Windows what directory that DLL
664 * was loaded from. If that directory's last component is "bin" or
665 * "lib", the parent directory is returned, otherwise the directory
666 * itself. If that DLL isn't loaded, the function proceeds as if
667 * @dll_name was %NULL.
669 * If both @package and @dll_name are %NULL, the directory from where
670 * the main executable of the process was loaded is uses instead in
671 * the same way as above.
673 * Returns: a string containing the installation directory for @package.
674 * The return value should be freed with g_free() when not needed any longer.
678 g_win32_get_package_installation_directory (gchar *package,
681 static GHashTable *package_dirs = NULL;
682 G_LOCK_DEFINE_STATIC (package_dirs);
683 gchar *result = NULL;
691 G_LOCK (package_dirs);
693 if (package_dirs == NULL)
694 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
696 result = g_hash_table_lookup (package_dirs, package);
698 if (result && result[0])
700 G_UNLOCK (package_dirs);
701 return g_strdup (result);
704 key = g_strconcat ("Software\\", package, NULL);
707 if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
708 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
709 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
710 &type, NULL, &nbytes) == ERROR_SUCCESS)
712 ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
713 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
714 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
715 &type, NULL, &nbytes) == ERROR_SUCCESS)
718 result = g_malloc (nbytes + 1);
719 RegQueryValueEx (reg_key, "InstallationDirectory", 0,
720 &type, result, &nbytes);
721 result[nbytes] = '\0';
725 RegCloseKey (reg_key);
731 g_hash_table_insert (package_dirs, package, result);
732 G_UNLOCK (package_dirs);
733 return g_strdup (result);
735 G_UNLOCK (package_dirs);
738 if (dll_name != NULL)
739 result = get_package_directory_from_module (dll_name);
742 result = get_package_directory_from_module (NULL);
748 * g_win32_get_package_installation_subdirectory:
749 * @package: An identifier for a software package, or %NULL.
750 * @dll_name: The name of a DLL that a package provides, or %NULL.
751 * @subdir: A subdirectory of the package installation directory.
753 * Returns a newly-allocated string containing the path of the
754 * subdirectory @subdir in the return value from calling
755 * g_win32_get_package_installation_directory() with the @package and
756 * @dll_name parameters.
758 * Returns: a string containing the complete path to @subdir inside the
759 * installation directory of @package. The return value should be freed with
760 * g_free() when no longer needed.
764 g_win32_get_package_installation_subdirectory (gchar *package,
771 prefix = g_win32_get_package_installation_directory (package, dll_name);
773 sep = (prefix[strlen (prefix) - 1] == G_DIR_SEPARATOR ?
774 "" : G_DIR_SEPARATOR_S);
776 return g_strconcat (prefix, sep, subdir, NULL);