GSettings docs: clarify what is a good path
[glib.git] / glib / gwin32.c
blob97eccd76ebc8b86ea0f9809d887c7f5600b97215
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/.
28 /*
29 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
32 #include "config.h"
34 #include "glibconfig.h"
36 #include <stdlib.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <wchar.h>
40 #include <errno.h>
42 #define STRICT /* Strict typing, please */
43 #include <windows.h>
44 #undef STRICT
45 #ifndef G_WITH_CYGWIN
46 #include <direct.h>
47 #endif
48 #include <errno.h>
49 #include <ctype.h>
50 #if defined(_MSC_VER) || defined(__DMC__)
51 # include <io.h>
52 #endif /* _MSC_VER || __DMC__ */
54 #include "glib.h"
55 #include "gthreadprivate.h"
57 #ifdef G_WITH_CYGWIN
58 #include <sys/cygwin.h>
59 #endif
61 #ifndef G_WITH_CYGWIN
63 gint
64 g_win32_ftruncate (gint fd,
65 guint size)
67 return _chsize (fd, size);
70 #endif
72 /**
73 * g_win32_getlocale:
75 * The setlocale() function in the Microsoft C library uses locale
76 * names of the form "English_United States.1252" etc. We want the
77 * UNIXish standard form "en_US", "zh_TW" etc. This function gets the
78 * current thread locale from Windows - without any encoding info -
79 * and returns it as a string of the above form for use in forming
80 * file names etc. The returned string should be deallocated with
81 * g_free().
83 * Returns: newly-allocated locale name.
84 **/
86 #ifndef SUBLANG_SERBIAN_LATIN_BA
87 #define SUBLANG_SERBIAN_LATIN_BA 0x06
88 #endif
90 gchar *
91 g_win32_getlocale (void)
93 LCID lcid;
94 LANGID langid;
95 gchar *ev;
96 gint primary, sub;
97 char iso639[10];
98 char iso3166[10];
99 const gchar *script = NULL;
101 /* Let the user override the system settings through environment
102 * variables, as on POSIX systems. Note that in GTK+ applications
103 * since GTK+ 2.10.7 setting either LC_ALL or LANG also sets the
104 * Win32 locale and C library locale through code in gtkmain.c.
106 if (((ev = getenv ("LC_ALL")) != NULL && ev[0] != '\0')
107 || ((ev = getenv ("LC_MESSAGES")) != NULL && ev[0] != '\0')
108 || ((ev = getenv ("LANG")) != NULL && ev[0] != '\0'))
109 return g_strdup (ev);
111 lcid = GetThreadLocale ();
113 if (!GetLocaleInfo (lcid, LOCALE_SISO639LANGNAME, iso639, sizeof (iso639)) ||
114 !GetLocaleInfo (lcid, LOCALE_SISO3166CTRYNAME, iso3166, sizeof (iso3166)))
115 return g_strdup ("C");
117 /* Strip off the sorting rules, keep only the language part. */
118 langid = LANGIDFROMLCID (lcid);
120 /* Split into language and territory part. */
121 primary = PRIMARYLANGID (langid);
122 sub = SUBLANGID (langid);
124 /* Handle special cases */
125 switch (primary)
127 case LANG_AZERI:
128 switch (sub)
130 case SUBLANG_AZERI_LATIN:
131 script = "@Latn";
132 break;
133 case SUBLANG_AZERI_CYRILLIC:
134 script = "@Cyrl";
135 break;
137 break;
138 case LANG_SERBIAN: /* LANG_CROATIAN == LANG_SERBIAN */
139 switch (sub)
141 case SUBLANG_SERBIAN_LATIN:
142 case 0x06: /* Serbian (Latin) - Bosnia and Herzegovina */
143 script = "@Latn";
144 break;
146 break;
147 case LANG_UZBEK:
148 switch (sub)
150 case SUBLANG_UZBEK_LATIN:
151 script = "@Latn";
152 break;
153 case SUBLANG_UZBEK_CYRILLIC:
154 script = "@Cyrl";
155 break;
157 break;
159 return g_strconcat (iso639, "_", iso3166, script, NULL);
163 * g_win32_error_message:
164 * @error: error code.
166 * Translate a Win32 error code (as returned by GetLastError()) into
167 * the corresponding message. The message is either language neutral,
168 * or in the thread's language, or the user's language, the system's
169 * language, or US English (see docs for FormatMessage()). The
170 * returned string is in UTF-8. It should be deallocated with
171 * g_free().
173 * Returns: newly-allocated error message
175 gchar *
176 g_win32_error_message (gint error)
178 gchar *retval;
179 wchar_t *msg = NULL;
180 int nchars;
182 FormatMessageW (FORMAT_MESSAGE_ALLOCATE_BUFFER
183 |FORMAT_MESSAGE_IGNORE_INSERTS
184 |FORMAT_MESSAGE_FROM_SYSTEM,
185 NULL, error, 0,
186 (LPWSTR) &msg, 0, NULL);
187 if (msg != NULL)
189 nchars = wcslen (msg);
191 if (nchars > 2 && msg[nchars-1] == '\n' && msg[nchars-2] == '\r')
192 msg[nchars-2] = '\0';
194 retval = g_utf16_to_utf8 (msg, -1, NULL, NULL, NULL);
196 LocalFree (msg);
198 else
199 retval = g_strdup ("");
201 return retval;
205 * g_win32_get_package_installation_directory_of_module:
206 * @hmodule: (allow-none): The Win32 handle for a DLL loaded into the current process, or %NULL
208 * This function tries to determine the installation directory of a
209 * software package based on the location of a DLL of the software
210 * package.
212 * @hmodule should be the handle of a loaded DLL or %NULL. The
213 * function looks up the directory that DLL was loaded from. If
214 * @hmodule is NULL, the directory the main executable of the current
215 * process is looked up. If that directory's last component is "bin"
216 * or "lib", its parent directory is returned, otherwise the directory
217 * itself.
219 * It thus makes sense to pass only the handle to a "public" DLL of a
220 * software package to this function, as such DLLs typically are known
221 * to be installed in a "bin" or occasionally "lib" subfolder of the
222 * installation folder. DLLs that are of the dynamically loaded module
223 * or plugin variety are often located in more private locations
224 * deeper down in the tree, from which it is impossible for GLib to
225 * deduce the root of the package installation.
227 * The typical use case for this function is to have a DllMain() that
228 * saves the handle for the DLL. Then when code in the DLL needs to
229 * construct names of files in the installation tree it calls this
230 * function passing the DLL handle.
232 * Returns: a string containing the guessed installation directory for
233 * the software package @hmodule is from. The string is in the GLib
234 * file name encoding, i.e. UTF-8. The return value should be freed
235 * with g_free() when not needed any longer. If the function fails
236 * %NULL is returned.
238 * Since: 2.16
240 gchar *
241 g_win32_get_package_installation_directory_of_module (gpointer hmodule)
243 gchar *retval;
244 gchar *p;
245 wchar_t wc_fn[MAX_PATH];
247 if (!GetModuleFileNameW (hmodule, wc_fn, MAX_PATH))
248 return NULL;
250 retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
252 if ((p = strrchr (retval, G_DIR_SEPARATOR)) != NULL)
253 *p = '\0';
255 p = strrchr (retval, G_DIR_SEPARATOR);
256 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0 ||
257 g_ascii_strcasecmp (p + 1, "lib") == 0))
258 *p = '\0';
260 #ifdef G_WITH_CYGWIN
261 /* In Cygwin we need to have POSIX paths */
263 gchar tmp[MAX_PATH];
265 cygwin_conv_to_posix_path (retval, tmp);
266 g_free (retval);
267 retval = g_strdup (tmp);
269 #endif
271 return retval;
274 static gchar *
275 get_package_directory_from_module (const gchar *module_name)
277 static GHashTable *module_dirs = NULL;
278 G_LOCK_DEFINE_STATIC (module_dirs);
279 HMODULE hmodule = NULL;
280 gchar *fn;
282 G_LOCK (module_dirs);
284 if (module_dirs == NULL)
285 module_dirs = g_hash_table_new (g_str_hash, g_str_equal);
287 fn = g_hash_table_lookup (module_dirs, module_name ? module_name : "");
289 if (fn)
291 G_UNLOCK (module_dirs);
292 return g_strdup (fn);
295 if (module_name)
297 wchar_t *wc_module_name = g_utf8_to_utf16 (module_name, -1, NULL, NULL, NULL);
298 hmodule = GetModuleHandleW (wc_module_name);
299 g_free (wc_module_name);
301 if (!hmodule)
303 G_UNLOCK (module_dirs);
304 return NULL;
308 fn = g_win32_get_package_installation_directory_of_module (hmodule);
310 if (fn == NULL)
312 G_UNLOCK (module_dirs);
313 return NULL;
316 g_hash_table_insert (module_dirs, module_name ? g_strdup (module_name) : "", fn);
318 G_UNLOCK (module_dirs);
320 return g_strdup (fn);
324 * g_win32_get_package_installation_directory:
325 * @package: (allow-none): You should pass %NULL for this.
326 * @dll_name: (allow-none): The name of a DLL that a package provides in UTF-8, or %NULL.
328 * Try to determine the installation directory for a software package.
330 * This function is deprecated. Use
331 * g_win32_get_package_installation_directory_of_module() instead.
333 * The use of @package is deprecated. You should always pass %NULL. A
334 * warning is printed if non-NULL is passed as @package.
336 * The original intended use of @package was for a short identifier of
337 * the package, typically the same identifier as used for
338 * <literal>GETTEXT_PACKAGE</literal> in software configured using GNU
339 * autotools. The function first looks in the Windows Registry for the
340 * value <literal>&num;InstallationDirectory</literal> in the key
341 * <literal>&num;HKLM\Software\@package</literal>, and if that value
342 * exists and is a string, returns that.
344 * It is strongly recommended that packagers of GLib-using libraries
345 * for Windows do not store installation paths in the Registry to be
346 * used by this function as that interfers with having several
347 * parallel installations of the library. Enabling multiple
348 * installations of different versions of some GLib-using library, or
349 * GLib itself, is desirable for various reasons.
351 * For this reason it is recommeded to always pass %NULL as
352 * @package to this function, to avoid the temptation to use the
353 * Registry. In version 2.20 of GLib the @package parameter
354 * will be ignored and this function won't look in the Registry at all.
356 * If @package is %NULL, or the above value isn't found in the
357 * Registry, but @dll_name is non-%NULL, it should name a DLL loaded
358 * into the current process. Typically that would be the name of the
359 * DLL calling this function, looking for its installation
360 * directory. The function then asks Windows what directory that DLL
361 * was loaded from. If that directory's last component is "bin" or
362 * "lib", the parent directory is returned, otherwise the directory
363 * itself. If that DLL isn't loaded, the function proceeds as if
364 * @dll_name was %NULL.
366 * If both @package and @dll_name are %NULL, the directory from where
367 * the main executable of the process was loaded is used instead in
368 * the same way as above.
370 * Returns: a string containing the installation directory for
371 * @package. The string is in the GLib file name encoding,
372 * i.e. UTF-8. The return value should be freed with g_free() when not
373 * needed any longer. If the function fails %NULL is returned.
375 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
376 * g_win32_get_package_installation_directory_of_module() instead.
379 gchar *
380 g_win32_get_package_installation_directory_utf8 (const gchar *package,
381 const gchar *dll_name)
383 gchar *result = NULL;
385 if (package != NULL)
386 g_warning ("Passing a non-NULL package to g_win32_get_package_installation_directory() is deprecated and it is ignored.");
388 if (dll_name != NULL)
389 result = get_package_directory_from_module (dll_name);
391 if (result == NULL)
392 result = get_package_directory_from_module (NULL);
394 return result;
397 #if !defined (_WIN64)
399 /* DLL ABI binary compatibility version that uses system codepage file names */
401 gchar *
402 g_win32_get_package_installation_directory (const gchar *package,
403 const gchar *dll_name)
405 gchar *utf8_package = NULL, *utf8_dll_name = NULL;
406 gchar *utf8_retval, *retval;
408 if (package != NULL)
409 utf8_package = g_locale_to_utf8 (package, -1, NULL, NULL, NULL);
411 if (dll_name != NULL)
412 utf8_dll_name = g_locale_to_utf8 (dll_name, -1, NULL, NULL, NULL);
414 utf8_retval =
415 g_win32_get_package_installation_directory_utf8 (utf8_package,
416 utf8_dll_name);
418 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
420 g_free (utf8_package);
421 g_free (utf8_dll_name);
422 g_free (utf8_retval);
424 return retval;
427 #endif
430 * g_win32_get_package_installation_subdirectory:
431 * @package: (allow-none): You should pass %NULL for this.
432 * @dll_name: (allow-none): The name of a DLL that a package provides, in UTF-8, or %NULL.
433 * @subdir: A subdirectory of the package installation directory, also in UTF-8
435 * This function is deprecated. Use
436 * g_win32_get_package_installation_directory_of_module() and
437 * g_build_filename() instead.
439 * Returns a newly-allocated string containing the path of the
440 * subdirectory @subdir in the return value from calling
441 * g_win32_get_package_installation_directory() with the @package and
442 * @dll_name parameters. See the documentation for
443 * g_win32_get_package_installation_directory() for more details. In
444 * particular, note that it is deprecated to pass anything except NULL
445 * as @package.
447 * Returns: a string containing the complete path to @subdir inside
448 * the installation directory of @package. The returned string is in
449 * the GLib file name encoding, i.e. UTF-8. The return value should be
450 * freed with g_free() when no longer needed. If something goes wrong,
451 * %NULL is returned.
453 * Deprecated: 2.18: Pass the HMODULE of a DLL or EXE to
454 * g_win32_get_package_installation_directory_of_module() instead, and
455 * then construct a subdirectory pathname with g_build_filename().
458 gchar *
459 g_win32_get_package_installation_subdirectory_utf8 (const gchar *package,
460 const gchar *dll_name,
461 const gchar *subdir)
463 gchar *prefix;
464 gchar *dirname;
466 prefix = g_win32_get_package_installation_directory_utf8 (package, dll_name);
468 dirname = g_build_filename (prefix, subdir, NULL);
469 g_free (prefix);
471 return dirname;
474 #if !defined (_WIN64)
476 /* DLL ABI binary compatibility version that uses system codepage file names */
478 gchar *
479 g_win32_get_package_installation_subdirectory (const gchar *package,
480 const gchar *dll_name,
481 const gchar *subdir)
483 gchar *prefix;
484 gchar *dirname;
486 prefix = g_win32_get_package_installation_directory (package, dll_name);
488 dirname = g_build_filename (prefix, subdir, NULL);
489 g_free (prefix);
491 return dirname;
494 #endif
497 * g_win32_get_windows_version:
499 * Returns version information for the Windows operating system the
500 * code is running on. See MSDN documentation for the GetVersion()
501 * function. To summarize, the most significant bit is one on Win9x,
502 * and zero on NT-based systems. Since version 2.14, GLib works only
503 * on NT-based systems, so checking whether your are running on Win9x
504 * in your own software is moot. The least significant byte is 4 on
505 * Windows NT 4, and 5 on Windows XP. Software that needs really
506 * detailed version and feature information should use Win32 API like
507 * GetVersionEx() and VerifyVersionInfo().
509 * Returns: The version information.
511 * Since: 2.6
513 guint
514 g_win32_get_windows_version (void)
516 static gsize windows_version;
518 if (g_once_init_enter (&windows_version))
519 g_once_init_leave (&windows_version, GetVersion ());
521 return windows_version;
525 * g_win32_locale_filename_from_utf8:
526 * @utf8filename: a UTF-8 encoded filename.
528 * Converts a filename from UTF-8 to the system codepage.
530 * On NT-based Windows, on NTFS file systems, file names are in
531 * Unicode. It is quite possible that Unicode file names contain
532 * characters not representable in the system codepage. (For instance,
533 * Greek or Cyrillic characters on Western European or US Windows
534 * installations, or various less common CJK characters on CJK Windows
535 * installations.)
537 * In such a case, and if the filename refers to an existing file, and
538 * the file system stores alternate short (8.3) names for directory
539 * entries, the short form of the filename is returned. Note that the
540 * "short" name might in fact be longer than the Unicode name if the
541 * Unicode name has very short pathname components containing
542 * non-ASCII characters. If no system codepage name for the file is
543 * possible, %NULL is returned.
545 * The return value is dynamically allocated and should be freed with
546 * g_free() when no longer needed.
548 * Return value: The converted filename, or %NULL on conversion
549 * failure and lack of short names.
551 * Since: 2.8
553 gchar *
554 g_win32_locale_filename_from_utf8 (const gchar *utf8filename)
556 gchar *retval = g_locale_from_utf8 (utf8filename, -1, NULL, NULL, NULL);
558 if (retval == NULL)
560 /* Conversion failed, so convert to wide chars, check if there
561 * is a 8.3 version, and use that.
563 wchar_t *wname = g_utf8_to_utf16 (utf8filename, -1, NULL, NULL, NULL);
564 if (wname != NULL)
566 wchar_t wshortname[MAX_PATH + 1];
567 if (GetShortPathNameW (wname, wshortname, G_N_ELEMENTS (wshortname)))
569 gchar *tem = g_utf16_to_utf8 (wshortname, -1, NULL, NULL, NULL);
570 retval = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
571 g_free (tem);
573 g_free (wname);
576 return retval;