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)
212 _glib_get_dll_directory (void)
216 wchar_t wc_fn[MAX_PATH];
219 if (glib_dll == NULL)
223 /* This code is different from that in
224 * g_win32_get_package_installation_directory_of_module() in that
225 * here we return the actual folder where the GLib DLL is. We don't
226 * do the check for it being in a "bin" or "lib" subfolder and then
227 * returning the parent of that.
229 * In a statically built GLib, glib_dll will be NULL and we will
230 * thus look up the application's .exe file's location.
232 if (!GetModuleFileNameW (glib_dll, wc_fn, MAX_PATH))
235 retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
237 p = strrchr (retval, G_DIR_SEPARATOR);
251 * glib_check_version:
252 * @required_major: the required major version.
253 * @required_minor: the required minor version.
254 * @required_micro: the required micro version.
256 * Checks that the GLib library in use is compatible with the
257 * given version. Generally you would pass in the constants
258 * #GLIB_MAJOR_VERSION, #GLIB_MINOR_VERSION, #GLIB_MICRO_VERSION
259 * as the three arguments to this function; that produces
260 * a check that the library in use is compatible with
261 * the version of GLib the application or module was compiled
264 * Compatibility is defined by two things: first the version
265 * of the running library is newer than the version
266 * @required_major.required_minor.@required_micro. Second
267 * the running library must be binary compatible with the
268 * version @required_major.required_minor.@required_micro
269 * (same major version.)
271 * Return value: %NULL if the GLib library is compatible with the
272 * given version, or a string describing the version mismatch.
273 * The returned string is owned by GLib and must not be modified
279 glib_check_version (guint required_major,
280 guint required_minor,
281 guint required_micro)
283 gint glib_effective_micro = 100 * GLIB_MINOR_VERSION + GLIB_MICRO_VERSION;
284 gint required_effective_micro = 100 * required_minor + required_micro;
286 if (required_major > GLIB_MAJOR_VERSION)
287 return "GLib version too old (major mismatch)";
288 if (required_major < GLIB_MAJOR_VERSION)
289 return "GLib version too new (major mismatch)";
290 if (required_effective_micro < glib_effective_micro - GLIB_BINARY_AGE)
291 return "GLib version too new (micro mismatch)";
292 if (required_effective_micro > glib_effective_micro)
293 return "GLib version too old (micro mismatch)";
297 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
300 * @dest: the destination address to copy the bytes to.
301 * @src: the source address to copy the bytes from.
302 * @len: the number of bytes to copy.
304 * Copies a block of memory @len bytes long, from @src to @dest.
305 * The source and destination areas may overlap.
307 * In order to use this function, you must include
308 * <filename>string.h</filename> yourself, because this macro will
309 * typically simply resolve to memmove() and GLib does not include
310 * <filename>string.h</filename> for you.
313 g_memmove (gpointer dest,
317 gchar* destptr = dest;
318 const gchar* srcptr = src;
319 if (src + len < dest || dest + len < src)
321 bcopy (src, dest, len);
324 else if (dest <= src)
327 *(destptr++) = *(srcptr++);
334 *(--destptr) = *(--srcptr);
337 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
345 * @func: (scope async): the function to call on normal program termination.
347 * Specifies a function to be called at normal program termination.
349 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
350 * macro that maps to a call to the atexit() function in the C
351 * library. This means that in case the code that calls g_atexit(),
352 * i.e. atexit(), is in a DLL, the function will be called when the
353 * DLL is detached from the program. This typically makes more sense
354 * than that the function is called when the GLib DLL is detached,
355 * which happened earlier when g_atexit() was a function in the GLib
358 * The behaviour of atexit() in the context of dynamically loaded
359 * modules is not formally specified and varies wildly.
361 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
362 * loaded module which is unloaded before the program terminates might
363 * well cause a crash at program exit.
365 * Some POSIX systems implement atexit() like Windows, and have each
366 * dynamically loaded module maintain an own atexit chain that is
367 * called when the module is unloaded.
369 * On other POSIX systems, before a dynamically loaded module is
370 * unloaded, the registered atexit functions (if any) residing in that
371 * module are called, regardless where the code that registered them
372 * resided. This is presumably the most robust approach.
374 * As can be seen from the above, for portability it's best to avoid
375 * calling g_atexit() (or atexit()) except in the main executable of a
379 g_atexit (GVoidFunc func)
382 const gchar *error = NULL;
384 /* keep this in sync with glib.h */
386 #ifdef G_NATIVE_ATEXIT
387 result = ATEXIT (func);
389 error = g_strerror (errno);
390 #elif defined (HAVE_ATEXIT)
391 # ifdef NeXT /* @#%@! NeXTStep */
392 result = !atexit ((void (*)(void)) func);
394 error = g_strerror (errno);
396 result = atexit ((void (*)(void)) func);
398 error = g_strerror (errno);
400 #elif defined (HAVE_ON_EXIT)
401 result = on_exit ((void (*)(int, void *)) func, NULL);
403 error = g_strerror (errno);
406 error = "no implementation";
407 #endif /* G_NATIVE_ATEXIT */
410 g_error ("Could not register atexit() function: %s", error);
413 /* Based on execvp() from GNU Libc.
414 * Some of this code is cut-and-pasted into gspawn.c
418 my_strchrnul (const gchar *str,
421 gchar *p = (gchar*)str;
422 while (*p && (*p != c))
430 static gchar *inner_find_program_in_path (const gchar *program);
433 g_find_program_in_path (const gchar *program)
435 const gchar *last_dot = strrchr (program, '.');
437 if (last_dot == NULL ||
438 strchr (last_dot, '\\') != NULL ||
439 strchr (last_dot, '/') != NULL)
441 const gint program_length = strlen (program);
442 gchar *pathext = g_build_path (";",
443 ".exe;.cmd;.bat;.com",
444 g_getenv ("PATHEXT"),
447 gchar *decorated_program;
453 gchar *q = my_strchrnul (p, ';');
455 decorated_program = g_malloc (program_length + (q-p) + 1);
456 memcpy (decorated_program, program, program_length);
457 memcpy (decorated_program+program_length, p, q-p);
458 decorated_program [program_length + (q-p)] = '\0';
460 retval = inner_find_program_in_path (decorated_program);
461 g_free (decorated_program);
469 } while (*p++ != '\0');
474 return inner_find_program_in_path (program);
480 * g_find_program_in_path:
481 * @program: a program name in the GLib file name encoding
483 * Locates the first executable named @program in the user's path, in the
484 * same way that execvp() would locate it. Returns an allocated string
485 * with the absolute path name, or %NULL if the program is not found in
486 * the path. If @program is already an absolute path, returns a copy of
487 * @program if @program exists and is executable, and %NULL otherwise.
489 * On Windows, if @program does not have a file type suffix, tries
490 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
491 * the <envar>PATHEXT</envar> environment variable.
493 * On Windows, it looks for the file in the same way as CreateProcess()
494 * would. This means first in the directory where the executing
495 * program was loaded from, then in the current directory, then in the
496 * Windows 32-bit system directory, then in the Windows directory, and
497 * finally in the directories in the <envar>PATH</envar> environment
498 * variable. If the program is found, the return value contains the
499 * full name including the type suffix.
501 * Return value: absolute path, or %NULL
505 inner_find_program_in_path (const gchar *program)
508 g_find_program_in_path (const gchar *program)
511 const gchar *path, *p;
512 gchar *name, *freeme;
514 const gchar *path_copy;
515 gchar *filename = NULL, *appdir = NULL;
516 gchar *sysdir = NULL, *windir = NULL;
518 wchar_t wfilename[MAXPATHLEN], wsysdir[MAXPATHLEN],
524 g_return_val_if_fail (program != NULL, NULL);
526 /* If it is an absolute path, or a relative path including subdirectories,
527 * don't look in PATH.
529 if (g_path_is_absolute (program)
530 || strchr (program, G_DIR_SEPARATOR) != NULL
532 || strchr (program, '/') != NULL
536 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE) &&
537 !g_file_test (program, G_FILE_TEST_IS_DIR))
538 return g_strdup (program);
543 path = g_getenv ("PATH");
544 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
547 /* There is no `PATH' in the environment. The default
548 * search path in GNU libc is the current directory followed by
549 * the path `confstr' returns for `_CS_PATH'.
552 /* In GLib we put . last, for security, and don't use the
553 * unportable confstr(); UNIX98 does not actually specify
554 * what to search if PATH is unset. POSIX may, dunno.
557 path = "/bin:/usr/bin:.";
560 n = GetModuleFileNameW (NULL, wfilename, MAXPATHLEN);
561 if (n > 0 && n < MAXPATHLEN)
562 filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
564 n = GetSystemDirectoryW (wsysdir, MAXPATHLEN);
565 if (n > 0 && n < MAXPATHLEN)
566 sysdir = g_utf16_to_utf8 (wsysdir, -1, NULL, NULL, NULL);
568 n = GetWindowsDirectoryW (wwindir, MAXPATHLEN);
569 if (n > 0 && n < MAXPATHLEN)
570 windir = g_utf16_to_utf8 (wwindir, -1, NULL, NULL, NULL);
574 appdir = g_path_get_dirname (filename);
578 path = g_strdup (path);
582 const gchar *tem = path;
583 path = g_strconcat (windir, ";", path, NULL);
584 g_free ((gchar *) tem);
590 const gchar *tem = path;
591 path = g_strconcat (sysdir, ";", path, NULL);
592 g_free ((gchar *) tem);
597 const gchar *tem = path;
598 path = g_strconcat (".;", path, NULL);
599 g_free ((gchar *) tem);
604 const gchar *tem = path;
605 path = g_strconcat (appdir, ";", path, NULL);
606 g_free ((gchar *) tem);
613 len = strlen (program) + 1;
614 pathlen = strlen (path);
615 freeme = name = g_malloc (pathlen + len + 1);
617 /* Copy the file name at the top, including '\0' */
618 memcpy (name + pathlen + 1, program, len);
619 name = name + pathlen;
620 /* And add the slash before the filename */
621 *name = G_DIR_SEPARATOR;
629 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
632 /* Two adjacent colons, or a colon at the beginning or the end
633 * of `PATH' means to search the current directory.
637 startp = memcpy (name - (p - path), path, p - path);
639 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE) &&
640 !g_file_test (startp, G_FILE_TEST_IS_DIR))
643 ret = g_strdup (startp);
646 g_free ((gchar *) path_copy);
651 while (*p++ != '\0');
655 g_free ((gchar *) path_copy);
662 debug_key_matches (const gchar *key,
666 for (; length; length--, key++, token++)
668 char k = (*key == '_') ? '-' : tolower (*key );
669 char t = (*token == '_') ? '-' : tolower (*token);
679 * g_parse_debug_string:
680 * @string: (allow-none): a list of debug options separated by colons, spaces, or
682 * @keys: (array length=nkeys): pointer to an array of #GDebugKey which associate
683 * strings with bit flags.
684 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
686 * Parses a string containing debugging options
687 * into a %guint containing bit flags. This is used
688 * within GDK and GTK+ to parse the debug options passed on the
689 * command line or through environment variables.
691 * If @string is equal to "all", all flags are set. If @string
692 * is equal to "help", all the available keys in @keys are printed
693 * out to standard error.
695 * Returns: the combined set of bit flags.
698 g_parse_debug_string (const gchar *string,
699 const GDebugKey *keys,
708 /* this function is used by gmem.c/gslice.c initialization code,
709 * so introducing malloc dependencies here would require adaptions
710 * of those code portions.
713 if (!g_ascii_strcasecmp (string, "all"))
715 for (i=0; i<nkeys; i++)
716 result |= keys[i].value;
718 else if (!g_ascii_strcasecmp (string, "help"))
720 /* using stdio directly for the reason stated above */
721 fprintf (stderr, "Supported debug values: ");
722 for (i=0; i<nkeys; i++)
723 fprintf (stderr, " %s", keys[i].key);
724 fprintf (stderr, "\n");
728 const gchar *p = string;
733 q = strpbrk (p, ":;, \t");
737 for (i = 0; i < nkeys; i++)
738 if (debug_key_matches (keys[i].key, p, q - p))
739 result |= keys[i].value;
752 * @file_name: the name of the file.
754 * Gets the name of the file without any leading directory components.
755 * It returns a pointer into the given file name string.
757 * Return value: the name of the file without any leading directory components.
759 * Deprecated:2.2: Use g_path_get_basename() instead, but notice that
760 * g_path_get_basename() allocates new memory for the returned string, unlike
761 * this function which returns a pointer into the argument.
764 g_basename (const gchar *file_name)
766 register gchar *base;
768 g_return_val_if_fail (file_name != NULL, NULL);
770 base = strrchr (file_name, G_DIR_SEPARATOR);
774 gchar *q = strrchr (file_name, '/');
775 if (base == NULL || (q != NULL && q > base))
784 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
785 return (gchar*) file_name + 2;
786 #endif /* G_OS_WIN32 */
788 return (gchar*) file_name;
792 * g_path_get_basename:
793 * @file_name: the name of the file.
795 * Gets the last component of the filename. If @file_name ends with a
796 * directory separator it gets the component before the last slash. If
797 * @file_name consists only of directory separators (and on Windows,
798 * possibly a drive letter), a single separator is returned. If
799 * @file_name is empty, it gets ".".
801 * Return value: a newly allocated string containing the last component of
805 g_path_get_basename (const gchar *file_name)
807 register gssize base;
808 register gssize last_nonslash;
812 g_return_val_if_fail (file_name != NULL, NULL);
814 if (file_name[0] == '\0')
816 return g_strdup (".");
818 last_nonslash = strlen (file_name) - 1;
820 while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
823 if (last_nonslash == -1)
824 /* string only containing slashes */
825 return g_strdup (G_DIR_SEPARATOR_S);
828 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
829 /* string only containing slashes and a drive */
830 return g_strdup (G_DIR_SEPARATOR_S);
831 #endif /* G_OS_WIN32 */
833 base = last_nonslash;
835 while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
839 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
841 #endif /* G_OS_WIN32 */
843 len = last_nonslash - base;
844 retval = g_malloc (len + 1);
845 memcpy (retval, file_name + base + 1, len);
851 * g_path_is_absolute:
852 * @file_name: a file name.
854 * Returns %TRUE if the given @file_name is an absolute file name.
855 * Note that this is a somewhat vague concept on Windows.
857 * On POSIX systems, an absolute file name is well-defined. It always
858 * starts from the single root directory. For example "/usr/local".
860 * On Windows, the concepts of current drive and drive-specific
861 * current directory introduce vagueness. This function interprets as
862 * an absolute file name one that either begins with a directory
863 * separator such as "\Users\tml" or begins with the root on a drive,
864 * for example "C:\Windows". The first case also includes UNC paths
865 * such as "\\myserver\docs\foo". In all cases, either slashes or
866 * backslashes are accepted.
868 * Note that a file name relative to the current drive root does not
869 * truly specify a file uniquely over time and across processes, as
870 * the current drive is a per-process value and can be changed.
872 * File names relative the current directory on some specific drive,
873 * such as "D:foo/bar", are not interpreted as absolute by this
874 * function, but they obviously are not relative to the normal current
875 * directory as returned by getcwd() or g_get_current_dir()
876 * either. Such paths should be avoided, or need to be handled using
877 * Windows-specific code.
879 * Returns: %TRUE if @file_name is absolute.
882 g_path_is_absolute (const gchar *file_name)
884 g_return_val_if_fail (file_name != NULL, FALSE);
886 if (G_IS_DIR_SEPARATOR (file_name[0]))
890 /* Recognize drive letter on native Windows */
891 if (g_ascii_isalpha (file_name[0]) &&
892 file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
894 #endif /* G_OS_WIN32 */
901 * @file_name: a file name.
903 * Returns a pointer into @file_name after the root component, i.e. after
904 * the "/" in UNIX or "C:\" under Windows. If @file_name is not an absolute
905 * path it returns %NULL.
907 * Returns: a pointer into @file_name after the root component.
910 g_path_skip_root (const gchar *file_name)
912 g_return_val_if_fail (file_name != NULL, NULL);
914 #ifdef G_PLATFORM_WIN32
915 /* Skip \\server\share or //server/share */
916 if (G_IS_DIR_SEPARATOR (file_name[0]) &&
917 G_IS_DIR_SEPARATOR (file_name[1]) &&
919 !G_IS_DIR_SEPARATOR (file_name[2]))
923 p = strchr (file_name + 2, G_DIR_SEPARATOR);
926 gchar *q = strchr (file_name + 2, '/');
927 if (p == NULL || (q != NULL && q < p))
937 while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
940 /* Possibly skip a backslash after the share name */
941 if (G_IS_DIR_SEPARATOR (file_name[0]))
944 return (gchar *)file_name;
949 /* Skip initial slashes */
950 if (G_IS_DIR_SEPARATOR (file_name[0]))
952 while (G_IS_DIR_SEPARATOR (file_name[0]))
954 return (gchar *)file_name;
959 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
960 return (gchar *)file_name + 3;
968 * @mask: a #gulong containing flags
969 * @nth_bit: the index of the bit to start the search from
971 * Find the position of the first bit set in @mask, searching
972 * from (but not including) @nth_bit upwards. Bits are numbered
973 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
974 * usually). To start searching from the 0th bit, set @nth_bit to -1.
976 * Returns: the index of the first bit set which is higher than @nth_bit
981 * @mask: a #gulong containing flags
982 * @nth_bit: the index of the bit to start the search from
984 * Find the position of the first bit set in @mask, searching
985 * from (but not including) @nth_bit downwards. Bits are numbered
986 * from 0 (least significant) to sizeof(#gulong) * 8 - 1 (31 or 63,
987 * usually). To start searching from the last bit, set @nth_bit to
988 * -1 or GLIB_SIZEOF_LONG * 8.
990 * Returns: the index of the first bit set which is lower than @nth_bit
997 * Gets the number of bits used to hold @number,
998 * e.g. if @number is 4, 3 bits are needed.
1000 * Returns: the number of bits used to hold @number
1005 * @file_name: the name of the file
1007 * Gets the directory components of a file name.
1008 * If the file name has no directory components "." is returned.
1009 * The returned string should be freed when no longer needed.
1011 * Returns: the directory components of the file
1013 * Deprecated: use g_path_get_dirname() instead
1017 * g_path_get_dirname:
1018 * @file_name: the name of the file.
1020 * Gets the directory components of a file name. If the file name has no
1021 * directory components "." is returned. The returned string should be
1022 * freed when no longer needed.
1024 * Returns: the directory components of the file.
1027 g_path_get_dirname (const gchar *file_name)
1029 register gchar *base;
1032 g_return_val_if_fail (file_name != NULL, NULL);
1034 base = strrchr (file_name, G_DIR_SEPARATOR);
1037 gchar *q = strrchr (file_name, '/');
1038 if (base == NULL || (q != NULL && q > base))
1045 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
1047 gchar drive_colon_dot[4];
1049 drive_colon_dot[0] = file_name[0];
1050 drive_colon_dot[1] = ':';
1051 drive_colon_dot[2] = '.';
1052 drive_colon_dot[3] = '\0';
1054 return g_strdup (drive_colon_dot);
1057 return g_strdup (".");
1060 while (base > file_name && G_IS_DIR_SEPARATOR (*base))
1064 /* base points to the char before the last slash.
1066 * In case file_name is the root of a drive (X:\) or a child of the
1067 * root of a drive (X:\foo), include the slash.
1069 * In case file_name is the root share of an UNC path
1070 * (\\server\share), add a slash, returning \\server\share\ .
1072 * In case file_name is a direct child of a share in an UNC path
1073 * (\\server\share\foo), include the slash after the share name,
1074 * returning \\server\share\ .
1076 if (base == file_name + 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
1078 else if (G_IS_DIR_SEPARATOR (file_name[0]) &&
1079 G_IS_DIR_SEPARATOR (file_name[1]) &&
1081 !G_IS_DIR_SEPARATOR (file_name[2]) &&
1082 base >= file_name + 2)
1084 const gchar *p = file_name + 2;
1085 while (*p && !G_IS_DIR_SEPARATOR (*p))
1089 len = (guint) strlen (file_name) + 1;
1090 base = g_new (gchar, len + 1);
1091 strcpy (base, file_name);
1092 base[len-1] = G_DIR_SEPARATOR;
1096 if (G_IS_DIR_SEPARATOR (*p))
1099 while (*p && !G_IS_DIR_SEPARATOR (*p))
1107 len = (guint) 1 + base - file_name;
1109 base = g_new (gchar, len + 1);
1110 g_memmove (base, file_name, len);
1117 * g_get_current_dir:
1119 * Gets the current directory.
1120 * The returned string should be freed when no longer needed. The encoding
1121 * of the returned string is system defined. On Windows, it is always UTF-8.
1123 * Returns: the current directory.
1126 g_get_current_dir (void)
1131 wchar_t dummy[2], *wdir;
1134 len = GetCurrentDirectoryW (2, dummy);
1135 wdir = g_new (wchar_t, len);
1137 if (GetCurrentDirectoryW (len, wdir) == len - 1)
1138 dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
1143 dir = g_strdup ("\\");
1149 gchar *buffer = NULL;
1151 static gulong max_len = 0;
1154 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
1156 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
1157 * and, if that wasn't bad enough, hangs in doing so.
1159 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
1160 buffer = g_new (gchar, max_len + 1);
1162 dir = getwd (buffer);
1163 #else /* !sun || !HAVE_GETCWD */
1164 while (max_len < G_MAXULONG / 2)
1167 buffer = g_new (gchar, max_len + 1);
1169 dir = getcwd (buffer, max_len);
1171 if (dir || errno != ERANGE)
1176 #endif /* !sun || !HAVE_GETCWD */
1178 if (!dir || !*buffer)
1180 /* hm, should we g_error() out here?
1181 * this can happen if e.g. "./" has mode \0000
1183 buffer[0] = G_DIR_SEPARATOR;
1187 dir = g_strdup (buffer);
1196 * @variable: the environment variable to get, in the GLib file name encoding.
1198 * Returns the value of an environment variable. The name and value
1199 * are in the GLib file name encoding. On UNIX, this means the actual
1200 * bytes which might or might not be in some consistent character set
1201 * and encoding. On Windows, it is in UTF-8. On Windows, in case the
1202 * environment variable's value contains references to other
1203 * environment variables, they are expanded.
1205 * Return value: the value of the environment variable, or %NULL if
1206 * the environment variable is not found. The returned string may be
1207 * overwritten by the next call to g_getenv(), g_setenv() or
1211 g_getenv (const gchar *variable)
1215 g_return_val_if_fail (variable != NULL, NULL);
1217 return getenv (variable);
1219 #else /* G_OS_WIN32 */
1223 wchar_t dummy[2], *wname, *wvalue;
1226 g_return_val_if_fail (variable != NULL, NULL);
1227 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
1229 /* On Windows NT, it is relatively typical that environment
1230 * variables contain references to other environment variables. If
1231 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
1232 * environment variables would be stored in the Registry as
1233 * REG_EXPAND_SZ type values, and would then get automatically
1234 * expanded before a program sees them. But there is broken software
1235 * that stores environment variables as REG_SZ values even if they
1236 * contain references to other environment variables.)
1239 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1241 len = GetEnvironmentVariableW (wname, dummy, 2);
1251 wvalue = g_new (wchar_t, len);
1253 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
1260 if (wcschr (wvalue, L'%') != NULL)
1262 wchar_t *tem = wvalue;
1264 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
1268 wvalue = g_new (wchar_t, len);
1270 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
1280 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
1285 quark = g_quark_from_string (value);
1288 return g_quark_to_string (quark);
1290 #endif /* G_OS_WIN32 */
1293 /* _g_getenv_nomalloc
1294 * this function does a getenv() without doing any kind of allocation
1295 * through glib. it's suitable for chars <= 127 only (both, for the
1296 * variable name and the contents) and for contents < 1024 chars in
1297 * length. also, it aliases "" to a NULL return value.
1300 _g_getenv_nomalloc (const gchar *variable,
1303 const gchar *retval = getenv (variable);
1304 if (retval && retval[0])
1306 gint l = strlen (retval);
1309 strncpy (buffer, retval, l);
1319 * @variable: the environment variable to set, must not contain '='.
1320 * @value: the value for to set the variable to.
1321 * @overwrite: whether to change the variable if it already exists.
1323 * Sets an environment variable. Both the variable's name and value
1324 * should be in the GLib file name encoding. On UNIX, this means that
1325 * they can be any sequence of bytes. On Windows, they should be in
1328 * Note that on some systems, when variables are overwritten, the memory
1329 * used for the previous variables and its value isn't reclaimed.
1331 * Returns: %FALSE if the environment variable couldn't be set.
1336 g_setenv (const gchar *variable,
1347 g_return_val_if_fail (variable != NULL, FALSE);
1348 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1351 result = setenv (variable, value, overwrite);
1353 if (!overwrite && getenv (variable) != NULL)
1356 /* This results in a leak when you overwrite existing
1357 * settings. It would be fairly easy to fix this by keeping
1358 * our own parallel array or hash table.
1360 string = g_strconcat (variable, "=", value, NULL);
1361 result = putenv (string);
1365 #else /* G_OS_WIN32 */
1368 wchar_t *wname, *wvalue, *wassignment;
1371 g_return_val_if_fail (variable != NULL, FALSE);
1372 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1373 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
1374 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
1376 if (!overwrite && g_getenv (variable) != NULL)
1379 /* We want to (if possible) set both the environment variable copy
1380 * kept by the C runtime and the one kept by the system.
1382 * We can't use only the C runtime's putenv or _wputenv() as that
1383 * won't work for arbitrary Unicode strings in a "non-Unicode" app
1384 * (with main() and not wmain()). In a "main()" app the C runtime
1385 * initializes the C runtime's environment table by converting the
1386 * real (wide char) environment variables to system codepage, thus
1387 * breaking those that aren't representable in the system codepage.
1389 * As the C runtime's putenv() will also set the system copy, we do
1390 * the putenv() first, then call SetEnvironmentValueW ourselves.
1393 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1394 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
1395 tem = g_strconcat (variable, "=", value, NULL);
1396 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1399 _wputenv (wassignment);
1400 g_free (wassignment);
1402 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
1409 #endif /* G_OS_WIN32 */
1412 #ifdef HAVE__NSGETENVIRON
1413 #define environ (*_NSGetEnviron())
1414 #elif !defined(G_OS_WIN32)
1416 /* According to the Single Unix Specification, environ is not in
1417 * any system header, although unistd.h often declares it.
1419 extern char **environ;
1424 * @variable: the environment variable to remove, must not contain '='.
1426 * Removes an environment variable from the environment.
1428 * Note that on some systems, when variables are overwritten, the memory
1429 * used for the previous variables and its value isn't reclaimed.
1430 * Furthermore, this function can't be guaranteed to operate in a
1436 g_unsetenv (const gchar *variable)
1440 #ifdef HAVE_UNSETENV
1441 g_return_if_fail (variable != NULL);
1442 g_return_if_fail (strchr (variable, '=') == NULL);
1444 unsetenv (variable);
1445 #else /* !HAVE_UNSETENV */
1449 g_return_if_fail (variable != NULL);
1450 g_return_if_fail (strchr (variable, '=') == NULL);
1452 len = strlen (variable);
1454 /* Mess directly with the environ array.
1455 * This seems to be the only portable way to do this.
1457 * Note that we remove *all* environment entries for
1458 * the variable name, not just the first.
1463 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
1471 #endif /* !HAVE_UNSETENV */
1473 #else /* G_OS_WIN32 */
1475 wchar_t *wname, *wassignment;
1478 g_return_if_fail (variable != NULL);
1479 g_return_if_fail (strchr (variable, '=') == NULL);
1480 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
1482 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1483 tem = g_strconcat (variable, "=", NULL);
1484 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1487 _wputenv (wassignment);
1488 g_free (wassignment);
1490 SetEnvironmentVariableW (wname, NULL);
1494 #endif /* G_OS_WIN32 */
1500 * Gets the names of all variables set in the environment.
1502 * Returns: (array zero-terminated=1) (transfer full): a %NULL-terminated list of strings which must be freed
1503 * with g_strfreev().
1505 * Programs that want to be portable to Windows should typically use
1506 * this function and g_getenv() instead of using the environ array
1507 * from the C library directly. On Windows, the strings in the environ
1508 * array are in system codepage encoding, while in most of the typical
1509 * use cases for environment variables in GLib-using programs you want
1510 * the UTF-8 encoding that this function and g_getenv() provide.
1518 gchar **result, *eq;
1521 len = g_strv_length (environ);
1522 result = g_new0 (gchar *, len + 1);
1525 for (i = 0; i < len; i++)
1527 eq = strchr (environ[i], '=');
1529 result[j++] = g_strndup (environ[i], eq - environ[i]);
1536 gchar **result, *eq;
1540 p = (wchar_t *) GetEnvironmentStringsW ();
1546 q += wcslen (q) + 1;
1550 result = g_new0 (gchar *, len + 1);
1556 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
1557 if (result[j] != NULL)
1559 eq = strchr (result[j], '=');
1560 if (eq && eq > result[j])
1568 q += wcslen (q) + 1;
1571 FreeEnvironmentStringsW (p);
1580 * Gets the list of environment variables for the current process. The
1581 * list is %NULL terminated and each item in the list is of the form
1584 * This is equivalent to direct access to the 'environ' global variable,
1587 * The return value is freshly allocated and it should be freed with
1588 * g_strfreev() when it is no longer needed.
1590 * Returns: (array zero-terminated=1) (transfer full): the list of environment variables
1595 g_get_environ (void)
1598 return g_strdupv (environ);
1604 strings = GetEnvironmentStringsW ();
1605 for (n = 0; strings[n]; n += wcslen (strings + n) + 1);
1606 result = g_new (char *, n + 1);
1607 for (i = 0; strings[i]; i += wcslen (strings + i) + 1)
1608 result[i] = g_utf16_to_utf8 (strings + i, -1, NULL, NULL, NULL);
1609 FreeEnvironmentStringsW (strings);
1616 G_LOCK_DEFINE_STATIC (g_utils_global);
1618 static gchar *g_tmp_dir = NULL;
1619 static gchar *g_user_name = NULL;
1620 static gchar *g_real_name = NULL;
1621 static gchar *g_home_dir = NULL;
1622 static gchar *g_host_name = NULL;
1625 /* System codepage versions of the above, kept at file level so that they,
1626 * too, are produced only once.
1628 static gchar *g_tmp_dir_cp = NULL;
1629 static gchar *g_user_name_cp = NULL;
1630 static gchar *g_real_name_cp = NULL;
1631 static gchar *g_home_dir_cp = NULL;
1634 static gchar *g_user_data_dir = NULL;
1635 static gchar **g_system_data_dirs = NULL;
1636 static gchar *g_user_cache_dir = NULL;
1637 static gchar *g_user_config_dir = NULL;
1638 static gchar **g_system_config_dirs = NULL;
1640 static gchar **g_user_special_dirs = NULL;
1642 /* fifteen minutes of fame for everybody */
1643 #define G_USER_DIRS_EXPIRE 15 * 60
1648 get_special_folder (int csidl)
1650 wchar_t path[MAX_PATH+1];
1652 LPITEMIDLIST pidl = NULL;
1654 gchar *retval = NULL;
1656 hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
1659 b = SHGetPathFromIDListW (pidl, path);
1661 retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
1662 CoTaskMemFree (pidl);
1668 get_windows_directory_root (void)
1670 wchar_t wwindowsdir[MAX_PATH];
1672 if (GetWindowsDirectoryW (wwindowsdir, G_N_ELEMENTS (wwindowsdir)))
1674 /* Usually X:\Windows, but in terminal server environments
1675 * might be an UNC path, AFAIK.
1677 char *windowsdir = g_utf16_to_utf8 (wwindowsdir, -1, NULL, NULL, NULL);
1680 if (windowsdir == NULL)
1681 return g_strdup ("C:\\");
1683 p = (char *) g_path_skip_root (windowsdir);
1684 if (G_IS_DIR_SEPARATOR (p[-1]) && p[-2] != ':')
1690 return g_strdup ("C:\\");
1695 /* HOLDS: g_utils_global_lock */
1697 g_get_any_init_do (void)
1699 gchar hostname[100];
1701 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
1702 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1703 g_tmp_dir = g_strdup (g_getenv ("TMP"));
1704 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1705 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
1708 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1709 g_tmp_dir = get_windows_directory_root ();
1712 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1715 g_tmp_dir = g_strdup (P_tmpdir);
1716 k = strlen (g_tmp_dir);
1717 if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
1718 g_tmp_dir[k - 1] = '\0';
1722 if (g_tmp_dir == NULL || *g_tmp_dir == '\0')
1724 g_tmp_dir = g_strdup ("/tmp");
1726 #endif /* !G_OS_WIN32 */
1729 /* We check $HOME first for Win32, though it is a last resort for Unix
1730 * where we prefer the results of getpwuid().
1732 g_home_dir = g_strdup (g_getenv ("HOME"));
1734 /* Only believe HOME if it is an absolute path and exists */
1737 if (!(g_path_is_absolute (g_home_dir) &&
1738 g_file_test (g_home_dir, G_FILE_TEST_IS_DIR)))
1740 g_free (g_home_dir);
1745 /* In case HOME is Unix-style (it happens), convert it to
1751 while ((p = strchr (g_home_dir, '/')) != NULL)
1757 /* USERPROFILE is probably the closest equivalent to $HOME? */
1758 if (g_getenv ("USERPROFILE") != NULL)
1759 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
1763 g_home_dir = get_special_folder (CSIDL_PROFILE);
1766 g_home_dir = get_windows_directory_root ();
1767 #endif /* G_OS_WIN32 */
1771 struct passwd *pw = NULL;
1772 gpointer buffer = NULL;
1776 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
1778 # ifdef _SC_GETPW_R_SIZE_MAX
1779 /* This reurns the maximum length */
1780 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
1784 # else /* _SC_GETPW_R_SIZE_MAX */
1786 # endif /* _SC_GETPW_R_SIZE_MAX */
1788 logname = (gchar *) g_getenv ("LOGNAME");
1793 /* we allocate 6 extra bytes to work around a bug in
1794 * Mac OS < 10.3. See #156446
1796 buffer = g_malloc (bufsize + 6);
1799 # ifdef HAVE_POSIX_GETPWUID_R
1801 error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
1802 if (!pw || (pw->pw_uid != getuid ())) {
1803 /* LOGNAME is lying, fall back to looking up the uid */
1804 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1807 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1809 error = error < 0 ? errno : error;
1810 # else /* HAVE_NONPOSIX_GETPWUID_R */
1811 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
1812 # if defined(_AIX) || defined(__hpux)
1813 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1814 pw = error == 0 ? &pwd : NULL;
1817 pw = getpwnam_r (logname, &pwd, buffer, bufsize);
1818 if (!pw || (pw->pw_uid != getuid ())) {
1819 /* LOGNAME is lying, fall back to looking up the uid */
1820 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1823 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1825 error = pw ? 0 : errno;
1827 # endif /* HAVE_NONPOSIX_GETPWUID_R */
1831 /* we bail out prematurely if the user id can't be found
1832 * (should be pretty rare case actually), or if the buffer
1833 * should be sufficiently big and lookups are still not
1836 if (error == 0 || error == ENOENT)
1838 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
1839 (gulong) getuid ());
1842 if (bufsize > 32 * 1024)
1844 g_warning ("getpwuid_r(): failed due to: %s.",
1845 g_strerror (error));
1853 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
1858 pw = getpwuid (getuid ());
1863 g_user_name = g_strdup (pw->pw_name);
1865 if (pw->pw_gecos && *pw->pw_gecos != '\0')
1867 gchar **gecos_fields;
1870 /* split the gecos field and substitute '&' */
1871 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
1872 name_parts = g_strsplit (gecos_fields[0], "&", 0);
1873 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
1874 g_real_name = g_strjoinv (pw->pw_name, name_parts);
1875 g_strfreev (gecos_fields);
1876 g_strfreev (name_parts);
1880 g_home_dir = g_strdup (pw->pw_dir);
1885 #else /* !HAVE_PWD_H */
1889 guint len = UNLEN+1;
1890 wchar_t buffer[UNLEN+1];
1892 if (GetUserNameW (buffer, (LPDWORD) &len))
1894 g_user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
1895 g_real_name = g_strdup (g_user_name);
1898 #endif /* G_OS_WIN32 */
1900 #endif /* !HAVE_PWD_H */
1904 g_home_dir = g_strdup (g_getenv ("HOME"));
1908 /* change '\\' in %HOME% to '/' */
1909 g_strdelimit (g_home_dir, "\\",'/');
1912 g_user_name = g_strdup ("somebody");
1914 g_real_name = g_strdup ("Unknown");
1918 gboolean hostname_fail = (gethostname (hostname, sizeof (hostname)) == -1);
1920 DWORD size = sizeof (hostname);
1921 gboolean hostname_fail = (!GetComputerName (hostname, &size));
1923 g_host_name = g_strdup (hostname_fail ? "localhost" : hostname);
1927 g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL);
1928 g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL);
1929 g_real_name_cp = g_locale_from_utf8 (g_real_name, -1, NULL, NULL, NULL);
1932 g_tmp_dir_cp = g_strdup ("\\");
1933 if (!g_user_name_cp)
1934 g_user_name_cp = g_strdup ("somebody");
1935 if (!g_real_name_cp)
1936 g_real_name_cp = g_strdup ("Unknown");
1938 /* home_dir might be NULL, unlike tmp_dir, user_name and
1942 g_home_dir_cp = g_locale_from_utf8 (g_home_dir, -1, NULL, NULL, NULL);
1944 g_home_dir_cp = NULL;
1945 #endif /* G_OS_WIN32 */
1949 g_get_any_init (void)
1952 g_get_any_init_do ();
1956 g_get_any_init_locked (void)
1958 G_LOCK (g_utils_global);
1960 G_UNLOCK (g_utils_global);
1967 * Gets the user name of the current user. The encoding of the returned
1968 * string is system-defined. On UNIX, it might be the preferred file name
1969 * encoding, or something else, and there is no guarantee that it is even
1970 * consistent on a machine. On Windows, it is always UTF-8.
1972 * Returns: the user name of the current user.
1975 g_get_user_name (void)
1977 g_get_any_init_locked ();
1984 * Gets the real name of the user. This usually comes from the user's entry
1985 * in the <filename>passwd</filename> file. The encoding of the returned
1986 * string is system-defined. (On Windows, it is, however, always UTF-8.)
1987 * If the real user name cannot be determined, the string "Unknown" is
1990 * Returns: the user's real name.
1993 g_get_real_name (void)
1995 g_get_any_init_locked ();
2002 * Gets the current user's home directory as defined in the
2003 * password database.
2005 * Note that in contrast to traditional UNIX tools, this function
2006 * prefers <filename>passwd</filename> entries over the <envar>HOME</envar>
2007 * environment variable.
2009 * One of the reasons for this decision is that applications in many
2010 * cases need special handling to deal with the case where
2011 * <envar>HOME</envar> is
2013 * <member>Not owned by the user</member>
2014 * <member>Not writeable</member>
2015 * <member>Not even readable</member>
2017 * Since applications are in general <emphasis>not</emphasis> written
2018 * to deal with these situations it was considered better to make
2019 * g_get_home_dir() not pay attention to <envar>HOME</envar> and to
2020 * return the real home directory for the user. If applications
2021 * want to pay attention to <envar>HOME</envar>, they can do:
2023 * const char *homedir = g_getenv ("HOME");
2025 * homedir = g_get_home_dir (<!-- -->);
2028 * Returns: the current user's home directory
2031 g_get_home_dir (void)
2033 g_get_any_init_locked ();
2040 * Gets the directory to use for temporary files. This is found from
2041 * inspecting the environment variables <envar>TMPDIR</envar>,
2042 * <envar>TMP</envar>, and <envar>TEMP</envar> in that order. If none
2043 * of those are defined "/tmp" is returned on UNIX and "C:\" on Windows.
2044 * The encoding of the returned string is system-defined. On Windows,
2045 * it is always UTF-8. The return value is never %NULL or the empty string.
2047 * Returns: the directory to use for temporary files.
2050 g_get_tmp_dir (void)
2052 g_get_any_init_locked ();
2059 * Return a name for the machine.
2061 * The returned name is not necessarily a fully-qualified domain name,
2062 * or even present in DNS or some other name service at all. It need
2063 * not even be unique on your local network or site, but usually it
2064 * is. Callers should not rely on the return value having any specific
2065 * properties like uniqueness for security purposes. Even if the name
2066 * of the machine is changed while an application is running, the
2067 * return value from this function does not change. The returned
2068 * string is owned by GLib and should not be modified or freed. If no
2069 * name can be determined, a default fixed string "localhost" is
2072 * Returns: the host name of the machine.
2077 g_get_host_name (void)
2079 g_get_any_init_locked ();
2083 G_LOCK_DEFINE_STATIC (g_prgname);
2084 static gchar *g_prgname = NULL;
2089 * Gets the name of the program. This name should <emphasis>not</emphasis>
2090 * be localized, contrast with g_get_application_name().
2091 * (If you are using GDK or GTK+ the program name is set in gdk_init(),
2092 * which is called by gtk_init(). The program name is found by taking
2093 * the last component of <literal>argv[0]</literal>.)
2095 * Returns: the name of the program. The returned string belongs
2096 * to GLib and must not be modified or freed.
2099 g_get_prgname (void)
2105 if (g_prgname == NULL)
2107 static gboolean beenhere = FALSE;
2111 gchar *utf8_buf = NULL;
2112 wchar_t buf[MAX_PATH+1];
2115 if (GetModuleFileNameW (GetModuleHandle (NULL),
2116 buf, G_N_ELEMENTS (buf)) > 0)
2117 utf8_buf = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
2121 g_prgname = g_path_get_basename (utf8_buf);
2128 G_UNLOCK (g_prgname);
2135 * @prgname: the name of the program.
2137 * Sets the name of the program. This name should <emphasis>not</emphasis>
2138 * be localized, contrast with g_set_application_name(). Note that for
2139 * thread-safety reasons this function can only be called once.
2142 g_set_prgname (const gchar *prgname)
2146 g_prgname = g_strdup (prgname);
2147 G_UNLOCK (g_prgname);
2150 G_LOCK_DEFINE_STATIC (g_application_name);
2151 static gchar *g_application_name = NULL;
2154 * g_get_application_name:
2156 * Gets a human-readable name for the application, as set by
2157 * g_set_application_name(). This name should be localized if
2158 * possible, and is intended for display to the user. Contrast with
2159 * g_get_prgname(), which gets a non-localized name. If
2160 * g_set_application_name() has not been called, returns the result of
2161 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
2164 * Return value: human-readable application name. may return %NULL
2169 g_get_application_name (void)
2173 G_LOCK (g_application_name);
2174 retval = g_application_name;
2175 G_UNLOCK (g_application_name);
2178 return g_get_prgname ();
2184 * g_set_application_name:
2185 * @application_name: localized name of the application
2187 * Sets a human-readable name for the application. This name should be
2188 * localized if possible, and is intended for display to the user.
2189 * Contrast with g_set_prgname(), which sets a non-localized name.
2190 * g_set_prgname() will be called automatically by gtk_init(),
2191 * but g_set_application_name() will not.
2193 * Note that for thread safety reasons, this function can only
2196 * The application name will be used in contexts such as error messages,
2197 * or when displaying an application's name in the task list.
2202 g_set_application_name (const gchar *application_name)
2204 gboolean already_set = FALSE;
2206 G_LOCK (g_application_name);
2207 if (g_application_name)
2210 g_application_name = g_strdup (application_name);
2211 G_UNLOCK (g_application_name);
2214 g_warning ("g_set_application_name() called multiple times");
2218 * g_get_user_data_dir:
2220 * Returns a base directory in which to access application data such
2221 * as icons that is customized for a particular user.
2223 * On UNIX platforms this is determined using the mechanisms described in
2224 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2225 * XDG Base Directory Specification</ulink>.
2226 * In this case the directory retrieved will be XDG_DATA_HOME.
2228 * On Windows this is the folder to use for local (as opposed to
2229 * roaming) application data. See documentation for
2230 * CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as
2231 * what g_get_user_config_dir() returns.
2233 * Return value: a string owned by GLib that must not be modified
2238 g_get_user_data_dir (void)
2242 G_LOCK (g_utils_global);
2244 if (!g_user_data_dir)
2247 data_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
2249 data_dir = (gchar *) g_getenv ("XDG_DATA_HOME");
2251 if (data_dir && data_dir[0])
2252 data_dir = g_strdup (data_dir);
2254 if (!data_dir || !data_dir[0])
2259 data_dir = g_build_filename (g_home_dir, ".local",
2262 data_dir = g_build_filename (g_tmp_dir, g_user_name, ".local",
2266 g_user_data_dir = data_dir;
2269 data_dir = g_user_data_dir;
2271 G_UNLOCK (g_utils_global);
2277 g_init_user_config_dir (void)
2281 if (!g_user_config_dir)
2284 config_dir = get_special_folder (CSIDL_LOCAL_APPDATA);
2286 config_dir = (gchar *) g_getenv ("XDG_CONFIG_HOME");
2288 if (config_dir && config_dir[0])
2289 config_dir = g_strdup (config_dir);
2291 if (!config_dir || !config_dir[0])
2296 config_dir = g_build_filename (g_home_dir, ".config", NULL);
2298 config_dir = g_build_filename (g_tmp_dir, g_user_name, ".config", NULL);
2301 g_user_config_dir = config_dir;
2306 * g_get_user_config_dir:
2308 * Returns a base directory in which to store user-specific application
2309 * configuration information such as user preferences and settings.
2311 * On UNIX platforms this is determined using the mechanisms described in
2312 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2313 * XDG Base Directory Specification</ulink>.
2314 * In this case the directory retrieved will be XDG_CONFIG_HOME.
2316 * On Windows this is the folder to use for local (as opposed to
2317 * roaming) application data. See documentation for
2318 * CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as
2319 * what g_get_user_data_dir() returns.
2321 * Return value: a string owned by GLib that must not be modified
2326 g_get_user_config_dir (void)
2328 G_LOCK (g_utils_global);
2330 g_init_user_config_dir ();
2332 G_UNLOCK (g_utils_global);
2334 return g_user_config_dir;
2338 * g_get_user_cache_dir:
2340 * Returns a base directory in which to store non-essential, cached
2341 * data specific to particular user.
2343 * On UNIX platforms this is determined using the mechanisms described in
2344 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2345 * XDG Base Directory Specification</ulink>.
2346 * In this case the directory retrieved will be XDG_CACHE_HOME.
2348 * On Windows is the directory that serves as a common repository for
2349 * temporary Internet files. A typical path is
2350 * C:\Documents and Settings\username\Local Settings\Temporary Internet Files.
2351 * See documentation for CSIDL_INTERNET_CACHE.
2353 * Return value: a string owned by GLib that must not be modified
2358 g_get_user_cache_dir (void)
2362 G_LOCK (g_utils_global);
2364 if (!g_user_cache_dir)
2367 cache_dir = get_special_folder (CSIDL_INTERNET_CACHE); /* XXX correct? */
2369 cache_dir = (gchar *) g_getenv ("XDG_CACHE_HOME");
2371 if (cache_dir && cache_dir[0])
2372 cache_dir = g_strdup (cache_dir);
2374 if (!cache_dir || !cache_dir[0])
2379 cache_dir = g_build_filename (g_home_dir, ".cache", NULL);
2381 cache_dir = g_build_filename (g_tmp_dir, g_user_name, ".cache", NULL);
2383 g_user_cache_dir = cache_dir;
2386 cache_dir = g_user_cache_dir;
2388 G_UNLOCK (g_utils_global);
2394 * g_get_user_runtime_dir:
2396 * Returns a directory that is unique to the current user on the local
2399 * On UNIX platforms this is determined using the mechanisms described in
2400 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2401 * XDG Base Directory Specification</ulink>. This is the directory
2402 * specified in the <envar>XDG_RUNTIME_DIR</envar> environment variable.
2403 * In the case that this variable is not set, GLib will issue a warning
2404 * message to stderr and return the value of g_get_user_cache_dir().
2406 * On Windows this is the folder to use for local (as opposed to
2407 * roaming) application data. See documentation for
2408 * CSIDL_LOCAL_APPDATA. Note that on Windows it thus is the same as
2409 * what g_get_user_config_dir() returns.
2411 * Returns: a string owned by GLib that must not be modified or freed.
2416 g_get_user_runtime_dir (void)
2419 static const gchar *runtime_dir;
2420 static gsize initialised;
2422 if (g_once_init_enter (&initialised))
2424 runtime_dir = g_strdup (getenv ("XDG_RUNTIME_DIR"));
2426 g_once_init_leave (&initialised, 1);
2432 /* Both fallback for UNIX and the default
2433 * in Windows: use the user cache directory.
2437 return g_get_user_cache_dir ();
2443 find_folder (OSType type)
2445 gchar *filename = NULL;
2448 if (FSFindFolder (kUserDomain, type, kDontCreateFolder, &found) == noErr)
2450 CFURLRef url = CFURLCreateFromFSRef (kCFAllocatorSystemDefault, &found);
2454 CFStringRef path = CFURLCopyFileSystemPath (url, kCFURLPOSIXPathStyle);
2458 filename = g_strdup (CFStringGetCStringPtr (path, kCFStringEncodingUTF8));
2462 filename = g_new0 (gchar, CFStringGetLength (path) * 3 + 1);
2464 CFStringGetCString (path, filename,
2465 CFStringGetLength (path) * 3 + 1,
2466 kCFStringEncodingUTF8);
2480 load_user_special_dirs (void)
2482 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = find_folder (kDesktopFolderType);
2483 g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = find_folder (kDocumentsFolderType);
2484 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = find_folder (kDesktopFolderType); /* XXX correct ? */
2485 g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = find_folder (kMusicDocumentsFolderType);
2486 g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = find_folder (kPictureDocumentsFolderType);
2487 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = NULL;
2488 g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = NULL;
2489 g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = find_folder (kMovieDocumentsFolderType);
2492 #endif /* HAVE_CARBON */
2494 #if defined(G_OS_WIN32)
2496 load_user_special_dirs (void)
2498 typedef HRESULT (WINAPI *t_SHGetKnownFolderPath) (const GUID *rfid,
2502 t_SHGetKnownFolderPath p_SHGetKnownFolderPath;
2504 static const GUID FOLDERID_Downloads =
2505 { 0x374de290, 0x123f, 0x4565, { 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b } };
2506 static const GUID FOLDERID_Public =
2507 { 0xDFDF76A2, 0xC82A, 0x4D63, { 0x90, 0x6A, 0x56, 0x44, 0xAC, 0x45, 0x73, 0x85 } };
2511 p_SHGetKnownFolderPath = (t_SHGetKnownFolderPath) GetProcAddress (GetModuleHandle ("shell32.dll"),
2512 "SHGetKnownFolderPath");
2514 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2515 g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = get_special_folder (CSIDL_PERSONAL);
2517 if (p_SHGetKnownFolderPath == NULL)
2519 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2524 (*p_SHGetKnownFolderPath) (&FOLDERID_Downloads, 0, NULL, &wcp);
2527 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
2528 if (g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] == NULL)
2529 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2530 CoTaskMemFree (wcp);
2533 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2536 g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = get_special_folder (CSIDL_MYMUSIC);
2537 g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = get_special_folder (CSIDL_MYPICTURES);
2539 if (p_SHGetKnownFolderPath == NULL)
2542 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2547 (*p_SHGetKnownFolderPath) (&FOLDERID_Public, 0, NULL, &wcp);
2550 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
2551 if (g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] == NULL)
2552 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2553 CoTaskMemFree (wcp);
2556 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2559 g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = get_special_folder (CSIDL_TEMPLATES);
2560 g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = get_special_folder (CSIDL_MYVIDEO);
2562 #endif /* G_OS_WIN32 */
2564 static void g_init_user_config_dir (void);
2566 #if defined(G_OS_UNIX) && !defined(HAVE_CARBON)
2568 /* adapted from xdg-user-dir-lookup.c
2570 * Copyright (C) 2007 Red Hat Inc.
2572 * Permission is hereby granted, free of charge, to any person
2573 * obtaining a copy of this software and associated documentation files
2574 * (the "Software"), to deal in the Software without restriction,
2575 * including without limitation the rights to use, copy, modify, merge,
2576 * publish, distribute, sublicense, and/or sell copies of the Software,
2577 * and to permit persons to whom the Software is furnished to do so,
2578 * subject to the following conditions:
2580 * The above copyright notice and this permission notice shall be
2581 * included in all copies or substantial portions of the Software.
2583 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2584 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2585 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2586 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2587 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2588 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2589 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2593 load_user_special_dirs (void)
2600 g_init_user_config_dir ();
2601 config_file = g_build_filename (g_user_config_dir,
2605 if (!g_file_get_contents (config_file, &data, NULL, NULL))
2607 g_free (config_file);
2611 lines = g_strsplit (data, "\n", -1);
2612 n_lines = g_strv_length (lines);
2615 for (i = 0; i < n_lines; i++)
2617 gchar *buffer = lines[i];
2620 gboolean is_relative = FALSE;
2621 GUserDirectory directory;
2623 /* Remove newline at end */
2624 len = strlen (buffer);
2625 if (len > 0 && buffer[len - 1] == '\n')
2626 buffer[len - 1] = 0;
2629 while (*p == ' ' || *p == '\t')
2632 if (strncmp (p, "XDG_DESKTOP_DIR", strlen ("XDG_DESKTOP_DIR")) == 0)
2634 directory = G_USER_DIRECTORY_DESKTOP;
2635 p += strlen ("XDG_DESKTOP_DIR");
2637 else if (strncmp (p, "XDG_DOCUMENTS_DIR", strlen ("XDG_DOCUMENTS_DIR")) == 0)
2639 directory = G_USER_DIRECTORY_DOCUMENTS;
2640 p += strlen ("XDG_DOCUMENTS_DIR");
2642 else if (strncmp (p, "XDG_DOWNLOAD_DIR", strlen ("XDG_DOWNLOAD_DIR")) == 0)
2644 directory = G_USER_DIRECTORY_DOWNLOAD;
2645 p += strlen ("XDG_DOWNLOAD_DIR");
2647 else if (strncmp (p, "XDG_MUSIC_DIR", strlen ("XDG_MUSIC_DIR")) == 0)
2649 directory = G_USER_DIRECTORY_MUSIC;
2650 p += strlen ("XDG_MUSIC_DIR");
2652 else if (strncmp (p, "XDG_PICTURES_DIR", strlen ("XDG_PICTURES_DIR")) == 0)
2654 directory = G_USER_DIRECTORY_PICTURES;
2655 p += strlen ("XDG_PICTURES_DIR");
2657 else if (strncmp (p, "XDG_PUBLICSHARE_DIR", strlen ("XDG_PUBLICSHARE_DIR")) == 0)
2659 directory = G_USER_DIRECTORY_PUBLIC_SHARE;
2660 p += strlen ("XDG_PUBLICSHARE_DIR");
2662 else if (strncmp (p, "XDG_TEMPLATES_DIR", strlen ("XDG_TEMPLATES_DIR")) == 0)
2664 directory = G_USER_DIRECTORY_TEMPLATES;
2665 p += strlen ("XDG_TEMPLATES_DIR");
2667 else if (strncmp (p, "XDG_VIDEOS_DIR", strlen ("XDG_VIDEOS_DIR")) == 0)
2669 directory = G_USER_DIRECTORY_VIDEOS;
2670 p += strlen ("XDG_VIDEOS_DIR");
2675 while (*p == ' ' || *p == '\t')
2682 while (*p == ' ' || *p == '\t')
2689 if (strncmp (p, "$HOME", 5) == 0)
2697 d = strrchr (p, '"');
2704 /* remove trailing slashes */
2706 if (d[len - 1] == '/')
2712 g_user_special_dirs[directory] = g_build_filename (g_home_dir, d, NULL);
2715 g_user_special_dirs[directory] = g_strdup (d);
2719 g_free (config_file);
2722 #endif /* G_OS_UNIX && !HAVE_CARBON */
2726 * g_reload_user_special_dirs_cache:
2728 * Resets the cache used for g_get_user_special_dir(), so
2729 * that the latest on-disk version is used. Call this only
2730 * if you just changed the data on disk yourself.
2732 * Due to threadsafety issues this may cause leaking of strings
2733 * that were previously returned from g_get_user_special_dir()
2734 * that can't be freed. We ensure to only leak the data for
2735 * the directories that actually changed value though.
2740 g_reload_user_special_dirs_cache (void)
2744 G_LOCK (g_utils_global);
2746 if (g_user_special_dirs != NULL)
2748 /* save a copy of the pointer, to check if some memory can be preserved */
2749 char **old_g_user_special_dirs = g_user_special_dirs;
2752 /* recreate and reload our cache */
2753 g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
2754 load_user_special_dirs ();
2756 /* only leak changed directories */
2757 for (i = 0; i < G_USER_N_DIRECTORIES; i++)
2759 old_val = old_g_user_special_dirs[i];
2760 if (g_strcmp0 (old_val, g_user_special_dirs[i]) == 0)
2763 g_free (g_user_special_dirs[i]);
2764 g_user_special_dirs[i] = old_val;
2770 /* free the old array */
2771 g_free (old_g_user_special_dirs);
2774 G_UNLOCK (g_utils_global);
2778 * g_get_user_special_dir:
2779 * @directory: the logical id of special directory
2781 * Returns the full path of a special directory using its logical id.
2783 * On Unix this is done using the XDG special user directories.
2784 * For compatibility with existing practise, %G_USER_DIRECTORY_DESKTOP
2785 * falls back to <filename>$HOME/Desktop</filename> when XDG special
2786 * user directories have not been set up.
2788 * Depending on the platform, the user might be able to change the path
2789 * of the special directory without requiring the session to restart; GLib
2790 * will not reflect any change once the special directories are loaded.
2792 * Return value: the path to the specified special directory, or %NULL
2793 * if the logical id was not found. The returned string is owned by
2794 * GLib and should not be modified or freed.
2799 g_get_user_special_dir (GUserDirectory directory)
2801 g_return_val_if_fail (directory >= G_USER_DIRECTORY_DESKTOP &&
2802 directory < G_USER_N_DIRECTORIES, NULL);
2804 G_LOCK (g_utils_global);
2806 if (G_UNLIKELY (g_user_special_dirs == NULL))
2808 g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
2810 load_user_special_dirs ();
2812 /* Special-case desktop for historical compatibility */
2813 if (g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] == NULL)
2817 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] =
2818 g_build_filename (g_home_dir, "Desktop", NULL);
2822 G_UNLOCK (g_utils_global);
2824 return g_user_special_dirs[directory];
2829 #undef g_get_system_data_dirs
2832 get_module_for_address (gconstpointer address)
2834 /* Holds the g_utils_global lock */
2836 static gboolean beenhere = FALSE;
2837 typedef BOOL (WINAPI *t_GetModuleHandleExA) (DWORD, LPCTSTR, HMODULE *);
2838 static t_GetModuleHandleExA p_GetModuleHandleExA = NULL;
2839 HMODULE hmodule = NULL;
2846 p_GetModuleHandleExA =
2847 (t_GetModuleHandleExA) GetProcAddress (GetModuleHandle ("kernel32.dll"),
2848 "GetModuleHandleExA");
2852 if (p_GetModuleHandleExA == NULL ||
2853 !(*p_GetModuleHandleExA) (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
2854 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2857 MEMORY_BASIC_INFORMATION mbi;
2858 VirtualQuery (address, &mbi, sizeof (mbi));
2859 hmodule = (HMODULE) mbi.AllocationBase;
2866 get_module_share_dir (gconstpointer address)
2872 hmodule = get_module_for_address (address);
2873 if (hmodule == NULL)
2876 filename = g_win32_get_package_installation_directory_of_module (hmodule);
2877 retval = g_build_filename (filename, "share", NULL);
2883 const gchar * const *
2884 g_win32_get_system_data_dirs_for_module (void (*address_of_function)())
2888 static GHashTable *per_module_data_dirs = NULL;
2893 if (address_of_function)
2895 G_LOCK (g_utils_global);
2896 hmodule = get_module_for_address (address_of_function);
2897 if (hmodule != NULL)
2899 if (per_module_data_dirs == NULL)
2900 per_module_data_dirs = g_hash_table_new (NULL, NULL);
2903 retval = g_hash_table_lookup (per_module_data_dirs, hmodule);
2907 G_UNLOCK (g_utils_global);
2908 return (const gchar * const *) retval;
2914 data_dirs = g_array_new (TRUE, TRUE, sizeof (char *));
2916 /* Documents and Settings\All Users\Application Data */
2917 p = get_special_folder (CSIDL_COMMON_APPDATA);
2919 g_array_append_val (data_dirs, p);
2921 /* Documents and Settings\All Users\Documents */
2922 p = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2924 g_array_append_val (data_dirs, p);
2926 /* Using the above subfolders of Documents and Settings perhaps
2927 * makes sense from a Windows perspective.
2929 * But looking at the actual use cases of this function in GTK+
2930 * and GNOME software, what we really want is the "share"
2931 * subdirectory of the installation directory for the package
2932 * our caller is a part of.
2934 * The address_of_function parameter, if non-NULL, points to a
2935 * function in the calling module. Use that to determine that
2936 * module's installation folder, and use its "share" subfolder.
2938 * Additionally, also use the "share" subfolder of the installation
2939 * locations of GLib and the .exe file being run.
2941 * To guard against none of the above being what is really wanted,
2942 * callers of this function should have Win32-specific code to look
2943 * up their installation folder themselves, and handle a subfolder
2944 * "share" of it in the same way as the folders returned from this
2948 p = get_module_share_dir (address_of_function);
2950 g_array_append_val (data_dirs, p);
2952 if (glib_dll != NULL)
2954 gchar *glib_root = g_win32_get_package_installation_directory_of_module (glib_dll);
2955 p = g_build_filename (glib_root, "share", NULL);
2957 g_array_append_val (data_dirs, p);
2961 exe_root = g_win32_get_package_installation_directory_of_module (NULL);
2962 p = g_build_filename (exe_root, "share", NULL);
2964 g_array_append_val (data_dirs, p);
2967 retval = (gchar **) g_array_free (data_dirs, FALSE);
2969 if (address_of_function)
2971 if (hmodule != NULL)
2972 g_hash_table_insert (per_module_data_dirs, hmodule, retval);
2973 G_UNLOCK (g_utils_global);
2976 return (const gchar * const *) retval;
2982 * g_get_system_data_dirs:
2984 * Returns an ordered list of base directories in which to access
2985 * system-wide application data.
2987 * On UNIX platforms this is determined using the mechanisms described in
2988 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2989 * XDG Base Directory Specification</ulink>
2990 * In this case the list of directories retrieved will be XDG_DATA_DIRS.
2992 * On Windows the first elements in the list are the Application Data
2993 * and Documents folders for All Users. (These can be determined only
2994 * on Windows 2000 or later and are not present in the list on other
2995 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and
2996 * CSIDL_COMMON_DOCUMENTS.
2998 * Then follows the "share" subfolder in the installation folder for
2999 * the package containing the DLL that calls this function, if it can
3002 * Finally the list contains the "share" subfolder in the installation
3003 * folder for GLib, and in the installation folder for the package the
3004 * application's .exe file belongs to.
3006 * The installation folders above are determined by looking up the
3007 * folder where the module (DLL or EXE) in question is located. If the
3008 * folder's name is "bin", its parent is used, otherwise the folder
3011 * Note that on Windows the returned list can vary depending on where
3012 * this function is called.
3014 * Return value: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib that must
3015 * not be modified or freed.
3018 const gchar * const *
3019 g_get_system_data_dirs (void)
3021 gchar **data_dir_vector;
3023 G_LOCK (g_utils_global);
3025 if (!g_system_data_dirs)
3028 data_dir_vector = (gchar **) g_win32_get_system_data_dirs_for_module (NULL);
3030 gchar *data_dirs = (gchar *) g_getenv ("XDG_DATA_DIRS");
3032 if (!data_dirs || !data_dirs[0])
3033 data_dirs = "/usr/local/share/:/usr/share/";
3035 data_dir_vector = g_strsplit (data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
3038 g_system_data_dirs = data_dir_vector;
3041 data_dir_vector = g_system_data_dirs;
3043 G_UNLOCK (g_utils_global);
3045 return (const gchar * const *) data_dir_vector;
3049 * g_get_system_config_dirs:
3051 * Returns an ordered list of base directories in which to access
3052 * system-wide configuration information.
3054 * On UNIX platforms this is determined using the mechanisms described in
3055 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
3056 * XDG Base Directory Specification</ulink>.
3057 * In this case the list of directories retrieved will be XDG_CONFIG_DIRS.
3059 * On Windows is the directory that contains application data for all users.
3060 * A typical path is C:\Documents and Settings\All Users\Application Data.
3061 * This folder is used for application data that is not user specific.
3062 * For example, an application can store a spell-check dictionary, a database
3063 * of clip art, or a log file in the CSIDL_COMMON_APPDATA folder.
3064 * This information will not roam and is available to anyone using the computer.
3066 * Return value: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib that must
3067 * not be modified or freed.
3070 const gchar * const *
3071 g_get_system_config_dirs (void)
3073 gchar *conf_dirs, **conf_dir_vector;
3075 G_LOCK (g_utils_global);
3077 if (!g_system_config_dirs)
3080 conf_dirs = get_special_folder (CSIDL_COMMON_APPDATA);
3083 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
3088 /* Return empty list */
3089 conf_dir_vector = g_strsplit ("", G_SEARCHPATH_SEPARATOR_S, 0);
3092 conf_dirs = (gchar *) g_getenv ("XDG_CONFIG_DIRS");
3094 if (!conf_dirs || !conf_dirs[0])
3095 conf_dirs = "/etc/xdg";
3097 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
3100 g_system_config_dirs = conf_dir_vector;
3103 conf_dir_vector = g_system_config_dirs;
3104 G_UNLOCK (g_utils_global);
3106 return (const gchar * const *) conf_dir_vector;
3111 static GHashTable *alias_table = NULL;
3113 /* read an alias file for the locales */
3115 read_aliases (gchar *file)
3121 alias_table = g_hash_table_new (g_str_hash, g_str_equal);
3122 fp = fopen (file,"r");
3125 while (fgets (buf, 256, fp))
3131 /* Line is a comment */
3132 if ((buf[0] == '#') || (buf[0] == '\0'))
3135 /* Reads first column */
3136 for (p = buf, q = NULL; *p; p++) {
3137 if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
3140 while ((*q == '\t') || (*q == ' ')) {
3146 /* The line only had one column */
3147 if (!q || *q == '\0')
3150 /* Read second column */
3151 for (p = q; *p; p++) {
3152 if ((*p == '\t') || (*p == ' ')) {
3158 /* Add to alias table if necessary */
3159 if (!g_hash_table_lookup (alias_table, buf)) {
3160 g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
3169 unalias_lang (char *lang)
3176 read_aliases ("/usr/share/locale/locale.alias");
3179 while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
3184 static gboolean said_before = FALSE;
3186 g_warning ("Too many alias levels for a locale, "
3187 "may indicate a loop");
3196 /* Mask for components of locale spec. The ordering here is from
3197 * least significant to most significant
3201 COMPONENT_CODESET = 1 << 0,
3202 COMPONENT_TERRITORY = 1 << 1,
3203 COMPONENT_MODIFIER = 1 << 2
3206 /* Break an X/Open style locale specification into components
3209 explode_locale (const gchar *locale,
3215 const gchar *uscore_pos;
3216 const gchar *at_pos;
3217 const gchar *dot_pos;
3221 uscore_pos = strchr (locale, '_');
3222 dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
3223 at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
3227 mask |= COMPONENT_MODIFIER;
3228 *modifier = g_strdup (at_pos);
3231 at_pos = locale + strlen (locale);
3235 mask |= COMPONENT_CODESET;
3236 *codeset = g_strndup (dot_pos, at_pos - dot_pos);
3243 mask |= COMPONENT_TERRITORY;
3244 *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
3247 uscore_pos = dot_pos;
3249 *language = g_strndup (locale, uscore_pos - locale);
3255 * Compute all interesting variants for a given locale name -
3256 * by stripping off different components of the value.
3258 * For simplicity, we assume that the locale is in
3259 * X/Open format: language[_territory][.codeset][@modifier]
3261 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
3262 * as well. We could just copy the code from glibc wholesale
3263 * but it is big, ugly, and complicated, so I'm reluctant
3264 * to do so when this should handle 99% of the time...
3267 append_locale_variants (GPtrArray *array,
3268 const gchar *locale)
3270 gchar *language = NULL;
3271 gchar *territory = NULL;
3272 gchar *codeset = NULL;
3273 gchar *modifier = NULL;
3278 g_return_if_fail (locale != NULL);
3280 mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
3282 /* Iterate through all possible combinations, from least attractive
3283 * to most attractive.
3285 for (j = 0; j <= mask; ++j)
3289 if ((i & ~mask) == 0)
3291 gchar *val = g_strconcat (language,
3292 (i & COMPONENT_TERRITORY) ? territory : "",
3293 (i & COMPONENT_CODESET) ? codeset : "",
3294 (i & COMPONENT_MODIFIER) ? modifier : "",
3296 g_ptr_array_add (array, val);
3301 if (mask & COMPONENT_CODESET)
3303 if (mask & COMPONENT_TERRITORY)
3305 if (mask & COMPONENT_MODIFIER)
3310 * g_get_locale_variants:
3311 * @locale: a locale identifier
3313 * Returns a list of derived variants of @locale, which can be used to
3314 * e.g. construct locale-dependent filenames or search paths. The returned
3315 * list is sorted from most desirable to least desirable.
3316 * This function handles territory, charset and extra locale modifiers.
3318 * For example, if @locale is "fr_BE", then the returned list
3321 * If you need the list of variants for the <emphasis>current locale</emphasis>,
3322 * use g_get_language_names().
3324 * Returns: (transfer full) (array zero-terminated=1) (element-type utf8): a newly
3325 * allocated array of newly allocated strings with the locale variants. Free with
3331 g_get_locale_variants (const gchar *locale)
3335 g_return_val_if_fail (locale != NULL, NULL);
3337 array = g_ptr_array_sized_new (8);
3338 append_locale_variants (array, locale);
3339 g_ptr_array_add (array, NULL);
3341 return (gchar **) g_ptr_array_free (array, FALSE);
3344 /* The following is (partly) taken from the gettext package.
3345 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */
3347 static const gchar *
3348 guess_category_value (const gchar *category_name)
3350 const gchar *retval;
3352 /* The highest priority value is the `LANGUAGE' environment
3353 variable. This is a GNU extension. */
3354 retval = g_getenv ("LANGUAGE");
3355 if ((retval != NULL) && (retval[0] != '\0'))
3358 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
3359 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
3360 systems this can be done by the `setlocale' function itself. */
3362 /* Setting of LC_ALL overwrites all other. */
3363 retval = g_getenv ("LC_ALL");
3364 if ((retval != NULL) && (retval[0] != '\0'))
3367 /* Next comes the name of the desired category. */
3368 retval = g_getenv (category_name);
3369 if ((retval != NULL) && (retval[0] != '\0'))
3372 /* Last possibility is the LANG environment variable. */
3373 retval = g_getenv ("LANG");
3374 if ((retval != NULL) && (retval[0] != '\0'))
3377 #ifdef G_PLATFORM_WIN32
3378 /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
3379 * LANG, which we already did above. Oh well. The main point of
3380 * calling g_win32_getlocale() is to get the thread's locale as used
3381 * by Windows and the Microsoft C runtime (in the "English_United
3382 * States" format) translated into the Unixish format.
3385 char *locale = g_win32_getlocale ();
3386 retval = g_intern_string (locale);
3395 typedef struct _GLanguageNamesCache GLanguageNamesCache;
3397 struct _GLanguageNamesCache {
3399 gchar **language_names;
3403 language_names_cache_free (gpointer data)
3405 GLanguageNamesCache *cache = data;
3406 g_free (cache->languages);
3407 g_strfreev (cache->language_names);
3412 * g_get_language_names:
3414 * Computes a list of applicable locale names, which can be used to
3415 * e.g. construct locale-dependent filenames or search paths. The returned
3416 * list is sorted from most desirable to least desirable and always contains
3417 * the default locale "C".
3419 * For example, if LANGUAGE=de:en_US, then the returned list is
3420 * "de", "en_US", "en", "C".
3422 * This function consults the environment variables <envar>LANGUAGE</envar>,
3423 * <envar>LC_ALL</envar>, <envar>LC_MESSAGES</envar> and <envar>LANG</envar>
3424 * to find the list of locales specified by the user.
3426 * Return value: (array zero-terminated=1) (transfer none): a %NULL-terminated array of strings owned by GLib
3427 * that must not be modified or freed.
3431 const gchar * const *
3432 g_get_language_names (void)
3434 static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
3435 GLanguageNamesCache *cache = g_static_private_get (&cache_private);
3440 cache = g_new0 (GLanguageNamesCache, 1);
3441 g_static_private_set (&cache_private, cache, language_names_cache_free);
3444 value = guess_category_value ("LC_MESSAGES");
3448 if (!(cache->languages && strcmp (cache->languages, value) == 0))
3453 g_free (cache->languages);
3454 g_strfreev (cache->language_names);
3455 cache->languages = g_strdup (value);
3457 array = g_ptr_array_sized_new (8);
3459 alist = g_strsplit (value, ":", 0);
3460 for (a = alist; *a; a++)
3461 append_locale_variants (array, unalias_lang (*a));
3463 g_ptr_array_add (array, g_strdup ("C"));
3464 g_ptr_array_add (array, NULL);
3466 cache->language_names = (gchar **) g_ptr_array_free (array, FALSE);
3469 return (const gchar * const *) cache->language_names;
3474 * @v: a #gpointer key
3476 * Converts a gpointer to a hash value.
3477 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3478 * when using pointers as keys in a #GHashTable.
3480 * Returns: a hash value corresponding to the key.
3483 g_direct_hash (gconstpointer v)
3485 return GPOINTER_TO_UINT (v);
3491 * @v2: a key to compare with @v1.
3493 * Compares two #gpointer arguments and returns %TRUE if they are equal.
3494 * It can be passed to g_hash_table_new() as the @key_equal_func
3495 * parameter, when using pointers as keys in a #GHashTable.
3497 * Returns: %TRUE if the two keys match.
3500 g_direct_equal (gconstpointer v1,
3508 * @v1: a pointer to a #gint key.
3509 * @v2: a pointer to a #gint key to compare with @v1.
3511 * Compares the two #gint values being pointed to and returns
3512 * %TRUE if they are equal.
3513 * It can be passed to g_hash_table_new() as the @key_equal_func
3514 * parameter, when using pointers to integers as keys in a #GHashTable.
3516 * Returns: %TRUE if the two keys match.
3519 g_int_equal (gconstpointer v1,
3522 return *((const gint*) v1) == *((const gint*) v2);
3527 * @v: a pointer to a #gint key
3529 * Converts a pointer to a #gint to a hash value.
3530 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3531 * when using pointers to integers values as keys in a #GHashTable.
3533 * Returns: a hash value corresponding to the key.
3536 g_int_hash (gconstpointer v)
3538 return *(const gint*) v;
3543 * @v1: a pointer to a #gint64 key.
3544 * @v2: a pointer to a #gint64 key to compare with @v1.
3546 * Compares the two #gint64 values being pointed to and returns
3547 * %TRUE if they are equal.
3548 * It can be passed to g_hash_table_new() as the @key_equal_func
3549 * parameter, when using pointers to 64-bit integers as keys in a #GHashTable.
3551 * Returns: %TRUE if the two keys match.
3556 g_int64_equal (gconstpointer v1,
3559 return *((const gint64*) v1) == *((const gint64*) v2);
3564 * @v: a pointer to a #gint64 key
3566 * Converts a pointer to a #gint64 to a hash value.
3567 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3568 * when using pointers to 64-bit integers values as keys in a #GHashTable.
3570 * Returns: a hash value corresponding to the key.
3575 g_int64_hash (gconstpointer v)
3577 return (guint) *(const gint64*) v;
3582 * @v1: a pointer to a #gdouble key.
3583 * @v2: a pointer to a #gdouble key to compare with @v1.
3585 * Compares the two #gdouble values being pointed to and returns
3586 * %TRUE if they are equal.
3587 * It can be passed to g_hash_table_new() as the @key_equal_func
3588 * parameter, when using pointers to doubles as keys in a #GHashTable.
3590 * Returns: %TRUE if the two keys match.
3595 g_double_equal (gconstpointer v1,
3598 return *((const gdouble*) v1) == *((const gdouble*) v2);
3603 * @v: a pointer to a #gdouble key
3605 * Converts a pointer to a #gdouble to a hash value.
3606 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3607 * when using pointers to doubles as keys in a #GHashTable.
3609 * Returns: a hash value corresponding to the key.
3614 g_double_hash (gconstpointer v)
3616 return (guint) *(const gdouble*) v;
3620 * g_nullify_pointer:
3621 * @nullify_location: the memory address of the pointer.
3623 * Set the pointer at the specified location to %NULL.
3626 g_nullify_pointer (gpointer *nullify_location)
3628 g_return_if_fail (nullify_location != NULL);
3630 *nullify_location = NULL;
3636 * Get the codeset for the current locale.
3638 * Return value: a newly allocated string containing the name
3639 * of the codeset. This string must be freed with g_free().
3642 g_get_codeset (void)
3644 const gchar *charset;
3646 g_get_charset (&charset);
3648 return g_strdup (charset);
3651 /* This is called from g_thread_init(). It's used to
3652 * initialize some static data in a threadsafe way.
3655 _g_utils_thread_init (void)
3657 g_get_language_names ();
3663 * _glib_get_locale_dir:
3665 * Return the path to the share\locale or lib\locale subfolder of the
3666 * GLib installation folder. The path is in the system codepage. We
3667 * have to use system codepage as bindtextdomain() doesn't have a
3671 _glib_get_locale_dir (void)
3673 gchar *install_dir = NULL, *locale_dir;
3674 gchar *retval = NULL;
3676 if (glib_dll != NULL)
3677 install_dir = g_win32_get_package_installation_directory_of_module (glib_dll);
3682 * Append "/share/locale" or "/lib/locale" depending on whether
3683 * autoconfigury detected GNU gettext or not.
3685 const char *p = GLIB_LOCALE_DIR + strlen (GLIB_LOCALE_DIR);
3691 locale_dir = g_build_filename (install_dir, p, NULL);
3693 retval = g_win32_locale_filename_from_utf8 (locale_dir);
3695 g_free (install_dir);
3696 g_free (locale_dir);
3702 return g_strdup ("");
3705 #undef GLIB_LOCALE_DIR
3707 #endif /* G_OS_WIN32 */
3710 ensure_gettext_initialized(void)
3712 static gboolean _glib_gettext_initialized = FALSE;
3714 if (!_glib_gettext_initialized)
3717 gchar *tmp = _glib_get_locale_dir ();
3718 bindtextdomain (GETTEXT_PACKAGE, tmp);
3721 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
3723 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
3724 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
3726 _glib_gettext_initialized = TRUE;
3732 * @str: The string to be translated
3734 * Returns the translated string from the glib translations.
3735 * This is an internal function and should only be used by
3736 * the internals of glib (such as libgio).
3738 * Returns: the transation of @str to the current locale
3741 glib_gettext (const gchar *str)
3743 ensure_gettext_initialized();
3745 return g_dgettext (GETTEXT_PACKAGE, str);
3750 * @msgctxtid: a combined message context and message id, separated
3751 * by a \004 character
3752 * @msgidoffset: the offset of the message id in @msgctxid
3754 * This function is a variant of glib_gettext() which supports
3755 * a disambiguating message context. See g_dpgettext() for full
3758 * This is an internal function and should only be used by
3759 * the internals of glib (such as libgio).
3761 * Returns: the transation of @str to the current locale
3764 glib_pgettext(const gchar *msgctxtid,
3767 ensure_gettext_initialized();
3769 return g_dpgettext (GETTEXT_PACKAGE, msgctxtid, msgidoffset);
3772 #if defined (G_OS_WIN32) && !defined (_WIN64)
3774 /* Binary compatibility versions. Not for newly compiled code. */
3776 #undef g_find_program_in_path
3779 g_find_program_in_path (const gchar *program)
3781 gchar *utf8_program = g_locale_to_utf8 (program, -1, NULL, NULL, NULL);
3782 gchar *utf8_retval = g_find_program_in_path_utf8 (utf8_program);
3785 g_free (utf8_program);
3786 if (utf8_retval == NULL)
3788 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
3789 g_free (utf8_retval);
3794 #undef g_get_current_dir
3797 g_get_current_dir (void)
3799 gchar *utf8_dir = g_get_current_dir_utf8 ();
3800 gchar *dir = g_locale_from_utf8 (utf8_dir, -1, NULL, NULL, NULL);
3808 g_getenv (const gchar *variable)
3810 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3811 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
3815 g_free (utf8_variable);
3818 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
3819 quark = g_quark_from_string (value);
3822 return g_quark_to_string (quark);
3828 g_setenv (const gchar *variable,
3832 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3833 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
3834 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
3836 g_free (utf8_variable);
3837 g_free (utf8_value);
3845 g_unsetenv (const gchar *variable)
3847 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3849 g_unsetenv_utf8 (utf8_variable);
3851 g_free (utf8_variable);
3854 #undef g_get_user_name
3857 g_get_user_name (void)
3859 g_get_any_init_locked ();
3860 return g_user_name_cp;
3863 #undef g_get_real_name
3866 g_get_real_name (void)
3868 g_get_any_init_locked ();
3869 return g_real_name_cp;
3872 #undef g_get_home_dir
3875 g_get_home_dir (void)
3877 g_get_any_init_locked ();
3878 return g_home_dir_cp;
3881 #undef g_get_tmp_dir
3884 g_get_tmp_dir (void)
3886 g_get_any_init_locked ();
3887 return g_tmp_dir_cp;