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 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, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 * MT safe for the unix part, FIXME: make the win32 part MT safe as well.
33 * @Title: Version Information
34 * @Short_description: Variables and functions to check the GLib version
36 * GLib provides version information, primarily useful in configure
37 * checks for builds that have a configure script. Applications will
38 * not typically use the features described here.
44 * The major version number of the GLib library.
46 * Like #glib_major_version, but from the headers used at
47 * application compile time, rather than from the library
48 * linked against at application run time.
54 * The minor version number of the GLib library.
56 * Like #gtk_minor_version, but from the headers used at
57 * application compile time, rather than from the library
58 * linked against at application run time.
64 * The micro version number of the GLib library.
66 * Like #gtk_micro_version, but from the headers used at
67 * application compile time, rather than from the library
68 * linked against at application run time.
81 #include <ctype.h> /* For tolower() */
83 #include <sys/types.h>
88 #include <sys/types.h>
89 #ifdef HAVE_SYS_PARAM_H
90 #include <sys/param.h>
92 #ifdef HAVE_CRT_EXTERNS_H
93 #include <crt_externs.h> /* for _NSGetEnviron */
96 /* implement gutils's inline functions
98 #define G_IMPLEMENT_INLINES 1
102 #include "gfileutils.h"
105 #include "gprintfint.h"
107 #include "gthreadprivate.h"
108 #include "gtestutils.h"
109 #include "gunicode.h"
110 #include "gstrfuncs.h"
112 #include "glibintl.h"
114 #ifdef G_PLATFORM_WIN32
116 #include "gconvert.h"
123 * @title: Miscellaneous Utility Functions
124 * @short_description: a selection of portable utility functions
126 * These are portable utility functions.
130 #define G_PATH_LENGTH MAXPATHLEN
131 #elif defined (PATH_MAX)
132 #define G_PATH_LENGTH PATH_MAX
133 #elif defined (_PC_PATH_MAX)
134 #define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
136 #define G_PATH_LENGTH 2048
139 #ifdef G_PLATFORM_WIN32
140 # define STRICT /* Strict typing, please */
141 # include <windows.h>
143 # ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
144 # define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
145 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
147 # include <lmcons.h> /* For UNLEN */
148 #endif /* G_PLATFORM_WIN32 */
153 /* older SDK (e.g. msvc 5.0) does not have these*/
154 # ifndef CSIDL_MYMUSIC
155 # define CSIDL_MYMUSIC 13
157 # ifndef CSIDL_MYVIDEO
158 # define CSIDL_MYVIDEO 14
160 # ifndef CSIDL_INTERNET_CACHE
161 # define CSIDL_INTERNET_CACHE 32
163 # ifndef CSIDL_COMMON_APPDATA
164 # define CSIDL_COMMON_APPDATA 35
166 # ifndef CSIDL_MYPICTURES
167 # define CSIDL_MYPICTURES 0x27
169 # ifndef CSIDL_COMMON_DOCUMENTS
170 # define CSIDL_COMMON_DOCUMENTS 46
172 # ifndef CSIDL_PROFILE
173 # define CSIDL_PROFILE 40
175 # include <process.h>
179 #include <CoreServices/CoreServices.h>
183 #include <langinfo.h>
186 const guint glib_major_version = GLIB_MAJOR_VERSION;
187 const guint glib_minor_version = GLIB_MINOR_VERSION;
188 const guint glib_micro_version = GLIB_MICRO_VERSION;
189 const guint glib_interface_age = GLIB_INTERFACE_AGE;
190 const guint glib_binary_age = GLIB_BINARY_AGE;
192 #ifdef G_PLATFORM_WIN32
194 static HMODULE glib_dll = NULL;
199 DllMain (HINSTANCE hinstDLL,
203 if (fdwReason == DLL_PROCESS_ATTACH)
215 _glib_get_dll_directory (void)
219 wchar_t wc_fn[MAX_PATH];
222 if (glib_dll == NULL)
226 /* This code is different from that in
227 * g_win32_get_package_installation_directory_of_module() in that
228 * here we return the actual folder where the GLib DLL is. We don't
229 * do the check for it being in a "bin" or "lib" subfolder and then
230 * returning the parent of that.
232 * In a statically built GLib, glib_dll will be NULL and we will
233 * thus look up the application's .exe file's location.
235 if (!GetModuleFileNameW (glib_dll, wc_fn, MAX_PATH))
238 retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
240 p = strrchr (retval, G_DIR_SEPARATOR);
254 * glib_check_version:
255 * @required_major: the required major version.
256 * @required_minor: the required minor version.
257 * @required_micro: the required micro version.
259 * Checks that the GLib library in use is compatible with the
260 * given version. Generally you would pass in the constants
261 * #GLIB_MAJOR_VERSION, #GLIB_MINOR_VERSION, #GLIB_MICRO_VERSION
262 * as the three arguments to this function; that produces
263 * a check that the library in use is compatible with
264 * the version of GLib the application or module was compiled
267 * Compatibility is defined by two things: first the version
268 * of the running library is newer than the version
269 * @required_major.required_minor.@required_micro. Second
270 * the running library must be binary compatible with the
271 * version @required_major.required_minor.@required_micro
272 * (same major version.)
274 * Return value: %NULL if the GLib library is compatible with the
275 * given version, or a string describing the version mismatch.
276 * The returned string is owned by GLib and must not be modified
282 glib_check_version (guint required_major,
283 guint required_minor,
284 guint required_micro)
286 gint glib_effective_micro = 100 * GLIB_MINOR_VERSION + GLIB_MICRO_VERSION;
287 gint required_effective_micro = 100 * required_minor + required_micro;
289 if (required_major > GLIB_MAJOR_VERSION)
290 return "GLib version too old (major mismatch)";
291 if (required_major < GLIB_MAJOR_VERSION)
292 return "GLib version too new (major mismatch)";
293 if (required_effective_micro < glib_effective_micro - GLIB_BINARY_AGE)
294 return "GLib version too new (micro mismatch)";
295 if (required_effective_micro > glib_effective_micro)
296 return "GLib version too old (micro mismatch)";
300 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
303 * @dest: the destination address to copy the bytes to.
304 * @src: the source address to copy the bytes from.
305 * @len: the number of bytes to copy.
307 * Copies a block of memory @len bytes long, from @src to @dest.
308 * The source and destination areas may overlap.
310 * In order to use this function, you must include
311 * <filename>string.h</filename> yourself, because this macro will
312 * typically simply resolve to memmove() and GLib does not include
313 * <filename>string.h</filename> for you.
316 g_memmove (gpointer dest,
320 gchar* destptr = dest;
321 const gchar* srcptr = src;
322 if (src + len < dest || dest + len < src)
324 bcopy (src, dest, len);
327 else if (dest <= src)
330 *(destptr++) = *(srcptr++);
337 *(--destptr) = *(--srcptr);
340 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
348 * @func: (scope async): the function to call on normal program termination.
350 * Specifies a function to be called at normal program termination.
352 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
353 * macro that maps to a call to the atexit() function in the C
354 * library. This means that in case the code that calls g_atexit(),
355 * i.e. atexit(), is in a DLL, the function will be called when the
356 * DLL is detached from the program. This typically makes more sense
357 * than that the function is called when the GLib DLL is detached,
358 * which happened earlier when g_atexit() was a function in the GLib
361 * The behaviour of atexit() in the context of dynamically loaded
362 * modules is not formally specified and varies wildly.
364 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
365 * loaded module which is unloaded before the program terminates might
366 * well cause a crash at program exit.
368 * Some POSIX systems implement atexit() like Windows, and have each
369 * dynamically loaded module maintain an own atexit chain that is
370 * called when the module is unloaded.
372 * On other POSIX systems, before a dynamically loaded module is
373 * unloaded, the registered atexit functions (if any) residing in that
374 * module are called, regardless where the code that registered them
375 * resided. This is presumably the most robust approach.
377 * As can be seen from the above, for portability it's best to avoid
378 * calling g_atexit() (or atexit()) except in the main executable of a
382 g_atexit (GVoidFunc func)
385 const gchar *error = NULL;
387 /* keep this in sync with glib.h */
389 #ifdef G_NATIVE_ATEXIT
390 result = ATEXIT (func);
392 error = g_strerror (errno);
393 #elif defined (HAVE_ATEXIT)
394 # ifdef NeXT /* @#%@! NeXTStep */
395 result = !atexit ((void (*)(void)) func);
397 error = g_strerror (errno);
399 result = atexit ((void (*)(void)) func);
401 error = g_strerror (errno);
403 #elif defined (HAVE_ON_EXIT)
404 result = on_exit ((void (*)(int, void *)) func, NULL);
406 error = g_strerror (errno);
409 error = "no implementation";
410 #endif /* G_NATIVE_ATEXIT */
413 g_error ("Could not register atexit() function: %s", error);
416 /* Based on execvp() from GNU Libc.
417 * Some of this code is cut-and-pasted into gspawn.c
421 my_strchrnul (const gchar *str,
424 gchar *p = (gchar*)str;
425 while (*p && (*p != c))
433 static gchar *inner_find_program_in_path (const gchar *program);
436 g_find_program_in_path (const gchar *program)
438 const gchar *last_dot = strrchr (program, '.');
440 if (last_dot == NULL ||
441 strchr (last_dot, '\\') != NULL ||
442 strchr (last_dot, '/') != NULL)
444 const gint program_length = strlen (program);
445 gchar *pathext = g_build_path (";",
446 ".exe;.cmd;.bat;.com",
447 g_getenv ("PATHEXT"),
450 gchar *decorated_program;
456 gchar *q = my_strchrnul (p, ';');
458 decorated_program = g_malloc (program_length + (q-p) + 1);
459 memcpy (decorated_program, program, program_length);
460 memcpy (decorated_program+program_length, p, q-p);
461 decorated_program [program_length + (q-p)] = '\0';
463 retval = inner_find_program_in_path (decorated_program);
464 g_free (decorated_program);
472 } while (*p++ != '\0');
477 return inner_find_program_in_path (program);
483 * g_find_program_in_path:
484 * @program: a program name in the GLib file name encoding
486 * Locates the first executable named @program in the user's path, in the
487 * same way that execvp() would locate it. Returns an allocated string
488 * with the absolute path name, or %NULL if the program is not found in
489 * the path. If @program is already an absolute path, returns a copy of
490 * @program if @program exists and is executable, and %NULL otherwise.
492 * On Windows, if @program does not have a file type suffix, tries
493 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
494 * the <envar>PATHEXT</envar> environment variable.
496 * On Windows, it looks for the file in the same way as CreateProcess()
497 * would. This means first in the directory where the executing
498 * program was loaded from, then in the current directory, then in the
499 * Windows 32-bit system directory, then in the Windows directory, and
500 * finally in the directories in the <envar>PATH</envar> environment
501 * variable. If the program is found, the return value contains the
502 * full name including the type suffix.
504 * Return value: absolute path, or %NULL
508 inner_find_program_in_path (const gchar *program)
511 g_find_program_in_path (const gchar *program)
514 const gchar *path, *p;
515 gchar *name, *freeme;
517 const gchar *path_copy;
518 gchar *filename = NULL, *appdir = NULL;
519 gchar *sysdir = NULL, *windir = NULL;
521 wchar_t wfilename[MAXPATHLEN], wsysdir[MAXPATHLEN],
527 g_return_val_if_fail (program != NULL, NULL);
529 /* If it is an absolute path, or a relative path including subdirectories,
530 * don't look in PATH.
532 if (g_path_is_absolute (program)
533 || strchr (program, G_DIR_SEPARATOR) != NULL
535 || strchr (program, '/') != NULL
539 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE) &&
540 !g_file_test (program, G_FILE_TEST_IS_DIR))
541 return g_strdup (program);
546 path = g_getenv ("PATH");
547 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
550 /* There is no `PATH' in the environment. The default
551 * search path in GNU libc is the current directory followed by
552 * the path `confstr' returns for `_CS_PATH'.
555 /* In GLib we put . last, for security, and don't use the
556 * unportable confstr(); UNIX98 does not actually specify
557 * what to search if PATH is unset. POSIX may, dunno.
560 path = "/bin:/usr/bin:.";
563 n = GetModuleFileNameW (NULL, wfilename, MAXPATHLEN);
564 if (n > 0 && n < MAXPATHLEN)
565 filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
567 n = GetSystemDirectoryW (wsysdir, MAXPATHLEN);
568 if (n > 0 && n < MAXPATHLEN)
569 sysdir = g_utf16_to_utf8 (wsysdir, -1, NULL, NULL, NULL);
571 n = GetWindowsDirectoryW (wwindir, MAXPATHLEN);
572 if (n > 0 && n < MAXPATHLEN)
573 windir = g_utf16_to_utf8 (wwindir, -1, NULL, NULL, NULL);
577 appdir = g_path_get_dirname (filename);
581 path = g_strdup (path);
585 const gchar *tem = path;
586 path = g_strconcat (windir, ";", path, NULL);
587 g_free ((gchar *) tem);
593 const gchar *tem = path;
594 path = g_strconcat (sysdir, ";", path, NULL);
595 g_free ((gchar *) tem);
600 const gchar *tem = path;
601 path = g_strconcat (".;", path, NULL);
602 g_free ((gchar *) tem);
607 const gchar *tem = path;
608 path = g_strconcat (appdir, ";", path, NULL);
609 g_free ((gchar *) tem);
616 len = strlen (program) + 1;
617 pathlen = strlen (path);
618 freeme = name = g_malloc (pathlen + len + 1);
620 /* Copy the file name at the top, including '\0' */
621 memcpy (name + pathlen + 1, program, len);
622 name = name + pathlen;
623 /* And add the slash before the filename */
624 *name = G_DIR_SEPARATOR;
632 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
635 /* Two adjacent colons, or a colon at the beginning or the end
636 * of `PATH' means to search the current directory.
640 startp = memcpy (name - (p - path), path, p - path);
642 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE) &&
643 !g_file_test (startp, G_FILE_TEST_IS_DIR))
646 ret = g_strdup (startp);
649 g_free ((gchar *) path_copy);
654 while (*p++ != '\0');
658 g_free ((gchar *) path_copy);
665 debug_key_matches (const gchar *key,
669 for (; length; length--, key++, token++)
671 char k = (*key == '_') ? '-' : tolower (*key );
672 char t = (*token == '_') ? '-' : tolower (*token);
682 * g_parse_debug_string:
683 * @string: (allow-none): a list of debug options separated by colons, spaces, or
685 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
686 * strings with bit flags.
687 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
689 * Parses a string containing debugging options
690 * into a %guint containing bit flags. This is used
691 * within GDK and GTK+ to parse the debug options passed on the
692 * command line or through environment variables.
694 * If @string is equal to "all", all flags are set. If @string
695 * is equal to "help", all the available keys in @keys are printed
696 * out to standard error.
698 * Returns: the combined set of bit flags.
701 g_parse_debug_string (const gchar *string,
702 const GDebugKey *keys,
711 /* this function is used by gmem.c/gslice.c initialization code,
712 * so introducing malloc dependencies here would require adaptions
713 * of those code portions.
716 if (!g_ascii_strcasecmp (string, "all"))
718 for (i=0; i<nkeys; i++)
719 result |= keys[i].value;
721 else if (!g_ascii_strcasecmp (string, "help"))
723 /* using stdio directly for the reason stated above */
724 fprintf (stderr, "Supported debug values: ");
725 for (i=0; i<nkeys; i++)
726 fprintf (stderr, " %s", keys[i].key);
727 fprintf (stderr, "\n");
731 const gchar *p = string;
736 q = strpbrk (p, ":;, \t");
740 for (i = 0; i < nkeys; i++)
741 if (debug_key_matches (keys[i].key, p, q - p))
742 result |= keys[i].value;
755 * @file_name: the name of the file.
757 * Gets the name of the file without any leading directory components.
758 * It returns a pointer into the given file name string.
760 * Return value: the name of the file without any leading directory components.
762 * Deprecated:2.2: Use g_path_get_basename() instead, but notice that
763 * g_path_get_basename() allocates new memory for the returned string, unlike
764 * this function which returns a pointer into the argument.
767 g_basename (const gchar *file_name)
769 register gchar *base;
771 g_return_val_if_fail (file_name != NULL, NULL);
773 base = strrchr (file_name, G_DIR_SEPARATOR);
777 gchar *q = strrchr (file_name, '/');
778 if (base == NULL || (q != NULL && q > base))
787 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
788 return (gchar*) file_name + 2;
789 #endif /* G_OS_WIN32 */
791 return (gchar*) file_name;
795 * g_path_get_basename:
796 * @file_name: the name of the file.
798 * Gets the last component of the filename. If @file_name ends with a
799 * directory separator it gets the component before the last slash. If
800 * @file_name consists only of directory separators (and on Windows,
801 * possibly a drive letter), a single separator is returned. If
802 * @file_name is empty, it gets ".".
804 * Return value: a newly allocated string containing the last component of
808 g_path_get_basename (const gchar *file_name)
810 register gssize base;
811 register gssize last_nonslash;
815 g_return_val_if_fail (file_name != NULL, NULL);
817 if (file_name[0] == '\0')
819 return g_strdup (".");
821 last_nonslash = strlen (file_name) - 1;
823 while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
826 if (last_nonslash == -1)
827 /* string only containing slashes */
828 return g_strdup (G_DIR_SEPARATOR_S);
831 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
832 /* string only containing slashes and a drive */
833 return g_strdup (G_DIR_SEPARATOR_S);
834 #endif /* G_OS_WIN32 */
836 base = last_nonslash;
838 while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
842 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
844 #endif /* G_OS_WIN32 */
846 len = last_nonslash - base;
847 retval = g_malloc (len + 1);
848 memcpy (retval, file_name + base + 1, len);
854 * g_path_is_absolute:
855 * @file_name: a file name.
857 * Returns %TRUE if the given @file_name is an absolute file name.
858 * Note that this is a somewhat vague concept on Windows.
860 * On POSIX systems, an absolute file name is well-defined. It always
861 * starts from the single root directory. For example "/usr/local".
863 * On Windows, the concepts of current drive and drive-specific
864 * current directory introduce vagueness. This function interprets as
865 * an absolute file name one that either begins with a directory
866 * separator such as "\Users\tml" or begins with the root on a drive,
867 * for example "C:\Windows". The first case also includes UNC paths
868 * such as "\\myserver\docs\foo". In all cases, either slashes or
869 * backslashes are accepted.
871 * Note that a file name relative to the current drive root does not
872 * truly specify a file uniquely over time and across processes, as
873 * the current drive is a per-process value and can be changed.
875 * File names relative the current directory on some specific drive,
876 * such as "D:foo/bar", are not interpreted as absolute by this
877 * function, but they obviously are not relative to the normal current
878 * directory as returned by getcwd() or g_get_current_dir()
879 * either. Such paths should be avoided, or need to be handled using
880 * Windows-specific code.
882 * Returns: %TRUE if @file_name is absolute.
885 g_path_is_absolute (const gchar *file_name)
887 g_return_val_if_fail (file_name != NULL, FALSE);
889 if (G_IS_DIR_SEPARATOR (file_name[0]))
893 /* Recognize drive letter on native Windows */
894 if (g_ascii_isalpha (file_name[0]) &&
895 file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
897 #endif /* G_OS_WIN32 */
904 * @file_name: a file name.
906 * Returns a pointer into @file_name after the root component, i.e. after
907 * the "/" in UNIX or "C:\" under Windows. If @file_name is not an absolute
908 * path it returns %NULL.
910 * Returns: a pointer into @file_name after the root component.
913 g_path_skip_root (const gchar *file_name)
915 g_return_val_if_fail (file_name != NULL, NULL);
917 #ifdef G_PLATFORM_WIN32
918 /* Skip \\server\share or //server/share */
919 if (G_IS_DIR_SEPARATOR (file_name[0]) &&
920 G_IS_DIR_SEPARATOR (file_name[1]) &&
922 !G_IS_DIR_SEPARATOR (file_name[2]))
926 p = strchr (file_name + 2, G_DIR_SEPARATOR);
929 gchar *q = strchr (file_name + 2, '/');
930 if (p == NULL || (q != NULL && q < p))
940 while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
943 /* Possibly skip a backslash after the share name */
944 if (G_IS_DIR_SEPARATOR (file_name[0]))
947 return (gchar *)file_name;
952 /* Skip initial slashes */
953 if (G_IS_DIR_SEPARATOR (file_name[0]))
955 while (G_IS_DIR_SEPARATOR (file_name[0]))
957 return (gchar *)file_name;
962 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
963 return (gchar *)file_name + 3;
971 * @mask: a #gulong containing flags
972 * @nth_bit: the index of the bit to start the search from
974 * Find the position of the first bit set in @mask, searching
975 * from (but not including) @nth_bit upwards. Bits are numbered
976 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
977 * usually). To start searching from the 0th bit, set @nth_bit to -1.
979 * Returns: the index of the first bit set which is higher than @nth_bit
984 * @mask: a #gulong containing flags
985 * @nth_bit: the index of the bit to start the search from
987 * Find the position of the first bit set in @mask, searching
988 * from (but not including) @nth_bit downwards. Bits are numbered
989 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
990 * usually). To start searching from the last bit, set @nth_bit to
991 * -1 or GLIB_SIZEOF_LONG * 8.
993 * Returns: the index of the first bit set which is lower than @nth_bit
1000 * Gets the number of bits used to hold @number,
1001 * e.g. if @number is 4, 3 bits are needed.
1003 * Returns: the number of bits used to hold @number
1008 * @file_name: the name of the file
1010 * Gets the directory components of a file name.
1011 * If the file name has no directory components "." is returned.
1012 * The returned string should be freed when no longer needed.
1014 * Returns: the directory components of the file
1016 * Deprecated: use g_path_get_dirname() instead
1020 * g_path_get_dirname:
1021 * @file_name: the name of the file.
1023 * Gets the directory components of a file name. If the file name has no
1024 * directory components "." is returned. The returned string should be
1025 * freed when no longer needed.
1027 * Returns: the directory components of the file.
1030 g_path_get_dirname (const gchar *file_name)
1032 register gchar *base;
1035 g_return_val_if_fail (file_name != NULL, NULL);
1037 base = strrchr (file_name, G_DIR_SEPARATOR);
1040 gchar *q = strrchr (file_name, '/');
1041 if (base == NULL || (q != NULL && q > base))
1048 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
1050 gchar drive_colon_dot[4];
1052 drive_colon_dot[0] = file_name[0];
1053 drive_colon_dot[1] = ':';
1054 drive_colon_dot[2] = '.';
1055 drive_colon_dot[3] = '\0';
1057 return g_strdup (drive_colon_dot);
1060 return g_strdup (".");
1063 while (base > file_name && G_IS_DIR_SEPARATOR (*base))
1067 /* base points to the char before the last slash.
1069 * In case file_name is the root of a drive (X:\) or a child of the
1070 * root of a drive (X:\foo), include the slash.
1072 * In case file_name is the root share of an UNC path
1073 * (\\server\share), add a slash, returning \\server\share\ .
1075 * In case file_name is a direct child of a share in an UNC path
1076 * (\\server\share\foo), include the slash after the share name,
1077 * returning \\server\share\ .
1079 if (base == file_name + 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
1081 else if (G_IS_DIR_SEPARATOR (file_name[0]) &&
1082 G_IS_DIR_SEPARATOR (file_name[1]) &&
1084 !G_IS_DIR_SEPARATOR (file_name[2]) &&
1085 base >= file_name + 2)
1087 const gchar *p = file_name + 2;
1088 while (*p && !G_IS_DIR_SEPARATOR (*p))
1092 len = (guint) strlen (file_name) + 1;
1093 base = g_new (gchar, len + 1);
1094 strcpy (base, file_name);
1095 base[len-1] = G_DIR_SEPARATOR;
1099 if (G_IS_DIR_SEPARATOR (*p))
1102 while (*p && !G_IS_DIR_SEPARATOR (*p))
1110 len = (guint) 1 + base - file_name;
1112 base = g_new (gchar, len + 1);
1113 g_memmove (base, file_name, len);
1120 * g_get_current_dir:
1122 * Gets the current directory.
1123 * The returned string should be freed when no longer needed. The encoding
1124 * of the returned string is system defined. On Windows, it is always UTF-8.
1126 * Returns: the current directory.
1129 g_get_current_dir (void)
1134 wchar_t dummy[2], *wdir;
1137 len = GetCurrentDirectoryW (2, dummy);
1138 wdir = g_new (wchar_t, len);
1140 if (GetCurrentDirectoryW (len, wdir) == len - 1)
1141 dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
1146 dir = g_strdup ("\\");
1152 gchar *buffer = NULL;
1154 static gulong max_len = 0;
1157 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
1159 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
1160 * and, if that wasn't bad enough, hangs in doing so.
1162 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
1163 buffer = g_new (gchar, max_len + 1);
1165 dir = getwd (buffer);
1166 #else /* !sun || !HAVE_GETCWD */
1167 while (max_len < G_MAXULONG / 2)
1170 buffer = g_new (gchar, max_len + 1);
1172 dir = getcwd (buffer, max_len);
1174 if (dir || errno != ERANGE)
1179 #endif /* !sun || !HAVE_GETCWD */
1181 if (!dir || !*buffer)
1183 /* hm, should we g_error() out here?
1184 * this can happen if e.g. "./" has mode \0000
1186 buffer[0] = G_DIR_SEPARATOR;
1190 dir = g_strdup (buffer);
1199 * @variable: the environment variable to get, in the GLib file name encoding.
1201 * Returns the value of an environment variable. The name and value
1202 * are in the GLib file name encoding. On UNIX, this means the actual
1203 * bytes which might or might not be in some consistent character set
1204 * and encoding. On Windows, it is in UTF-8. On Windows, in case the
1205 * environment variable's value contains references to other
1206 * environment variables, they are expanded.
1208 * Return value: the value of the environment variable, or %NULL if
1209 * the environment variable is not found. The returned string may be
1210 * overwritten by the next call to g_getenv(), g_setenv() or
1214 g_getenv (const gchar *variable)
1218 g_return_val_if_fail (variable != NULL, NULL);
1220 return getenv (variable);
1222 #else /* G_OS_WIN32 */
1226 wchar_t dummy[2], *wname, *wvalue;
1229 g_return_val_if_fail (variable != NULL, NULL);
1230 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
1232 /* On Windows NT, it is relatively typical that environment
1233 * variables contain references to other environment variables. If
1234 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
1235 * environment variables would be stored in the Registry as
1236 * REG_EXPAND_SZ type values, and would then get automatically
1237 * expanded before a program sees them. But there is broken software
1238 * that stores environment variables as REG_SZ values even if they
1239 * contain references to other environment variables.)
1242 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1244 len = GetEnvironmentVariableW (wname, dummy, 2);
1254 wvalue = g_new (wchar_t, len);
1256 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
1263 if (wcschr (wvalue, L'%') != NULL)
1265 wchar_t *tem = wvalue;
1267 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
1271 wvalue = g_new (wchar_t, len);
1273 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
1283 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
1288 quark = g_quark_from_string (value);
1291 return g_quark_to_string (quark);
1293 #endif /* G_OS_WIN32 */
1296 /* _g_getenv_nomalloc
1297 * this function does a getenv() without doing any kind of allocation
1298 * through glib. it's suitable for chars <= 127 only (both, for the
1299 * variable name and the contents) and for contents < 1024 chars in
1300 * length. also, it aliases "" to a NULL return value.
1303 _g_getenv_nomalloc (const gchar *variable,
1306 const gchar *retval = getenv (variable);
1307 if (retval && retval[0])
1309 gint l = strlen (retval);
1312 strncpy (buffer, retval, l);
1322 * @variable: the environment variable to set, must not contain '='.
1323 * @value: the value for to set the variable to.
1324 * @overwrite: whether to change the variable if it already exists.
1326 * Sets an environment variable. Both the variable's name and value
1327 * should be in the GLib file name encoding. On UNIX, this means that
1328 * they can be any sequence of bytes. On Windows, they should be in
1331 * Note that on some systems, when variables are overwritten, the memory
1332 * used for the previous variables and its value isn't reclaimed.
1335 * Environment variable handling in UNIX is not thread-safe, and your
1336 * program may crash if one thread calls g_setenv() while another
1337 * thread is calling getenv(). (And note that many functions, such as
1338 * gettext(), call getenv() internally.) This function is only safe to
1339 * use at the very start of your program, before creating any other
1340 * threads (or creating objects that create worker threads of their
1344 * Returns: %FALSE if the environment variable couldn't be set.
1349 g_setenv (const gchar *variable,
1360 g_return_val_if_fail (variable != NULL, FALSE);
1361 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1364 result = setenv (variable, value, overwrite);
1366 if (!overwrite && getenv (variable) != NULL)
1369 /* This results in a leak when you overwrite existing
1370 * settings. It would be fairly easy to fix this by keeping
1371 * our own parallel array or hash table.
1373 string = g_strconcat (variable, "=", value, NULL);
1374 result = putenv (string);
1378 #else /* G_OS_WIN32 */
1381 wchar_t *wname, *wvalue, *wassignment;
1384 g_return_val_if_fail (variable != NULL, FALSE);
1385 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1386 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
1387 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
1389 if (!overwrite && g_getenv (variable) != NULL)
1392 /* We want to (if possible) set both the environment variable copy
1393 * kept by the C runtime and the one kept by the system.
1395 * We can't use only the C runtime's putenv or _wputenv() as that
1396 * won't work for arbitrary Unicode strings in a "non-Unicode" app
1397 * (with main() and not wmain()). In a "main()" app the C runtime
1398 * initializes the C runtime's environment table by converting the
1399 * real (wide char) environment variables to system codepage, thus
1400 * breaking those that aren't representable in the system codepage.
1402 * As the C runtime's putenv() will also set the system copy, we do
1403 * the putenv() first, then call SetEnvironmentValueW ourselves.
1406 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1407 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
1408 tem = g_strconcat (variable, "=", value, NULL);
1409 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1412 _wputenv (wassignment);
1413 g_free (wassignment);
1415 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
1422 #endif /* G_OS_WIN32 */
1425 #ifdef HAVE__NSGETENVIRON
1426 #define environ (*_NSGetEnviron())
1427 #elif !defined(G_OS_WIN32)
1429 /* According to the Single Unix Specification, environ is not in
1430 * any system header, although unistd.h often declares it.
1432 extern char **environ;
1437 * @variable: the environment variable to remove, must not contain '='.
1439 * Removes an environment variable from the environment.
1441 * Note that on some systems, when variables are overwritten, the memory
1442 * used for the previous variables and its value isn't reclaimed.
1445 * Environment variable handling in UNIX is not thread-safe, and your
1446 * program may crash if one thread calls g_unsetenv() while another
1447 * thread is calling getenv(). (And note that many functions, such as
1448 * gettext(), call getenv() internally.) This function is only safe to
1449 * use at the very start of your program, before creating any other
1450 * threads (or creating objects that create worker threads of their
1457 g_unsetenv (const gchar *variable)
1461 #ifdef HAVE_UNSETENV
1462 g_return_if_fail (variable != NULL);
1463 g_return_if_fail (strchr (variable, '=') == NULL);
1465 unsetenv (variable);
1466 #else /* !HAVE_UNSETENV */
1470 g_return_if_fail (variable != NULL);
1471 g_return_if_fail (strchr (variable, '=') == NULL);
1473 len = strlen (variable);
1475 /* Mess directly with the environ array.
1476 * This seems to be the only portable way to do this.
1478 * Note that we remove *all* environment entries for
1479 * the variable name, not just the first.
1484 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
1492 #endif /* !HAVE_UNSETENV */
1494 #else /* G_OS_WIN32 */
1496 wchar_t *wname, *wassignment;
1499 g_return_if_fail (variable != NULL);
1500 g_return_if_fail (strchr (variable, '=') == NULL);
1501 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
1503 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1504 tem = g_strconcat (variable, "=", NULL);
1505 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1508 _wputenv (wassignment);
1509 g_free (wassignment);
1511 SetEnvironmentVariableW (wname, NULL);
1515 #endif /* G_OS_WIN32 */
1521 * Gets the names of all variables set in the environment.
1523 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated list of strings which must be freed
1524 * with g_strfreev().
1526 * Programs that want to be portable to Windows should typically use
1527 * this function and g_getenv() instead of using the environ array
1528 * from the C library directly. On Windows, the strings in the environ
1529 * array are in system codepage encoding, while in most of the typical
1530 * use cases for environment variables in GLib-using programs you want
1531 * the UTF-8 encoding that this function and g_getenv() provide.
1539 gchar **result, *eq;
1542 len = g_strv_length (environ);
1543 result = g_new0 (gchar *, len + 1);
1546 for (i = 0; i < len; i++)
1548 eq = strchr (environ[i], '=');
1550 result[j++] = g_strndup (environ[i], eq - environ[i]);
1557 gchar **result, *eq;
1561 p = (wchar_t *) GetEnvironmentStringsW ();
1567 q += wcslen (q) + 1;
1571 result = g_new0 (gchar *, len + 1);
1577 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
1578 if (result[j] != NULL)
1580 eq = strchr (result[j], '=');
1581 if (eq && eq > result[j])
1589 q += wcslen (q) + 1;
1592 FreeEnvironmentStringsW (p);
1601 * Gets the list of environment variables for the current process. The
1602 * list is %NULL terminated and each item in the list is of the form
1605 * This is equivalent to direct access to the 'environ' global variable,
1608 * The return value is freshly allocated and it should be freed with
1609 * g_strfreev() when it is no longer needed.
1611 * Returns: (array zero-terminated=1) (transfer full): the list of environment variables
1616 g_get_environ (void)
1619 return g_strdupv (environ);
1625 strings = GetEnvironmentStringsW ();
1626 for (n = 0; strings[n]; n += wcslen (strings + n) + 1);
1627 result = g_new (char *, n + 1);
1628 for (i = 0; strings[i]; i += wcslen (strings + i) + 1)
1629 result[i] = g_utf16_to_utf8 (strings + i, -1, NULL, NULL, NULL);
1630 FreeEnvironmentStringsW (strings);
1637 G_LOCK_DEFINE_STATIC (g_utils_global);
1639 static gchar *g_tmp_dir = NULL;
1640 static gchar *g_user_name = NULL;
1641 static gchar *g_real_name = NULL;
1642 static gchar *g_home_dir = NULL;
1643 static gchar *g_host_name = NULL;
1646 /* System codepage versions of the above, kept at file level so that they,
1647 * too, are produced only once.
1649 static gchar *g_tmp_dir_cp = NULL;
1650 static gchar *g_user_name_cp = NULL;
1651 static gchar *g_real_name_cp = NULL;
1652 static gchar *g_home_dir_cp = NULL;
1655 static gchar *g_user_data_dir = NULL;
1656 static gchar **g_system_data_dirs = NULL;
1657 static gchar *g_user_cache_dir = NULL;
1658 static gchar *g_user_config_dir = NULL;
1659 static gchar **g_system_config_dirs = NULL;
1661 static gchar **g_user_special_dirs = NULL;
1663 /* fifteen minutes of fame for everybody */
1664 #define G_USER_DIRS_EXPIRE 15 * 60
1669 get_special_folder (int csidl)
1671 wchar_t path[MAX_PATH+1];
1673 LPITEMIDLIST pidl = NULL;
1675 gchar *retval = NULL;
1677 hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
1680 b = SHGetPathFromIDListW (pidl, path);
1682 retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
1683 CoTaskMemFree (pidl);
1689 get_windows_directory_root (void)
1691 wchar_t wwindowsdir[MAX_PATH];
1693 if (GetWindowsDirectoryW (wwindowsdir, G_N_ELEMENTS (wwindowsdir)))
1695 /* Usually X:\Windows, but in terminal server environments
1696 * might be an UNC path, AFAIK.
1698 char *windowsdir = g_utf16_to_utf8 (wwindowsdir, -1, NULL, NULL, NULL);
1701 if (windowsdir == NULL)
1702 return g_strdup ("C:\\");
1704 p = (char *) g_path_skip_root (windowsdir);
1705 if (G_IS_DIR_SEPARATOR (p[-1]) && p[-2] != ':')
1711 return g_strdup ("C:\\");
1716 /* HOLDS: g_utils_global_lock */
1718 g_get_any_init_do (void)
1720 gchar hostname[100];
1722 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
1723 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1724 g_tmp_dir = g_strdup (g_getenv ("TMP"));
1725 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1726 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
1729 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1730 g_tmp_dir = get_windows_directory_root ();
1733 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1736 g_tmp_dir = g_strdup (P_tmpdir);
1737 k = strlen (g_tmp_dir);
1738 if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
1739 g_tmp_dir[k - 1] = '\0';
1743 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1745 g_tmp_dir = g_strdup ("/tmp");
1747 #endif /* !G_OS_WIN32 */
1750 /* We check $HOME first for Win32, though it is a last resort for Unix
1751 * where we prefer the results of getpwuid().
1753 g_home_dir = g_strdup (g_getenv ("HOME"));
1755 /* Only believe HOME if it is an absolute path and exists */
1758 if (!(g_path_is_absolute (g_home_dir) &&
1759 g_file_test (g_home_dir, G_FILE_TEST_IS_DIR)))
1761 g_free (g_home_dir);
1766 /* In case HOME is Unix-style (it happens), convert it to
1772 while ((p = strchr (g_home_dir, '/')) != NULL)
1778 /* USERPROFILE is probably the closest equivalent to $HOME? */
1779 if (g_getenv ("USERPROFILE") != NULL)
1780 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
1784 g_home_dir = get_special_folder (CSIDL_PROFILE);
1787 g_home_dir = get_windows_directory_root ();
1788 #endif /* G_OS_WIN32 */
1792 struct passwd *pw = NULL;
1793 gpointer buffer = NULL;
1797 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
1799 # ifdef _SC_GETPW_R_SIZE_MAX
1800 /* This reurns the maximum length */
1801 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
1805 # else /* _SC_GETPW_R_SIZE_MAX */
1807 # endif /* _SC_GETPW_R_SIZE_MAX */
1809 logname = (gchar *) g_getenv ("LOGNAME");
1814 /* we allocate 6 extra bytes to work around a bug in
1815 * Mac OS < 10.3. See #156446
1817 buffer = g_malloc (bufsize + 6);
1820 # ifdef HAVE_POSIX_GETPWUID_R
1822 error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
1823 if (!pw || (pw->pw_uid != getuid ())) {
1824 /* LOGNAME is lying, fall back to looking up the uid */
1825 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1828 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1830 error = error < 0 ? errno : error;
1831 # else /* HAVE_NONPOSIX_GETPWUID_R */
1832 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
1833 # if defined(_AIX) || defined(__hpux)
1834 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1835 pw = error == 0 ? &pwd : NULL;
1838 pw = getpwnam_r (logname, &pwd, buffer, bufsize);
1839 if (!pw || (pw->pw_uid != getuid ())) {
1840 /* LOGNAME is lying, fall back to looking up the uid */
1841 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1844 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1846 error = pw ? 0 : errno;
1848 # endif /* HAVE_NONPOSIX_GETPWUID_R */
1852 /* we bail out prematurely if the user id can't be found
1853 * (should be pretty rare case actually), or if the buffer
1854 * should be sufficiently big and lookups are still not
1857 if (error == 0 || error == ENOENT)
1859 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
1860 (gulong) getuid ());
1863 if (bufsize > 32 * 1024)
1865 g_warning ("getpwuid_r(): failed due to: %s.",
1866 g_strerror (error));
1874 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
1879 pw = getpwuid (getuid ());
1884 g_user_name = g_strdup (pw->pw_name);
1886 if (pw->pw_gecos && *pw->pw_gecos != '\0')
1888 gchar **gecos_fields;
1891 /* split the gecos field and substitute '&' */
1892 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
1893 name_parts = g_strsplit (gecos_fields[0], "&", 0);
1894 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
1895 g_real_name = g_strjoinv (pw->pw_name, name_parts);
1896 g_strfreev (gecos_fields);
1897 g_strfreev (name_parts);
1901 g_home_dir = g_strdup (pw->pw_dir);
1906 #else /* !HAVE_PWD_H */
1910 guint len = UNLEN+1;
1911 wchar_t buffer[UNLEN+1];
1913 if (GetUserNameW (buffer, (LPDWORD) &len))
1915 g_user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
1916 g_real_name = g_strdup (g_user_name);
1919 #endif /* G_OS_WIN32 */
1921 #endif /* !HAVE_PWD_H */
1925 g_home_dir = g_strdup (g_getenv ("HOME"));
1929 /* change '\\' in %HOME% to '/' */
1930 g_strdelimit (g_home_dir, "\\",'/');
1933 g_user_name = g_strdup ("somebody");
1935 g_real_name = g_strdup ("Unknown");
1939 gboolean hostname_fail = (gethostname (hostname, sizeof (hostname)) == -1);
1941 DWORD size = sizeof (hostname);
1942 gboolean hostname_fail = (!GetComputerName (hostname, &size));
1944 g_host_name = g_strdup (hostname_fail ? "localhost" : hostname);
1948 g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL);
1949 g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL);
1950 g_real_name_cp = g_locale_from_utf8 (g_real_name, -1, NULL, NULL, NULL);
1953 g_tmp_dir_cp = g_strdup ("\\");
1954 if (!g_user_name_cp)
1955 g_user_name_cp = g_strdup ("somebody");
1956 if (!g_real_name_cp)
1957 g_real_name_cp = g_strdup ("Unknown");
1959 /* home_dir might be NULL, unlike tmp_dir, user_name and
1963 g_home_dir_cp = g_locale_from_utf8 (g_home_dir, -1, NULL, NULL, NULL);
1965 g_home_dir_cp = NULL;
1966 #endif /* G_OS_WIN32 */
1970 g_get_any_init (void)
1973 g_get_any_init_do ();
1977 g_get_any_init_locked (void)
1979 G_LOCK (g_utils_global);
1981 G_UNLOCK (g_utils_global);
1988 * Gets the user name of the current user. The encoding of the returned
1989 * string is system-defined. On UNIX, it might be the preferred file name
1990 * encoding, or something else, and there is no guarantee that it is even
1991 * consistent on a machine. On Windows, it is always UTF-8.
1993 * Returns: the user name of the current user.
1996 g_get_user_name (void)
1998 g_get_any_init_locked ();
2005 * Gets the real name of the user. This usually comes from the user's entry
2006 * in the <filename>passwd</filename> file. The encoding of the returned
2007 * string is system-defined. (On Windows, it is, however, always UTF-8.)
2008 * If the real user name cannot be determined, the string "Unknown" is
2011 * Returns: the user's real name.
2014 g_get_real_name (void)
2016 g_get_any_init_locked ();
2023 * Gets the current user's home directory as defined in the
2024 * password database.
2026 * Note that in contrast to traditional UNIX tools, this function
2027 * prefers <filename>passwd</filename> entries over the <envar>HOME</envar>
2028 * environment variable.
2030 * One of the reasons for this decision is that applications in many
2031 * cases need special handling to deal with the case where
2032 * <envar>HOME</envar> is
2034 * <member>Not owned by the user</member>
2035 * <member>Not writeable</member>
2036 * <member>Not even readable</member>
2038 * Since applications are in general <emphasis>not</emphasis> written
2039 * to deal with these situations it was considered better to make
2040 * g_get_home_dir() not pay attention to <envar>HOME</envar> and to
2041 * return the real home directory for the user. If applications
2042 * want to pay attention to <envar>HOME</envar>, they can do:
2044 * const char *homedir = g_getenv ("HOME");
2046 * homedir = g_get_home_dir (<!-- -->);
2049 * Returns: the current user's home directory
2052 g_get_home_dir (void)
2054 g_get_any_init_locked ();
2061 * Gets the directory to use for temporary files. This is found from
2062 * inspecting the environment variables <envar>TMPDIR</envar>,
2063 * <envar>TMP</envar>, and <envar>TEMP</envar> in that order. If none
2064 * of those are defined "/tmp" is returned on UNIX and "C:\" on Windows.
2065 * The encoding of the returned string is system-defined. On Windows,
2066 * it is always UTF-8. The return value is never %NULL or the empty string.
2068 * Returns: the directory to use for temporary files.
2071 g_get_tmp_dir (void)
2073 g_get_any_init_locked ();
2080 * Return a name for the machine.
2082 * The returned name is not necessarily a fully-qualified domain name,
2083 * or even present in DNS or some other name service at all. It need
2084 * not even be unique on your local network or site, but usually it
2085 * is. Callers should not rely on the return value having any specific
2086 * properties like uniqueness for security purposes. Even if the name
2087 * of the machine is changed while an application is running, the
2088 * return value from this function does not change. The returned
2089 * string is owned by GLib and should not be modified or freed. If no
2090 * name can be determined, a default fixed string "localhost" is
2093 * Returns: the host name of the machine.
2098 g_get_host_name (void)
2100 g_get_any_init_locked ();
2104 G_LOCK_DEFINE_STATIC (g_prgname);
2105 static gchar *g_prgname = NULL;
2110 * Gets the name of the program. This name should <emphasis>not</emphasis>
2111 * be localized, contrast with g_get_application_name().
2112 * (If you are using GDK or GTK+ the program name is set in gdk_init(),
2113 * which is called by gtk_init(). The program name is found by taking
2114 * the last component of <literal>argv[0]</literal>.)
2116 * Returns: the name of the program. The returned string belongs
2117 * to GLib and must not be modified or freed.
2120 g_get_prgname (void)
2126 if (g_prgname == NULL)
2128 static gboolean beenhere = FALSE;
2132 gchar *utf8_buf = NULL;
2133 wchar_t buf[MAX_PATH+1];
2136 if (GetModuleFileNameW (GetModuleHandle (NULL),
2137 buf, G_N_ELEMENTS (buf)) > 0)
2138 utf8_buf = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
2142 g_prgname = g_path_get_basename (utf8_buf);
2149 G_UNLOCK (g_prgname);
2156 * @prgname: the name of the program.
2158 * Sets the name of the program. This name should <emphasis>not</emphasis>
2159 * be localized, contrast with g_set_application_name(). Note that for
2160 * thread-safety reasons this function can only be called once.
2163 g_set_prgname (const gchar *prgname)
2167 g_prgname = g_strdup (prgname);
2168 G_UNLOCK (g_prgname);
2171 G_LOCK_DEFINE_STATIC (g_application_name);
2172 static gchar *g_application_name = NULL;
2175 * g_get_application_name:
2177 * Gets a human-readable name for the application, as set by
2178 * g_set_application_name(). This name should be localized if
2179 * possible, and is intended for display to the user. Contrast with
2180 * g_get_prgname(), which gets a non-localized name. If
2181 * g_set_application_name() has not been called, returns the result of
2182 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
2185 * Return value: human-readable application name. may return %NULL
2190 g_get_application_name (void)
2194 G_LOCK (g_application_name);
2195 retval = g_application_name;
2196 G_UNLOCK (g_application_name);
2199 return g_get_prgname ();
2205 * g_set_application_name:
2206 * @application_name: localized name of the application
2208 * Sets a human-readable name for the application. This name should be
2209 * localized if possible, and is intended for display to the user.
2210 * Contrast with g_set_prgname(), which sets a non-localized name.
2211 * g_set_prgname() will be called automatically by gtk_init(),
2212 * but g_set_application_name() will not.
2214 * Note that for thread safety reasons, this function can only
2217 * The application name will be used in contexts such as error messages,
2218 * or when displaying an application's name in the task list.
2223 g_set_application_name (const gchar *application_name)
2225 gboolean already_set = FALSE;
2227 G_LOCK (g_application_name);
2228 if (g_application_name)
2231 g_application_name = g_strdup (application_name);
2232 G_UNLOCK (g_application_name);
2235 g_warning ("g_set_application_name() called multiple times");
2239 * g_get_user_data_dir:
2241 * Returns a base directory in which to access application data such
2242 * as icons that is customized for a particular user.
2244 * On UNIX platforms this is determined using the mechanisms described in
2245 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2246 * XDG Base Directory Specification</ulink>.
2247 * In this case the directory retrieved will be XDG_DATA_HOME.
2249 * On Windows this is the folder to use for local (as opposed to
2250 * roaming) application data. See documentation for
2251 * CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as
2252 * what g_get_user_config_dir() returns.
2254 * Return value: a string owned by GLib that must not be modified
2259 g_get_user_data_dir (void)
2263 G_LOCK (g_utils_global);
2265 if (!g_user_data_dir)
2268 data_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
2270 data_dir = (gchar *) g_getenv ("XDG_DATA_HOME");
2272 if (data_dir && data_dir[0])
2273 data_dir = g_strdup (data_dir);
2275 if (!data_dir || !data_dir[0])
2280 data_dir = g_build_filename (g_home_dir, ".local",
2283 data_dir = g_build_filename (g_tmp_dir, g_user_name, ".local",
2287 g_user_data_dir = data_dir;
2290 data_dir = g_user_data_dir;
2292 G_UNLOCK (g_utils_global);
2298 g_init_user_config_dir (void)
2302 if (!g_user_config_dir)
2305 config_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
2307 config_dir = (gchar *) g_getenv ("XDG_CONFIG_HOME");
2309 if (config_dir && config_dir[0])
2310 config_dir = g_strdup (config_dir);
2312 if (!config_dir || !config_dir[0])
2317 config_dir = g_build_filename (g_home_dir, ".config", NULL);
2319 config_dir = g_build_filename (g_tmp_dir, g_user_name, ".config", NULL);
2322 g_user_config_dir = config_dir;
2327 * g_get_user_config_dir:
2329 * Returns a base directory in which to store user-specific application
2330 * configuration information such as user preferences and settings.
2332 * On UNIX platforms this is determined using the mechanisms described in
2333 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2334 * XDG Base Directory Specification</ulink>.
2335 * In this case the directory retrieved will be XDG_CONFIG_HOME.
2337 * On Windows this is the folder to use for local (as opposed to
2338 * roaming) application data. See documentation for
2339 * CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as
2340 * what g_get_user_data_dir() returns.
2342 * Return value: a string owned by GLib that must not be modified
2347 g_get_user_config_dir (void)
2349 G_LOCK (g_utils_global);
2351 g_init_user_config_dir ();
2353 G_UNLOCK (g_utils_global);
2355 return g_user_config_dir;
2359 * g_get_user_cache_dir:
2361 * Returns a base directory in which to store non-essential, cached
2362 * data specific to particular user.
2364 * On UNIX platforms this is determined using the mechanisms described in
2365 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2366 * XDG Base Directory Specification</ulink>.
2367 * In this case the directory retrieved will be XDG_CACHE_HOME.
2369 * On Windows is the directory that serves as a common repository for
2370 * temporary Internet files. A typical path is
2371 * C:\Documents and Settings\username\Local Settings\Temporary Internet Files.
2372 * See documentation for CSIDL_INTERNET_CACHE.
2374 * Return value: a string owned by GLib that must not be modified
2379 g_get_user_cache_dir (void)
2383 G_LOCK (g_utils_global);
2385 if (!g_user_cache_dir)
2388 cache_dir = get_special_folder (CSIDL_INTERNET_CACHE); /* XXX correct? */
2390 cache_dir = (gchar *) g_getenv ("XDG_CACHE_HOME");
2392 if (cache_dir && cache_dir[0])
2393 cache_dir = g_strdup (cache_dir);
2395 if (!cache_dir || !cache_dir[0])
2400 cache_dir = g_build_filename (g_home_dir, ".cache", NULL);
2402 cache_dir = g_build_filename (g_tmp_dir, g_user_name, ".cache", NULL);
2404 g_user_cache_dir = cache_dir;
2407 cache_dir = g_user_cache_dir;
2409 G_UNLOCK (g_utils_global);
2415 * g_get_user_runtime_dir:
2417 * Returns a directory that is unique to the current user on the local
2420 * On UNIX platforms this is determined using the mechanisms described in
2421 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2422 * XDG Base Directory Specification</ulink>. This is the directory
2423 * specified in the <envar>XDG_RUNTIME_DIR</envar> environment variable.
2424 * In the case that this variable is not set, GLib will issue a warning
2425 * message to stderr and return the value of g_get_user_cache_dir().
2427 * On Windows this is the folder to use for local (as opposed to
2428 * roaming) application data. See documentation for
2429 * CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as
2430 * what g_get_user_config_dir() returns.
2432 * Returns: a string owned by GLib that must not be modified or freed.
2437 g_get_user_runtime_dir (void)
2440 static const gchar *runtime_dir;
2441 static gsize initialised;
2443 if (g_once_init_enter (&initialised))
2445 runtime_dir = g_strdup (getenv ("XDG_RUNTIME_DIR"));
2447 g_once_init_leave (&initialised, 1);
2453 /* Both fallback for UNIX and the default
2454 * in Windows: use the user cache directory.
2458 return g_get_user_cache_dir ();
2464 find_folder (OSType type)
2466 gchar *filename = NULL;
2469 if (FSFindFolder (kUserDomain, type, kDontCreateFolder, &found) == noErr)
2471 CFURLRef url = CFURLCreateFromFSRef (kCFAllocatorSystemDefault, &found);
2475 CFStringRef path = CFURLCopyFileSystemPath (url, kCFURLPOSIXPathStyle);
2479 filename = g_strdup (CFStringGetCStringPtr (path, kCFStringEncodingUTF8));
2483 filename = g_new0 (gchar, CFStringGetLength (path) * 3 + 1);
2485 CFStringGetCString (path, filename,
2486 CFStringGetLength (path) * 3 + 1,
2487 kCFStringEncodingUTF8);
2501 load_user_special_dirs (void)
2503 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = find_folder (kDesktopFolderType);
2504 g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = find_folder (kDocumentsFolderType);
2505 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = find_folder (kDesktopFolderType); /* XXX correct ? */
2506 g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = find_folder (kMusicDocumentsFolderType);
2507 g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = find_folder (kPictureDocumentsFolderType);
2508 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = NULL;
2509 g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = NULL;
2510 g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = find_folder (kMovieDocumentsFolderType);
2513 #endif /* HAVE_CARBON */
2515 #if defined(G_OS_WIN32)
2517 load_user_special_dirs (void)
2519 typedef HRESULT (WINAPI *t_SHGetKnownFolderPath) (const GUID *rfid,
2523 t_SHGetKnownFolderPath p_SHGetKnownFolderPath;
2525 static const GUID FOLDERID_Downloads =
2526 { 0x374de290, 0x123f, 0x4565, { 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b } };
2527 static const GUID FOLDERID_Public =
2528 { 0xDFDF76A2, 0xC82A, 0x4D63, { 0x90, 0x6A, 0x56, 0x44, 0xAC, 0x45, 0x73, 0x85 } };
2532 p_SHGetKnownFolderPath = (t_SHGetKnownFolderPath) GetProcAddress (GetModuleHandle ("shell32.dll"),
2533 "SHGetKnownFolderPath");
2535 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2536 g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = get_special_folder (CSIDL_PERSONAL);
2538 if (p_SHGetKnownFolderPath == NULL)
2540 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2545 (*p_SHGetKnownFolderPath) (&FOLDERID_Downloads, 0, NULL, &wcp);
2548 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
2549 if (g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] == NULL)
2550 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2551 CoTaskMemFree (wcp);
2554 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2557 g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = get_special_folder (CSIDL_MYMUSIC);
2558 g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = get_special_folder (CSIDL_MYPICTURES);
2560 if (p_SHGetKnownFolderPath == NULL)
2563 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2568 (*p_SHGetKnownFolderPath) (&FOLDERID_Public, 0, NULL, &wcp);
2571 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
2572 if (g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] == NULL)
2573 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2574 CoTaskMemFree (wcp);
2577 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2580 g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = get_special_folder (CSIDL_TEMPLATES);
2581 g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = get_special_folder (CSIDL_MYVIDEO);
2583 #endif /* G_OS_WIN32 */
2585 static void g_init_user_config_dir (void);
2587 #if defined(G_OS_UNIX) && !defined(HAVE_CARBON)
2589 /* adapted from xdg-user-dir-lookup.c
2591 * Copyright (C) 2007 Red Hat Inc.
2593 * Permission is hereby granted, free of charge, to any person
2594 * obtaining a copy of this software and associated documentation files
2595 * (the "Software"), to deal in the Software without restriction,
2596 * including without limitation the rights to use, copy, modify, merge,
2597 * publish, distribute, sublicense, and/or sell copies of the Software,
2598 * and to permit persons to whom the Software is furnished to do so,
2599 * subject to the following conditions:
2601 * The above copyright notice and this permission notice shall be
2602 * included in all copies or substantial portions of the Software.
2604 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2605 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2606 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2607 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2608 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2609 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2610 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2614 load_user_special_dirs (void)
2621 g_init_user_config_dir ();
2622 config_file = g_build_filename (g_user_config_dir,
2626 if (!g_file_get_contents (config_file, &data, NULL, NULL))
2628 g_free (config_file);
2632 lines = g_strsplit (data, "\n", -1);
2633 n_lines = g_strv_length (lines);
2636 for (i = 0; i < n_lines; i++)
2638 gchar *buffer = lines[i];
2641 gboolean is_relative = FALSE;
2642 GUserDirectory directory;
2644 /* Remove newline at end */
2645 len = strlen (buffer);
2646 if (len > 0 && buffer[len - 1] == '\n')
2647 buffer[len - 1] = 0;
2650 while (*p == ' ' || *p == '\t')
2653 if (strncmp (p, "XDG_DESKTOP_DIR", strlen ("XDG_DESKTOP_DIR")) == 0)
2655 directory = G_USER_DIRECTORY_DESKTOP;
2656 p += strlen ("XDG_DESKTOP_DIR");
2658 else if (strncmp (p, "XDG_DOCUMENTS_DIR", strlen ("XDG_DOCUMENTS_DIR")) == 0)
2660 directory = G_USER_DIRECTORY_DOCUMENTS;
2661 p += strlen ("XDG_DOCUMENTS_DIR");
2663 else if (strncmp (p, "XDG_DOWNLOAD_DIR", strlen ("XDG_DOWNLOAD_DIR")) == 0)
2665 directory = G_USER_DIRECTORY_DOWNLOAD;
2666 p += strlen ("XDG_DOWNLOAD_DIR");
2668 else if (strncmp (p, "XDG_MUSIC_DIR", strlen ("XDG_MUSIC_DIR")) == 0)
2670 directory = G_USER_DIRECTORY_MUSIC;
2671 p += strlen ("XDG_MUSIC_DIR");
2673 else if (strncmp (p, "XDG_PICTURES_DIR", strlen ("XDG_PICTURES_DIR")) == 0)
2675 directory = G_USER_DIRECTORY_PICTURES;
2676 p += strlen ("XDG_PICTURES_DIR");
2678 else if (strncmp (p, "XDG_PUBLICSHARE_DIR", strlen ("XDG_PUBLICSHARE_DIR")) == 0)
2680 directory = G_USER_DIRECTORY_PUBLIC_SHARE;
2681 p += strlen ("XDG_PUBLICSHARE_DIR");
2683 else if (strncmp (p, "XDG_TEMPLATES_DIR", strlen ("XDG_TEMPLATES_DIR")) == 0)
2685 directory = G_USER_DIRECTORY_TEMPLATES;
2686 p += strlen ("XDG_TEMPLATES_DIR");
2688 else if (strncmp (p, "XDG_VIDEOS_DIR", strlen ("XDG_VIDEOS_DIR")) == 0)
2690 directory = G_USER_DIRECTORY_VIDEOS;
2691 p += strlen ("XDG_VIDEOS_DIR");
2696 while (*p == ' ' || *p == '\t')
2703 while (*p == ' ' || *p == '\t')
2710 if (strncmp (p, "$HOME", 5) == 0)
2718 d = strrchr (p, '"');
2725 /* remove trailing slashes */
2727 if (d[len - 1] == '/')
2733 g_user_special_dirs[directory] = g_build_filename (g_home_dir, d, NULL);
2736 g_user_special_dirs[directory] = g_strdup (d);
2740 g_free (config_file);
2743 #endif /* G_OS_UNIX && !HAVE_CARBON */
2747 * g_reload_user_special_dirs_cache:
2749 * Resets the cache used for g_get_user_special_dir(), so
2750 * that the latest on-disk version is used. Call this only
2751 * if you just changed the data on disk yourself.
2753 * Due to threadsafety issues this may cause leaking of strings
2754 * that were previously returned from g_get_user_special_dir()
2755 * that can't be freed. We ensure to only leak the data for
2756 * the directories that actually changed value though.
2761 g_reload_user_special_dirs_cache (void)
2765 G_LOCK (g_utils_global);
2767 if (g_user_special_dirs != NULL)
2769 /* save a copy of the pointer, to check if some memory can be preserved */
2770 char **old_g_user_special_dirs = g_user_special_dirs;
2773 /* recreate and reload our cache */
2774 g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
2775 load_user_special_dirs ();
2777 /* only leak changed directories */
2778 for (i = 0; i < G_USER_N_DIRECTORIES; i++)
2780 old_val = old_g_user_special_dirs[i];
2781 if (g_strcmp0 (old_val, g_user_special_dirs[i]) == 0)
2784 g_free (g_user_special_dirs[i]);
2785 g_user_special_dirs[i] = old_val;
2791 /* free the old array */
2792 g_free (old_g_user_special_dirs);
2795 G_UNLOCK (g_utils_global);
2799 * g_get_user_special_dir:
2800 * @directory: the logical id of special directory
2802 * Returns the full path of a special directory using its logical id.
2804 * On Unix this is done using the XDG special user directories.
2805 * For compatibility with existing practise, %G_USER_DIRECTORY_DESKTOP
2806 * falls back to <filename>$HOME/Desktop</filename> when XDG special
2807 * user directories have not been set up.
2809 * Depending on the platform, the user might be able to change the path
2810 * of the special directory without requiring the session to restart; GLib
2811 * will not reflect any change once the special directories are loaded.
2813 * Return value: the path to the specified special directory, or %NULL
2814 * if the logical id was not found. The returned string is owned by
2815 * GLib and should not be modified or freed.
2820 g_get_user_special_dir (GUserDirectory directory)
2822 g_return_val_if_fail (directory >= G_USER_DIRECTORY_DESKTOP &&
2823 directory < G_USER_N_DIRECTORIES, NULL);
2825 G_LOCK (g_utils_global);
2827 if (G_UNLIKELY (g_user_special_dirs == NULL))
2829 g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
2831 load_user_special_dirs ();
2833 /* Special-case desktop for historical compatibility */
2834 if (g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] == NULL)
2838 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] =
2839 g_build_filename (g_home_dir, "Desktop", NULL);
2843 G_UNLOCK (g_utils_global);
2845 return g_user_special_dirs[directory];
2850 #undef g_get_system_data_dirs
2853 get_module_for_address (gconstpointer address)
2855 /* Holds the g_utils_global lock */
2857 static gboolean beenhere = FALSE;
2858 typedef BOOL (WINAPI *t_GetModuleHandleExA) (DWORD, LPCTSTR, HMODULE *);
2859 static t_GetModuleHandleExA p_GetModuleHandleExA = NULL;
2860 HMODULE hmodule = NULL;
2867 p_GetModuleHandleExA =
2868 (t_GetModuleHandleExA) GetProcAddress (GetModuleHandle ("kernel32.dll"),
2869 "GetModuleHandleExA");
2873 if (p_GetModuleHandleExA == NULL ||
2874 !(*p_GetModuleHandleExA) (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
2875 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2878 MEMORY_BASIC_INFORMATION mbi;
2879 VirtualQuery (address, &mbi, sizeof (mbi));
2880 hmodule = (HMODULE) mbi.AllocationBase;
2887 get_module_share_dir (gconstpointer address)
2893 hmodule = get_module_for_address (address);
2894 if (hmodule == NULL)
2897 filename = g_win32_get_package_installation_directory_of_module (hmodule);
2898 retval = g_build_filename (filename, "share", NULL);
2904 const gchar * const *
2905 g_win32_get_system_data_dirs_for_module (void (*address_of_function)())
2909 static GHashTable *per_module_data_dirs = NULL;
2914 if (address_of_function)
2916 G_LOCK (g_utils_global);
2917 hmodule = get_module_for_address (address_of_function);
2918 if (hmodule != NULL)
2920 if (per_module_data_dirs == NULL)
2921 per_module_data_dirs = g_hash_table_new (NULL, NULL);
2924 retval = g_hash_table_lookup (per_module_data_dirs, hmodule);
2928 G_UNLOCK (g_utils_global);
2929 return (const gchar * const *) retval;
2935 data_dirs = g_array_new (TRUE, TRUE, sizeof (char *));
2937 /* Documents and Settings\All Users\Application Data */
2938 p = get_special_folder (CSIDL_COMMON_APPDATA);
2940 g_array_append_val (data_dirs, p);
2942 /* Documents and Settings\All Users\Documents */
2943 p = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2945 g_array_append_val (data_dirs, p);
2947 /* Using the above subfolders of Documents and Settings perhaps
2948 * makes sense from a Windows perspective.
2950 * But looking at the actual use cases of this function in GTK+
2951 * and GNOME software, what we really want is the "share"
2952 * subdirectory of the installation directory for the package
2953 * our caller is a part of.
2955 * The address_of_function parameter, if non-NULL, points to a
2956 * function in the calling module. Use that to determine that
2957 * module's installation folder, and use its "share" subfolder.
2959 * Additionally, also use the "share" subfolder of the installation
2960 * locations of GLib and the .exe file being run.
2962 * To guard against none of the above being what is really wanted,
2963 * callers of this function should have Win32-specific code to look
2964 * up their installation folder themselves, and handle a subfolder
2965 * "share" of it in the same way as the folders returned from this
2969 p = get_module_share_dir (address_of_function);
2971 g_array_append_val (data_dirs, p);
2973 if (glib_dll != NULL)
2975 gchar *glib_root = g_win32_get_package_installation_directory_of_module (glib_dll);
2976 p = g_build_filename (glib_root, "share", NULL);
2978 g_array_append_val (data_dirs, p);
2982 exe_root = g_win32_get_package_installation_directory_of_module (NULL);
2983 p = g_build_filename (exe_root, "share", NULL);
2985 g_array_append_val (data_dirs, p);
2988 retval = (gchar **) g_array_free (data_dirs, FALSE);
2990 if (address_of_function)
2992 if (hmodule != NULL)
2993 g_hash_table_insert (per_module_data_dirs, hmodule, retval);
2994 G_UNLOCK (g_utils_global);
2997 return (const gchar * const *) retval;
3003 * g_get_system_data_dirs:
3005 * Returns an ordered list of base directories in which to access
3006 * system-wide application data.
3008 * On UNIX platforms this is determined using the mechanisms described in
3009 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
3010 * XDG Base Directory Specification</ulink>
3011 * In this case the list of directories retrieved will be XDG_DATA_DIRS.
3013 * On Windows the first elements in the list are the Application Data
3014 * and Documents folders for All Users. (These can be determined only
3015 * on Windows 2000 or later and are not present in the list on other
3016 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and
3017 * CSIDL_COMMON_DOCUMENTS.
3019 * Then follows the "share" subfolder in the installation folder for
3020 * the package containing the DLL that calls this function, if it can
3023 * Finally the list contains the "share" subfolder in the installation
3024 * folder for GLib, and in the installation folder for the package the
3025 * application's .exe file belongs to.
3027 * The installation folders above are determined by looking up the
3028 * folder where the module (DLL or EXE) in question is located. If the
3029 * folder's name is "bin", its parent is used, otherwise the folder
3032 * Note that on Windows the returned list can vary depending on where
3033 * this function is called.
3035 * Return value: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib that must
3036 * not be modified or freed.
3039 const gchar * const *
3040 g_get_system_data_dirs (void)
3042 gchar **data_dir_vector;
3044 G_LOCK (g_utils_global);
3046 if (!g_system_data_dirs)
3049 data_dir_vector = (gchar **) g_win32_get_system_data_dirs_for_module (NULL);
3051 gchar *data_dirs = (gchar *) g_getenv ("XDG_DATA_DIRS");
3053 if (!data_dirs || !data_dirs[0])
3054 data_dirs = "/usr/local/share/:/usr/share/";
3056 data_dir_vector = g_strsplit (data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
3059 g_system_data_dirs = data_dir_vector;
3062 data_dir_vector = g_system_data_dirs;
3064 G_UNLOCK (g_utils_global);
3066 return (const gchar * const *) data_dir_vector;
3070 * g_get_system_config_dirs:
3072 * Returns an ordered list of base directories in which to access
3073 * system-wide configuration information.
3075 * On UNIX platforms this is determined using the mechanisms described in
3076 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
3077 * XDG Base Directory Specification</ulink>.
3078 * In this case the list of directories retrieved will be XDG_CONFIG_DIRS.
3080 * On Windows is the directory that contains application data for all users.
3081 * A typical path is C:\Documents and Settings\All Users\Application Data.
3082 * This folder is used for application data that is not user specific.
3083 * For example, an application can store a spell-check dictionary, a database
3084 * of clip art, or a log file in the CSIDL_COMMON_APPDATA folder.
3085 * This information will not roam and is available to anyone using the computer.
3087 * Return value: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib that must
3088 * not be modified or freed.
3091 const gchar * const *
3092 g_get_system_config_dirs (void)
3094 gchar *conf_dirs, **conf_dir_vector;
3096 G_LOCK (g_utils_global);
3098 if (!g_system_config_dirs)
3101 conf_dirs = get_special_folder (CSIDL_COMMON_APPDATA);
3104 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
3109 /* Return empty list */
3110 conf_dir_vector = g_strsplit ("", G_SEARCHPATH_SEPARATOR_S, 0);
3113 conf_dirs = (gchar *) g_getenv ("XDG_CONFIG_DIRS");
3115 if (!conf_dirs || !conf_dirs[0])
3116 conf_dirs = "/etc/xdg";
3118 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
3121 g_system_config_dirs = conf_dir_vector;
3124 conf_dir_vector = g_system_config_dirs;
3125 G_UNLOCK (g_utils_global);
3127 return (const gchar * const *) conf_dir_vector;
3132 static GHashTable *alias_table = NULL;
3134 /* read an alias file for the locales */
3136 read_aliases (gchar *file)
3142 alias_table = g_hash_table_new (g_str_hash, g_str_equal);
3143 fp = fopen (file,"r");
3146 while (fgets (buf, 256, fp))
3152 /* Line is a comment */
3153 if ((buf[0] == '#') || (buf[0] == '\0'))
3156 /* Reads first column */
3157 for (p = buf, q = NULL; *p; p++) {
3158 if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
3161 while ((*q == '\t') || (*q == ' ')) {
3167 /* The line only had one column */
3168 if (!q || *q == '\0')
3171 /* Read second column */
3172 for (p = q; *p; p++) {
3173 if ((*p == '\t') || (*p == ' ')) {
3179 /* Add to alias table if necessary */
3180 if (!g_hash_table_lookup (alias_table, buf)) {
3181 g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
3190 unalias_lang (char *lang)
3197 read_aliases ("/usr/share/locale/locale.alias");
3200 while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
3205 static gboolean said_before = FALSE;
3207 g_warning ("Too many alias levels for a locale, "
3208 "may indicate a loop");
3217 /* Mask for components of locale spec. The ordering here is from
3218 * least significant to most significant
3222 COMPONENT_CODESET = 1 << 0,
3223 COMPONENT_TERRITORY = 1 << 1,
3224 COMPONENT_MODIFIER = 1 << 2
3227 /* Break an X/Open style locale specification into components
3230 explode_locale (const gchar *locale,
3236 const gchar *uscore_pos;
3237 const gchar *at_pos;
3238 const gchar *dot_pos;
3242 uscore_pos = strchr (locale, '_');
3243 dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
3244 at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
3248 mask |= COMPONENT_MODIFIER;
3249 *modifier = g_strdup (at_pos);
3252 at_pos = locale + strlen (locale);
3256 mask |= COMPONENT_CODESET;
3257 *codeset = g_strndup (dot_pos, at_pos - dot_pos);
3264 mask |= COMPONENT_TERRITORY;
3265 *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
3268 uscore_pos = dot_pos;
3270 *language = g_strndup (locale, uscore_pos - locale);
3276 * Compute all interesting variants for a given locale name -
3277 * by stripping off different components of the value.
3279 * For simplicity, we assume that the locale is in
3280 * X/Open format: language[_territory][.codeset][@modifier]
3282 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
3283 * as well. We could just copy the code from glibc wholesale
3284 * but it is big, ugly, and complicated, so I'm reluctant
3285 * to do so when this should handle 99% of the time...
3288 append_locale_variants (GPtrArray *array,
3289 const gchar *locale)
3291 gchar *language = NULL;
3292 gchar *territory = NULL;
3293 gchar *codeset = NULL;
3294 gchar *modifier = NULL;
3299 g_return_if_fail (locale != NULL);
3301 mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
3303 /* Iterate through all possible combinations, from least attractive
3304 * to most attractive.
3306 for (j = 0; j <= mask; ++j)
3310 if ((i & ~mask) == 0)
3312 gchar *val = g_strconcat (language,
3313 (i & COMPONENT_TERRITORY) ? territory : "",
3314 (i & COMPONENT_CODESET) ? codeset : "",
3315 (i & COMPONENT_MODIFIER) ? modifier : "",
3317 g_ptr_array_add (array, val);
3322 if (mask & COMPONENT_CODESET)
3324 if (mask & COMPONENT_TERRITORY)
3326 if (mask & COMPONENT_MODIFIER)
3331 * g_get_locale_variants:
3332 * @locale: a locale identifier
3334 * Returns a list of derived variants of @locale, which can be used to
3335 * e.g. construct locale-dependent filenames or search paths. The returned
3336 * list is sorted from most desirable to least desirable.
3337 * This function handles territory, charset and extra locale modifiers.
3339 * For example, if @locale is "fr_BE", then the returned list
3342 * If you need the list of variants for the <emphasis>current locale</emphasis>,
3343 * use g_get_language_names().
3345 * Returns: (transfer full) (array zero-terminated=1) (element-type utf8): a newly
3346 * allocated array of newly allocated strings with the locale variants. Free with
3352 g_get_locale_variants (const gchar *locale)
3356 g_return_val_if_fail (locale != NULL, NULL);
3358 array = g_ptr_array_sized_new (8);
3359 append_locale_variants (array, locale);
3360 g_ptr_array_add (array, NULL);
3362 return (gchar **) g_ptr_array_free (array, FALSE);
3365 /* The following is (partly) taken from the gettext package.
3366 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */
3368 static const gchar *
3369 guess_category_value (const gchar *category_name)
3371 const gchar *retval;
3373 /* The highest priority value is the `LANGUAGE' environment
3374 variable. This is a GNU extension. */
3375 retval = g_getenv ("LANGUAGE");
3376 if ((retval != NULL) && (retval[0] != '\0'))
3379 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
3380 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
3381 systems this can be done by the `setlocale' function itself. */
3383 /* Setting of LC_ALL overwrites all other. */
3384 retval = g_getenv ("LC_ALL");
3385 if ((retval != NULL) && (retval[0] != '\0'))
3388 /* Next comes the name of the desired category. */
3389 retval = g_getenv (category_name);
3390 if ((retval != NULL) && (retval[0] != '\0'))
3393 /* Last possibility is the LANG environment variable. */
3394 retval = g_getenv ("LANG");
3395 if ((retval != NULL) && (retval[0] != '\0'))
3398 #ifdef G_PLATFORM_WIN32
3399 /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
3400 * LANG, which we already did above. Oh well. The main point of
3401 * calling g_win32_getlocale() is to get the thread's locale as used
3402 * by Windows and the Microsoft C runtime (in the "English_United
3403 * States" format) translated into the Unixish format.
3406 char *locale = g_win32_getlocale ();
3407 retval = g_intern_string (locale);
3416 typedef struct _GLanguageNamesCache GLanguageNamesCache;
3418 struct _GLanguageNamesCache {
3420 gchar **language_names;
3424 language_names_cache_free (gpointer data)
3426 GLanguageNamesCache *cache = data;
3427 g_free (cache->languages);
3428 g_strfreev (cache->language_names);
3433 * g_get_language_names:
3435 * Computes a list of applicable locale names, which can be used to
3436 * e.g. construct locale-dependent filenames or search paths. The returned
3437 * list is sorted from most desirable to least desirable and always contains
3438 * the default locale "C".
3440 * For example, if LANGUAGE=de:en_US, then the returned list is
3441 * "de", "en_US", "en", "C".
3443 * This function consults the environment variables <envar>LANGUAGE</envar>,
3444 * <envar>LC_ALL</envar>, <envar>LC_MESSAGES</envar> and <envar>LANG</envar>
3445 * to find the list of locales specified by the user.
3447 * Return value: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib
3448 * that must not be modified or freed.
3452 const gchar * const *
3453 g_get_language_names (void)
3455 static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
3456 GLanguageNamesCache *cache = g_static_private_get (&cache_private);
3461 cache = g_new0 (GLanguageNamesCache, 1);
3462 g_static_private_set (&cache_private, cache, language_names_cache_free);
3465 value = guess_category_value ("LC_MESSAGES");
3469 if (!(cache->languages && strcmp (cache->languages, value) == 0))
3474 g_free (cache->languages);
3475 g_strfreev (cache->language_names);
3476 cache->languages = g_strdup (value);
3478 array = g_ptr_array_sized_new (8);
3480 alist = g_strsplit (value, ":", 0);
3481 for (a = alist; *a; a++)
3482 append_locale_variants (array, unalias_lang (*a));
3484 g_ptr_array_add (array, g_strdup ("C"));
3485 g_ptr_array_add (array, NULL);
3487 cache->language_names = (gchar **) g_ptr_array_free (array, FALSE);
3490 return (const gchar * const *) cache->language_names;
3495 * @v: a #gpointer key
3497 * Converts a gpointer to a hash value.
3498 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3499 * when using pointers as keys in a #GHashTable.
3501 * Returns: a hash value corresponding to the key.
3504 g_direct_hash (gconstpointer v)
3506 return GPOINTER_TO_UINT (v);
3512 * @v2: a key to compare with @v1.
3514 * Compares two #gpointer arguments and returns %TRUE if they are equal.
3515 * It can be passed to g_hash_table_new() as the @key_equal_func
3516 * parameter, when using pointers as keys in a #GHashTable.
3518 * Returns: %TRUE if the two keys match.
3521 g_direct_equal (gconstpointer v1,
3529 * @v1: a pointer to a #gint key.
3530 * @v2: a pointer to a #gint key to compare with @v1.
3532 * Compares the two #gint values being pointed to and returns
3533 * %TRUE if they are equal.
3534 * It can be passed to g_hash_table_new() as the @key_equal_func
3535 * parameter, when using pointers to integers as keys in a #GHashTable.
3537 * Returns: %TRUE if the two keys match.
3540 g_int_equal (gconstpointer v1,
3543 return *((const gint*) v1) == *((const gint*) v2);
3548 * @v: a pointer to a #gint key
3550 * Converts a pointer to a #gint to a hash value.
3551 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3552 * when using pointers to integers values as keys in a #GHashTable.
3554 * Returns: a hash value corresponding to the key.
3557 g_int_hash (gconstpointer v)
3559 return *(const gint*) v;
3564 * @v1: a pointer to a #gint64 key.
3565 * @v2: a pointer to a #gint64 key to compare with @v1.
3567 * Compares the two #gint64 values being pointed to and returns
3568 * %TRUE if they are equal.
3569 * It can be passed to g_hash_table_new() as the @key_equal_func
3570 * parameter, when using pointers to 64-bit integers as keys in a #GHashTable.
3572 * Returns: %TRUE if the two keys match.
3577 g_int64_equal (gconstpointer v1,
3580 return *((const gint64*) v1) == *((const gint64*) v2);
3585 * @v: a pointer to a #gint64 key
3587 * Converts a pointer to a #gint64 to a hash value.
3588 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3589 * when using pointers to 64-bit integers values as keys in a #GHashTable.
3591 * Returns: a hash value corresponding to the key.
3596 g_int64_hash (gconstpointer v)
3598 return (guint) *(const gint64*) v;
3603 * @v1: a pointer to a #gdouble key.
3604 * @v2: a pointer to a #gdouble key to compare with @v1.
3606 * Compares the two #gdouble values being pointed to and returns
3607 * %TRUE if they are equal.
3608 * It can be passed to g_hash_table_new() as the @key_equal_func
3609 * parameter, when using pointers to doubles as keys in a #GHashTable.
3611 * Returns: %TRUE if the two keys match.
3616 g_double_equal (gconstpointer v1,
3619 return *((const gdouble*) v1) == *((const gdouble*) v2);
3624 * @v: a pointer to a #gdouble key
3626 * Converts a pointer to a #gdouble to a hash value.
3627 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3628 * when using pointers to doubles as keys in a #GHashTable.
3630 * Returns: a hash value corresponding to the key.
3635 g_double_hash (gconstpointer v)
3637 return (guint) *(const gdouble*) v;
3641 * g_nullify_pointer:
3642 * @nullify_location: the memory address of the pointer.
3644 * Set the pointer at the specified location to %NULL.
3647 g_nullify_pointer (gpointer *nullify_location)
3649 g_return_if_fail (nullify_location != NULL);
3651 *nullify_location = NULL;
3657 * Get the codeset for the current locale.
3659 * Return value: a newly allocated string containing the name
3660 * of the codeset. This string must be freed with g_free().
3663 g_get_codeset (void)
3665 const gchar *charset;
3667 g_get_charset (&charset);
3669 return g_strdup (charset);
3675 * _glib_get_locale_dir:
3677 * Return the path to the share\locale or lib\locale subfolder of the
3678 * GLib installation folder. The path is in the system codepage. We
3679 * have to use system codepage as bindtextdomain() doesn't have a
3683 _glib_get_locale_dir (void)
3685 gchar *install_dir = NULL, *locale_dir;
3686 gchar *retval = NULL;
3688 if (glib_dll != NULL)
3689 install_dir = g_win32_get_package_installation_directory_of_module (glib_dll);
3694 * Append "/share/locale" or "/lib/locale" depending on whether
3695 * autoconfigury detected GNU gettext or not.
3697 const char *p = GLIB_LOCALE_DIR + strlen (GLIB_LOCALE_DIR);
3703 locale_dir = g_build_filename (install_dir, p, NULL);
3705 retval = g_win32_locale_filename_from_utf8 (locale_dir);
3707 g_free (install_dir);
3708 g_free (locale_dir);
3714 return g_strdup ("");
3717 #undef GLIB_LOCALE_DIR
3719 #endif /* G_OS_WIN32 */
3722 ensure_gettext_initialized (void)
3724 static gsize initialised;
3726 g_thread_init_glib ();
3728 if (g_once_init_enter (&initialised))
3731 gchar *tmp = _glib_get_locale_dir ();
3732 bindtextdomain (GETTEXT_PACKAGE, tmp);
3735 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
3737 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
3738 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
3740 g_once_init_leave (&initialised, TRUE);
3746 * @str: The string to be translated
3748 * Returns the translated string from the glib translations.
3749 * This is an internal function and should only be used by
3750 * the internals of glib (such as libgio).
3752 * Returns: the transation of @str to the current locale
3755 glib_gettext (const gchar *str)
3757 ensure_gettext_initialized();
3759 return g_dgettext (GETTEXT_PACKAGE, str);
3764 * @msgctxtid: a combined message context and message id, separated
3765 * by a \004 character
3766 * @msgidoffset: the offset of the message id in @msgctxid
3768 * This function is a variant of glib_gettext() which supports
3769 * a disambiguating message context. See g_dpgettext() for full
3772 * This is an internal function and should only be used by
3773 * the internals of glib (such as libgio).
3775 * Returns: the transation of @str to the current locale
3778 glib_pgettext(const gchar *msgctxtid,
3781 ensure_gettext_initialized();
3783 return g_dpgettext (GETTEXT_PACKAGE, msgctxtid, msgidoffset);
3786 #if defined (G_OS_WIN32) && !defined (_WIN64)
3788 /* Binary compatibility versions. Not for newly compiled code. */
3790 #undef g_find_program_in_path
3793 g_find_program_in_path (const gchar *program)
3795 gchar *utf8_program = g_locale_to_utf8 (program, -1, NULL, NULL, NULL);
3796 gchar *utf8_retval = g_find_program_in_path_utf8 (utf8_program);
3799 g_free (utf8_program);
3800 if (utf8_retval == NULL)
3802 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
3803 g_free (utf8_retval);
3808 #undef g_get_current_dir
3811 g_get_current_dir (void)
3813 gchar *utf8_dir = g_get_current_dir_utf8 ();
3814 gchar *dir = g_locale_from_utf8 (utf8_dir, -1, NULL, NULL, NULL);
3822 g_getenv (const gchar *variable)
3824 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3825 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
3829 g_free (utf8_variable);
3832 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
3833 quark = g_quark_from_string (value);
3836 return g_quark_to_string (quark);
3842 g_setenv (const gchar *variable,
3846 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3847 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
3848 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
3850 g_free (utf8_variable);
3851 g_free (utf8_value);
3859 g_unsetenv (const gchar *variable)
3861 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3863 g_unsetenv_utf8 (utf8_variable);
3865 g_free (utf8_variable);
3868 #undef g_get_user_name
3871 g_get_user_name (void)
3873 g_get_any_init_locked ();
3874 return g_user_name_cp;
3877 #undef g_get_real_name
3880 g_get_real_name (void)
3882 g_get_any_init_locked ();
3883 return g_real_name_cp;
3886 #undef g_get_home_dir
3889 g_get_home_dir (void)
3891 g_get_any_init_locked ();
3892 return g_home_dir_cp;
3895 #undef g_get_tmp_dir
3898 g_get_tmp_dir (void)
3900 g_get_any_init_locked ();
3901 return g_tmp_dir_cp;