1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1998 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
26 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
32 #include "gutilsprivate.h"
39 #include <ctype.h> /* For tolower() */
41 #include <sys/types.h>
45 #include <sys/utsname.h>
48 #include <sys/types.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
52 #ifdef HAVE_CRT_EXTERNS_H
53 #include <crt_externs.h> /* for _NSGetEnviron */
55 #ifdef HAVE_SYS_AUXV_H
59 #include "glib-init.h"
60 #include "glib-private.h"
62 #include "gfileutils.h"
66 #include "gtestutils.h"
68 #include "gstrfuncs.h"
73 #ifdef G_PLATFORM_WIN32
81 * @title: Miscellaneous Utility Functions
82 * @short_description: a selection of portable utility functions
84 * These are portable utility functions.
87 #ifdef G_PLATFORM_WIN32
89 # ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
90 # define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
91 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
93 # include <lmcons.h> /* For UNLEN */
94 #endif /* G_PLATFORM_WIN32 */
103 #include <langinfo.h>
106 #ifdef G_PLATFORM_WIN32
109 _glib_get_dll_directory (void)
113 wchar_t wc_fn[MAX_PATH];
116 if (glib_dll == NULL)
120 /* This code is different from that in
121 * g_win32_get_package_installation_directory_of_module() in that
122 * here we return the actual folder where the GLib DLL is. We don't
123 * do the check for it being in a "bin" or "lib" subfolder and then
124 * returning the parent of that.
126 * In a statically built GLib, glib_dll will be NULL and we will
127 * thus look up the application's .exe file's location.
129 if (!GetModuleFileNameW (glib_dll, wc_fn, MAX_PATH))
132 retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
134 p = strrchr (retval, G_DIR_SEPARATOR);
149 * @dest: the destination address to copy the bytes to.
150 * @src: the source address to copy the bytes from.
151 * @len: the number of bytes to copy.
153 * Copies a block of memory @len bytes long, from @src to @dest.
154 * The source and destination areas may overlap.
156 * Deprecated:2.40: Just use memmove().
165 * @func: (scope async): the function to call on normal program termination.
167 * Specifies a function to be called at normal program termination.
169 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
170 * macro that maps to a call to the atexit() function in the C
171 * library. This means that in case the code that calls g_atexit(),
172 * i.e. atexit(), is in a DLL, the function will be called when the
173 * DLL is detached from the program. This typically makes more sense
174 * than that the function is called when the GLib DLL is detached,
175 * which happened earlier when g_atexit() was a function in the GLib
178 * The behaviour of atexit() in the context of dynamically loaded
179 * modules is not formally specified and varies wildly.
181 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
182 * loaded module which is unloaded before the program terminates might
183 * well cause a crash at program exit.
185 * Some POSIX systems implement atexit() like Windows, and have each
186 * dynamically loaded module maintain an own atexit chain that is
187 * called when the module is unloaded.
189 * On other POSIX systems, before a dynamically loaded module is
190 * unloaded, the registered atexit functions (if any) residing in that
191 * module are called, regardless where the code that registered them
192 * resided. This is presumably the most robust approach.
194 * As can be seen from the above, for portability it's best to avoid
195 * calling g_atexit() (or atexit()) except in the main executable of a
198 * Deprecated:2.32: It is best to avoid g_atexit().
200 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
202 g_atexit (GVoidFunc func)
207 result = atexit ((void (*)(void)) func);
211 g_error ("Could not register atexit() function: %s",
215 G_GNUC_END_IGNORE_DEPRECATIONS
217 /* Based on execvp() from GNU Libc.
218 * Some of this code is cut-and-pasted into gspawn.c
222 my_strchrnul (const gchar *str,
225 gchar *p = (gchar*)str;
226 while (*p && (*p != c))
234 static gchar *inner_find_program_in_path (const gchar *program);
237 g_find_program_in_path (const gchar *program)
239 const gchar *last_dot = strrchr (program, '.');
241 if (last_dot == NULL ||
242 strchr (last_dot, '\\') != NULL ||
243 strchr (last_dot, '/') != NULL)
245 const gint program_length = strlen (program);
246 gchar *pathext = g_build_path (";",
247 ".exe;.cmd;.bat;.com",
248 g_getenv ("PATHEXT"),
251 gchar *decorated_program;
257 gchar *q = my_strchrnul (p, ';');
259 decorated_program = g_malloc (program_length + (q-p) + 1);
260 memcpy (decorated_program, program, program_length);
261 memcpy (decorated_program+program_length, p, q-p);
262 decorated_program [program_length + (q-p)] = '\0';
264 retval = inner_find_program_in_path (decorated_program);
265 g_free (decorated_program);
273 } while (*p++ != '\0');
278 return inner_find_program_in_path (program);
284 * g_find_program_in_path:
285 * @program: (type filename): a program name in the GLib file name encoding
287 * Locates the first executable named @program in the user's path, in the
288 * same way that execvp() would locate it. Returns an allocated string
289 * with the absolute path name, or %NULL if the program is not found in
290 * the path. If @program is already an absolute path, returns a copy of
291 * @program if @program exists and is executable, and %NULL otherwise.
293 * On Windows, if @program does not have a file type suffix, tries
294 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
295 * the `PATHEXT` environment variable.
297 * On Windows, it looks for the file in the same way as CreateProcess()
298 * would. This means first in the directory where the executing
299 * program was loaded from, then in the current directory, then in the
300 * Windows 32-bit system directory, then in the Windows directory, and
301 * finally in the directories in the `PATH` environment variable. If
302 * the program is found, the return value contains the full name
303 * including the type suffix.
305 * Returns: (type filename) (transfer full) (nullable): a newly-allocated
306 * string with the absolute path, or %NULL
310 inner_find_program_in_path (const gchar *program)
313 g_find_program_in_path (const gchar *program)
316 const gchar *path, *p;
317 gchar *name, *freeme;
319 const gchar *path_copy;
320 gchar *filename = NULL, *appdir = NULL;
321 gchar *sysdir = NULL, *windir = NULL;
323 wchar_t wfilename[MAXPATHLEN], wsysdir[MAXPATHLEN],
329 g_return_val_if_fail (program != NULL, NULL);
331 /* If it is an absolute path, or a relative path including subdirectories,
332 * don't look in PATH.
334 if (g_path_is_absolute (program)
335 || strchr (program, G_DIR_SEPARATOR) != NULL
337 || strchr (program, '/') != NULL
341 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE) &&
342 !g_file_test (program, G_FILE_TEST_IS_DIR))
343 return g_strdup (program);
348 path = g_getenv ("PATH");
349 #if defined(G_OS_UNIX)
352 /* There is no 'PATH' in the environment. The default
353 * search path in GNU libc is the current directory followed by
354 * the path 'confstr' returns for '_CS_PATH'.
357 /* In GLib we put . last, for security, and don't use the
358 * unportable confstr(); UNIX98 does not actually specify
359 * what to search if PATH is unset. POSIX may, dunno.
362 path = "/bin:/usr/bin:.";
365 n = GetModuleFileNameW (NULL, wfilename, MAXPATHLEN);
366 if (n > 0 && n < MAXPATHLEN)
367 filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
369 n = GetSystemDirectoryW (wsysdir, MAXPATHLEN);
370 if (n > 0 && n < MAXPATHLEN)
371 sysdir = g_utf16_to_utf8 (wsysdir, -1, NULL, NULL, NULL);
373 n = GetWindowsDirectoryW (wwindir, MAXPATHLEN);
374 if (n > 0 && n < MAXPATHLEN)
375 windir = g_utf16_to_utf8 (wwindir, -1, NULL, NULL, NULL);
379 appdir = g_path_get_dirname (filename);
383 path = g_strdup (path);
387 const gchar *tem = path;
388 path = g_strconcat (windir, ";", path, NULL);
389 g_free ((gchar *) tem);
395 const gchar *tem = path;
396 path = g_strconcat (sysdir, ";", path, NULL);
397 g_free ((gchar *) tem);
402 const gchar *tem = path;
403 path = g_strconcat (".;", path, NULL);
404 g_free ((gchar *) tem);
409 const gchar *tem = path;
410 path = g_strconcat (appdir, ";", path, NULL);
411 g_free ((gchar *) tem);
418 len = strlen (program) + 1;
419 pathlen = strlen (path);
420 freeme = name = g_malloc (pathlen + len + 1);
422 /* Copy the file name at the top, including '\0' */
423 memcpy (name + pathlen + 1, program, len);
424 name = name + pathlen;
425 /* And add the slash before the filename */
426 *name = G_DIR_SEPARATOR;
434 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
437 /* Two adjacent colons, or a colon at the beginning or the end
438 * of 'PATH' means to search the current directory.
442 startp = memcpy (name - (p - path), path, p - path);
444 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE) &&
445 !g_file_test (startp, G_FILE_TEST_IS_DIR))
448 ret = g_strdup (startp);
451 g_free ((gchar *) path_copy);
456 while (*p++ != '\0');
460 g_free ((gchar *) path_copy);
466 /* The functions below are defined this way for compatibility reasons.
467 * See the note in gutils.h.
472 * @mask: a #gulong containing flags
473 * @nth_bit: the index of the bit to start the search from
475 * Find the position of the first bit set in @mask, searching
476 * from (but not including) @nth_bit upwards. Bits are numbered
477 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
478 * usually). To start searching from the 0th bit, set @nth_bit to -1.
480 * Returns: the index of the first bit set which is higher than @nth_bit, or -1
481 * if no higher bits are set
484 (g_bit_nth_lsf) (gulong mask,
487 return g_bit_nth_lsf_impl (mask, nth_bit);
492 * @mask: a #gulong containing flags
493 * @nth_bit: the index of the bit to start the search from
495 * Find the position of the first bit set in @mask, searching
496 * from (but not including) @nth_bit downwards. Bits are numbered
497 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
498 * usually). To start searching from the last bit, set @nth_bit to
499 * -1 or GLIB_SIZEOF_LONG * 8.
501 * Returns: the index of the first bit set which is lower than @nth_bit, or -1
502 * if no lower bits are set
505 (g_bit_nth_msf) (gulong mask,
508 return g_bit_nth_msf_impl (mask, nth_bit);
516 * Gets the number of bits used to hold @number,
517 * e.g. if @number is 4, 3 bits are needed.
519 * Returns: the number of bits used to hold @number
522 (g_bit_storage) (gulong number)
524 return g_bit_storage_impl (number);
527 G_LOCK_DEFINE_STATIC (g_utils_global);
536 /* These must all be read/written with @g_utils_global held. */
537 static gchar *g_user_data_dir = NULL;
538 static gchar **g_system_data_dirs = NULL;
539 static gchar *g_user_cache_dir = NULL;
540 static gchar *g_user_config_dir = NULL;
541 static gchar *g_user_runtime_dir = NULL;
542 static gchar **g_system_config_dirs = NULL;
543 static gchar **g_user_special_dirs = NULL;
545 /* fifteen minutes of fame for everybody */
546 #define G_USER_DIRS_EXPIRE 15 * 60
551 get_special_folder (int csidl)
553 wchar_t path[MAX_PATH+1];
555 LPITEMIDLIST pidl = NULL;
557 gchar *retval = NULL;
559 hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
562 b = SHGetPathFromIDListW (pidl, path);
564 retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
565 CoTaskMemFree (pidl);
571 get_windows_directory_root (void)
573 wchar_t wwindowsdir[MAX_PATH];
575 if (GetWindowsDirectoryW (wwindowsdir, G_N_ELEMENTS (wwindowsdir)))
577 /* Usually X:\Windows, but in terminal server environments
578 * might be an UNC path, AFAIK.
580 char *windowsdir = g_utf16_to_utf8 (wwindowsdir, -1, NULL, NULL, NULL);
583 if (windowsdir == NULL)
584 return g_strdup ("C:\\");
586 p = (char *) g_path_skip_root (windowsdir);
587 if (G_IS_DIR_SEPARATOR (p[-1]) && p[-2] != ':')
593 return g_strdup ("C:\\");
598 /* HOLDS: g_utils_global_lock */
599 static UserDatabaseEntry *
600 g_get_user_database_entry (void)
602 static UserDatabaseEntry *entry;
604 if (g_once_init_enter (&entry))
606 static UserDatabaseEntry e;
610 struct passwd *pw = NULL;
611 gpointer buffer = NULL;
615 # if defined (HAVE_GETPWUID_R)
617 # ifdef _SC_GETPW_R_SIZE_MAX
618 /* This reurns the maximum length */
619 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
623 # else /* _SC_GETPW_R_SIZE_MAX */
625 # endif /* _SC_GETPW_R_SIZE_MAX */
627 logname = (gchar *) g_getenv ("LOGNAME");
632 /* we allocate 6 extra bytes to work around a bug in
633 * Mac OS < 10.3. See #156446
635 buffer = g_malloc (bufsize + 6);
639 error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
640 if (!pw || (pw->pw_uid != getuid ())) {
641 /* LOGNAME is lying, fall back to looking up the uid */
642 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
645 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
647 error = error < 0 ? errno : error;
651 /* we bail out prematurely if the user id can't be found
652 * (should be pretty rare case actually), or if the buffer
653 * should be sufficiently big and lookups are still not
656 if (error == 0 || error == ENOENT)
658 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
662 if (bufsize > 32 * 1024)
664 g_warning ("getpwuid_r(): failed due to: %s.",
673 # endif /* HAVE_GETPWUID_R */
677 pw = getpwuid (getuid ());
681 e.user_name = g_strdup (pw->pw_name);
684 if (pw->pw_gecos && *pw->pw_gecos != '\0' && pw->pw_name)
686 gchar **gecos_fields;
689 /* split the gecos field and substitute '&' */
690 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
691 name_parts = g_strsplit (gecos_fields[0], "&", 0);
692 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
693 e.real_name = g_strjoinv (pw->pw_name, name_parts);
694 g_strfreev (gecos_fields);
695 g_strfreev (name_parts);
700 e.home_dir = g_strdup (pw->pw_dir);
705 #endif /* G_OS_UNIX */
710 wchar_t buffer[UNLEN+1];
712 if (GetUserNameW (buffer, (LPDWORD) &len))
714 e.user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
715 e.real_name = g_strdup (e.user_name);
718 #endif /* G_OS_WIN32 */
721 e.user_name = g_strdup ("somebody");
723 e.real_name = g_strdup ("Unknown");
725 g_once_init_leave (&entry, &e);
734 * Gets the user name of the current user. The encoding of the returned
735 * string is system-defined. On UNIX, it might be the preferred file name
736 * encoding, or something else, and there is no guarantee that it is even
737 * consistent on a machine. On Windows, it is always UTF-8.
739 * Returns: (type filename) (transfer none): the user name of the current user.
742 g_get_user_name (void)
744 UserDatabaseEntry *entry;
746 entry = g_get_user_database_entry ();
748 return entry->user_name;
754 * Gets the real name of the user. This usually comes from the user's
755 * entry in the `passwd` file. The encoding of the returned string is
756 * system-defined. (On Windows, it is, however, always UTF-8.) If the
757 * real user name cannot be determined, the string "Unknown" is
760 * Returns: (type filename) (transfer none): the user's real name.
763 g_get_real_name (void)
765 UserDatabaseEntry *entry;
767 entry = g_get_user_database_entry ();
769 return entry->real_name;
772 /* Protected by @g_utils_global_lock. */
773 static gchar *g_home_dir = NULL; /* (owned) (nullable before initialised) */
776 g_build_home_dir (void)
780 /* We first check HOME and use it if it is set */
781 home_dir = g_strdup (g_getenv ("HOME"));
784 /* Only believe HOME if it is an absolute path and exists.
786 * We only do this check on Windows for a couple of reasons.
787 * Historically, we only did it there because we used to ignore $HOME
788 * on UNIX. There are concerns about enabling it now on UNIX because
789 * of things like autofs. In short, if the user has a bogus value in
790 * $HOME then they get what they pay for...
792 if (home_dir != NULL)
794 if (!(g_path_is_absolute (home_dir) &&
795 g_file_test (home_dir, G_FILE_TEST_IS_DIR)))
796 g_clear_pointer (&home_dir, g_free);
799 /* In case HOME is Unix-style (it happens), convert it to
802 if (home_dir != NULL)
805 while ((p = strchr (home_dir, '/')) != NULL)
809 if (home_dir == NULL)
811 /* USERPROFILE is probably the closest equivalent to $HOME? */
812 if (g_getenv ("USERPROFILE") != NULL)
813 home_dir = g_strdup (g_getenv ("USERPROFILE"));
816 if (home_dir == NULL)
817 home_dir = get_special_folder (CSIDL_PROFILE);
819 if (home_dir == NULL)
820 home_dir = get_windows_directory_root ();
821 #endif /* G_OS_WIN32 */
823 if (home_dir == NULL)
825 /* If we didn't get it from any of those methods, we will have
826 * to read the user database entry.
828 UserDatabaseEntry *entry = g_get_user_database_entry ();
829 home_dir = g_strdup (entry->home_dir);
832 /* If we have been denied access to /etc/passwd (for example, by an
833 * overly-zealous LSM), make up a junk value. The return value at this
834 * point is explicitly documented as ‘undefined’. */
835 if (home_dir == NULL)
837 g_warning ("Could not find home directory: $HOME is not set, and "
838 "user database could not be read.");
839 home_dir = g_strdup ("/");
842 return g_steal_pointer (&home_dir);
848 * Gets the current user's home directory.
850 * As with most UNIX tools, this function will return the value of the
851 * `HOME` environment variable if it is set to an existing absolute path
852 * name, falling back to the `passwd` file in the case that it is unset.
854 * If the path given in `HOME` is non-absolute, does not exist, or is
855 * not a directory, the result is undefined.
857 * Before version 2.36 this function would ignore the `HOME` environment
858 * variable, taking the value from the `passwd` database instead. This was
859 * changed to increase the compatibility of GLib with other programs (and
860 * the XDG basedir specification) and to increase testability of programs
861 * based on GLib (by making it easier to run them from test frameworks).
863 * If your program has a strong requirement for either the new or the
864 * old behaviour (and if you don't wish to increase your GLib
865 * dependency to ensure that the new behaviour is in effect) then you
866 * should either directly check the `HOME` environment variable yourself
867 * or unset it before calling any functions in GLib.
869 * Returns: (type filename) (transfer none): the current user's home directory
872 g_get_home_dir (void)
874 const gchar *home_dir;
876 G_LOCK (g_utils_global);
878 if (g_home_dir == NULL)
879 g_home_dir = g_build_home_dir ();
880 home_dir = g_home_dir;
882 G_UNLOCK (g_utils_global);
890 * Gets the directory to use for temporary files.
892 * On UNIX, this is taken from the `TMPDIR` environment variable.
893 * If the variable is not set, `P_tmpdir` is
894 * used, as defined by the system C library. Failing that, a
895 * hard-coded default of "/tmp" is returned.
897 * On Windows, the `TEMP` environment variable is used, with the
898 * root directory of the Windows installation (eg: "C:\") used
901 * The encoding of the returned string is system-defined. On Windows,
902 * it is always UTF-8. The return value is never %NULL or the empty
905 * Returns: (type filename) (transfer none): the directory to use for temporary files.
910 static gchar *tmp_dir;
912 if (g_once_init_enter (&tmp_dir))
917 tmp = g_strdup (g_getenv ("TEMP"));
919 if (tmp == NULL || *tmp == '\0')
922 tmp = get_windows_directory_root ();
924 #else /* G_OS_WIN32 */
925 tmp = g_strdup (g_getenv ("TMPDIR"));
928 if (tmp == NULL || *tmp == '\0')
932 tmp = g_strdup (P_tmpdir);
934 if (k > 1 && G_IS_DIR_SEPARATOR (tmp[k - 1]))
937 #endif /* P_tmpdir */
939 if (tmp == NULL || *tmp == '\0')
942 tmp = g_strdup ("/tmp");
944 #endif /* !G_OS_WIN32 */
946 g_once_init_leave (&tmp_dir, tmp);
955 * Return a name for the machine.
957 * The returned name is not necessarily a fully-qualified domain name,
958 * or even present in DNS or some other name service at all. It need
959 * not even be unique on your local network or site, but usually it
960 * is. Callers should not rely on the return value having any specific
961 * properties like uniqueness for security purposes. Even if the name
962 * of the machine is changed while an application is running, the
963 * return value from this function does not change. The returned
964 * string is owned by GLib and should not be modified or freed. If no
965 * name can be determined, a default fixed string "localhost" is
968 * The encoding of the returned string is UTF-8.
970 * Returns: (transfer none): the host name of the machine.
975 g_get_host_name (void)
977 static gchar *hostname;
979 if (g_once_init_enter (&hostname))
986 /* The number 256 * 256 is taken from the value of _POSIX_HOST_NAME_MAX,
987 * which is 255. Since we use _POSIX_HOST_NAME_MAX + 1 (= 256) in the
988 * fallback case, we pick 256 * 256 as the size of the larger buffer here.
989 * It should be large enough. It doesn't looks reasonable to name a host
990 * with a string that is longer than 64 KiB.
992 const gsize size_large = (gsize) 256 * 256;
995 #ifdef _SC_HOST_NAME_MAX
999 max = sysconf (_SC_HOST_NAME_MAX);
1000 if (max > 0 && (gsize) max <= G_MAXSIZE - 1)
1001 size = (gsize) max + 1;
1003 #ifdef HOST_NAME_MAX
1004 size = HOST_NAME_MAX + 1;
1006 size = _POSIX_HOST_NAME_MAX + 1;
1007 #endif /* HOST_NAME_MAX */
1010 /* Fallback to some reasonable value */
1012 #endif /* _SC_HOST_NAME_MAX */
1013 tmp = g_malloc (size);
1014 failed = (gethostname (tmp, size) == -1);
1015 if (failed && size < size_large)
1017 /* Try again with a larger buffer if 'size' may be too small. */
1019 tmp = g_malloc (size_large);
1020 failed = (gethostname (tmp, size_large) == -1);
1024 g_clear_pointer (&tmp, g_free);
1027 wchar_t tmp[MAX_COMPUTERNAME_LENGTH + 1];
1028 DWORD size = sizeof (tmp) / sizeof (tmp[0]);
1029 failed = (!GetComputerNameW (tmp, &size));
1031 utmp = g_utf16_to_utf8 (tmp, size, NULL, NULL, NULL);
1036 g_once_init_leave (&hostname, failed ? g_strdup ("localhost") : utmp);
1042 G_LOCK_DEFINE_STATIC (g_prgname);
1043 static gchar *g_prgname = NULL;
1048 * Gets the name of the program. This name should not be localized,
1049 * in contrast to g_get_application_name().
1051 * If you are using #GApplication the program name is set in
1052 * g_application_run(). In case of GDK or GTK+ it is set in
1053 * gdk_init(), which is called by gtk_init() and the
1054 * #GtkApplication::startup handler. The program name is found by
1055 * taking the last component of @argv[0].
1057 * Returns: (nullable) (transfer none): the name of the program,
1058 * or %NULL if it has not been set yet. The returned string belongs
1059 * to GLib and must not be modified or freed.
1062 g_get_prgname (void)
1068 G_UNLOCK (g_prgname);
1075 * @prgname: the name of the program.
1077 * Sets the name of the program. This name should not be localized,
1078 * in contrast to g_set_application_name().
1080 * If you are using #GApplication the program name is set in
1081 * g_application_run(). In case of GDK or GTK+ it is set in
1082 * gdk_init(), which is called by gtk_init() and the
1083 * #GtkApplication::startup handler. The program name is found by
1084 * taking the last component of @argv[0].
1086 * Note that for thread-safety reasons this function can only be called once.
1089 g_set_prgname (const gchar *prgname)
1093 g_prgname = g_strdup (prgname);
1094 G_UNLOCK (g_prgname);
1097 G_LOCK_DEFINE_STATIC (g_application_name);
1098 static gchar *g_application_name = NULL;
1101 * g_get_application_name:
1103 * Gets a human-readable name for the application, as set by
1104 * g_set_application_name(). This name should be localized if
1105 * possible, and is intended for display to the user. Contrast with
1106 * g_get_prgname(), which gets a non-localized name. If
1107 * g_set_application_name() has not been called, returns the result of
1108 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1111 * Returns: (transfer none) (nullable): human-readable application
1112 * name. May return %NULL
1117 g_get_application_name (void)
1121 G_LOCK (g_application_name);
1122 retval = g_application_name;
1123 G_UNLOCK (g_application_name);
1126 return g_get_prgname ();
1132 * g_set_application_name:
1133 * @application_name: localized name of the application
1135 * Sets a human-readable name for the application. This name should be
1136 * localized if possible, and is intended for display to the user.
1137 * Contrast with g_set_prgname(), which sets a non-localized name.
1138 * g_set_prgname() will be called automatically by gtk_init(),
1139 * but g_set_application_name() will not.
1141 * Note that for thread safety reasons, this function can only
1144 * The application name will be used in contexts such as error messages,
1145 * or when displaying an application's name in the task list.
1150 g_set_application_name (const gchar *application_name)
1152 gboolean already_set = FALSE;
1154 G_LOCK (g_application_name);
1155 if (g_application_name)
1158 g_application_name = g_strdup (application_name);
1159 G_UNLOCK (g_application_name);
1162 g_warning ("g_set_application_name() called multiple times");
1166 /* For the past versions we can just
1167 * hardcode all the names.
1169 static const struct winver
1174 const char *version;
1175 const char *spversion;
1179 {6, 1, 1, "7", " SP1"},
1181 {6, 0, 2, "Vista", " SP2"},
1182 {6, 0, 1, "Vista", " SP1"},
1183 {6, 0, 0, "Vista", ""},
1184 {5, 1, 3, "XP", " SP3"},
1185 {5, 1, 2, "XP", " SP2"},
1186 {5, 1, 1, "XP", " SP1"},
1187 {5, 1, 0, "XP", ""},
1188 {0, 0, 0, NULL, NULL},
1192 get_registry_str (HKEY root_key, const wchar_t *path, const wchar_t *value_name)
1195 DWORD req_value_data_size;
1196 DWORD req_value_data_size2;
1199 DWORD value_type_w2;
1200 char *req_value_data;
1203 status = RegOpenKeyExW (root_key, path, 0, KEY_READ, &key_handle);
1204 if (status != ERROR_SUCCESS)
1207 req_value_data_size = 0;
1208 status = RegQueryValueExW (key_handle,
1213 &req_value_data_size);
1215 if (status != ERROR_MORE_DATA && status != ERROR_SUCCESS)
1217 RegCloseKey (key_handle);
1222 req_value_data = g_malloc (req_value_data_size);
1223 req_value_data_size2 = req_value_data_size;
1225 status = RegQueryValueExW (key_handle,
1229 (gpointer) req_value_data,
1230 &req_value_data_size2);
1234 if (status == ERROR_SUCCESS && value_type_w2 == REG_SZ)
1235 result = g_utf16_to_utf8 ((gunichar2 *) req_value_data,
1236 req_value_data_size / sizeof (gunichar2),
1241 g_free (req_value_data);
1242 RegCloseKey (key_handle);
1247 /* Windows 8.1 can be either plain or with Update 1,
1248 * depending on its build number (9200 or 9600).
1251 get_windows_8_1_update (void)
1253 gchar *current_build;
1254 gchar *result = NULL;
1256 current_build = get_registry_str (HKEY_LOCAL_MACHINE,
1260 L"\\CurrentVersion",
1263 if (current_build != NULL)
1266 long build = wcstol ((const wchar_t *) current_build, &end, 10);
1268 if (build <= INT_MAX &&
1274 result = g_strdup ("Update 1");
1278 g_clear_pointer (¤t_build, g_free);
1284 get_windows_version (gboolean with_windows)
1286 GString *version = g_string_new (NULL);
1288 if (g_win32_check_windows_version (10, 0, 0, G_WIN32_OS_ANY))
1290 gchar *win10_release;
1292 g_string_append (version, "10");
1294 if (!g_win32_check_windows_version (10, 0, 0, G_WIN32_OS_WORKSTATION))
1295 g_string_append (version, " Server");
1297 /* Windows 10 is identified by its release number, such as
1298 * 1511, 1607, 1703, 1709, 1803, 1809 or 1903.
1299 * The first version of Windows 10 has no release number.
1301 win10_release = get_registry_str (HKEY_LOCAL_MACHINE,
1305 L"\\CurrentVersion",
1308 if (win10_release != NULL)
1309 g_string_append_printf (version, " %s", win10_release);
1311 g_free (win10_release);
1313 else if (g_win32_check_windows_version (6, 3, 0, G_WIN32_OS_ANY))
1315 gchar *win81_update;
1317 g_string_append (version, "8.1");
1319 if (!g_win32_check_windows_version (6, 3, 0, G_WIN32_OS_WORKSTATION))
1320 g_string_append (version, " Server");
1322 win81_update = get_windows_8_1_update ();
1324 if (win81_update != NULL)
1325 g_string_append_printf (version, " %s", win81_update);
1327 g_free (win81_update);
1333 for (i = 0; versions[i].major > 0; i++)
1335 if (!g_win32_check_windows_version (versions[i].major, versions[i].minor, versions[i].sp, G_WIN32_OS_ANY))
1338 g_string_append (version, versions[i].version);
1340 if (!g_win32_check_windows_version (versions[i].major, versions[i].minor, versions[i].sp, G_WIN32_OS_WORKSTATION))
1341 g_string_append (version, " Server");
1343 g_string_append (version, versions[i].spversion);
1347 if (version->len == 0)
1349 g_string_free (version, TRUE);
1355 g_string_prepend (version, "Windows ");
1357 return g_string_free (version, FALSE);
1363 get_os_info_from_os_release (const gchar *key_name,
1364 const gchar *buffer)
1369 gchar *result = NULL;
1371 lines = g_strsplit (buffer, "\n", -1);
1372 prefix = g_strdup_printf ("%s=", key_name);
1373 for (i = 0; lines[i] != NULL; i++)
1375 const gchar *line = lines[i];
1378 if (g_str_has_prefix (line, prefix))
1380 value = line + strlen (prefix);
1381 result = g_shell_unquote (value, NULL);
1383 result = g_strdup (value);
1391 /* Default values in spec */
1394 if (g_str_equal (key_name, G_OS_INFO_KEY_NAME))
1395 return g_strdup ("Linux");
1396 if (g_str_equal (key_name, G_OS_INFO_KEY_ID))
1397 return g_strdup ("linux");
1398 if (g_str_equal (key_name, G_OS_INFO_KEY_PRETTY_NAME))
1399 return g_strdup ("Linux");
1403 return g_steal_pointer (&result);
1407 get_os_info_from_uname (const gchar *key_name)
1409 struct utsname info;
1411 if (uname (&info) == -1)
1414 if (strcmp (key_name, G_OS_INFO_KEY_NAME) == 0)
1415 return g_strdup (info.sysname);
1416 else if (strcmp (key_name, G_OS_INFO_KEY_VERSION) == 0)
1417 return g_strdup (info.release);
1418 else if (strcmp (key_name, G_OS_INFO_KEY_PRETTY_NAME) == 0)
1419 return g_strdup_printf ("%s %s", info.sysname, info.release);
1420 else if (strcmp (key_name, G_OS_INFO_KEY_ID) == 0)
1422 gchar *result = g_ascii_strdown (info.sysname, -1);
1424 g_strcanon (result, "abcdefghijklmnopqrstuvwxyz0123456789_-.", '_');
1425 return g_steal_pointer (&result);
1427 else if (strcmp (key_name, G_OS_INFO_KEY_VERSION_ID) == 0)
1429 /* We attempt to convert the version string to the format returned by
1430 * config.guess, which is the script used to generate target triplets
1431 * in GNU autotools. There are a lot of rules in the script. We only
1432 * implement a few rules which are easy to understand here.
1434 * config.guess can be found at https://savannah.gnu.org/projects/config.
1438 if (strcmp (info.sysname, "NetBSD") == 0)
1440 /* sed -e 's,[-_].*,,' */
1441 gssize len = G_MAXSSIZE;
1444 if ((c = strchr (info.release, '-')) != NULL)
1445 len = MIN (len, c - info.release);
1446 if ((c = strchr (info.release, '_')) != NULL)
1447 len = MIN (len, c - info.release);
1448 if (len == G_MAXSSIZE)
1451 result = g_ascii_strdown (info.release, len);
1453 else if (strcmp (info.sysname, "GNU") == 0)
1455 /* sed -e 's,/.*$,,' */
1457 const gchar *c = strchr (info.release, '/');
1460 len = c - info.release;
1462 result = g_ascii_strdown (info.release, len);
1464 else if (g_str_has_prefix (info.sysname, "GNU/") ||
1465 strcmp (info.sysname, "FreeBSD") == 0 ||
1466 strcmp (info.sysname, "DragonFly") == 0)
1468 /* sed -e 's,[-(].*,,' */
1469 gssize len = G_MAXSSIZE;
1472 if ((c = strchr (info.release, '-')) != NULL)
1473 len = MIN (len, c - info.release);
1474 if ((c = strchr (info.release, '(')) != NULL)
1475 len = MIN (len, c - info.release);
1476 if (len == G_MAXSSIZE)
1479 result = g_ascii_strdown (info.release, len);
1482 result = g_ascii_strdown (info.release, -1);
1484 g_strcanon (result, "abcdefghijklmnopqrstuvwxyz0123456789_-.", '_');
1485 return g_steal_pointer (&result);
1494 * @key_name: a key for the OS info being requested, for example %G_OS_INFO_KEY_NAME.
1496 * Get information about the operating system.
1498 * On Linux this comes from the `/etc/os-release` file. On other systems, it may
1499 * come from a variety of sources. You can either use the standard key names
1500 * like %G_OS_INFO_KEY_NAME or pass any UTF-8 string key name. For example,
1501 * `/etc/os-release` provides a number of other less commonly used values that may
1502 * be useful. No key is guaranteed to be provided, so the caller should always
1503 * check if the result is %NULL.
1505 * Returns: (nullable): The associated value for the requested key or %NULL if
1506 * this information is not provided.
1511 g_get_os_info (const gchar *key_name)
1513 #if defined (__APPLE__)
1514 if (g_strcmp0 (key_name, G_OS_INFO_KEY_NAME) == 0)
1515 return g_strdup ("macOS");
1518 #elif defined (G_OS_UNIX)
1519 const gchar * const os_release_files[] = { "/etc/os-release", "/usr/lib/os-release" };
1521 gchar *buffer = NULL;
1522 gchar *result = NULL;
1524 g_return_val_if_fail (key_name != NULL, NULL);
1526 for (i = 0; i < G_N_ELEMENTS (os_release_files); i++)
1528 GError *error = NULL;
1529 gboolean file_missing;
1531 if (g_file_get_contents (os_release_files[i], &buffer, NULL, &error))
1534 file_missing = g_error_matches (error, G_FILE_ERROR, G_FILE_ERROR_NOENT);
1535 g_clear_error (&error);
1542 result = get_os_info_from_os_release (key_name, buffer);
1544 result = get_os_info_from_uname (key_name);
1547 return g_steal_pointer (&result);
1548 #elif defined (G_OS_WIN32)
1549 if (g_strcmp0 (key_name, G_OS_INFO_KEY_NAME) == 0)
1550 return g_strdup ("Windows");
1551 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_ID) == 0)
1552 return g_strdup ("windows");
1553 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_PRETTY_NAME) == 0)
1554 /* Windows XP SP2 or Windows 10 1903 or Windows 7 Server SP1 */
1555 return get_windows_version (TRUE);
1556 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_VERSION) == 0)
1557 /* XP SP2 or 10 1903 or 7 Server SP1 */
1558 return get_windows_version (FALSE);
1559 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_VERSION_ID) == 0)
1561 /* xp_sp2 or 10_1903 or 7_server_sp1 */
1563 gchar *version = get_windows_version (FALSE);
1565 if (version == NULL)
1568 result = g_ascii_strdown (version, -1);
1571 return g_strcanon (result, "abcdefghijklmnopqrstuvwxyz0123456789_-.", '_');
1573 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_HOME_URL) == 0)
1574 return g_strdup ("https://microsoft.com/windows/");
1575 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_DOCUMENTATION_URL) == 0)
1576 return g_strdup ("https://docs.microsoft.com/");
1577 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_SUPPORT_URL) == 0)
1578 return g_strdup ("https://support.microsoft.com/");
1579 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_BUG_REPORT_URL) == 0)
1580 return g_strdup ("https://support.microsoft.com/contactus/");
1581 else if (g_strcmp0 (key_name, G_OS_INFO_KEY_PRIVACY_POLICY_URL) == 0)
1582 return g_strdup ("https://privacy.microsoft.com/");
1588 /* Set @global_str to a copy of @new_value if it’s currently unset or has a
1589 * different value. If its current value matches @new_value, do nothing. If
1590 * replaced, we have to leak the old value as client code could still have
1591 * pointers to it. */
1593 set_str_if_different (gchar **global_str,
1595 const gchar *new_value)
1597 if (*global_str == NULL ||
1598 !g_str_equal (new_value, *global_str))
1600 g_debug ("g_set_user_dirs: Setting %s to %s", type, new_value);
1602 /* We have to leak the old value, as user code could be retaining pointers
1604 *global_str = g_strdup (new_value);
1609 set_strv_if_different (gchar ***global_strv,
1611 const gchar * const *new_value)
1613 if (*global_strv == NULL ||
1614 !g_strv_equal (new_value, (const gchar * const *) *global_strv))
1616 gchar *new_value_str = g_strjoinv (":", (gchar **) new_value);
1617 g_debug ("g_set_user_dirs: Setting %s to %s", type, new_value_str);
1618 g_free (new_value_str);
1620 /* We have to leak the old value, as user code could be retaining pointers
1622 *global_strv = g_strdupv ((gchar **) new_value);
1628 * @first_dir_type: Type of the first directory to set
1629 * @...: Value to set the first directory to, followed by additional type/value
1630 * pairs, followed by %NULL
1632 * Set one or more ‘user’ directories to custom values. This is intended to be
1633 * used by test code (particularly with the %G_TEST_OPTION_ISOLATE_DIRS option)
1634 * to override the values returned by the following functions, so that test
1635 * code can be run without touching an installed system and user data:
1637 * - g_get_home_dir() — use type `HOME`, pass a string
1638 * - g_get_user_cache_dir() — use type `XDG_CACHE_HOME`, pass a string
1639 * - g_get_system_config_dirs() — use type `XDG_CONFIG_DIRS`, pass a
1640 * %NULL-terminated string array
1641 * - g_get_user_config_dir() — use type `XDG_CONFIG_HOME`, pass a string
1642 * - g_get_system_data_dirs() — use type `XDG_DATA_DIRS`, pass a
1643 * %NULL-terminated string array
1644 * - g_get_user_data_dir() — use type `XDG_DATA_HOME`, pass a string
1645 * - g_get_user_runtime_dir() — use type `XDG_RUNTIME_DIR`, pass a string
1647 * The list must be terminated with a %NULL type. All of the values must be
1648 * non-%NULL — passing %NULL as a value won’t reset a directory. If a reference
1649 * to a directory from the calling environment needs to be kept, copy it before
1650 * the first call to g_set_user_dirs(). g_set_user_dirs() can be called multiple
1657 g_set_user_dirs (const gchar *first_dir_type,
1661 const gchar *dir_type;
1663 G_LOCK (g_utils_global);
1665 va_start (args, first_dir_type);
1667 for (dir_type = first_dir_type; dir_type != NULL; dir_type = va_arg (args, const gchar *))
1669 gconstpointer dir_value = va_arg (args, gconstpointer);
1670 g_assert (dir_value != NULL);
1672 if (g_str_equal (dir_type, "HOME"))
1673 set_str_if_different (&g_home_dir, dir_type, dir_value);
1674 else if (g_str_equal (dir_type, "XDG_CACHE_HOME"))
1675 set_str_if_different (&g_user_cache_dir, dir_type, dir_value);
1676 else if (g_str_equal (dir_type, "XDG_CONFIG_DIRS"))
1677 set_strv_if_different (&g_system_config_dirs, dir_type, dir_value);
1678 else if (g_str_equal (dir_type, "XDG_CONFIG_HOME"))
1679 set_str_if_different (&g_user_config_dir, dir_type, dir_value);
1680 else if (g_str_equal (dir_type, "XDG_DATA_DIRS"))
1681 set_strv_if_different (&g_system_data_dirs, dir_type, dir_value);
1682 else if (g_str_equal (dir_type, "XDG_DATA_HOME"))
1683 set_str_if_different (&g_user_data_dir, dir_type, dir_value);
1684 else if (g_str_equal (dir_type, "XDG_RUNTIME_DIR"))
1685 set_str_if_different (&g_user_runtime_dir, dir_type, dir_value);
1687 g_assert_not_reached ();
1692 G_UNLOCK (g_utils_global);
1696 g_build_user_data_dir (void)
1698 gchar *data_dir = NULL;
1699 const gchar *data_dir_env = g_getenv ("XDG_DATA_HOME");
1701 if (data_dir_env && data_dir_env[0])
1702 data_dir = g_strdup (data_dir_env);
1705 data_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
1707 if (!data_dir || !data_dir[0])
1709 gchar *home_dir = g_build_home_dir ();
1710 data_dir = g_build_filename (home_dir, ".local", "share", NULL);
1714 return g_steal_pointer (&data_dir);
1718 * g_get_user_data_dir:
1720 * Returns a base directory in which to access application data such
1721 * as icons that is customized for a particular user.
1723 * On UNIX platforms this is determined using the mechanisms described
1725 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1726 * In this case the directory retrieved will be `XDG_DATA_HOME`.
1728 * On Windows it follows XDG Base Directory Specification if `XDG_DATA_HOME`
1729 * is defined. If `XDG_DATA_HOME` is undefined, the folder to use for local (as
1730 * opposed to roaming) application data is used instead. See the
1731 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata).
1732 * Note that in this case on Windows it will be the same
1733 * as what g_get_user_config_dir() returns.
1735 * Returns: (type filename) (transfer none): a string owned by GLib that must
1736 * not be modified or freed.
1741 g_get_user_data_dir (void)
1743 const gchar *user_data_dir;
1745 G_LOCK (g_utils_global);
1747 if (g_user_data_dir == NULL)
1748 g_user_data_dir = g_build_user_data_dir ();
1749 user_data_dir = g_user_data_dir;
1751 G_UNLOCK (g_utils_global);
1753 return user_data_dir;
1757 g_build_user_config_dir (void)
1759 gchar *config_dir = NULL;
1760 const gchar *config_dir_env = g_getenv ("XDG_CONFIG_HOME");
1762 if (config_dir_env && config_dir_env[0])
1763 config_dir = g_strdup (config_dir_env);
1766 config_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
1768 if (!config_dir || !config_dir[0])
1770 gchar *home_dir = g_build_home_dir ();
1771 config_dir = g_build_filename (home_dir, ".config", NULL);
1775 return g_steal_pointer (&config_dir);
1779 * g_get_user_config_dir:
1781 * Returns a base directory in which to store user-specific application
1782 * configuration information such as user preferences and settings.
1784 * On UNIX platforms this is determined using the mechanisms described
1786 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1787 * In this case the directory retrieved will be `XDG_CONFIG_HOME`.
1789 * On Windows it follows XDG Base Directory Specification if `XDG_CONFIG_HOME` is defined.
1790 * If `XDG_CONFIG_HOME` is undefined, the folder to use for local (as opposed
1791 * to roaming) application data is used instead. See the
1792 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata).
1793 * Note that in this case on Windows it will be the same
1794 * as what g_get_user_data_dir() returns.
1796 * Returns: (type filename) (transfer none): a string owned by GLib that
1797 * must not be modified or freed.
1801 g_get_user_config_dir (void)
1803 const gchar *user_config_dir;
1805 G_LOCK (g_utils_global);
1807 if (g_user_config_dir == NULL)
1808 g_user_config_dir = g_build_user_config_dir ();
1809 user_config_dir = g_user_config_dir;
1811 G_UNLOCK (g_utils_global);
1813 return user_config_dir;
1817 g_build_user_cache_dir (void)
1819 gchar *cache_dir = NULL;
1820 const gchar *cache_dir_env = g_getenv ("XDG_CACHE_HOME");
1822 if (cache_dir_env && cache_dir_env[0])
1823 cache_dir = g_strdup (cache_dir_env);
1826 cache_dir = get_special_folder (CSIDL_INTERNET_CACHE);
1828 if (!cache_dir || !cache_dir[0])
1830 gchar *home_dir = g_build_home_dir ();
1831 cache_dir = g_build_filename (home_dir, ".cache", NULL);
1835 return g_steal_pointer (&cache_dir);
1839 * g_get_user_cache_dir:
1841 * Returns a base directory in which to store non-essential, cached
1842 * data specific to particular user.
1844 * On UNIX platforms this is determined using the mechanisms described
1846 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1847 * In this case the directory retrieved will be `XDG_CACHE_HOME`.
1849 * On Windows it follows XDG Base Directory Specification if `XDG_CACHE_HOME` is defined.
1850 * If `XDG_CACHE_HOME` is undefined, the directory that serves as a common
1851 * repository for temporary Internet files is used instead. A typical path is
1852 * `C:\Documents and Settings\username\Local Settings\Temporary Internet Files`.
1853 * See the [documentation for `CSIDL_INTERNET_CACHE`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_internet_cache).
1855 * Returns: (type filename) (transfer none): a string owned by GLib that
1856 * must not be modified or freed.
1860 g_get_user_cache_dir (void)
1862 const gchar *user_cache_dir;
1864 G_LOCK (g_utils_global);
1866 if (g_user_cache_dir == NULL)
1867 g_user_cache_dir = g_build_user_cache_dir ();
1868 user_cache_dir = g_user_cache_dir;
1870 G_UNLOCK (g_utils_global);
1872 return user_cache_dir;
1876 g_build_user_runtime_dir (void)
1878 gchar *runtime_dir = NULL;
1879 const gchar *runtime_dir_env = g_getenv ("XDG_RUNTIME_DIR");
1881 if (runtime_dir_env && runtime_dir_env[0])
1882 runtime_dir = g_strdup (runtime_dir_env);
1885 runtime_dir = g_build_user_cache_dir ();
1887 /* The user should be able to rely on the directory existing
1888 * when the function returns. Probably it already does, but
1889 * let's make sure. Just do mkdir() directly since it will be
1890 * no more expensive than a stat() in the case that the
1891 * directory already exists and is a lot easier.
1893 * $XDG_CACHE_HOME is probably ~/.cache/ so as long as $HOME
1894 * exists this will work. If the user changed $XDG_CACHE_HOME
1895 * then they can make sure that it exists...
1897 (void) g_mkdir (runtime_dir, 0700);
1900 return g_steal_pointer (&runtime_dir);
1904 * g_get_user_runtime_dir:
1906 * Returns a directory that is unique to the current user on the local
1909 * This is determined using the mechanisms described
1911 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1912 * This is the directory
1913 * specified in the `XDG_RUNTIME_DIR` environment variable.
1914 * In the case that this variable is not set, we return the value of
1915 * g_get_user_cache_dir(), after verifying that it exists.
1917 * Returns: (type filename): a string owned by GLib that must not be
1918 * modified or freed.
1923 g_get_user_runtime_dir (void)
1925 const gchar *user_runtime_dir;
1927 G_LOCK (g_utils_global);
1929 if (g_user_runtime_dir == NULL)
1930 g_user_runtime_dir = g_build_user_runtime_dir ();
1931 user_runtime_dir = g_user_runtime_dir;
1933 G_UNLOCK (g_utils_global);
1935 return user_runtime_dir;
1940 /* Implemented in gutils-macos.m */
1941 void load_user_special_dirs_macos (gchar **table);
1944 load_user_special_dirs (void)
1946 load_user_special_dirs_macos (g_user_special_dirs);
1949 #elif defined(G_OS_WIN32)
1952 load_user_special_dirs (void)
1954 typedef HRESULT (WINAPI *t_SHGetKnownFolderPath) (const GUID *rfid,
1958 t_SHGetKnownFolderPath p_SHGetKnownFolderPath;
1960 static const GUID FOLDERID_Downloads =
1961 { 0x374de290, 0x123f, 0x4565, { 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b } };
1962 static const GUID FOLDERID_Public =
1963 { 0xDFDF76A2, 0xC82A, 0x4D63, { 0x90, 0x6A, 0x56, 0x44, 0xAC, 0x45, 0x73, 0x85 } };
1967 p_SHGetKnownFolderPath = (t_SHGetKnownFolderPath) GetProcAddress (GetModuleHandleW (L"shell32.dll"),
1968 "SHGetKnownFolderPath");
1970 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
1971 g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = get_special_folder (CSIDL_PERSONAL);
1973 if (p_SHGetKnownFolderPath == NULL)
1975 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
1980 (*p_SHGetKnownFolderPath) (&FOLDERID_Downloads, 0, NULL, &wcp);
1983 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
1984 if (g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] == NULL)
1985 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
1986 CoTaskMemFree (wcp);
1989 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
1992 g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = get_special_folder (CSIDL_MYMUSIC);
1993 g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = get_special_folder (CSIDL_MYPICTURES);
1995 if (p_SHGetKnownFolderPath == NULL)
1998 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2003 (*p_SHGetKnownFolderPath) (&FOLDERID_Public, 0, NULL, &wcp);
2006 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
2007 if (g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] == NULL)
2008 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2009 CoTaskMemFree (wcp);
2012 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2015 g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = get_special_folder (CSIDL_TEMPLATES);
2016 g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = get_special_folder (CSIDL_MYVIDEO);
2019 #else /* default is unix */
2021 /* adapted from xdg-user-dir-lookup.c
2023 * Copyright (C) 2007 Red Hat Inc.
2025 * Permission is hereby granted, free of charge, to any person
2026 * obtaining a copy of this software and associated documentation files
2027 * (the "Software"), to deal in the Software without restriction,
2028 * including without limitation the rights to use, copy, modify, merge,
2029 * publish, distribute, sublicense, and/or sell copies of the Software,
2030 * and to permit persons to whom the Software is furnished to do so,
2031 * subject to the following conditions:
2033 * The above copyright notice and this permission notice shall be
2034 * included in all copies or substantial portions of the Software.
2036 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2037 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2038 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2039 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2040 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2041 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2042 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2046 load_user_special_dirs (void)
2048 gchar *config_dir = NULL;
2054 config_dir = g_build_user_config_dir ();
2055 config_file = g_build_filename (config_dir,
2058 g_free (config_dir);
2060 if (!g_file_get_contents (config_file, &data, NULL, NULL))
2062 g_free (config_file);
2066 lines = g_strsplit (data, "\n", -1);
2067 n_lines = g_strv_length (lines);
2070 for (i = 0; i < n_lines; i++)
2072 gchar *buffer = lines[i];
2075 gboolean is_relative = FALSE;
2076 GUserDirectory directory;
2078 /* Remove newline at end */
2079 len = strlen (buffer);
2080 if (len > 0 && buffer[len - 1] == '\n')
2081 buffer[len - 1] = 0;
2084 while (*p == ' ' || *p == '\t')
2087 if (strncmp (p, "XDG_DESKTOP_DIR", strlen ("XDG_DESKTOP_DIR")) == 0)
2089 directory = G_USER_DIRECTORY_DESKTOP;
2090 p += strlen ("XDG_DESKTOP_DIR");
2092 else if (strncmp (p, "XDG_DOCUMENTS_DIR", strlen ("XDG_DOCUMENTS_DIR")) == 0)
2094 directory = G_USER_DIRECTORY_DOCUMENTS;
2095 p += strlen ("XDG_DOCUMENTS_DIR");
2097 else if (strncmp (p, "XDG_DOWNLOAD_DIR", strlen ("XDG_DOWNLOAD_DIR")) == 0)
2099 directory = G_USER_DIRECTORY_DOWNLOAD;
2100 p += strlen ("XDG_DOWNLOAD_DIR");
2102 else if (strncmp (p, "XDG_MUSIC_DIR", strlen ("XDG_MUSIC_DIR")) == 0)
2104 directory = G_USER_DIRECTORY_MUSIC;
2105 p += strlen ("XDG_MUSIC_DIR");
2107 else if (strncmp (p, "XDG_PICTURES_DIR", strlen ("XDG_PICTURES_DIR")) == 0)
2109 directory = G_USER_DIRECTORY_PICTURES;
2110 p += strlen ("XDG_PICTURES_DIR");
2112 else if (strncmp (p, "XDG_PUBLICSHARE_DIR", strlen ("XDG_PUBLICSHARE_DIR")) == 0)
2114 directory = G_USER_DIRECTORY_PUBLIC_SHARE;
2115 p += strlen ("XDG_PUBLICSHARE_DIR");
2117 else if (strncmp (p, "XDG_TEMPLATES_DIR", strlen ("XDG_TEMPLATES_DIR")) == 0)
2119 directory = G_USER_DIRECTORY_TEMPLATES;
2120 p += strlen ("XDG_TEMPLATES_DIR");
2122 else if (strncmp (p, "XDG_VIDEOS_DIR", strlen ("XDG_VIDEOS_DIR")) == 0)
2124 directory = G_USER_DIRECTORY_VIDEOS;
2125 p += strlen ("XDG_VIDEOS_DIR");
2130 while (*p == ' ' || *p == '\t')
2137 while (*p == ' ' || *p == '\t')
2144 if (strncmp (p, "$HOME", 5) == 0)
2152 d = strrchr (p, '"');
2159 /* remove trailing slashes */
2161 if (d[len - 1] == '/')
2166 gchar *home_dir = g_build_home_dir ();
2167 g_user_special_dirs[directory] = g_build_filename (home_dir, d, NULL);
2171 g_user_special_dirs[directory] = g_strdup (d);
2175 g_free (config_file);
2178 #endif /* platform-specific load_user_special_dirs implementations */
2182 * g_reload_user_special_dirs_cache:
2184 * Resets the cache used for g_get_user_special_dir(), so
2185 * that the latest on-disk version is used. Call this only
2186 * if you just changed the data on disk yourself.
2188 * Due to thread safety issues this may cause leaking of strings
2189 * that were previously returned from g_get_user_special_dir()
2190 * that can't be freed. We ensure to only leak the data for
2191 * the directories that actually changed value though.
2196 g_reload_user_special_dirs_cache (void)
2200 G_LOCK (g_utils_global);
2202 if (g_user_special_dirs != NULL)
2204 /* save a copy of the pointer, to check if some memory can be preserved */
2205 char **old_g_user_special_dirs = g_user_special_dirs;
2208 /* recreate and reload our cache */
2209 g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
2210 load_user_special_dirs ();
2212 /* only leak changed directories */
2213 for (i = 0; i < G_USER_N_DIRECTORIES; i++)
2215 old_val = old_g_user_special_dirs[i];
2216 if (g_user_special_dirs[i] == NULL)
2218 g_user_special_dirs[i] = old_val;
2220 else if (g_strcmp0 (old_val, g_user_special_dirs[i]) == 0)
2223 g_free (g_user_special_dirs[i]);
2224 g_user_special_dirs[i] = old_val;
2230 /* free the old array */
2231 g_free (old_g_user_special_dirs);
2234 G_UNLOCK (g_utils_global);
2238 * g_get_user_special_dir:
2239 * @directory: the logical id of special directory
2241 * Returns the full path of a special directory using its logical id.
2243 * On UNIX this is done using the XDG special user directories.
2244 * For compatibility with existing practise, %G_USER_DIRECTORY_DESKTOP
2245 * falls back to `$HOME/Desktop` when XDG special user directories have
2248 * Depending on the platform, the user might be able to change the path
2249 * of the special directory without requiring the session to restart; GLib
2250 * will not reflect any change once the special directories are loaded.
2252 * Returns: (type filename): the path to the specified special directory, or
2253 * %NULL if the logical id was not found. The returned string is owned by
2254 * GLib and should not be modified or freed.
2259 g_get_user_special_dir (GUserDirectory directory)
2261 const gchar *user_special_dir;
2263 g_return_val_if_fail (directory >= G_USER_DIRECTORY_DESKTOP &&
2264 directory < G_USER_N_DIRECTORIES, NULL);
2266 G_LOCK (g_utils_global);
2268 if (G_UNLIKELY (g_user_special_dirs == NULL))
2270 g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
2272 load_user_special_dirs ();
2274 /* Special-case desktop for historical compatibility */
2275 if (g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] == NULL)
2277 gchar *home_dir = g_build_home_dir ();
2278 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = g_build_filename (home_dir, "Desktop", NULL);
2282 user_special_dir = g_user_special_dirs[directory];
2284 G_UNLOCK (g_utils_global);
2286 return user_special_dir;
2291 #undef g_get_system_data_dirs
2294 get_module_for_address (gconstpointer address)
2296 /* Holds the g_utils_global lock */
2298 HMODULE hmodule = NULL;
2303 if (!GetModuleHandleExW (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
2304 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2307 MEMORY_BASIC_INFORMATION mbi;
2308 VirtualQuery (address, &mbi, sizeof (mbi));
2309 hmodule = (HMODULE) mbi.AllocationBase;
2316 get_module_share_dir (gconstpointer address)
2322 hmodule = get_module_for_address (address);
2323 if (hmodule == NULL)
2326 filename = g_win32_get_package_installation_directory_of_module (hmodule);
2327 retval = g_build_filename (filename, "share", NULL);
2333 static const gchar * const *
2334 g_win32_get_system_data_dirs_for_module_real (void (*address_of_function)(void))
2338 static GHashTable *per_module_data_dirs = NULL;
2344 if (address_of_function)
2346 G_LOCK (g_utils_global);
2347 hmodule = get_module_for_address (address_of_function);
2348 if (hmodule != NULL)
2350 if (per_module_data_dirs == NULL)
2351 per_module_data_dirs = g_hash_table_new (NULL, NULL);
2354 retval = g_hash_table_lookup (per_module_data_dirs, hmodule);
2358 G_UNLOCK (g_utils_global);
2359 return (const gchar * const *) retval;
2365 data_dirs = g_array_new (TRUE, TRUE, sizeof (char *));
2367 /* Documents and Settings\All Users\Application Data */
2368 p = get_special_folder (CSIDL_COMMON_APPDATA);
2370 g_array_append_val (data_dirs, p);
2372 /* Documents and Settings\All Users\Documents */
2373 p = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2375 g_array_append_val (data_dirs, p);
2377 /* Using the above subfolders of Documents and Settings perhaps
2378 * makes sense from a Windows perspective.
2380 * But looking at the actual use cases of this function in GTK+
2381 * and GNOME software, what we really want is the "share"
2382 * subdirectory of the installation directory for the package
2383 * our caller is a part of.
2385 * The address_of_function parameter, if non-NULL, points to a
2386 * function in the calling module. Use that to determine that
2387 * module's installation folder, and use its "share" subfolder.
2389 * Additionally, also use the "share" subfolder of the installation
2390 * locations of GLib and the .exe file being run.
2392 * To guard against none of the above being what is really wanted,
2393 * callers of this function should have Win32-specific code to look
2394 * up their installation folder themselves, and handle a subfolder
2395 * "share" of it in the same way as the folders returned from this
2399 p = get_module_share_dir (address_of_function);
2401 g_array_append_val (data_dirs, p);
2403 if (glib_dll != NULL)
2405 gchar *glib_root = g_win32_get_package_installation_directory_of_module (glib_dll);
2406 p = g_build_filename (glib_root, "share", NULL);
2408 g_array_append_val (data_dirs, p);
2412 exe_root = g_win32_get_package_installation_directory_of_module (NULL);
2413 p = g_build_filename (exe_root, "share", NULL);
2415 g_array_append_val (data_dirs, p);
2418 retval = (gchar **) g_array_free (data_dirs, FALSE);
2420 if (address_of_function)
2422 if (hmodule != NULL)
2423 g_hash_table_insert (per_module_data_dirs, hmodule, retval);
2424 G_UNLOCK (g_utils_global);
2427 return (const gchar * const *) retval;
2430 const gchar * const *
2431 g_win32_get_system_data_dirs_for_module (void (*address_of_function)(void))
2433 gboolean should_call_g_get_system_data_dirs;
2435 should_call_g_get_system_data_dirs = TRUE;
2436 /* These checks are the same as the ones that g_build_system_data_dirs() does.
2437 * Please keep them in sync.
2439 G_LOCK (g_utils_global);
2441 if (!g_system_data_dirs)
2443 const gchar *data_dirs = g_getenv ("XDG_DATA_DIRS");
2445 if (!data_dirs || !data_dirs[0])
2446 should_call_g_get_system_data_dirs = FALSE;
2449 G_UNLOCK (g_utils_global);
2451 /* There is a subtle difference between g_win32_get_system_data_dirs_for_module (NULL),
2452 * which is what GLib code can normally call,
2453 * and g_win32_get_system_data_dirs_for_module (&_g_win32_get_system_data_dirs),
2454 * which is what the inline function used by non-GLib code calls.
2455 * The former gets prefix relative to currently-running executable,
2456 * the latter - relative to the module that calls _g_win32_get_system_data_dirs()
2457 * (disguised as g_get_system_data_dirs()), which could be an executable or
2458 * a DLL that is located somewhere else.
2459 * This is why that inline function in gutils.h exists, and why we can't just
2460 * call g_get_system_data_dirs() from there - because we need to get the address
2461 * local to the non-GLib caller-module.
2465 * g_get_system_data_dirs() will fall back to calling
2466 * g_win32_get_system_data_dirs_for_module_real(NULL) if XDG_DATA_DIRS is NULL
2467 * or an empty string. The checks above ensure that we do not call it in such
2468 * cases and use the address_of_function that we've been given by the inline function.
2469 * The reason we're calling g_get_system_data_dirs /at all/ is to give
2470 * XDG_DATA_DIRS precedence (if it is set).
2472 if (should_call_g_get_system_data_dirs)
2473 return g_get_system_data_dirs ();
2475 return g_win32_get_system_data_dirs_for_module_real (address_of_function);
2481 g_build_system_data_dirs (void)
2483 gchar **data_dir_vector = NULL;
2484 gchar *data_dirs = (gchar *) g_getenv ("XDG_DATA_DIRS");
2486 /* These checks are the same as the ones that g_win32_get_system_data_dirs_for_module()
2487 * does. Please keep them in sync.
2490 if (!data_dirs || !data_dirs[0])
2491 data_dirs = "/usr/local/share/:/usr/share/";
2493 data_dir_vector = g_strsplit (data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2495 if (!data_dirs || !data_dirs[0])
2496 data_dir_vector = g_strdupv ((gchar **) g_win32_get_system_data_dirs_for_module_real (NULL));
2498 data_dir_vector = g_strsplit (data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2501 return g_steal_pointer (&data_dir_vector);
2505 * g_get_system_data_dirs:
2507 * Returns an ordered list of base directories in which to access
2508 * system-wide application data.
2510 * On UNIX platforms this is determined using the mechanisms described
2512 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec)
2513 * In this case the list of directories retrieved will be `XDG_DATA_DIRS`.
2515 * On Windows it follows XDG Base Directory Specification if `XDG_DATA_DIRS` is defined.
2516 * If `XDG_DATA_DIRS` is undefined,
2517 * the first elements in the list are the Application Data
2518 * and Documents folders for All Users. (These can be determined only
2519 * on Windows 2000 or later and are not present in the list on other
2520 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and
2521 * CSIDL_COMMON_DOCUMENTS.
2523 * Then follows the "share" subfolder in the installation folder for
2524 * the package containing the DLL that calls this function, if it can
2527 * Finally the list contains the "share" subfolder in the installation
2528 * folder for GLib, and in the installation folder for the package the
2529 * application's .exe file belongs to.
2531 * The installation folders above are determined by looking up the
2532 * folder where the module (DLL or EXE) in question is located. If the
2533 * folder's name is "bin", its parent is used, otherwise the folder
2536 * Note that on Windows the returned list can vary depending on where
2537 * this function is called.
2539 * Returns: (array zero-terminated=1) (element-type filename) (transfer none):
2540 * a %NULL-terminated array of strings owned by GLib that must not be
2541 * modified or freed.
2545 const gchar * const *
2546 g_get_system_data_dirs (void)
2548 const gchar * const *system_data_dirs;
2550 G_LOCK (g_utils_global);
2552 if (g_system_data_dirs == NULL)
2553 g_system_data_dirs = g_build_system_data_dirs ();
2554 system_data_dirs = (const gchar * const *) g_system_data_dirs;
2556 G_UNLOCK (g_utils_global);
2558 return system_data_dirs;
2562 g_build_system_config_dirs (void)
2564 gchar **conf_dir_vector = NULL;
2565 const gchar *conf_dirs = g_getenv ("XDG_CONFIG_DIRS");
2569 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2573 gchar *special_conf_dirs = get_special_folder (CSIDL_COMMON_APPDATA);
2575 if (special_conf_dirs)
2576 conf_dir_vector = g_strsplit (special_conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2578 /* Return empty list */
2579 conf_dir_vector = g_strsplit ("", G_SEARCHPATH_SEPARATOR_S, 0);
2581 g_free (special_conf_dirs);
2584 if (!conf_dirs || !conf_dirs[0])
2585 conf_dirs = "/etc/xdg";
2587 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2590 return g_steal_pointer (&conf_dir_vector);
2594 * g_get_system_config_dirs:
2596 * Returns an ordered list of base directories in which to access
2597 * system-wide configuration information.
2599 * On UNIX platforms this is determined using the mechanisms described
2601 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
2602 * In this case the list of directories retrieved will be `XDG_CONFIG_DIRS`.
2604 * On Windows it follows XDG Base Directory Specification if `XDG_CONFIG_DIRS` is defined.
2605 * If `XDG_CONFIG_DIRS` is undefined, the directory that contains application
2606 * data for all users is used instead. A typical path is
2607 * `C:\Documents and Settings\All Users\Application Data`.
2608 * This folder is used for application data
2609 * that is not user specific. For example, an application can store
2610 * a spell-check dictionary, a database of clip art, or a log file in the
2611 * CSIDL_COMMON_APPDATA folder. This information will not roam and is available
2612 * to anyone using the computer.
2614 * Returns: (array zero-terminated=1) (element-type filename) (transfer none):
2615 * a %NULL-terminated array of strings owned by GLib that must not be
2616 * modified or freed.
2620 const gchar * const *
2621 g_get_system_config_dirs (void)
2623 const gchar * const *system_config_dirs;
2625 G_LOCK (g_utils_global);
2627 if (g_system_config_dirs == NULL)
2628 g_system_config_dirs = g_build_system_config_dirs ();
2629 system_config_dirs = (const gchar * const *) g_system_config_dirs;
2631 G_UNLOCK (g_utils_global);
2633 return system_config_dirs;
2637 * g_nullify_pointer:
2638 * @nullify_location: (not nullable): the memory address of the pointer.
2640 * Set the pointer at the specified location to %NULL.
2643 g_nullify_pointer (gpointer *nullify_location)
2645 g_return_if_fail (nullify_location != NULL);
2647 *nullify_location = NULL;
2650 #define KILOBYTE_FACTOR (G_GOFFSET_CONSTANT (1000))
2651 #define MEGABYTE_FACTOR (KILOBYTE_FACTOR * KILOBYTE_FACTOR)
2652 #define GIGABYTE_FACTOR (MEGABYTE_FACTOR * KILOBYTE_FACTOR)
2653 #define TERABYTE_FACTOR (GIGABYTE_FACTOR * KILOBYTE_FACTOR)
2654 #define PETABYTE_FACTOR (TERABYTE_FACTOR * KILOBYTE_FACTOR)
2655 #define EXABYTE_FACTOR (PETABYTE_FACTOR * KILOBYTE_FACTOR)
2657 #define KIBIBYTE_FACTOR (G_GOFFSET_CONSTANT (1024))
2658 #define MEBIBYTE_FACTOR (KIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2659 #define GIBIBYTE_FACTOR (MEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2660 #define TEBIBYTE_FACTOR (GIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2661 #define PEBIBYTE_FACTOR (TEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2662 #define EXBIBYTE_FACTOR (PEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2666 * @size: a size in bytes
2668 * Formats a size (for example the size of a file) into a human readable
2669 * string. Sizes are rounded to the nearest size prefix (kB, MB, GB)
2670 * and are displayed rounded to the nearest tenth. E.g. the file size
2671 * 3292528 bytes will be converted into the string "3.2 MB". The returned string
2672 * is UTF-8, and may use a non-breaking space to separate the number and units,
2673 * to ensure they aren’t separated when line wrapped.
2675 * The prefix units base is 1000 (i.e. 1 kB is 1000 bytes).
2677 * This string should be freed with g_free() when not needed any longer.
2679 * See g_format_size_full() for more options about how the size might be
2682 * Returns: (transfer full): a newly-allocated formatted string containing
2683 * a human readable file size
2688 g_format_size (guint64 size)
2690 return g_format_size_full (size, G_FORMAT_SIZE_DEFAULT);
2695 * @G_FORMAT_SIZE_DEFAULT: behave the same as g_format_size()
2696 * @G_FORMAT_SIZE_LONG_FORMAT: include the exact number of bytes as part
2697 * of the returned string. For example, "45.6 kB (45,612 bytes)".
2698 * @G_FORMAT_SIZE_IEC_UNITS: use IEC (base 1024) units with "KiB"-style
2699 * suffixes. IEC units should only be used for reporting things with
2700 * a strong "power of 2" basis, like RAM sizes or RAID stripe sizes.
2701 * Network and storage sizes should be reported in the normal SI units.
2702 * @G_FORMAT_SIZE_BITS: set the size as a quantity in bits, rather than
2703 * bytes, and return units in bits. For example, ‘Mb’ rather than ‘MB’.
2705 * Flags to modify the format of the string returned by g_format_size_full().
2708 #pragma GCC diagnostic push
2709 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
2712 * g_format_size_full:
2713 * @size: a size in bytes
2714 * @flags: #GFormatSizeFlags to modify the output
2718 * This function is similar to g_format_size() but allows for flags
2719 * that modify the output. See #GFormatSizeFlags.
2721 * Returns: (transfer full): a newly-allocated formatted string
2722 * containing a human readable file size
2727 g_format_size_full (guint64 size,
2728 GFormatSizeFlags flags)
2744 const struct Format formats[4][6] = {
2746 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2747 { KILOBYTE_FACTOR, N_("%.1f kB") },
2748 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2749 { MEGABYTE_FACTOR, N_("%.1f MB") },
2750 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2751 { GIGABYTE_FACTOR, N_("%.1f GB") },
2752 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2753 { TERABYTE_FACTOR, N_("%.1f TB") },
2754 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2755 { PETABYTE_FACTOR, N_("%.1f PB") },
2756 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2757 { EXABYTE_FACTOR, N_("%.1f EB") }
2760 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2761 { KIBIBYTE_FACTOR, N_("%.1f KiB") },
2762 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2763 { MEBIBYTE_FACTOR, N_("%.1f MiB") },
2764 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2765 { GIBIBYTE_FACTOR, N_("%.1f GiB") },
2766 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2767 { TEBIBYTE_FACTOR, N_("%.1f TiB") },
2768 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2769 { PEBIBYTE_FACTOR, N_("%.1f PiB") },
2770 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2771 { EXBIBYTE_FACTOR, N_("%.1f EiB") }
2774 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2775 { KILOBYTE_FACTOR, N_("%.1f kb") },
2776 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2777 { MEGABYTE_FACTOR, N_("%.1f Mb") },
2778 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2779 { GIGABYTE_FACTOR, N_("%.1f Gb") },
2780 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2781 { TERABYTE_FACTOR, N_("%.1f Tb") },
2782 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2783 { PETABYTE_FACTOR, N_("%.1f Pb") },
2784 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2785 { EXABYTE_FACTOR, N_("%.1f Eb") }
2788 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2789 { KIBIBYTE_FACTOR, N_("%.1f Kib") },
2790 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2791 { MEBIBYTE_FACTOR, N_("%.1f Mib") },
2792 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2793 { GIBIBYTE_FACTOR, N_("%.1f Gib") },
2794 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2795 { TEBIBYTE_FACTOR, N_("%.1f Tib") },
2796 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2797 { PEBIBYTE_FACTOR, N_("%.1f Pib") },
2798 /* Translators: Keep the no-break space between %.1f and the unit symbol */
2799 { EXBIBYTE_FACTOR, N_("%.1f Eib") }
2806 string = g_string_new (NULL);
2808 switch (flags & ~G_FORMAT_SIZE_LONG_FORMAT)
2810 case G_FORMAT_SIZE_DEFAULT:
2811 index = FORMAT_BYTES;
2813 case (G_FORMAT_SIZE_DEFAULT | G_FORMAT_SIZE_IEC_UNITS):
2814 index = FORMAT_BYTES_IEC;
2816 case G_FORMAT_SIZE_BITS:
2817 index = FORMAT_BITS;
2819 case (G_FORMAT_SIZE_BITS | G_FORMAT_SIZE_IEC_UNITS):
2820 index = FORMAT_BITS_IEC;
2823 g_assert_not_reached ();
2827 if (size < formats[index][0].factor)
2829 const char * format;
2831 if (index == FORMAT_BYTES || index == FORMAT_BYTES_IEC)
2833 format = g_dngettext (GETTEXT_PACKAGE, "%u byte", "%u bytes", (guint) size);
2837 format = g_dngettext (GETTEXT_PACKAGE, "%u bit", "%u bits", (guint) size);
2840 g_string_printf (string, format, (guint) size);
2842 flags &= ~G_FORMAT_SIZE_LONG_FORMAT;
2846 const gsize n = G_N_ELEMENTS (formats[index]);
2850 * Point the last format (the highest unit) by default
2851 * and then then scan all formats, starting with the 2nd one
2852 * because the 1st is already managed by with the plural form
2854 const struct Format * f = &formats[index][n - 1];
2856 for (i = 1; i < n; i++)
2858 if (size < formats[index][i].factor)
2860 f = &formats[index][i - 1];
2865 g_string_printf (string, _(f->string), (gdouble) size / (gdouble) f->factor);
2868 if (flags & G_FORMAT_SIZE_LONG_FORMAT)
2870 /* First problem: we need to use the number of bytes to decide on
2871 * the plural form that is used for display, but the number of
2872 * bytes potentially exceeds the size of a guint (which is what
2873 * ngettext() takes).
2875 * From a pragmatic standpoint, it seems that all known languages
2876 * base plural forms on one or both of the following:
2878 * - the lowest digits of the number
2880 * - if the number if greater than some small value
2882 * Here's how we fake it: Draw an arbitrary line at one thousand.
2883 * If the number is below that, then fine. If it is above it,
2884 * then we take the modulus of the number by one thousand (in
2885 * order to keep the lowest digits) and add one thousand to that
2886 * (in order to ensure that 1001 is not treated the same as 1).
2888 guint plural_form = size < 1000 ? size : size % 1000 + 1000;
2890 /* Second problem: we need to translate the string "%u byte/bit" and
2891 * "%u bytes/bits" for pluralisation, but the correct number format to
2892 * use for a gsize is different depending on which architecture
2895 * Solution: format the number separately and use "%s bytes/bits" on
2898 const gchar *translated_format;
2899 gchar *formatted_number;
2901 if (index == FORMAT_BYTES || index == FORMAT_BYTES_IEC)
2903 /* Translators: the %s in "%s bytes" will always be replaced by a number. */
2904 translated_format = g_dngettext (GETTEXT_PACKAGE, "%s byte", "%s bytes", plural_form);
2908 /* Translators: the %s in "%s bits" will always be replaced by a number. */
2909 translated_format = g_dngettext (GETTEXT_PACKAGE, "%s bit", "%s bits", plural_form);
2911 formatted_number = g_strdup_printf ("%'"G_GUINT64_FORMAT, size);
2913 g_string_append (string, " (");
2914 g_string_append_printf (string, translated_format, formatted_number);
2915 g_free (formatted_number);
2916 g_string_append (string, ")");
2919 return g_string_free (string, FALSE);
2922 #pragma GCC diagnostic pop
2925 * g_format_size_for_display:
2926 * @size: a size in bytes
2928 * Formats a size (for example the size of a file) into a human
2929 * readable string. Sizes are rounded to the nearest size prefix
2930 * (KB, MB, GB) and are displayed rounded to the nearest tenth.
2931 * E.g. the file size 3292528 bytes will be converted into the
2934 * The prefix units base is 1024 (i.e. 1 KB is 1024 bytes).
2936 * This string should be freed with g_free() when not needed any longer.
2938 * Returns: (transfer full): a newly-allocated formatted string
2939 * containing a human readable file size
2943 * Deprecated:2.30: This function is broken due to its use of SI
2944 * suffixes to denote IEC units. Use g_format_size() instead.
2947 g_format_size_for_display (goffset size)
2949 if (size < (goffset) KIBIBYTE_FACTOR)
2950 return g_strdup_printf (g_dngettext(GETTEXT_PACKAGE, "%u byte", "%u bytes",(guint) size), (guint) size);
2953 gdouble displayed_size;
2955 if (size < (goffset) MEBIBYTE_FACTOR)
2957 displayed_size = (gdouble) size / (gdouble) KIBIBYTE_FACTOR;
2958 /* Translators: this is from the deprecated function g_format_size_for_display() which uses 'KB' to
2959 * mean 1024 bytes. I am aware that 'KB' is not correct, but it has been preserved for reasons of
2960 * compatibility. Users will not see this string unless a program is using this deprecated function.
2961 * Please translate as literally as possible.
2963 return g_strdup_printf (_("%.1f KB"), displayed_size);
2965 else if (size < (goffset) GIBIBYTE_FACTOR)
2967 displayed_size = (gdouble) size / (gdouble) MEBIBYTE_FACTOR;
2968 return g_strdup_printf (_("%.1f MB"), displayed_size);
2970 else if (size < (goffset) TEBIBYTE_FACTOR)
2972 displayed_size = (gdouble) size / (gdouble) GIBIBYTE_FACTOR;
2973 return g_strdup_printf (_("%.1f GB"), displayed_size);
2975 else if (size < (goffset) PEBIBYTE_FACTOR)
2977 displayed_size = (gdouble) size / (gdouble) TEBIBYTE_FACTOR;
2978 return g_strdup_printf (_("%.1f TB"), displayed_size);
2980 else if (size < (goffset) EXBIBYTE_FACTOR)
2982 displayed_size = (gdouble) size / (gdouble) PEBIBYTE_FACTOR;
2983 return g_strdup_printf (_("%.1f PB"), displayed_size);
2987 displayed_size = (gdouble) size / (gdouble) EXBIBYTE_FACTOR;
2988 return g_strdup_printf (_("%.1f EB"), displayed_size);
2993 #if defined (G_OS_WIN32) && !defined (_WIN64)
2995 /* Binary compatibility versions. Not for newly compiled code. */
2997 _GLIB_EXTERN const gchar *g_get_user_name_utf8 (void);
2998 _GLIB_EXTERN const gchar *g_get_real_name_utf8 (void);
2999 _GLIB_EXTERN const gchar *g_get_home_dir_utf8 (void);
3000 _GLIB_EXTERN const gchar *g_get_tmp_dir_utf8 (void);
3001 _GLIB_EXTERN gchar *g_find_program_in_path_utf8 (const gchar *program);
3004 g_find_program_in_path_utf8 (const gchar *program)
3006 return g_find_program_in_path (program);
3009 const gchar *g_get_user_name_utf8 (void) { return g_get_user_name (); }
3010 const gchar *g_get_real_name_utf8 (void) { return g_get_real_name (); }
3011 const gchar *g_get_home_dir_utf8 (void) { return g_get_home_dir (); }
3012 const gchar *g_get_tmp_dir_utf8 (void) { return g_get_tmp_dir (); }
3018 * Returns %TRUE if the current process was executed as setuid
3021 g_check_setuid (void)
3023 #if defined(HAVE_SYS_AUXV_H) && defined(HAVE_GETAUXVAL) && defined(AT_SECURE)
3024 unsigned long value;
3028 value = getauxval (AT_SECURE);
3031 g_error ("getauxval () failed: %s", g_strerror (errsv));
3033 #elif defined(HAVE_ISSETUGID) && !defined(__BIONIC__)
3034 /* BSD: http://www.freebsd.org/cgi/man.cgi?query=issetugid&sektion=2 */
3036 /* Android had it in older versions but the new 64 bit ABI does not
3037 * have it anymore, and some versions of the 32 bit ABI neither.
3038 * https://code.google.com/p/android-developer-preview/issues/detail?id=168
3040 return issetugid ();
3041 #elif defined(G_OS_UNIX)
3042 uid_t ruid, euid, suid; /* Real, effective and saved user ID's */
3043 gid_t rgid, egid, sgid; /* Real, effective and saved group ID's */
3045 static gsize check_setuid_initialised;
3046 static gboolean is_setuid;
3048 if (g_once_init_enter (&check_setuid_initialised))
3050 #ifdef HAVE_GETRESUID
3051 /* These aren't in the header files, so we prototype them here.
3053 int getresuid(uid_t *ruid, uid_t *euid, uid_t *suid);
3054 int getresgid(gid_t *rgid, gid_t *egid, gid_t *sgid);
3056 if (getresuid (&ruid, &euid, &suid) != 0 ||
3057 getresgid (&rgid, &egid, &sgid) != 0)
3058 #endif /* HAVE_GETRESUID */
3060 suid = ruid = getuid ();
3061 sgid = rgid = getgid ();
3066 is_setuid = (ruid != euid || ruid != suid ||
3067 rgid != egid || rgid != sgid);
3069 g_once_init_leave (&check_setuid_initialised, 1);
3081 * A wrapper for the POSIX abort() function.
3083 * On Windows it is a function that makes extra effort (including a call
3084 * to abort()) to ensure that a debugger-catchable exception is thrown
3085 * before the program terminates.
3087 * See your C library manual for more details about abort().
3094 /* One call to break the debugger */
3096 /* One call in case CRT changes its abort() behaviour */
3098 /* And one call to bind them all and terminate the program for sure */