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.
38 #include <ctype.h> /* For tolower() */
40 #include <sys/types.h>
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
50 #ifdef HAVE_CRT_EXTERNS_H
51 #include <crt_externs.h> /* for _NSGetEnviron */
54 #include "glib-init.h"
55 #include "glib-private.h"
57 #include "gfileutils.h"
61 #include "gtestutils.h"
63 #include "gstrfuncs.h"
68 #ifdef G_PLATFORM_WIN32
76 * @title: Miscellaneous Utility Functions
77 * @short_description: a selection of portable utility functions
79 * These are portable utility functions.
82 #ifdef G_PLATFORM_WIN32
84 # ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
85 # define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
86 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
88 # include <lmcons.h> /* For UNLEN */
89 #endif /* G_PLATFORM_WIN32 */
94 /* older SDK (e.g. msvc 5.0) does not have these*/
95 # ifndef CSIDL_MYMUSIC
96 # define CSIDL_MYMUSIC 13
98 # ifndef CSIDL_MYVIDEO
99 # define CSIDL_MYVIDEO 14
101 # ifndef CSIDL_INTERNET_CACHE
102 # define CSIDL_INTERNET_CACHE 32
104 # ifndef CSIDL_COMMON_APPDATA
105 # define CSIDL_COMMON_APPDATA 35
107 # ifndef CSIDL_MYPICTURES
108 # define CSIDL_MYPICTURES 0x27
110 # ifndef CSIDL_COMMON_DOCUMENTS
111 # define CSIDL_COMMON_DOCUMENTS 46
113 # ifndef CSIDL_PROFILE
114 # define CSIDL_PROFILE 40
116 # include <process.h>
120 #include <CoreServices/CoreServices.h>
124 #include <langinfo.h>
127 #ifdef G_PLATFORM_WIN32
130 _glib_get_dll_directory (void)
134 wchar_t wc_fn
[MAX_PATH
];
137 if (glib_dll
== NULL
)
141 /* This code is different from that in
142 * g_win32_get_package_installation_directory_of_module() in that
143 * here we return the actual folder where the GLib DLL is. We don't
144 * do the check for it being in a "bin" or "lib" subfolder and then
145 * returning the parent of that.
147 * In a statically built GLib, glib_dll will be NULL and we will
148 * thus look up the application's .exe file's location.
150 if (!GetModuleFileNameW (glib_dll
, wc_fn
, MAX_PATH
))
153 retval
= g_utf16_to_utf8 (wc_fn
, -1, NULL
, NULL
, NULL
);
155 p
= strrchr (retval
, G_DIR_SEPARATOR
);
170 * @dest: the destination address to copy the bytes to.
171 * @src: the source address to copy the bytes from.
172 * @len: the number of bytes to copy.
174 * Copies a block of memory @len bytes long, from @src to @dest.
175 * The source and destination areas may overlap.
177 * Deprecated:2.40: Just use memmove().
186 * @func: (scope async): the function to call on normal program termination.
188 * Specifies a function to be called at normal program termination.
190 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
191 * macro that maps to a call to the atexit() function in the C
192 * library. This means that in case the code that calls g_atexit(),
193 * i.e. atexit(), is in a DLL, the function will be called when the
194 * DLL is detached from the program. This typically makes more sense
195 * than that the function is called when the GLib DLL is detached,
196 * which happened earlier when g_atexit() was a function in the GLib
199 * The behaviour of atexit() in the context of dynamically loaded
200 * modules is not formally specified and varies wildly.
202 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
203 * loaded module which is unloaded before the program terminates might
204 * well cause a crash at program exit.
206 * Some POSIX systems implement atexit() like Windows, and have each
207 * dynamically loaded module maintain an own atexit chain that is
208 * called when the module is unloaded.
210 * On other POSIX systems, before a dynamically loaded module is
211 * unloaded, the registered atexit functions (if any) residing in that
212 * module are called, regardless where the code that registered them
213 * resided. This is presumably the most robust approach.
215 * As can be seen from the above, for portability it's best to avoid
216 * calling g_atexit() (or atexit()) except in the main executable of a
219 * Deprecated:2.32: It is best to avoid g_atexit().
222 g_atexit (GVoidFunc func
)
227 result
= atexit ((void (*)(void)) func
);
231 g_error ("Could not register atexit() function: %s",
236 /* Based on execvp() from GNU Libc.
237 * Some of this code is cut-and-pasted into gspawn.c
241 my_strchrnul (const gchar
*str
,
244 gchar
*p
= (gchar
*)str
;
245 while (*p
&& (*p
!= c
))
253 static gchar
*inner_find_program_in_path (const gchar
*program
);
256 g_find_program_in_path (const gchar
*program
)
258 const gchar
*last_dot
= strrchr (program
, '.');
260 if (last_dot
== NULL
||
261 strchr (last_dot
, '\\') != NULL
||
262 strchr (last_dot
, '/') != NULL
)
264 const gint program_length
= strlen (program
);
265 gchar
*pathext
= g_build_path (";",
266 ".exe;.cmd;.bat;.com",
267 g_getenv ("PATHEXT"),
270 gchar
*decorated_program
;
276 gchar
*q
= my_strchrnul (p
, ';');
278 decorated_program
= g_malloc (program_length
+ (q
-p
) + 1);
279 memcpy (decorated_program
, program
, program_length
);
280 memcpy (decorated_program
+program_length
, p
, q
-p
);
281 decorated_program
[program_length
+ (q
-p
)] = '\0';
283 retval
= inner_find_program_in_path (decorated_program
);
284 g_free (decorated_program
);
292 } while (*p
++ != '\0');
297 return inner_find_program_in_path (program
);
303 * g_find_program_in_path:
304 * @program: (type filename): a program name in the GLib file name encoding
306 * Locates the first executable named @program in the user's path, in the
307 * same way that execvp() would locate it. Returns an allocated string
308 * with the absolute path name, or %NULL if the program is not found in
309 * the path. If @program is already an absolute path, returns a copy of
310 * @program if @program exists and is executable, and %NULL otherwise.
312 * On Windows, if @program does not have a file type suffix, tries
313 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
314 * the `PATHEXT` environment variable.
316 * On Windows, it looks for the file in the same way as CreateProcess()
317 * would. This means first in the directory where the executing
318 * program was loaded from, then in the current directory, then in the
319 * Windows 32-bit system directory, then in the Windows directory, and
320 * finally in the directories in the `PATH` environment variable. If
321 * the program is found, the return value contains the full name
322 * including the type suffix.
324 * Returns: (type filename): a newly-allocated string with the absolute path,
329 inner_find_program_in_path (const gchar
*program
)
332 g_find_program_in_path (const gchar
*program
)
335 const gchar
*path
, *p
;
336 gchar
*name
, *freeme
;
338 const gchar
*path_copy
;
339 gchar
*filename
= NULL
, *appdir
= NULL
;
340 gchar
*sysdir
= NULL
, *windir
= NULL
;
342 wchar_t wfilename
[MAXPATHLEN
], wsysdir
[MAXPATHLEN
],
348 g_return_val_if_fail (program
!= NULL
, NULL
);
350 /* If it is an absolute path, or a relative path including subdirectories,
351 * don't look in PATH.
353 if (g_path_is_absolute (program
)
354 || strchr (program
, G_DIR_SEPARATOR
) != NULL
356 || strchr (program
, '/') != NULL
360 if (g_file_test (program
, G_FILE_TEST_IS_EXECUTABLE
) &&
361 !g_file_test (program
, G_FILE_TEST_IS_DIR
))
362 return g_strdup (program
);
367 path
= g_getenv ("PATH");
368 #if defined(G_OS_UNIX)
371 /* There is no 'PATH' in the environment. The default
372 * search path in GNU libc is the current directory followed by
373 * the path 'confstr' returns for '_CS_PATH'.
376 /* In GLib we put . last, for security, and don't use the
377 * unportable confstr(); UNIX98 does not actually specify
378 * what to search if PATH is unset. POSIX may, dunno.
381 path
= "/bin:/usr/bin:.";
384 n
= GetModuleFileNameW (NULL
, wfilename
, MAXPATHLEN
);
385 if (n
> 0 && n
< MAXPATHLEN
)
386 filename
= g_utf16_to_utf8 (wfilename
, -1, NULL
, NULL
, NULL
);
388 n
= GetSystemDirectoryW (wsysdir
, MAXPATHLEN
);
389 if (n
> 0 && n
< MAXPATHLEN
)
390 sysdir
= g_utf16_to_utf8 (wsysdir
, -1, NULL
, NULL
, NULL
);
392 n
= GetWindowsDirectoryW (wwindir
, MAXPATHLEN
);
393 if (n
> 0 && n
< MAXPATHLEN
)
394 windir
= g_utf16_to_utf8 (wwindir
, -1, NULL
, NULL
, NULL
);
398 appdir
= g_path_get_dirname (filename
);
402 path
= g_strdup (path
);
406 const gchar
*tem
= path
;
407 path
= g_strconcat (windir
, ";", path
, NULL
);
408 g_free ((gchar
*) tem
);
414 const gchar
*tem
= path
;
415 path
= g_strconcat (sysdir
, ";", path
, NULL
);
416 g_free ((gchar
*) tem
);
421 const gchar
*tem
= path
;
422 path
= g_strconcat (".;", path
, NULL
);
423 g_free ((gchar
*) tem
);
428 const gchar
*tem
= path
;
429 path
= g_strconcat (appdir
, ";", path
, NULL
);
430 g_free ((gchar
*) tem
);
437 len
= strlen (program
) + 1;
438 pathlen
= strlen (path
);
439 freeme
= name
= g_malloc (pathlen
+ len
+ 1);
441 /* Copy the file name at the top, including '\0' */
442 memcpy (name
+ pathlen
+ 1, program
, len
);
443 name
= name
+ pathlen
;
444 /* And add the slash before the filename */
445 *name
= G_DIR_SEPARATOR
;
453 p
= my_strchrnul (path
, G_SEARCHPATH_SEPARATOR
);
456 /* Two adjacent colons, or a colon at the beginning or the end
457 * of 'PATH' means to search the current directory.
461 startp
= memcpy (name
- (p
- path
), path
, p
- path
);
463 if (g_file_test (startp
, G_FILE_TEST_IS_EXECUTABLE
) &&
464 !g_file_test (startp
, G_FILE_TEST_IS_DIR
))
467 ret
= g_strdup (startp
);
470 g_free ((gchar
*) path_copy
);
475 while (*p
++ != '\0');
479 g_free ((gchar
*) path_copy
);
485 /* The functions below are defined this way for compatibility reasons.
486 * See the note in gutils.h.
491 * @mask: a #gulong containing flags
492 * @nth_bit: the index of the bit to start the search from
494 * Find the position of the first bit set in @mask, searching
495 * from (but not including) @nth_bit upwards. Bits are numbered
496 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
497 * usually). To start searching from the 0th bit, set @nth_bit to -1.
499 * Returns: the index of the first bit set which is higher than @nth_bit, or -1
500 * if no higher bits are set
503 (g_bit_nth_lsf
) (gulong mask
,
506 return g_bit_nth_lsf_impl (mask
, nth_bit
);
511 * @mask: a #gulong containing flags
512 * @nth_bit: the index of the bit to start the search from
514 * Find the position of the first bit set in @mask, searching
515 * from (but not including) @nth_bit downwards. Bits are numbered
516 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
517 * usually). To start searching from the last bit, set @nth_bit to
518 * -1 or GLIB_SIZEOF_LONG * 8.
520 * Returns: the index of the first bit set which is lower than @nth_bit, or -1
521 * if no lower bits are set
524 (g_bit_nth_msf
) (gulong mask
,
527 return g_bit_nth_msf_impl (mask
, nth_bit
);
535 * Gets the number of bits used to hold @number,
536 * e.g. if @number is 4, 3 bits are needed.
538 * Returns: the number of bits used to hold @number
541 (g_bit_storage
) (gulong number
)
543 return g_bit_storage_impl (number
);
546 G_LOCK_DEFINE_STATIC (g_utils_global
);
555 static gchar
*g_user_data_dir
= NULL
;
556 static gchar
**g_system_data_dirs
= NULL
;
557 static gchar
*g_user_cache_dir
= NULL
;
558 static gchar
*g_user_config_dir
= NULL
;
559 static gchar
**g_system_config_dirs
= NULL
;
561 static gchar
**g_user_special_dirs
= NULL
;
563 /* fifteen minutes of fame for everybody */
564 #define G_USER_DIRS_EXPIRE 15 * 60
569 get_special_folder (int csidl
)
571 wchar_t path
[MAX_PATH
+1];
573 LPITEMIDLIST pidl
= NULL
;
575 gchar
*retval
= NULL
;
577 hr
= SHGetSpecialFolderLocation (NULL
, csidl
, &pidl
);
580 b
= SHGetPathFromIDListW (pidl
, path
);
582 retval
= g_utf16_to_utf8 (path
, -1, NULL
, NULL
, NULL
);
583 CoTaskMemFree (pidl
);
589 get_windows_directory_root (void)
591 wchar_t wwindowsdir
[MAX_PATH
];
593 if (GetWindowsDirectoryW (wwindowsdir
, G_N_ELEMENTS (wwindowsdir
)))
595 /* Usually X:\Windows, but in terminal server environments
596 * might be an UNC path, AFAIK.
598 char *windowsdir
= g_utf16_to_utf8 (wwindowsdir
, -1, NULL
, NULL
, NULL
);
601 if (windowsdir
== NULL
)
602 return g_strdup ("C:\\");
604 p
= (char *) g_path_skip_root (windowsdir
);
605 if (G_IS_DIR_SEPARATOR (p
[-1]) && p
[-2] != ':')
611 return g_strdup ("C:\\");
616 /* HOLDS: g_utils_global_lock */
617 static UserDatabaseEntry
*
618 g_get_user_database_entry (void)
620 static UserDatabaseEntry
*entry
;
622 if (g_once_init_enter (&entry
))
624 static UserDatabaseEntry e
;
628 struct passwd
*pw
= NULL
;
629 gpointer buffer
= NULL
;
633 # if defined (HAVE_GETPWUID_R)
635 # ifdef _SC_GETPW_R_SIZE_MAX
636 /* This reurns the maximum length */
637 glong bufsize
= sysconf (_SC_GETPW_R_SIZE_MAX
);
641 # else /* _SC_GETPW_R_SIZE_MAX */
643 # endif /* _SC_GETPW_R_SIZE_MAX */
645 logname
= (gchar
*) g_getenv ("LOGNAME");
650 /* we allocate 6 extra bytes to work around a bug in
651 * Mac OS < 10.3. See #156446
653 buffer
= g_malloc (bufsize
+ 6);
657 error
= getpwnam_r (logname
, &pwd
, buffer
, bufsize
, &pw
);
658 if (!pw
|| (pw
->pw_uid
!= getuid ())) {
659 /* LOGNAME is lying, fall back to looking up the uid */
660 error
= getpwuid_r (getuid (), &pwd
, buffer
, bufsize
, &pw
);
663 error
= getpwuid_r (getuid (), &pwd
, buffer
, bufsize
, &pw
);
665 error
= error
< 0 ? errno
: error
;
669 /* we bail out prematurely if the user id can't be found
670 * (should be pretty rare case actually), or if the buffer
671 * should be sufficiently big and lookups are still not
674 if (error
== 0 || error
== ENOENT
)
676 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
680 if (bufsize
> 32 * 1024)
682 g_warning ("getpwuid_r(): failed due to: %s.",
691 # endif /* HAVE_GETPWUID_R */
695 pw
= getpwuid (getuid ());
699 e
.user_name
= g_strdup (pw
->pw_name
);
702 if (pw
->pw_gecos
&& *pw
->pw_gecos
!= '\0')
704 gchar
**gecos_fields
;
707 /* split the gecos field and substitute '&' */
708 gecos_fields
= g_strsplit (pw
->pw_gecos
, ",", 0);
709 name_parts
= g_strsplit (gecos_fields
[0], "&", 0);
710 pw
->pw_name
[0] = g_ascii_toupper (pw
->pw_name
[0]);
711 e
.real_name
= g_strjoinv (pw
->pw_name
, name_parts
);
712 g_strfreev (gecos_fields
);
713 g_strfreev (name_parts
);
718 e
.home_dir
= g_strdup (pw
->pw_dir
);
723 #endif /* G_OS_UNIX */
728 wchar_t buffer
[UNLEN
+1];
730 if (GetUserNameW (buffer
, (LPDWORD
) &len
))
732 e
.user_name
= g_utf16_to_utf8 (buffer
, -1, NULL
, NULL
, NULL
);
733 e
.real_name
= g_strdup (e
.user_name
);
736 #endif /* G_OS_WIN32 */
739 e
.user_name
= g_strdup ("somebody");
741 e
.real_name
= g_strdup ("Unknown");
743 g_once_init_leave (&entry
, &e
);
752 * Gets the user name of the current user. The encoding of the returned
753 * string is system-defined. On UNIX, it might be the preferred file name
754 * encoding, or something else, and there is no guarantee that it is even
755 * consistent on a machine. On Windows, it is always UTF-8.
757 * Returns: (type filename): the user name of the current user.
760 g_get_user_name (void)
762 UserDatabaseEntry
*entry
;
764 entry
= g_get_user_database_entry ();
766 return entry
->user_name
;
772 * Gets the real name of the user. This usually comes from the user's
773 * entry in the `passwd` file. The encoding of the returned string is
774 * system-defined. (On Windows, it is, however, always UTF-8.) If the
775 * real user name cannot be determined, the string "Unknown" is
778 * Returns: (type filename): the user's real name.
781 g_get_real_name (void)
783 UserDatabaseEntry
*entry
;
785 entry
= g_get_user_database_entry ();
787 return entry
->real_name
;
793 * Gets the current user's home directory.
795 * As with most UNIX tools, this function will return the value of the
796 * `HOME` environment variable if it is set to an existing absolute path
797 * name, falling back to the `passwd` file in the case that it is unset.
799 * If the path given in `HOME` is non-absolute, does not exist, or is
800 * not a directory, the result is undefined.
802 * Before version 2.36 this function would ignore the `HOME` environment
803 * variable, taking the value from the `passwd` database instead. This was
804 * changed to increase the compatibility of GLib with other programs (and
805 * the XDG basedir specification) and to increase testability of programs
806 * based on GLib (by making it easier to run them from test frameworks).
808 * If your program has a strong requirement for either the new or the
809 * old behaviour (and if you don't wish to increase your GLib
810 * dependency to ensure that the new behaviour is in effect) then you
811 * should either directly check the `HOME` environment variable yourself
812 * or unset it before calling any functions in GLib.
814 * Returns: (type filename): the current user's home directory
817 g_get_home_dir (void)
819 static gchar
*home_dir
;
821 if (g_once_init_enter (&home_dir
))
825 /* We first check HOME and use it if it is set */
826 tmp
= g_strdup (g_getenv ("HOME"));
829 /* Only believe HOME if it is an absolute path and exists.
831 * We only do this check on Windows for a couple of reasons.
832 * Historically, we only did it there because we used to ignore $HOME
833 * on UNIX. There are concerns about enabling it now on UNIX because
834 * of things like autofs. In short, if the user has a bogus value in
835 * $HOME then they get what they pay for...
839 if (!(g_path_is_absolute (tmp
) &&
840 g_file_test (tmp
, G_FILE_TEST_IS_DIR
)))
847 /* In case HOME is Unix-style (it happens), convert it to
853 while ((p
= strchr (tmp
, '/')) != NULL
)
859 /* USERPROFILE is probably the closest equivalent to $HOME? */
860 if (g_getenv ("USERPROFILE") != NULL
)
861 tmp
= g_strdup (g_getenv ("USERPROFILE"));
865 tmp
= get_special_folder (CSIDL_PROFILE
);
868 tmp
= get_windows_directory_root ();
869 #endif /* G_OS_WIN32 */
873 /* If we didn't get it from any of those methods, we will have
874 * to read the user database entry.
876 UserDatabaseEntry
*entry
;
878 entry
= g_get_user_database_entry ();
880 /* Strictly speaking, we should copy this, but we know that
881 * neither will ever be freed, so don't bother...
883 tmp
= entry
->home_dir
;
886 g_once_init_leave (&home_dir
, tmp
);
895 * Gets the directory to use for temporary files.
897 * On UNIX, this is taken from the `TMPDIR` environment variable.
898 * If the variable is not set, `P_tmpdir` is
899 * used, as defined by the system C library. Failing that, a
900 * hard-coded default of "/tmp" is returned.
902 * On Windows, the `TEMP` environment variable is used, with the
903 * root directory of the Windows installation (eg: "C:\") used
906 * The encoding of the returned string is system-defined. On Windows,
907 * it is always UTF-8. The return value is never %NULL or the empty
910 * Returns: (type filename): the directory to use for temporary files.
915 static gchar
*tmp_dir
;
917 if (g_once_init_enter (&tmp_dir
))
922 tmp
= g_strdup (g_getenv ("TEMP"));
924 if (tmp
== NULL
|| *tmp
== '\0')
927 tmp
= get_windows_directory_root ();
929 #else /* G_OS_WIN32 */
930 tmp
= g_strdup (g_getenv ("TMPDIR"));
933 if (tmp
== NULL
|| *tmp
== '\0')
937 tmp
= g_strdup (P_tmpdir
);
939 if (k
> 1 && G_IS_DIR_SEPARATOR (tmp
[k
- 1]))
942 #endif /* P_tmpdir */
944 if (tmp
== NULL
|| *tmp
== '\0')
947 tmp
= g_strdup ("/tmp");
949 #endif /* !G_OS_WIN32 */
951 g_once_init_leave (&tmp_dir
, tmp
);
960 * Return a name for the machine.
962 * The returned name is not necessarily a fully-qualified domain name,
963 * or even present in DNS or some other name service at all. It need
964 * not even be unique on your local network or site, but usually it
965 * is. Callers should not rely on the return value having any specific
966 * properties like uniqueness for security purposes. Even if the name
967 * of the machine is changed while an application is running, the
968 * return value from this function does not change. The returned
969 * string is owned by GLib and should not be modified or freed. If no
970 * name can be determined, a default fixed string "localhost" is
973 * The encoding of the returned string is UTF-8.
975 * Returns: the host name of the machine.
980 g_get_host_name (void)
982 static gchar
*hostname
;
984 if (g_once_init_enter (&hostname
))
990 gchar
*tmp
= g_malloc (sizeof (gchar
) * 100);
991 failed
= (gethostname (tmp
, sizeof (gchar
) * 100) == -1);
993 g_clear_pointer (&tmp
, g_free
);
996 wchar_t tmp
[MAX_COMPUTERNAME_LENGTH
+ 1];
997 DWORD size
= sizeof (tmp
) / sizeof (tmp
[0]);
998 failed
= (!GetComputerNameW (tmp
, &size
));
1000 utmp
= g_utf16_to_utf8 (tmp
, size
, NULL
, NULL
, NULL
);
1005 g_once_init_leave (&hostname
, failed
? g_strdup ("localhost") : utmp
);
1011 G_LOCK_DEFINE_STATIC (g_prgname
);
1012 static gchar
*g_prgname
= NULL
;
1017 * Gets the name of the program. This name should not be localized,
1018 * in contrast to g_get_application_name().
1020 * If you are using #GApplication the program name is set in
1021 * g_application_run(). In case of GDK or GTK+ it is set in
1022 * gdk_init(), which is called by gtk_init() and the
1023 * #GtkApplication::startup handler. The program name is found by
1024 * taking the last component of @argv[0].
1026 * Returns: the name of the program. The returned string belongs
1027 * to GLib and must not be modified or freed.
1030 g_get_prgname (void)
1036 if (g_prgname
== NULL
)
1038 static gboolean beenhere
= FALSE
;
1042 gchar
*utf8_buf
= NULL
;
1043 wchar_t buf
[MAX_PATH
+1];
1046 if (GetModuleFileNameW (GetModuleHandle (NULL
),
1047 buf
, G_N_ELEMENTS (buf
)) > 0)
1048 utf8_buf
= g_utf16_to_utf8 (buf
, -1, NULL
, NULL
, NULL
);
1052 g_prgname
= g_path_get_basename (utf8_buf
);
1059 G_UNLOCK (g_prgname
);
1066 * @prgname: the name of the program.
1068 * Sets the name of the program. This name should not be localized,
1069 * in contrast to g_set_application_name().
1071 * If you are using #GApplication the program name is set in
1072 * g_application_run(). In case of GDK or GTK+ it is set in
1073 * gdk_init(), which is called by gtk_init() and the
1074 * #GtkApplication::startup handler. The program name is found by
1075 * taking the last component of @argv[0].
1077 * Note that for thread-safety reasons this function can only be called once.
1080 g_set_prgname (const gchar
*prgname
)
1084 g_prgname
= g_strdup (prgname
);
1085 G_UNLOCK (g_prgname
);
1088 G_LOCK_DEFINE_STATIC (g_application_name
);
1089 static gchar
*g_application_name
= NULL
;
1092 * g_get_application_name:
1094 * Gets a human-readable name for the application, as set by
1095 * g_set_application_name(). This name should be localized if
1096 * possible, and is intended for display to the user. Contrast with
1097 * g_get_prgname(), which gets a non-localized name. If
1098 * g_set_application_name() has not been called, returns the result of
1099 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1102 * Returns: human-readable application name. may return %NULL
1107 g_get_application_name (void)
1111 G_LOCK (g_application_name
);
1112 retval
= g_application_name
;
1113 G_UNLOCK (g_application_name
);
1116 return g_get_prgname ();
1122 * g_set_application_name:
1123 * @application_name: localized name of the application
1125 * Sets a human-readable name for the application. This name should be
1126 * localized if possible, and is intended for display to the user.
1127 * Contrast with g_set_prgname(), which sets a non-localized name.
1128 * g_set_prgname() will be called automatically by gtk_init(),
1129 * but g_set_application_name() will not.
1131 * Note that for thread safety reasons, this function can only
1134 * The application name will be used in contexts such as error messages,
1135 * or when displaying an application's name in the task list.
1140 g_set_application_name (const gchar
*application_name
)
1142 gboolean already_set
= FALSE
;
1144 G_LOCK (g_application_name
);
1145 if (g_application_name
)
1148 g_application_name
= g_strdup (application_name
);
1149 G_UNLOCK (g_application_name
);
1152 g_warning ("g_set_application_name() called multiple times");
1156 * g_get_user_data_dir:
1158 * Returns a base directory in which to access application data such
1159 * as icons that is customized for a particular user.
1161 * On UNIX platforms this is determined using the mechanisms described
1163 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1164 * In this case the directory retrieved will be `XDG_DATA_HOME`.
1166 * On Windows it follows XDG Base Directory Specification if `XDG_DATA_HOME`
1167 * is defined. If `XDG_DATA_HOME` is undefined, the folder to use for local (as
1168 * opposed to roaming) application data is used instead. See the
1169 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata).
1170 * Note that in this case on Windows it will be the same
1171 * as what g_get_user_config_dir() returns.
1173 * Returns: (type filename): a string owned by GLib that must not be modified
1178 g_get_user_data_dir (void)
1180 gchar
*data_dir
= NULL
;
1182 G_LOCK (g_utils_global
);
1184 if (!g_user_data_dir
)
1186 const gchar
*data_dir_env
= g_getenv ("XDG_DATA_HOME");
1188 if (data_dir_env
&& data_dir_env
[0])
1189 data_dir
= g_strdup (data_dir_env
);
1192 data_dir
= get_special_folder (CSIDL_LOCAL_APPDATA
);
1194 if (!data_dir
|| !data_dir
[0])
1196 const gchar
*home_dir
= g_get_home_dir ();
1199 data_dir
= g_build_filename (home_dir
, ".local", "share", NULL
);
1201 data_dir
= g_build_filename (g_get_tmp_dir (), g_get_user_name (), ".local", "share", NULL
);
1204 g_user_data_dir
= data_dir
;
1207 data_dir
= g_user_data_dir
;
1209 G_UNLOCK (g_utils_global
);
1215 g_init_user_config_dir (void)
1217 gchar
*config_dir
= NULL
;
1219 if (!g_user_config_dir
)
1221 const gchar
*config_dir_env
= g_getenv ("XDG_CONFIG_HOME");
1223 if (config_dir_env
&& config_dir_env
[0])
1224 config_dir
= g_strdup (config_dir_env
);
1227 config_dir
= get_special_folder (CSIDL_LOCAL_APPDATA
);
1229 if (!config_dir
|| !config_dir
[0])
1231 const gchar
*home_dir
= g_get_home_dir ();
1234 config_dir
= g_build_filename (home_dir
, ".config", NULL
);
1236 config_dir
= g_build_filename (g_get_tmp_dir (), g_get_user_name (), ".config", NULL
);
1239 g_user_config_dir
= config_dir
;
1244 * g_get_user_config_dir:
1246 * Returns a base directory in which to store user-specific application
1247 * configuration information such as user preferences and settings.
1249 * On UNIX platforms this is determined using the mechanisms described
1251 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1252 * In this case the directory retrieved will be `XDG_CONFIG_HOME`.
1254 * On Windows it follows XDG Base Directory Specification if `XDG_CONFIG_HOME` is defined.
1255 * If `XDG_CONFIG_HOME` is undefined, the folder to use for local (as opposed
1256 * to roaming) application data is used instead. See the
1257 * [documentation for `CSIDL_LOCAL_APPDATA`](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762494%28v=vs.85%29.aspx#csidl_local_appdata).
1258 * Note that in this case on Windows it will be the same
1259 * as what g_get_user_data_dir() returns.
1261 * Returns: (type filename): a string owned by GLib that must not be modified
1266 g_get_user_config_dir (void)
1268 G_LOCK (g_utils_global
);
1270 g_init_user_config_dir ();
1272 G_UNLOCK (g_utils_global
);
1274 return g_user_config_dir
;
1278 * g_get_user_cache_dir:
1280 * Returns a base directory in which to store non-essential, cached
1281 * data specific to particular user.
1283 * On UNIX platforms this is determined using the mechanisms described
1285 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1286 * In this case the directory retrieved will be `XDG_CACHE_HOME`.
1288 * On Windows it follows XDG Base Directory Specification if `XDG_CACHE_HOME` is defined.
1289 * If `XDG_CACHE_HOME` is undefined, the directory that serves as a common
1290 * repository for temporary Internet files is used instead. A typical path is
1291 * `C:\Documents and Settings\username\Local Settings\Temporary Internet Files`.
1292 * 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).
1294 * Returns: (type filename): a string owned by GLib that must not be modified
1299 g_get_user_cache_dir (void)
1301 gchar
*cache_dir
= NULL
;
1303 G_LOCK (g_utils_global
);
1305 if (!g_user_cache_dir
)
1307 const gchar
*cache_dir_env
= g_getenv ("XDG_CACHE_HOME");
1309 if (cache_dir_env
&& cache_dir_env
[0])
1310 cache_dir
= g_strdup (cache_dir_env
);
1313 cache_dir
= get_special_folder (CSIDL_INTERNET_CACHE
);
1315 if (!cache_dir
|| !cache_dir
[0])
1317 const gchar
*home_dir
= g_get_home_dir ();
1320 cache_dir
= g_build_filename (home_dir
, ".cache", NULL
);
1322 cache_dir
= g_build_filename (g_get_tmp_dir (), g_get_user_name (), ".cache", NULL
);
1324 g_user_cache_dir
= cache_dir
;
1327 cache_dir
= g_user_cache_dir
;
1329 G_UNLOCK (g_utils_global
);
1335 * g_get_user_runtime_dir:
1337 * Returns a directory that is unique to the current user on the local
1340 * This is determined using the mechanisms described
1342 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
1343 * This is the directory
1344 * specified in the `XDG_RUNTIME_DIR` environment variable.
1345 * In the case that this variable is not set, we return the value of
1346 * g_get_user_cache_dir(), after verifying that it exists.
1348 * Returns: (type filename): a string owned by GLib that must not be
1349 * modified or freed.
1354 g_get_user_runtime_dir (void)
1356 static const gchar
*runtime_dir
;
1358 if (g_once_init_enter (&runtime_dir
))
1362 dir
= g_strdup (getenv ("XDG_RUNTIME_DIR"));
1366 /* No need to strdup this one since it is valid forever. */
1367 dir
= g_get_user_cache_dir ();
1369 /* The user should be able to rely on the directory existing
1370 * when the function returns. Probably it already does, but
1371 * let's make sure. Just do mkdir() directly since it will be
1372 * no more expensive than a stat() in the case that the
1373 * directory already exists and is a lot easier.
1375 * $XDG_CACHE_HOME is probably ~/.cache/ so as long as $HOME
1376 * exists this will work. If the user changed $XDG_CACHE_HOME
1377 * then they can make sure that it exists...
1379 (void) g_mkdir (dir
, 0700);
1382 g_assert (dir
!= NULL
);
1384 g_once_init_leave (&runtime_dir
, dir
);
1393 find_folder (OSType type
)
1395 gchar
*filename
= NULL
;
1398 if (FSFindFolder (kUserDomain
, type
, kDontCreateFolder
, &found
) == noErr
)
1400 CFURLRef url
= CFURLCreateFromFSRef (kCFAllocatorSystemDefault
, &found
);
1404 CFStringRef path
= CFURLCopyFileSystemPath (url
, kCFURLPOSIXPathStyle
);
1408 filename
= g_strdup (CFStringGetCStringPtr (path
, kCFStringEncodingUTF8
));
1412 filename
= g_new0 (gchar
, CFStringGetLength (path
) * 3 + 1);
1414 CFStringGetCString (path
, filename
,
1415 CFStringGetLength (path
) * 3 + 1,
1416 kCFStringEncodingUTF8
);
1430 load_user_special_dirs (void)
1432 g_user_special_dirs
[G_USER_DIRECTORY_DESKTOP
] = find_folder (kDesktopFolderType
);
1433 g_user_special_dirs
[G_USER_DIRECTORY_DOCUMENTS
] = find_folder (kDocumentsFolderType
);
1434 g_user_special_dirs
[G_USER_DIRECTORY_DOWNLOAD
] = find_folder (kDesktopFolderType
); /* XXX correct ? */
1435 g_user_special_dirs
[G_USER_DIRECTORY_MUSIC
] = find_folder (kMusicDocumentsFolderType
);
1436 g_user_special_dirs
[G_USER_DIRECTORY_PICTURES
] = find_folder (kPictureDocumentsFolderType
);
1437 g_user_special_dirs
[G_USER_DIRECTORY_PUBLIC_SHARE
] = NULL
;
1438 g_user_special_dirs
[G_USER_DIRECTORY_TEMPLATES
] = NULL
;
1439 g_user_special_dirs
[G_USER_DIRECTORY_VIDEOS
] = find_folder (kMovieDocumentsFolderType
);
1442 #elif defined(G_OS_WIN32)
1445 load_user_special_dirs (void)
1447 typedef HRESULT (WINAPI
*t_SHGetKnownFolderPath
) (const GUID
*rfid
,
1451 t_SHGetKnownFolderPath p_SHGetKnownFolderPath
;
1453 static const GUID FOLDERID_Downloads
=
1454 { 0x374de290, 0x123f, 0x4565, { 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b } };
1455 static const GUID FOLDERID_Public
=
1456 { 0xDFDF76A2, 0xC82A, 0x4D63, { 0x90, 0x6A, 0x56, 0x44, 0xAC, 0x45, 0x73, 0x85 } };
1460 p_SHGetKnownFolderPath
= (t_SHGetKnownFolderPath
) GetProcAddress (GetModuleHandle ("shell32.dll"),
1461 "SHGetKnownFolderPath");
1463 g_user_special_dirs
[G_USER_DIRECTORY_DESKTOP
] = get_special_folder (CSIDL_DESKTOPDIRECTORY
);
1464 g_user_special_dirs
[G_USER_DIRECTORY_DOCUMENTS
] = get_special_folder (CSIDL_PERSONAL
);
1466 if (p_SHGetKnownFolderPath
== NULL
)
1468 g_user_special_dirs
[G_USER_DIRECTORY_DOWNLOAD
] = get_special_folder (CSIDL_DESKTOPDIRECTORY
);
1473 (*p_SHGetKnownFolderPath
) (&FOLDERID_Downloads
, 0, NULL
, &wcp
);
1476 g_user_special_dirs
[G_USER_DIRECTORY_DOWNLOAD
] = g_utf16_to_utf8 (wcp
, -1, NULL
, NULL
, NULL
);
1477 if (g_user_special_dirs
[G_USER_DIRECTORY_DOWNLOAD
] == NULL
)
1478 g_user_special_dirs
[G_USER_DIRECTORY_DOWNLOAD
] = get_special_folder (CSIDL_DESKTOPDIRECTORY
);
1479 CoTaskMemFree (wcp
);
1482 g_user_special_dirs
[G_USER_DIRECTORY_DOWNLOAD
] = get_special_folder (CSIDL_DESKTOPDIRECTORY
);
1485 g_user_special_dirs
[G_USER_DIRECTORY_MUSIC
] = get_special_folder (CSIDL_MYMUSIC
);
1486 g_user_special_dirs
[G_USER_DIRECTORY_PICTURES
] = get_special_folder (CSIDL_MYPICTURES
);
1488 if (p_SHGetKnownFolderPath
== NULL
)
1491 g_user_special_dirs
[G_USER_DIRECTORY_PUBLIC_SHARE
] = get_special_folder (CSIDL_COMMON_DOCUMENTS
);
1496 (*p_SHGetKnownFolderPath
) (&FOLDERID_Public
, 0, NULL
, &wcp
);
1499 g_user_special_dirs
[G_USER_DIRECTORY_PUBLIC_SHARE
] = g_utf16_to_utf8 (wcp
, -1, NULL
, NULL
, NULL
);
1500 if (g_user_special_dirs
[G_USER_DIRECTORY_PUBLIC_SHARE
] == NULL
)
1501 g_user_special_dirs
[G_USER_DIRECTORY_PUBLIC_SHARE
] = get_special_folder (CSIDL_COMMON_DOCUMENTS
);
1502 CoTaskMemFree (wcp
);
1505 g_user_special_dirs
[G_USER_DIRECTORY_PUBLIC_SHARE
] = get_special_folder (CSIDL_COMMON_DOCUMENTS
);
1508 g_user_special_dirs
[G_USER_DIRECTORY_TEMPLATES
] = get_special_folder (CSIDL_TEMPLATES
);
1509 g_user_special_dirs
[G_USER_DIRECTORY_VIDEOS
] = get_special_folder (CSIDL_MYVIDEO
);
1512 #else /* default is unix */
1514 /* adapted from xdg-user-dir-lookup.c
1516 * Copyright (C) 2007 Red Hat Inc.
1518 * Permission is hereby granted, free of charge, to any person
1519 * obtaining a copy of this software and associated documentation files
1520 * (the "Software"), to deal in the Software without restriction,
1521 * including without limitation the rights to use, copy, modify, merge,
1522 * publish, distribute, sublicense, and/or sell copies of the Software,
1523 * and to permit persons to whom the Software is furnished to do so,
1524 * subject to the following conditions:
1526 * The above copyright notice and this permission notice shall be
1527 * included in all copies or substantial portions of the Software.
1529 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1530 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1531 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1532 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
1533 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
1534 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
1535 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1539 load_user_special_dirs (void)
1546 g_init_user_config_dir ();
1547 config_file
= g_build_filename (g_user_config_dir
,
1551 if (!g_file_get_contents (config_file
, &data
, NULL
, NULL
))
1553 g_free (config_file
);
1557 lines
= g_strsplit (data
, "\n", -1);
1558 n_lines
= g_strv_length (lines
);
1561 for (i
= 0; i
< n_lines
; i
++)
1563 gchar
*buffer
= lines
[i
];
1566 gboolean is_relative
= FALSE
;
1567 GUserDirectory directory
;
1569 /* Remove newline at end */
1570 len
= strlen (buffer
);
1571 if (len
> 0 && buffer
[len
- 1] == '\n')
1572 buffer
[len
- 1] = 0;
1575 while (*p
== ' ' || *p
== '\t')
1578 if (strncmp (p
, "XDG_DESKTOP_DIR", strlen ("XDG_DESKTOP_DIR")) == 0)
1580 directory
= G_USER_DIRECTORY_DESKTOP
;
1581 p
+= strlen ("XDG_DESKTOP_DIR");
1583 else if (strncmp (p
, "XDG_DOCUMENTS_DIR", strlen ("XDG_DOCUMENTS_DIR")) == 0)
1585 directory
= G_USER_DIRECTORY_DOCUMENTS
;
1586 p
+= strlen ("XDG_DOCUMENTS_DIR");
1588 else if (strncmp (p
, "XDG_DOWNLOAD_DIR", strlen ("XDG_DOWNLOAD_DIR")) == 0)
1590 directory
= G_USER_DIRECTORY_DOWNLOAD
;
1591 p
+= strlen ("XDG_DOWNLOAD_DIR");
1593 else if (strncmp (p
, "XDG_MUSIC_DIR", strlen ("XDG_MUSIC_DIR")) == 0)
1595 directory
= G_USER_DIRECTORY_MUSIC
;
1596 p
+= strlen ("XDG_MUSIC_DIR");
1598 else if (strncmp (p
, "XDG_PICTURES_DIR", strlen ("XDG_PICTURES_DIR")) == 0)
1600 directory
= G_USER_DIRECTORY_PICTURES
;
1601 p
+= strlen ("XDG_PICTURES_DIR");
1603 else if (strncmp (p
, "XDG_PUBLICSHARE_DIR", strlen ("XDG_PUBLICSHARE_DIR")) == 0)
1605 directory
= G_USER_DIRECTORY_PUBLIC_SHARE
;
1606 p
+= strlen ("XDG_PUBLICSHARE_DIR");
1608 else if (strncmp (p
, "XDG_TEMPLATES_DIR", strlen ("XDG_TEMPLATES_DIR")) == 0)
1610 directory
= G_USER_DIRECTORY_TEMPLATES
;
1611 p
+= strlen ("XDG_TEMPLATES_DIR");
1613 else if (strncmp (p
, "XDG_VIDEOS_DIR", strlen ("XDG_VIDEOS_DIR")) == 0)
1615 directory
= G_USER_DIRECTORY_VIDEOS
;
1616 p
+= strlen ("XDG_VIDEOS_DIR");
1621 while (*p
== ' ' || *p
== '\t')
1628 while (*p
== ' ' || *p
== '\t')
1635 if (strncmp (p
, "$HOME", 5) == 0)
1643 d
= strrchr (p
, '"');
1650 /* remove trailing slashes */
1652 if (d
[len
- 1] == '/')
1657 g_user_special_dirs
[directory
] = g_build_filename (g_get_home_dir (), d
, NULL
);
1660 g_user_special_dirs
[directory
] = g_strdup (d
);
1664 g_free (config_file
);
1667 #endif /* platform-specific load_user_special_dirs implementations */
1671 * g_reload_user_special_dirs_cache:
1673 * Resets the cache used for g_get_user_special_dir(), so
1674 * that the latest on-disk version is used. Call this only
1675 * if you just changed the data on disk yourself.
1677 * Due to threadsafety issues this may cause leaking of strings
1678 * that were previously returned from g_get_user_special_dir()
1679 * that can't be freed. We ensure to only leak the data for
1680 * the directories that actually changed value though.
1685 g_reload_user_special_dirs_cache (void)
1689 G_LOCK (g_utils_global
);
1691 if (g_user_special_dirs
!= NULL
)
1693 /* save a copy of the pointer, to check if some memory can be preserved */
1694 char **old_g_user_special_dirs
= g_user_special_dirs
;
1697 /* recreate and reload our cache */
1698 g_user_special_dirs
= g_new0 (gchar
*, G_USER_N_DIRECTORIES
);
1699 load_user_special_dirs ();
1701 /* only leak changed directories */
1702 for (i
= 0; i
< G_USER_N_DIRECTORIES
; i
++)
1704 old_val
= old_g_user_special_dirs
[i
];
1705 if (g_user_special_dirs
[i
] == NULL
)
1707 g_user_special_dirs
[i
] = old_val
;
1709 else if (g_strcmp0 (old_val
, g_user_special_dirs
[i
]) == 0)
1712 g_free (g_user_special_dirs
[i
]);
1713 g_user_special_dirs
[i
] = old_val
;
1719 /* free the old array */
1720 g_free (old_g_user_special_dirs
);
1723 G_UNLOCK (g_utils_global
);
1727 * g_get_user_special_dir:
1728 * @directory: the logical id of special directory
1730 * Returns the full path of a special directory using its logical id.
1732 * On UNIX this is done using the XDG special user directories.
1733 * For compatibility with existing practise, %G_USER_DIRECTORY_DESKTOP
1734 * falls back to `$HOME/Desktop` when XDG special user directories have
1737 * Depending on the platform, the user might be able to change the path
1738 * of the special directory without requiring the session to restart; GLib
1739 * will not reflect any change once the special directories are loaded.
1741 * Returns: (type filename): the path to the specified special directory, or
1742 * %NULL if the logical id was not found. The returned string is owned by
1743 * GLib and should not be modified or freed.
1748 g_get_user_special_dir (GUserDirectory directory
)
1750 g_return_val_if_fail (directory
>= G_USER_DIRECTORY_DESKTOP
&&
1751 directory
< G_USER_N_DIRECTORIES
, NULL
);
1753 G_LOCK (g_utils_global
);
1755 if (G_UNLIKELY (g_user_special_dirs
== NULL
))
1757 g_user_special_dirs
= g_new0 (gchar
*, G_USER_N_DIRECTORIES
);
1759 load_user_special_dirs ();
1761 /* Special-case desktop for historical compatibility */
1762 if (g_user_special_dirs
[G_USER_DIRECTORY_DESKTOP
] == NULL
)
1763 g_user_special_dirs
[G_USER_DIRECTORY_DESKTOP
] = g_build_filename (g_get_home_dir (), "Desktop", NULL
);
1766 G_UNLOCK (g_utils_global
);
1768 return g_user_special_dirs
[directory
];
1773 #undef g_get_system_data_dirs
1776 get_module_for_address (gconstpointer address
)
1778 /* Holds the g_utils_global lock */
1780 static gboolean beenhere
= FALSE
;
1781 typedef BOOL (WINAPI
*t_GetModuleHandleExA
) (DWORD
, LPCTSTR
, HMODULE
*);
1782 static t_GetModuleHandleExA p_GetModuleHandleExA
= NULL
;
1783 HMODULE hmodule
= NULL
;
1790 p_GetModuleHandleExA
=
1791 (t_GetModuleHandleExA
) GetProcAddress (GetModuleHandle ("kernel32.dll"),
1792 "GetModuleHandleExA");
1796 if (p_GetModuleHandleExA
== NULL
||
1797 !(*p_GetModuleHandleExA
) (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
|
1798 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
,
1801 MEMORY_BASIC_INFORMATION mbi
;
1802 VirtualQuery (address
, &mbi
, sizeof (mbi
));
1803 hmodule
= (HMODULE
) mbi
.AllocationBase
;
1810 get_module_share_dir (gconstpointer address
)
1816 hmodule
= get_module_for_address (address
);
1817 if (hmodule
== NULL
)
1820 filename
= g_win32_get_package_installation_directory_of_module (hmodule
);
1821 retval
= g_build_filename (filename
, "share", NULL
);
1827 static const gchar
* const *
1828 g_win32_get_system_data_dirs_for_module_real (void (*address_of_function
)(void))
1832 static GHashTable
*per_module_data_dirs
= NULL
;
1838 if (address_of_function
)
1840 G_LOCK (g_utils_global
);
1841 hmodule
= get_module_for_address (address_of_function
);
1842 if (hmodule
!= NULL
)
1844 if (per_module_data_dirs
== NULL
)
1845 per_module_data_dirs
= g_hash_table_new (NULL
, NULL
);
1848 retval
= g_hash_table_lookup (per_module_data_dirs
, hmodule
);
1852 G_UNLOCK (g_utils_global
);
1853 return (const gchar
* const *) retval
;
1859 data_dirs
= g_array_new (TRUE
, TRUE
, sizeof (char *));
1861 /* Documents and Settings\All Users\Application Data */
1862 p
= get_special_folder (CSIDL_COMMON_APPDATA
);
1864 g_array_append_val (data_dirs
, p
);
1866 /* Documents and Settings\All Users\Documents */
1867 p
= get_special_folder (CSIDL_COMMON_DOCUMENTS
);
1869 g_array_append_val (data_dirs
, p
);
1871 /* Using the above subfolders of Documents and Settings perhaps
1872 * makes sense from a Windows perspective.
1874 * But looking at the actual use cases of this function in GTK+
1875 * and GNOME software, what we really want is the "share"
1876 * subdirectory of the installation directory for the package
1877 * our caller is a part of.
1879 * The address_of_function parameter, if non-NULL, points to a
1880 * function in the calling module. Use that to determine that
1881 * module's installation folder, and use its "share" subfolder.
1883 * Additionally, also use the "share" subfolder of the installation
1884 * locations of GLib and the .exe file being run.
1886 * To guard against none of the above being what is really wanted,
1887 * callers of this function should have Win32-specific code to look
1888 * up their installation folder themselves, and handle a subfolder
1889 * "share" of it in the same way as the folders returned from this
1893 p
= get_module_share_dir (address_of_function
);
1895 g_array_append_val (data_dirs
, p
);
1897 if (glib_dll
!= NULL
)
1899 gchar
*glib_root
= g_win32_get_package_installation_directory_of_module (glib_dll
);
1900 p
= g_build_filename (glib_root
, "share", NULL
);
1902 g_array_append_val (data_dirs
, p
);
1906 exe_root
= g_win32_get_package_installation_directory_of_module (NULL
);
1907 p
= g_build_filename (exe_root
, "share", NULL
);
1909 g_array_append_val (data_dirs
, p
);
1912 retval
= (gchar
**) g_array_free (data_dirs
, FALSE
);
1914 if (address_of_function
)
1916 if (hmodule
!= NULL
)
1917 g_hash_table_insert (per_module_data_dirs
, hmodule
, retval
);
1918 G_UNLOCK (g_utils_global
);
1921 return (const gchar
* const *) retval
;
1924 const gchar
* const *
1925 g_win32_get_system_data_dirs_for_module (void (*address_of_function
)(void))
1927 gboolean should_call_g_get_system_data_dirs
;
1929 should_call_g_get_system_data_dirs
= TRUE
;
1930 /* These checks are the same as the ones that g_get_system_data_dirs() does.
1931 * Please keep them in sync.
1933 G_LOCK (g_utils_global
);
1935 if (!g_system_data_dirs
)
1937 const gchar
*data_dirs
= g_getenv ("XDG_DATA_DIRS");
1939 if (!data_dirs
|| !data_dirs
[0])
1940 should_call_g_get_system_data_dirs
= FALSE
;
1943 G_UNLOCK (g_utils_global
);
1945 /* There is a subtle difference between g_win32_get_system_data_dirs_for_module (NULL),
1946 * which is what GLib code can normally call,
1947 * and g_win32_get_system_data_dirs_for_module (&_g_win32_get_system_data_dirs),
1948 * which is what the inline function used by non-GLib code calls.
1949 * The former gets prefix relative to currently-running executable,
1950 * the latter - relative to the module that calls _g_win32_get_system_data_dirs()
1951 * (disguised as g_get_system_data_dirs()), which could be an executable or
1952 * a DLL that is located somewhere else.
1953 * This is why that inline function in gutils.h exists, and why we can't just
1954 * call g_get_system_data_dirs() from there - because we need to get the address
1955 * local to the non-GLib caller-module.
1959 * g_get_system_data_dirs() will fall back to calling
1960 * g_win32_get_system_data_dirs_for_module_real(NULL) if XDG_DATA_DIRS is NULL
1961 * or an empty string. The checks above ensure that we do not call it in such
1962 * cases and use the address_of_function that we've been given by the inline function.
1963 * The reason we're calling g_get_system_data_dirs /at all/ is to give
1964 * XDG_DATA_DIRS precedence (if it is set).
1966 if (should_call_g_get_system_data_dirs
)
1967 return g_get_system_data_dirs ();
1969 return g_win32_get_system_data_dirs_for_module_real (address_of_function
);
1975 * g_get_system_data_dirs:
1977 * Returns an ordered list of base directories in which to access
1978 * system-wide application data.
1980 * On UNIX platforms this is determined using the mechanisms described
1982 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec)
1983 * In this case the list of directories retrieved will be `XDG_DATA_DIRS`.
1985 * On Windows it follows XDG Base Directory Specification if `XDG_DATA_DIRS` is defined.
1986 * If `XDG_DATA_DIRS` is undefined,
1987 * the first elements in the list are the Application Data
1988 * and Documents folders for All Users. (These can be determined only
1989 * on Windows 2000 or later and are not present in the list on other
1990 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and
1991 * CSIDL_COMMON_DOCUMENTS.
1993 * Then follows the "share" subfolder in the installation folder for
1994 * the package containing the DLL that calls this function, if it can
1997 * Finally the list contains the "share" subfolder in the installation
1998 * folder for GLib, and in the installation folder for the package the
1999 * application's .exe file belongs to.
2001 * The installation folders above are determined by looking up the
2002 * folder where the module (DLL or EXE) in question is located. If the
2003 * folder's name is "bin", its parent is used, otherwise the folder
2006 * Note that on Windows the returned list can vary depending on where
2007 * this function is called.
2009 * Returns: (array zero-terminated=1) (element-type filename) (transfer none):
2010 * a %NULL-terminated array of strings owned by GLib that must not be
2011 * modified or freed.
2015 const gchar
* const *
2016 g_get_system_data_dirs (void)
2018 gchar
**data_dir_vector
;
2020 /* These checks are the same as the ones that g_win32_get_system_data_dirs_for_module()
2021 * does. Please keep them in sync.
2023 G_LOCK (g_utils_global
);
2025 if (!g_system_data_dirs
)
2027 gchar
*data_dirs
= (gchar
*) g_getenv ("XDG_DATA_DIRS");
2030 if (!data_dirs
|| !data_dirs
[0])
2031 data_dirs
= "/usr/local/share/:/usr/share/";
2033 data_dir_vector
= g_strsplit (data_dirs
, G_SEARCHPATH_SEPARATOR_S
, 0);
2035 if (!data_dirs
|| !data_dirs
[0])
2036 data_dir_vector
= g_strdupv ((gchar
**) g_win32_get_system_data_dirs_for_module_real (NULL
));
2038 data_dir_vector
= g_strsplit (data_dirs
, G_SEARCHPATH_SEPARATOR_S
, 0);
2041 g_system_data_dirs
= data_dir_vector
;
2044 data_dir_vector
= g_system_data_dirs
;
2046 G_UNLOCK (g_utils_global
);
2048 return (const gchar
* const *) data_dir_vector
;
2052 * g_get_system_config_dirs:
2054 * Returns an ordered list of base directories in which to access
2055 * system-wide configuration information.
2057 * On UNIX platforms this is determined using the mechanisms described
2059 * [XDG Base Directory Specification](http://www.freedesktop.org/Standards/basedir-spec).
2060 * In this case the list of directories retrieved will be `XDG_CONFIG_DIRS`.
2062 * On Windows it follows XDG Base Directory Specification if `XDG_CONFIG_DIRS` is defined.
2063 * If `XDG_CONFIG_DIRS` is undefined, the directory that contains application
2064 * data for all users is used instead. A typical path is
2065 * `C:\Documents and Settings\All Users\Application Data`.
2066 * This folder is used for application data
2067 * that is not user specific. For example, an application can store
2068 * a spell-check dictionary, a database of clip art, or a log file in the
2069 * CSIDL_COMMON_APPDATA folder. This information will not roam and is available
2070 * to anyone using the computer.
2072 * Returns: (array zero-terminated=1) (element-type filename) (transfer none):
2073 * a %NULL-terminated array of strings owned by GLib that must not be
2074 * modified or freed.
2078 const gchar
* const *
2079 g_get_system_config_dirs (void)
2081 gchar
**conf_dir_vector
;
2083 G_LOCK (g_utils_global
);
2085 if (!g_system_config_dirs
)
2087 const gchar
*conf_dirs
= g_getenv ("XDG_CONFIG_DIRS");
2091 conf_dir_vector
= g_strsplit (conf_dirs
, G_SEARCHPATH_SEPARATOR_S
, 0);
2095 gchar
*special_conf_dirs
= get_special_folder (CSIDL_COMMON_APPDATA
);
2097 if (special_conf_dirs
)
2098 conf_dir_vector
= g_strsplit (special_conf_dirs
, G_SEARCHPATH_SEPARATOR_S
, 0);
2100 /* Return empty list */
2101 conf_dir_vector
= g_strsplit ("", G_SEARCHPATH_SEPARATOR_S
, 0);
2103 g_free (special_conf_dirs
);
2106 if (!conf_dirs
|| !conf_dirs
[0])
2107 conf_dirs
= "/etc/xdg";
2109 conf_dir_vector
= g_strsplit (conf_dirs
, G_SEARCHPATH_SEPARATOR_S
, 0);
2112 g_system_config_dirs
= conf_dir_vector
;
2115 conf_dir_vector
= g_system_config_dirs
;
2116 G_UNLOCK (g_utils_global
);
2118 return (const gchar
* const *) conf_dir_vector
;
2122 * g_nullify_pointer:
2123 * @nullify_location: (not nullable): the memory address of the pointer.
2125 * Set the pointer at the specified location to %NULL.
2128 g_nullify_pointer (gpointer
*nullify_location
)
2130 g_return_if_fail (nullify_location
!= NULL
);
2132 *nullify_location
= NULL
;
2135 #define KILOBYTE_FACTOR (G_GOFFSET_CONSTANT (1000))
2136 #define MEGABYTE_FACTOR (KILOBYTE_FACTOR * KILOBYTE_FACTOR)
2137 #define GIGABYTE_FACTOR (MEGABYTE_FACTOR * KILOBYTE_FACTOR)
2138 #define TERABYTE_FACTOR (GIGABYTE_FACTOR * KILOBYTE_FACTOR)
2139 #define PETABYTE_FACTOR (TERABYTE_FACTOR * KILOBYTE_FACTOR)
2140 #define EXABYTE_FACTOR (PETABYTE_FACTOR * KILOBYTE_FACTOR)
2142 #define KIBIBYTE_FACTOR (G_GOFFSET_CONSTANT (1024))
2143 #define MEBIBYTE_FACTOR (KIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2144 #define GIBIBYTE_FACTOR (MEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2145 #define TEBIBYTE_FACTOR (GIBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2146 #define PEBIBYTE_FACTOR (TEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2147 #define EXBIBYTE_FACTOR (PEBIBYTE_FACTOR * KIBIBYTE_FACTOR)
2151 * @size: a size in bytes
2153 * Formats a size (for example the size of a file) into a human readable
2154 * string. Sizes are rounded to the nearest size prefix (kB, MB, GB)
2155 * and are displayed rounded to the nearest tenth. E.g. the file size
2156 * 3292528 bytes will be converted into the string "3.2 MB".
2158 * The prefix units base is 1000 (i.e. 1 kB is 1000 bytes).
2160 * This string should be freed with g_free() when not needed any longer.
2162 * See g_format_size_full() for more options about how the size might be
2165 * Returns: a newly-allocated formatted string containing a human readable
2171 g_format_size (guint64 size
)
2173 return g_format_size_full (size
, G_FORMAT_SIZE_DEFAULT
);
2178 * @G_FORMAT_SIZE_DEFAULT: behave the same as g_format_size()
2179 * @G_FORMAT_SIZE_LONG_FORMAT: include the exact number of bytes as part
2180 * of the returned string. For example, "45.6 kB (45,612 bytes)".
2181 * @G_FORMAT_SIZE_IEC_UNITS: use IEC (base 1024) units with "KiB"-style
2182 * suffixes. IEC units should only be used for reporting things with
2183 * a strong "power of 2" basis, like RAM sizes or RAID stripe sizes.
2184 * Network and storage sizes should be reported in the normal SI units.
2185 * @G_FORMAT_SIZE_BITS: set the size as a quantity in bits, rather than
2186 * bytes, and return units in bits. For example, ‘Mb’ rather than ‘MB’.
2188 * Flags to modify the format of the string returned by g_format_size_full().
2191 #pragma GCC diagnostic push
2192 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
2195 * g_format_size_full:
2196 * @size: a size in bytes
2197 * @flags: #GFormatSizeFlags to modify the output
2201 * This function is similar to g_format_size() but allows for flags
2202 * that modify the output. See #GFormatSizeFlags.
2204 * Returns: a newly-allocated formatted string containing a human
2205 * readable file size
2210 g_format_size_full (guint64 size
,
2211 GFormatSizeFlags flags
)
2227 const struct Format formats
[4][6] = {
2229 { KILOBYTE_FACTOR
, N_("%.1f kB") },
2230 { MEGABYTE_FACTOR
, N_("%.1f MB") },
2231 { GIGABYTE_FACTOR
, N_("%.1f GB") },
2232 { TERABYTE_FACTOR
, N_("%.1f TB") },
2233 { PETABYTE_FACTOR
, N_("%.1f PB") },
2234 { EXABYTE_FACTOR
, N_("%.1f EB") }
2237 { KIBIBYTE_FACTOR
, N_("%.1f KiB") },
2238 { MEBIBYTE_FACTOR
, N_("%.1f MiB") },
2239 { GIBIBYTE_FACTOR
, N_("%.1f GiB") },
2240 { TEBIBYTE_FACTOR
, N_("%.1f TiB") },
2241 { PEBIBYTE_FACTOR
, N_("%.1f PiB") },
2242 { EXBIBYTE_FACTOR
, N_("%.1f EiB") }
2245 { KILOBYTE_FACTOR
, N_("%.1f kb") },
2246 { MEGABYTE_FACTOR
, N_("%.1f Mb") },
2247 { GIGABYTE_FACTOR
, N_("%.1f Gb") },
2248 { TERABYTE_FACTOR
, N_("%.1f Tb") },
2249 { PETABYTE_FACTOR
, N_("%.1f Pb") },
2250 { EXABYTE_FACTOR
, N_("%.1f Eb") }
2253 { KIBIBYTE_FACTOR
, N_("%.1f Kib") },
2254 { MEBIBYTE_FACTOR
, N_("%.1f Mib") },
2255 { GIBIBYTE_FACTOR
, N_("%.1f Gib") },
2256 { TEBIBYTE_FACTOR
, N_("%.1f Tib") },
2257 { PEBIBYTE_FACTOR
, N_("%.1f Pib") },
2258 { EXBIBYTE_FACTOR
, N_("%.1f Eib") }
2265 string
= g_string_new (NULL
);
2267 switch (flags
& ~G_FORMAT_SIZE_LONG_FORMAT
)
2269 case G_FORMAT_SIZE_DEFAULT
:
2270 index
= FORMAT_BYTES
;
2272 case (G_FORMAT_SIZE_DEFAULT
| G_FORMAT_SIZE_IEC_UNITS
):
2273 index
= FORMAT_BYTES_IEC
;
2275 case G_FORMAT_SIZE_BITS
:
2276 index
= FORMAT_BITS
;
2278 case (G_FORMAT_SIZE_BITS
| G_FORMAT_SIZE_IEC_UNITS
):
2279 index
= FORMAT_BITS_IEC
;
2282 g_assert_not_reached ();
2286 if (size
< formats
[index
][0].factor
)
2288 const char * format
;
2290 if (index
== FORMAT_BYTES
|| index
== FORMAT_BYTES_IEC
)
2292 format
= g_dngettext (GETTEXT_PACKAGE
, "%u byte", "%u bytes", (guint
) size
);
2296 format
= g_dngettext (GETTEXT_PACKAGE
, "%u bit", "%u bits", (guint
) size
);
2299 g_string_printf (string
, format
, (guint
) size
);
2301 flags
&= ~G_FORMAT_SIZE_LONG_FORMAT
;
2305 const gsize n
= G_N_ELEMENTS (formats
[index
]);
2309 * Point the last format (the highest unit) by default
2310 * and then then scan all formats, starting with the 2nd one
2311 * because the 1st is already managed by with the plural form
2313 const struct Format
* f
= &formats
[index
][n
- 1];
2315 for (i
= 1; i
< n
; i
++)
2317 if (size
< formats
[index
][i
].factor
)
2319 f
= &formats
[index
][i
- 1];
2324 g_string_printf (string
, _(f
->string
), (gdouble
) size
/ (gdouble
) f
->factor
);
2327 if (flags
& G_FORMAT_SIZE_LONG_FORMAT
)
2329 /* First problem: we need to use the number of bytes to decide on
2330 * the plural form that is used for display, but the number of
2331 * bytes potentially exceeds the size of a guint (which is what
2332 * ngettext() takes).
2334 * From a pragmatic standpoint, it seems that all known languages
2335 * base plural forms on one or both of the following:
2337 * - the lowest digits of the number
2339 * - if the number if greater than some small value
2341 * Here's how we fake it: Draw an arbitrary line at one thousand.
2342 * If the number is below that, then fine. If it is above it,
2343 * then we take the modulus of the number by one thousand (in
2344 * order to keep the lowest digits) and add one thousand to that
2345 * (in order to ensure that 1001 is not treated the same as 1).
2347 guint plural_form
= size
< 1000 ? size
: size
% 1000 + 1000;
2349 /* Second problem: we need to translate the string "%u byte/bit" and
2350 * "%u bytes/bits" for pluralisation, but the correct number format to
2351 * use for a gsize is different depending on which architecture
2354 * Solution: format the number separately and use "%s bytes/bits" on
2357 const gchar
*translated_format
;
2358 gchar
*formatted_number
;
2360 if (index
== FORMAT_BYTES
|| index
== FORMAT_BYTES_IEC
)
2362 /* Translators: the %s in "%s bytes" will always be replaced by a number. */
2363 translated_format
= g_dngettext (GETTEXT_PACKAGE
, "%s byte", "%s bytes", plural_form
);
2367 /* Translators: the %s in "%s bits" will always be replaced by a number. */
2368 translated_format
= g_dngettext (GETTEXT_PACKAGE
, "%s bit", "%s bits", plural_form
);
2370 /* XXX: Windows doesn't support the "'" format modifier, so we
2371 * must not use it there. Instead, just display the number
2372 * without separation. Bug #655336 is open until a solution is
2376 formatted_number
= g_strdup_printf ("%'"G_GUINT64_FORMAT
, size
);
2378 formatted_number
= g_strdup_printf ("%"G_GUINT64_FORMAT
, size
);
2381 g_string_append (string
, " (");
2382 g_string_append_printf (string
, translated_format
, formatted_number
);
2383 g_free (formatted_number
);
2384 g_string_append (string
, ")");
2387 return g_string_free (string
, FALSE
);
2390 #pragma GCC diagnostic pop
2393 * g_format_size_for_display:
2394 * @size: a size in bytes
2396 * Formats a size (for example the size of a file) into a human
2397 * readable string. Sizes are rounded to the nearest size prefix
2398 * (KB, MB, GB) and are displayed rounded to the nearest tenth.
2399 * E.g. the file size 3292528 bytes will be converted into the
2402 * The prefix units base is 1024 (i.e. 1 KB is 1024 bytes).
2404 * This string should be freed with g_free() when not needed any longer.
2406 * Returns: a newly-allocated formatted string containing a human
2407 * readable file size
2411 * Deprecated:2.30: This function is broken due to its use of SI
2412 * suffixes to denote IEC units. Use g_format_size() instead.
2415 g_format_size_for_display (goffset size
)
2417 if (size
< (goffset
) KIBIBYTE_FACTOR
)
2418 return g_strdup_printf (g_dngettext(GETTEXT_PACKAGE
, "%u byte", "%u bytes",(guint
) size
), (guint
) size
);
2421 gdouble displayed_size
;
2423 if (size
< (goffset
) MEBIBYTE_FACTOR
)
2425 displayed_size
= (gdouble
) size
/ (gdouble
) KIBIBYTE_FACTOR
;
2426 /* Translators: this is from the deprecated function g_format_size_for_display() which uses 'KB' to
2427 * mean 1024 bytes. I am aware that 'KB' is not correct, but it has been preserved for reasons of
2428 * compatibility. Users will not see this string unless a program is using this deprecated function.
2429 * Please translate as literally as possible.
2431 return g_strdup_printf (_("%.1f KB"), displayed_size
);
2433 else if (size
< (goffset
) GIBIBYTE_FACTOR
)
2435 displayed_size
= (gdouble
) size
/ (gdouble
) MEBIBYTE_FACTOR
;
2436 return g_strdup_printf (_("%.1f MB"), displayed_size
);
2438 else if (size
< (goffset
) TEBIBYTE_FACTOR
)
2440 displayed_size
= (gdouble
) size
/ (gdouble
) GIBIBYTE_FACTOR
;
2441 return g_strdup_printf (_("%.1f GB"), displayed_size
);
2443 else if (size
< (goffset
) PEBIBYTE_FACTOR
)
2445 displayed_size
= (gdouble
) size
/ (gdouble
) TEBIBYTE_FACTOR
;
2446 return g_strdup_printf (_("%.1f TB"), displayed_size
);
2448 else if (size
< (goffset
) EXBIBYTE_FACTOR
)
2450 displayed_size
= (gdouble
) size
/ (gdouble
) PEBIBYTE_FACTOR
;
2451 return g_strdup_printf (_("%.1f PB"), displayed_size
);
2455 displayed_size
= (gdouble
) size
/ (gdouble
) EXBIBYTE_FACTOR
;
2456 return g_strdup_printf (_("%.1f EB"), displayed_size
);
2461 #if defined (G_OS_WIN32) && !defined (_WIN64)
2463 /* Binary compatibility versions. Not for newly compiled code. */
2465 _GLIB_EXTERN
const gchar
*g_get_user_name_utf8 (void);
2466 _GLIB_EXTERN
const gchar
*g_get_real_name_utf8 (void);
2467 _GLIB_EXTERN
const gchar
*g_get_home_dir_utf8 (void);
2468 _GLIB_EXTERN
const gchar
*g_get_tmp_dir_utf8 (void);
2469 _GLIB_EXTERN gchar
*g_find_program_in_path_utf8 (const gchar
*program
);
2472 g_find_program_in_path_utf8 (const gchar
*program
)
2474 return g_find_program_in_path (program
);
2477 const gchar
*g_get_user_name_utf8 (void) { return g_get_user_name (); }
2478 const gchar
*g_get_real_name_utf8 (void) { return g_get_real_name (); }
2479 const gchar
*g_get_home_dir_utf8 (void) { return g_get_home_dir (); }
2480 const gchar
*g_get_tmp_dir_utf8 (void) { return g_get_tmp_dir (); }
2486 * Returns %TRUE if the current process was executed as setuid (or an
2487 * equivalent __libc_enable_secure is available). See:
2488 * http://osdir.com/ml/linux.lfs.hardened/2007-04/msg00032.html
2491 g_check_setuid (void)
2493 /* TODO: get __libc_enable_secure exported from glibc.
2494 * See http://www.openwall.com/lists/owl-dev/2012/08/14/1
2496 #if 0 && defined(HAVE_LIBC_ENABLE_SECURE)
2498 /* See glibc/include/unistd.h */
2499 extern int __libc_enable_secure
;
2500 return __libc_enable_secure
;
2502 #elif defined(HAVE_ISSETUGID) && !defined(__BIONIC__)
2503 /* BSD: http://www.freebsd.org/cgi/man.cgi?query=issetugid&sektion=2 */
2505 /* Android had it in older versions but the new 64 bit ABI does not
2506 * have it anymore, and some versions of the 32 bit ABI neither.
2507 * https://code.google.com/p/android-developer-preview/issues/detail?id=168
2509 return issetugid ();
2510 #elif defined(G_OS_UNIX)
2511 uid_t ruid
, euid
, suid
; /* Real, effective and saved user ID's */
2512 gid_t rgid
, egid
, sgid
; /* Real, effective and saved group ID's */
2514 static gsize check_setuid_initialised
;
2515 static gboolean is_setuid
;
2517 if (g_once_init_enter (&check_setuid_initialised
))
2519 #ifdef HAVE_GETRESUID
2520 /* These aren't in the header files, so we prototype them here.
2522 int getresuid(uid_t
*ruid
, uid_t
*euid
, uid_t
*suid
);
2523 int getresgid(gid_t
*rgid
, gid_t
*egid
, gid_t
*sgid
);
2525 if (getresuid (&ruid
, &euid
, &suid
) != 0 ||
2526 getresgid (&rgid
, &egid
, &sgid
) != 0)
2527 #endif /* HAVE_GETRESUID */
2529 suid
= ruid
= getuid ();
2530 sgid
= rgid
= getgid ();
2535 is_setuid
= (ruid
!= euid
|| ruid
!= suid
||
2536 rgid
!= egid
|| rgid
!= sgid
);
2538 g_once_init_leave (&check_setuid_initialised
, 1);
2550 * A wrapper for the POSIX abort() function.
2552 * On Windows it is a function that makes extra effort (including a call
2553 * to abort()) to ensure that a debugger-catchable exception is thrown
2554 * before the program terminates.
2556 * See your C library manual for more details about abort().
2563 /* One call to break the debugger */
2565 /* One call in case CRT does get saner about abort() behaviour */
2567 /* And one call to bind them all and terminate the program for sure */