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:
97 g_win32_opendir (const char *dirname)
103 g_return_val_if_fail (dirname != NULL, NULL);
105 result = g_new0 (DIR, 1);
106 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
107 result->dir_name = g_strdup (dirname);
109 k = strlen (result->dir_name);
110 if (k && result->dir_name[k - 1] == '\\')
112 result->dir_name[k - 1] = '\0';
114 mask = g_strdup_printf ("%s\\*", result->dir_name);
116 result->find_file_handle = (guint) FindFirstFile (mask,
117 (LPWIN32_FIND_DATA) result->find_file_data);
120 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
122 int error = GetLastError ();
124 g_free (result->dir_name);
125 g_free (result->find_file_data);
134 result->just_opened = TRUE;
140 g_win32_readdir (DIR *dir)
144 g_return_val_if_fail (dir != NULL, NULL);
146 if (dir->just_opened)
147 dir->just_opened = FALSE;
150 if (!FindNextFile ((HANDLE) dir->find_file_handle,
151 (LPWIN32_FIND_DATA) dir->find_file_data))
153 int error = GetLastError ();
157 case ERROR_NO_MORE_FILES:
166 basename = g_path_get_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName);
168 strcpy (dir->readdir_result.d_name, basename);
172 return &dir->readdir_result;
176 g_win32_rewinddir (DIR *dir)
180 g_return_if_fail (dir != NULL);
182 if (!FindClose ((HANDLE) dir->find_file_handle))
183 g_warning ("gwin_rewinddir(): FindClose() failed\n");
185 mask = g_strdup_printf ("%s\\*", dir->dir_name);
186 dir->find_file_handle = (guint) FindFirstFile (mask,
187 (LPWIN32_FIND_DATA) dir->find_file_data);
190 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
192 int error = GetLastError ();
201 dir->just_opened = TRUE;
205 g_win32_closedir (DIR *dir)
207 g_return_val_if_fail (dir != NULL, -1);
209 if (!FindClose ((HANDLE) dir->find_file_handle))
211 int error = GetLastError ();
216 errno = EIO; return -1;
220 g_free (dir->dir_name);
221 g_free (dir->find_file_data);
228 /* msvc 5.0 headers don't have latest language and sublanguage codes */
229 #ifndef LANG_ARMENIAN
230 #define LANG_ARMENIAN 0x2b
232 #ifndef LANG_ASSAMESE
233 #define LANG_ASSAMESE 0x4d
236 #define LANG_AZERI 0x2c
239 #define LANG_BENGALI 0x45
241 #ifndef LANG_GEORGIAN
242 #define LANG_GEORGIAN 0x37
244 #ifndef LANG_GUJARATI
245 #define LANG_GUJARATI 0x47
248 #define LANG_HINDI 0x39
251 #define LANG_KANNADA 0x4b
253 #ifndef LANG_KASHMIRI
254 #define LANG_KASHMIRI 0x60
257 #define LANG_KAZAK 0x3f
260 #define LANG_KONKANI 0x57
262 #ifndef LANG_MACEDONIAN
263 #define LANG_MACEDONIAN 0x2f
266 #define LANG_MALAY 0x3e
268 #ifndef LANG_MALAYALAM
269 #define LANG_MALAYALAM 0x4c
271 #ifndef LANG_MANIPURI
272 #define LANG_MANIPURI 0x58
275 #define LANG_MARATHI 0x4e
278 #define LANG_NEPALI 0x61
281 #define LANG_ORIYA 0x48
284 #define LANG_PUNJABI 0x46
286 #ifndef LANG_SANSKRIT
287 #define LANG_SANSKRIT 0x4f
290 #define LANG_SINDHI 0x59
293 #define LANG_SWAHILI 0x41
296 #define LANG_TAMIL 0x49
299 #define LANG_TATAR 0x44
302 #define LANG_TELUGU 0x4a
305 #define LANG_URDU 0x20
308 #define LANG_UZBEK 0x43
311 #ifndef SUBLANG_CHINESE_MACAU
312 #define SUBLANG_CHINESE_MACAU 0x05
314 #ifndef SUBLANG_ENGLISH_ZIMBABWE
315 #define SUBLANG_ENGLISH_ZIMBABWE 0x0c
317 #ifndef SUBLANG_ENGLISH_PHILIPPINES
318 #define SUBLANG_ENGLISH_PHILIPPINES 0x0d
320 #ifndef SUBLANG_FRENCH_MONACO
321 #define SUBLANG_FRENCH_MONACO 0x06
323 #ifndef SUBLANG_KASHMIRI_INDIA
324 #define SUBLANG_KASHMIRI_INDIA 0x02
326 #ifndef SUBLANG_MALAY_BRUNEI_DARUSSALAM
327 #define SUBLANG_MALAY_BRUNEI_DARUSSALAM 0x02
329 #ifndef SUBLANG_NEPALI_INDIA
330 #define SUBLANG_NEPALI_INDIA 0x02
332 #ifndef SUBLANG_URDU_PAKISTAN
333 #define SUBLANG_URDU_PAKISTAN 0x01
335 #ifndef SUBLANG_URDU_INDIA
336 #define SUBLANG_URDU_INDIA 0x02
338 #ifndef SUBLANG_UZBEK_CYRILLIC
339 #define SUBLANG_UZBEK_CYRILLIC 0x02
345 * The setlocale in the Microsoft C library uses locale names of the
346 * form "English_United States.1252" etc. We want the Unixish standard
347 * form "en", "zh_TW" etc. This function gets the current thread
348 * locale from Windows and returns it as a string of the above form
349 * for use in forming file names etc. The returned string should be
350 * deallocated with g_free().
352 * Returns: allocated locale name
356 g_win32_getlocale (void)
361 gchar *l = NULL, *sl = NULL;
364 if ((ev = getenv ("LC_ALL")) != NULL
365 || (ev = getenv ("LC_CTYPE")) != NULL
366 || (ev = getenv ("LANG")) != NULL)
367 return g_strdup (ev);
369 lcid = GetThreadLocale ();
370 primary = PRIMARYLANGID (LANGIDFROMLCID (lcid));
371 sub = SUBLANGID (LANGIDFROMLCID (lcid));
374 case LANG_AFRIKAANS: l = "af"; break;
375 case LANG_ALBANIAN: l = "sq"; break;
380 case SUBLANG_ARABIC_SAUDI_ARABIA: sl = "SA"; break;
381 case SUBLANG_ARABIC_IRAQ: sl = "IQ"; break;
382 case SUBLANG_ARABIC_EGYPT: sl = "EG"; break;
383 case SUBLANG_ARABIC_LIBYA: sl = "LY"; break;
384 case SUBLANG_ARABIC_ALGERIA: sl = "DZ"; break;
385 case SUBLANG_ARABIC_MOROCCO: sl = "MA"; break;
386 case SUBLANG_ARABIC_TUNISIA: sl = "TN"; break;
387 case SUBLANG_ARABIC_OMAN: sl = "OM"; break;
388 case SUBLANG_ARABIC_YEMEN: sl = "YE"; break;
389 case SUBLANG_ARABIC_SYRIA: sl = "SY"; break;
390 case SUBLANG_ARABIC_JORDAN: sl = "JO"; break;
391 case SUBLANG_ARABIC_LEBANON: sl = "LB"; break;
392 case SUBLANG_ARABIC_KUWAIT: sl = "KW"; break;
393 case SUBLANG_ARABIC_UAE: sl = "AE"; break;
394 case SUBLANG_ARABIC_BAHRAIN: sl = "BH"; break;
395 case SUBLANG_ARABIC_QATAR: sl = "QA"; break;
398 case LANG_ARMENIAN: l = "hy"; break;
399 case LANG_ASSAMESE: l = "as"; break;
400 case LANG_AZERI: l = "az"; break;
401 case LANG_BASQUE: l = "eu"; break;
402 case LANG_BELARUSIAN: l = "be"; break;
403 case LANG_BENGALI: l = "bn"; break;
404 case LANG_BULGARIAN: l = "bg"; break;
405 case LANG_CATALAN: l = "ca"; break;
410 case SUBLANG_CHINESE_TRADITIONAL: sl = "TW"; break;
411 case SUBLANG_CHINESE_SIMPLIFIED: sl = "CH"; break;
412 case SUBLANG_CHINESE_HONGKONG: sl = "HK"; break;
413 case SUBLANG_CHINESE_SINGAPORE: sl = "SG"; break;
414 case SUBLANG_CHINESE_MACAU: sl = "MO"; break;
417 case LANG_CROATIAN: /* LANG_CROATIAN == LANG_SERBIAN
418 * What used to be called Serbo-Croatian
419 * should really now be two separate
420 * languages because of political reasons.
421 * (Says tml, who knows nothing about Serbian
423 * (I can feel those flames coming already.)
427 case SUBLANG_SERBIAN_LATIN: l = "sp"; break;
428 case SUBLANG_SERBIAN_CYRILLIC: l = "sr"; break;
429 default: l = "hr"; /* ??? */
432 case LANG_CZECH: l = "cs"; break;
433 case LANG_DANISH: l = "da"; break;
438 case SUBLANG_DUTCH_BELGIAN: sl = "BE"; break;
445 /* SUBLANG_ENGLISH_US == SUBLANG_DEFAULT. Heh. I thought
446 * English was the language spoken in England.
449 case SUBLANG_ENGLISH_UK: sl = "GB"; break;
450 case SUBLANG_ENGLISH_AUS: sl = "AU"; break;
451 case SUBLANG_ENGLISH_CAN: sl = "CA"; break;
452 case SUBLANG_ENGLISH_NZ: sl = "NZ"; break;
453 case SUBLANG_ENGLISH_EIRE: sl = "IE"; break;
454 case SUBLANG_ENGLISH_SOUTH_AFRICA: sl = "ZA"; break;
455 case SUBLANG_ENGLISH_JAMAICA: sl = "JM"; break;
456 case SUBLANG_ENGLISH_CARIBBEAN: sl = "@caribbean"; break; /* ??? */
457 case SUBLANG_ENGLISH_BELIZE: sl = "BZ"; break;
458 case SUBLANG_ENGLISH_TRINIDAD: sl = "TT"; break;
459 case SUBLANG_ENGLISH_ZIMBABWE: sl = "ZW"; break;
460 case SUBLANG_ENGLISH_PHILIPPINES: sl = "PH"; break;
463 case LANG_ESTONIAN: l = "et"; break;
464 case LANG_FAEROESE: l = "fo"; break;
465 case LANG_FARSI: l = "fa"; break;
466 case LANG_FINNISH: l = "fi"; break;
471 case SUBLANG_FRENCH_BELGIAN: sl = "BE"; break;
472 case SUBLANG_FRENCH_CANADIAN: sl = "CA"; break;
473 case SUBLANG_FRENCH_SWISS: sl = "CH"; break;
474 case SUBLANG_FRENCH_LUXEMBOURG: sl = "LU"; break;
475 case SUBLANG_FRENCH_MONACO: sl = "MC"; break;
478 case LANG_GEORGIAN: l = "ka"; break;
483 case SUBLANG_GERMAN_SWISS: sl = "CH"; break;
484 case SUBLANG_GERMAN_AUSTRIAN: sl = "AT"; break;
485 case SUBLANG_GERMAN_LUXEMBOURG: sl = "LU"; break;
486 case SUBLANG_GERMAN_LIECHTENSTEIN: sl = "LI"; break;
489 case LANG_GREEK: l = "el"; break;
490 case LANG_GUJARATI: l = "gu"; break;
491 case LANG_HEBREW: l = "he"; break;
492 case LANG_HINDI: l = "hi"; break;
493 case LANG_HUNGARIAN: l = "hu"; break;
494 case LANG_ICELANDIC: l = "is"; break;
495 case LANG_INDONESIAN: l = "id"; break;
500 case SUBLANG_ITALIAN_SWISS: sl = "CH"; break;
503 case LANG_JAPANESE: l = "ja"; break;
504 case LANG_KANNADA: l = "kn"; break;
509 case SUBLANG_KASHMIRI_INDIA: sl = "IN"; break;
512 case LANG_KAZAK: l = "kk"; break;
513 case LANG_KONKANI: l = "kok"; break; /* ??? */
514 case LANG_KOREAN: l = "ko"; break;
515 case LANG_LATVIAN: l = "lv"; break;
516 case LANG_LITHUANIAN: l = "lt"; break;
517 case LANG_MACEDONIAN: l = "mk"; break;
522 case SUBLANG_MALAY_BRUNEI_DARUSSALAM: sl = "BN"; break;
525 case LANG_MALAYALAM: l = "ml"; break;
526 case LANG_MANIPURI: l = "mni"; break;
527 case LANG_MARATHI: l = "mr"; break;
532 case SUBLANG_NEPALI_INDIA: sl = "IN"; break;
539 /* SUBLANG_NORWEGIAN_BOKMAL == SUBLANG_DEFAULT */
540 case SUBLANG_NORWEGIAN_NYNORSK: l = "nn"; break;
543 case LANG_ORIYA: l = "or"; break;
544 case LANG_POLISH: l = "pl"; break;
545 case LANG_PORTUGUESE:
549 /* Hmm. SUBLANG_PORTUGUESE_BRAZILIAN == SUBLANG_DEFAULT */
550 case SUBLANG_PORTUGUESE_BRAZILIAN: sl = "BR"; break;
553 case LANG_PUNJABI: l = "pa"; break;
554 case LANG_ROMANIAN: l = "ro"; break;
555 case LANG_RUSSIAN: l = "ru"; break;
556 case LANG_SANSKRIT: l = "sa"; break;
557 case LANG_SINDHI: l = "sd"; break;
558 case LANG_SLOVAK: l = "sk"; break;
559 case LANG_SLOVENIAN: l = "sl"; break;
564 case SUBLANG_SPANISH_MEXICAN: sl = "MX"; break;
565 case SUBLANG_SPANISH_MODERN: sl = "@modern"; break; /* ??? */
566 case SUBLANG_SPANISH_GUATEMALA: sl = "GT"; break;
567 case SUBLANG_SPANISH_COSTA_RICA: sl = "CR"; break;
568 case SUBLANG_SPANISH_PANAMA: sl = "PA"; break;
569 case SUBLANG_SPANISH_DOMINICAN_REPUBLIC: sl = "DO"; break;
570 case SUBLANG_SPANISH_VENEZUELA: sl = "VE"; break;
571 case SUBLANG_SPANISH_COLOMBIA: sl = "CO"; break;
572 case SUBLANG_SPANISH_PERU: sl = "PE"; break;
573 case SUBLANG_SPANISH_ARGENTINA: sl = "AR"; break;
574 case SUBLANG_SPANISH_ECUADOR: sl = "EC"; break;
575 case SUBLANG_SPANISH_CHILE: sl = "CL"; break;
576 case SUBLANG_SPANISH_URUGUAY: sl = "UY"; break;
577 case SUBLANG_SPANISH_PARAGUAY: sl = "PY"; break;
578 case SUBLANG_SPANISH_BOLIVIA: sl = "BO"; break;
579 case SUBLANG_SPANISH_EL_SALVADOR: sl = "SV"; break;
580 case SUBLANG_SPANISH_HONDURAS: sl = "HN"; break;
581 case SUBLANG_SPANISH_NICARAGUA: sl = "NI"; break;
582 case SUBLANG_SPANISH_PUERTO_RICO: sl = "PR"; break;
585 case LANG_SWAHILI: l = "sw"; break;
590 case SUBLANG_SWEDISH_FINLAND: sl = "FI"; break;
593 case LANG_TAMIL: l = "ta"; break;
594 case LANG_TATAR: l = "tt"; break;
595 case LANG_TELUGU: l = "te"; break;
596 case LANG_THAI: l = "th"; break;
597 case LANG_TURKISH: l = "tr"; break;
598 case LANG_UKRAINIAN: l = "uk"; break;
603 case SUBLANG_URDU_PAKISTAN: sl = "PK"; break;
604 case SUBLANG_URDU_INDIA: sl = "IN"; break;
611 case SUBLANG_UZBEK_CYRILLIC: sl = "@cyrillic"; break;
614 case LANG_VIETNAMESE: l = "vi"; break;
615 default: l = "xx"; break;
625 return g_strdup (bfr);
629 * g_win32_error_message:
632 * Translate a Win32 error code (as returned by GetLastError()) into
633 * the corresponding message. The message is either language neutral,
634 * or in the thread's language, or the user's language, the system's
635 * langauge, or US English (see docs for FormatMessage). The returned
636 * string should be deallocated with g_free().
638 * Returns: allocated error message
641 g_win32_error_message (gint error)
647 FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER
648 |FORMAT_MESSAGE_IGNORE_INSERTS
649 |FORMAT_MESSAGE_FROM_SYSTEM,
651 (LPTSTR) &msg, 0, NULL);
652 nbytes = strlen (msg);
654 if (nbytes > 2 && msg[nbytes-1] == '\n' && msg[nbytes-2] == '\r')
655 msg[nbytes-2] = '\0';
657 retval = g_strdup (msg);
666 get_package_directory_from_module (gchar *module_name)
668 static GHashTable *module_dirs = NULL;
669 HMODULE hmodule = NULL;
674 if (module_dirs == NULL)
675 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
677 result = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
680 return g_strdup (result);
684 hmodule = GetModuleHandle (module_name);
689 fn = g_malloc (MAX_PATH);
690 if (!GetModuleFileName (hmodule, fn, MAX_PATH))
694 /* In Cygwin we need to have POSIX paths */
698 cygwin_conv_to_posix_path(fn, tmp);
704 if ((p = strrchr (fn, G_DIR_SEPARATOR)) != NULL)
707 p = strrchr (fn, G_DIR_SEPARATOR);
708 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
709 g_ascii_strcasecmp (p + 1, "lib") == 0))
712 g_hash_table_insert (module_dirs, module_name ? module_name : "", fn);
714 return g_strdup (fn);
718 * g_win32_get_package_installation_directory:
719 * @package: An identifier for a software package, or NULL
720 * @dll_name: The name of a DLL that a package provides, or NULL
722 * Try to determine the installation directory for a software package.
723 * Typically used by GNU software packages.
725 * @package should be a short identifier for the package. Typically it
726 * is the same identifier as used for GETTEXT_PACKAGE in software
727 * configured accoring to GNU standards. The function first looks in
728 * the Windows Registry for the value #InstallationDirectory in the
729 * key #HKLM\Software\@package, and if that value exists and is a
730 * string, returns that.
732 * If @package is NULL, or the above value isn't found in the
733 * Registry, but @dll_name is non-NULL, it should name a DLL loaded
734 * into the current process. Typically that would be the name of the
735 * DLL calling this function, looking for its installation
736 * directory. The function then asks Windows what directory that DLL
737 * was loaded from. If that directory's last component is "bin" or
738 * "lib", the parent directory is returned, otherwise the directory
739 * itself. If that DLL isn't loaded, the function proceeds as if
740 * @dll_name was NULL.
742 * If both @package and @dll_name are NULL, the directory from where
743 * the main executable of the process was loaded is uses instead in
744 * the same way as above.
746 * The return value should be freed with g_free() when not needed any longer. */
749 g_win32_get_package_installation_directory (gchar *package,
752 static GHashTable *package_dirs = NULL;
753 gchar *result = NULL;
761 if (package_dirs == NULL)
762 package_dirs = g_hash_table_new (g_str_hash, g_str_equal);
764 result = g_hash_table_lookup (package_dirs, package);
766 if (result && result[0])
767 return g_strdup (result);
769 key = g_strconcat ("Software\\", package, NULL);
772 if ((RegOpenKeyEx (HKEY_CURRENT_USER, key, 0,
773 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
774 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
775 &type, NULL, &nbytes) == ERROR_SUCCESS)
777 ((RegOpenKeyEx (HKEY_LOCAL_MACHINE, key, 0,
778 KEY_QUERY_VALUE, ®_key) == ERROR_SUCCESS
779 && RegQueryValueEx (reg_key, "InstallationDirectory", 0,
780 &type, NULL, &nbytes) == ERROR_SUCCESS)
783 result = g_malloc (nbytes + 1);
784 RegQueryValueEx (reg_key, "InstallationDirectory", 0,
785 &type, result, &nbytes);
786 result[nbytes] = '\0';
790 RegCloseKey (reg_key);
797 g_hash_table_insert (package_dirs, package, result);
798 return g_strdup (result);
801 if (dll_name != NULL)
802 result = get_package_directory_from_module (dll_name);
805 result = get_package_directory_from_module (NULL);
811 * g_win32_get_package_installation_subdirectory:
812 * @package: An identifier for a software package, or NULL
813 * @dll_name: The name of a DLL that a package provides, or NULL
814 * @subdir: A subdirectory of the package installation directory.
816 * Returns a string containg the path of the subdirectory @subdir in
817 * the return value from calling
818 * g_win32_get_package_installation_directory() with the @package and
819 * @dll_name parameters. The return value should be freed with
820 * g_free() when no longer needed.
824 g_win32_get_package_installation_subdirectory (gchar *package,
831 prefix = g_win32_get_package_installation_directory (package, dll_name);
833 sep = (prefix[strlen (prefix) - 1] == G_DIR_SEPARATOR ?
834 "" : G_DIR_SEPARATOR_S);
836 return g_strconcat (prefix, sep, subdir, NULL);