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.
41 #include <ctype.h> /* For tolower() */
43 #include <sys/types.h>
48 #include <sys/types.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
52 #ifdef HAVE_CRT_EXTERNS_H
53 #include <crt_externs.h> /* for _NSGetEnviron */
56 /* implement gutils's inline functions
58 #define G_IMPLEMENT_INLINES 1
61 #include "gprintfint.h"
62 #include "gthreadprivate.h"
67 #define G_PATH_LENGTH MAXPATHLEN
68 #elif defined (PATH_MAX)
69 #define G_PATH_LENGTH PATH_MAX
70 #elif defined (_PC_PATH_MAX)
71 #define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
73 #define G_PATH_LENGTH 2048
76 #ifdef G_PLATFORM_WIN32
77 # define STRICT /* Strict typing, please */
80 # ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
81 # define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
82 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
84 # include <lmcons.h> /* For UNLEN */
85 #endif /* G_PLATFORM_WIN32 */
90 /* older SDK (e.g. msvc 5.0) does not have these*/
91 # ifndef CSIDL_MYMUSIC
92 # define CSIDL_MYMUSIC 13
94 # ifndef CSIDL_MYVIDEO
95 # define CSIDL_MYVIDEO 14
97 # ifndef CSIDL_INTERNET_CACHE
98 # define CSIDL_INTERNET_CACHE 32
100 # ifndef CSIDL_COMMON_APPDATA
101 # define CSIDL_COMMON_APPDATA 35
103 # ifndef CSIDL_MYPICTURES
104 # define CSIDL_MYPICTURES 0x27
106 # ifndef CSIDL_COMMON_DOCUMENTS
107 # define CSIDL_COMMON_DOCUMENTS 46
109 # ifndef CSIDL_PROFILE
110 # define CSIDL_PROFILE 40
112 # include <process.h>
116 #include <CoreServices/CoreServices.h>
120 #include <langinfo.h>
123 const guint glib_major_version = GLIB_MAJOR_VERSION;
124 const guint glib_minor_version = GLIB_MINOR_VERSION;
125 const guint glib_micro_version = GLIB_MICRO_VERSION;
126 const guint glib_interface_age = GLIB_INTERFACE_AGE;
127 const guint glib_binary_age = GLIB_BINARY_AGE;
129 #ifdef G_PLATFORM_WIN32
131 static HMODULE glib_dll = NULL;
136 DllMain (HINSTANCE hinstDLL,
140 if (fdwReason == DLL_PROCESS_ATTACH)
149 _glib_get_dll_directory (void)
153 wchar_t wc_fn[MAX_PATH];
156 if (glib_dll == NULL)
160 /* This code is different from that in
161 * g_win32_get_package_installation_directory_of_module() in that
162 * here we return the actual folder where the GLib DLL is. We don't
163 * do the check for it being in a "bin" or "lib" subfolder and then
164 * returning the parent of that.
166 * In a statically built GLib, glib_dll will be NULL and we will
167 * thus look up the application's .exe file's location.
169 if (!GetModuleFileNameW (glib_dll, wc_fn, MAX_PATH))
172 retval = g_utf16_to_utf8 (wc_fn, -1, NULL, NULL, NULL);
174 p = strrchr (retval, G_DIR_SEPARATOR);
188 * glib_check_version:
189 * @required_major: the required major version.
190 * @required_minor: the required minor version.
191 * @required_micro: the required micro version.
193 * Checks that the GLib library in use is compatible with the
194 * given version. Generally you would pass in the constants
195 * #GLIB_MAJOR_VERSION, #GLIB_MINOR_VERSION, #GLIB_MICRO_VERSION
196 * as the three arguments to this function; that produces
197 * a check that the library in use is compatible with
198 * the version of GLib the application or module was compiled
201 * Compatibility is defined by two things: first the version
202 * of the running library is newer than the version
203 * @required_major.required_minor.@required_micro. Second
204 * the running library must be binary compatible with the
205 * version @required_major.required_minor.@required_micro
206 * (same major version.)
208 * Return value: %NULL if the GLib library is compatible with the
209 * given version, or a string describing the version mismatch.
210 * The returned string is owned by GLib and must not be modified
216 glib_check_version (guint required_major,
217 guint required_minor,
218 guint required_micro)
220 gint glib_effective_micro = 100 * GLIB_MINOR_VERSION + GLIB_MICRO_VERSION;
221 gint required_effective_micro = 100 * required_minor + required_micro;
223 if (required_major > GLIB_MAJOR_VERSION)
224 return "GLib version too old (major mismatch)";
225 if (required_major < GLIB_MAJOR_VERSION)
226 return "GLib version too new (major mismatch)";
227 if (required_effective_micro < glib_effective_micro - GLIB_BINARY_AGE)
228 return "GLib version too new (micro mismatch)";
229 if (required_effective_micro > glib_effective_micro)
230 return "GLib version too old (micro mismatch)";
234 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
237 * @dest: the destination address to copy the bytes to.
238 * @src: the source address to copy the bytes from.
239 * @len: the number of bytes to copy.
241 * Copies a block of memory @len bytes long, from @src to @dest.
242 * The source and destination areas may overlap.
244 * In order to use this function, you must include
245 * <filename>string.h</filename> yourself, because this macro will
246 * typically simply resolve to memmove() and GLib does not include
247 * <filename>string.h</filename> for you.
250 g_memmove (gpointer dest,
254 gchar* destptr = dest;
255 const gchar* srcptr = src;
256 if (src + len < dest || dest + len < src)
258 bcopy (src, dest, len);
261 else if (dest <= src)
264 *(destptr++) = *(srcptr++);
271 *(--destptr) = *(--srcptr);
274 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
282 * @func: the function to call on normal program termination.
284 * Specifies a function to be called at normal program termination.
286 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
287 * macro that maps to a call to the atexit() function in the C
288 * library. This means that in case the code that calls g_atexit(),
289 * i.e. atexit(), is in a DLL, the function will be called when the
290 * DLL is detached from the program. This typically makes more sense
291 * than that the function is called when the GLib DLL is detached,
292 * which happened earlier when g_atexit() was a function in the GLib
295 * The behaviour of atexit() in the context of dynamically loaded
296 * modules is not formally specified and varies wildly.
298 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
299 * loaded module which is unloaded before the program terminates might
300 * well cause a crash at program exit.
302 * Some POSIX systems implement atexit() like Windows, and have each
303 * dynamically loaded module maintain an own atexit chain that is
304 * called when the module is unloaded.
306 * On other POSIX systems, before a dynamically loaded module is
307 * unloaded, the registered atexit functions (if any) residing in that
308 * module are called, regardless where the code that registered them
309 * resided. This is presumably the most robust approach.
311 * As can be seen from the above, for portability it's best to avoid
312 * calling g_atexit() (or atexit()) except in the main executable of a
316 g_atexit (GVoidFunc func)
319 const gchar *error = NULL;
321 /* keep this in sync with glib.h */
323 #ifdef G_NATIVE_ATEXIT
324 result = ATEXIT (func);
326 error = g_strerror (errno);
327 #elif defined (HAVE_ATEXIT)
328 # ifdef NeXT /* @#%@! NeXTStep */
329 result = !atexit ((void (*)(void)) func);
331 error = g_strerror (errno);
333 result = atexit ((void (*)(void)) func);
335 error = g_strerror (errno);
337 #elif defined (HAVE_ON_EXIT)
338 result = on_exit ((void (*)(int, void *)) func, NULL);
340 error = g_strerror (errno);
343 error = "no implementation";
344 #endif /* G_NATIVE_ATEXIT */
347 g_error ("Could not register atexit() function: %s", error);
350 /* Based on execvp() from GNU Libc.
351 * Some of this code is cut-and-pasted into gspawn.c
355 my_strchrnul (const gchar *str,
358 gchar *p = (gchar*)str;
359 while (*p && (*p != c))
367 static gchar *inner_find_program_in_path (const gchar *program);
370 g_find_program_in_path (const gchar *program)
372 const gchar *last_dot = strrchr (program, '.');
374 if (last_dot == NULL ||
375 strchr (last_dot, '\\') != NULL ||
376 strchr (last_dot, '/') != NULL)
378 const gint program_length = strlen (program);
379 gchar *pathext = g_build_path (";",
380 ".exe;.cmd;.bat;.com",
381 g_getenv ("PATHEXT"),
384 gchar *decorated_program;
390 gchar *q = my_strchrnul (p, ';');
392 decorated_program = g_malloc (program_length + (q-p) + 1);
393 memcpy (decorated_program, program, program_length);
394 memcpy (decorated_program+program_length, p, q-p);
395 decorated_program [program_length + (q-p)] = '\0';
397 retval = inner_find_program_in_path (decorated_program);
398 g_free (decorated_program);
406 } while (*p++ != '\0');
411 return inner_find_program_in_path (program);
417 * g_find_program_in_path:
418 * @program: a program name in the GLib file name encoding
420 * Locates the first executable named @program in the user's path, in the
421 * same way that execvp() would locate it. Returns an allocated string
422 * with the absolute path name, or %NULL if the program is not found in
423 * the path. If @program is already an absolute path, returns a copy of
424 * @program if @program exists and is executable, and %NULL otherwise.
426 * On Windows, if @program does not have a file type suffix, tries
427 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
428 * the <envar>PATHEXT</envar> environment variable.
430 * On Windows, it looks for the file in the same way as CreateProcess()
431 * would. This means first in the directory where the executing
432 * program was loaded from, then in the current directory, then in the
433 * Windows 32-bit system directory, then in the Windows directory, and
434 * finally in the directories in the <envar>PATH</envar> environment
435 * variable. If the program is found, the return value contains the
436 * full name including the type suffix.
438 * Return value: absolute path, or %NULL
442 inner_find_program_in_path (const gchar *program)
445 g_find_program_in_path (const gchar *program)
448 const gchar *path, *p;
449 gchar *name, *freeme;
451 const gchar *path_copy;
452 gchar *filename = NULL, *appdir = NULL;
453 gchar *sysdir = NULL, *windir = NULL;
455 wchar_t wfilename[MAXPATHLEN], wsysdir[MAXPATHLEN],
461 g_return_val_if_fail (program != NULL, NULL);
463 /* If it is an absolute path, or a relative path including subdirectories,
464 * don't look in PATH.
466 if (g_path_is_absolute (program)
467 || strchr (program, G_DIR_SEPARATOR) != NULL
469 || strchr (program, '/') != NULL
473 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE) &&
474 !g_file_test (program, G_FILE_TEST_IS_DIR))
475 return g_strdup (program);
480 path = g_getenv ("PATH");
481 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
484 /* There is no `PATH' in the environment. The default
485 * search path in GNU libc is the current directory followed by
486 * the path `confstr' returns for `_CS_PATH'.
489 /* In GLib we put . last, for security, and don't use the
490 * unportable confstr(); UNIX98 does not actually specify
491 * what to search if PATH is unset. POSIX may, dunno.
494 path = "/bin:/usr/bin:.";
497 n = GetModuleFileNameW (NULL, wfilename, MAXPATHLEN);
498 if (n > 0 && n < MAXPATHLEN)
499 filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
501 n = GetSystemDirectoryW (wsysdir, MAXPATHLEN);
502 if (n > 0 && n < MAXPATHLEN)
503 sysdir = g_utf16_to_utf8 (wsysdir, -1, NULL, NULL, NULL);
505 n = GetWindowsDirectoryW (wwindir, MAXPATHLEN);
506 if (n > 0 && n < MAXPATHLEN)
507 windir = g_utf16_to_utf8 (wwindir, -1, NULL, NULL, NULL);
511 appdir = g_path_get_dirname (filename);
515 path = g_strdup (path);
519 const gchar *tem = path;
520 path = g_strconcat (windir, ";", path, NULL);
521 g_free ((gchar *) tem);
527 const gchar *tem = path;
528 path = g_strconcat (sysdir, ";", path, NULL);
529 g_free ((gchar *) tem);
534 const gchar *tem = path;
535 path = g_strconcat (".;", path, NULL);
536 g_free ((gchar *) tem);
541 const gchar *tem = path;
542 path = g_strconcat (appdir, ";", path, NULL);
543 g_free ((gchar *) tem);
550 len = strlen (program) + 1;
551 pathlen = strlen (path);
552 freeme = name = g_malloc (pathlen + len + 1);
554 /* Copy the file name at the top, including '\0' */
555 memcpy (name + pathlen + 1, program, len);
556 name = name + pathlen;
557 /* And add the slash before the filename */
558 *name = G_DIR_SEPARATOR;
566 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
569 /* Two adjacent colons, or a colon at the beginning or the end
570 * of `PATH' means to search the current directory.
574 startp = memcpy (name - (p - path), path, p - path);
576 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE) &&
577 !g_file_test (startp, G_FILE_TEST_IS_DIR))
580 ret = g_strdup (startp);
583 g_free ((gchar *) path_copy);
588 while (*p++ != '\0');
592 g_free ((gchar *) path_copy);
599 debug_key_matches (const gchar *key,
603 for (; length; length--, key++, token++)
605 char k = (*key == '_') ? '-' : tolower (*key );
606 char t = (*token == '_') ? '-' : tolower (*token);
616 * g_parse_debug_string:
617 * @string: a list of debug options separated by colons, spaces, or
618 * commas; or the string "all" to set all flags, or %NULL.
619 * @keys: pointer to an array of #GDebugKey which associate
620 * strings with bit flags.
621 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
623 * Parses a string containing debugging options
624 * into a %guint containing bit flags. This is used
625 * within GDK and GTK+ to parse the debug options passed on the
626 * command line or through environment variables.
628 * Returns: the combined set of bit flags.
631 g_parse_debug_string (const gchar *string,
632 const GDebugKey *keys,
641 /* this function is used by gmem.c/gslice.c initialization code,
642 * so introducing malloc dependencies here would require adaptions
643 * of those code portions.
646 if (!g_ascii_strcasecmp (string, "all"))
648 for (i=0; i<nkeys; i++)
649 result |= keys[i].value;
653 const gchar *p = string;
658 q = strpbrk (p, ":;, \t");
662 for (i = 0; i < nkeys; i++)
663 if (debug_key_matches (keys[i].key, p, q - p))
664 result |= keys[i].value;
677 * @file_name: the name of the file.
679 * Gets the name of the file without any leading directory components.
680 * It returns a pointer into the given file name string.
682 * Return value: the name of the file without any leading directory components.
684 * Deprecated:2.2: Use g_path_get_basename() instead, but notice that
685 * g_path_get_basename() allocates new memory for the returned string, unlike
686 * this function which returns a pointer into the argument.
688 G_CONST_RETURN gchar*
689 g_basename (const gchar *file_name)
691 register gchar *base;
693 g_return_val_if_fail (file_name != NULL, NULL);
695 base = strrchr (file_name, G_DIR_SEPARATOR);
699 gchar *q = strrchr (file_name, '/');
700 if (base == NULL || (q != NULL && q > base))
709 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
710 return (gchar*) file_name + 2;
711 #endif /* G_OS_WIN32 */
713 return (gchar*) file_name;
717 * g_path_get_basename:
718 * @file_name: the name of the file.
720 * Gets the last component of the filename. If @file_name ends with a
721 * directory separator it gets the component before the last slash. If
722 * @file_name consists only of directory separators (and on Windows,
723 * possibly a drive letter), a single separator is returned. If
724 * @file_name is empty, it gets ".".
726 * Return value: a newly allocated string containing the last component of
730 g_path_get_basename (const gchar *file_name)
732 register gssize base;
733 register gssize last_nonslash;
737 g_return_val_if_fail (file_name != NULL, NULL);
739 if (file_name[0] == '\0')
741 return g_strdup (".");
743 last_nonslash = strlen (file_name) - 1;
745 while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
748 if (last_nonslash == -1)
749 /* string only containing slashes */
750 return g_strdup (G_DIR_SEPARATOR_S);
753 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
754 /* string only containing slashes and a drive */
755 return g_strdup (G_DIR_SEPARATOR_S);
756 #endif /* G_OS_WIN32 */
758 base = last_nonslash;
760 while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
764 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
766 #endif /* G_OS_WIN32 */
768 len = last_nonslash - base;
769 retval = g_malloc (len + 1);
770 memcpy (retval, file_name + base + 1, len);
776 * g_path_is_absolute:
777 * @file_name: a file name.
779 * Returns %TRUE if the given @file_name is an absolute file name,
780 * i.e. it contains a full path from the root directory such as "/usr/local"
781 * on UNIX or "C:\windows" on Windows systems.
783 * Returns: %TRUE if @file_name is an absolute path.
786 g_path_is_absolute (const gchar *file_name)
788 g_return_val_if_fail (file_name != NULL, FALSE);
790 if (G_IS_DIR_SEPARATOR (file_name[0]))
794 /* Recognize drive letter on native Windows */
795 if (g_ascii_isalpha (file_name[0]) &&
796 file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
798 #endif /* G_OS_WIN32 */
805 * @file_name: a file name.
807 * Returns a pointer into @file_name after the root component, i.e. after
808 * the "/" in UNIX or "C:\" under Windows. If @file_name is not an absolute
809 * path it returns %NULL.
811 * Returns: a pointer into @file_name after the root component.
813 G_CONST_RETURN gchar*
814 g_path_skip_root (const gchar *file_name)
816 g_return_val_if_fail (file_name != NULL, NULL);
818 #ifdef G_PLATFORM_WIN32
819 /* Skip \\server\share or //server/share */
820 if (G_IS_DIR_SEPARATOR (file_name[0]) &&
821 G_IS_DIR_SEPARATOR (file_name[1]) &&
823 !G_IS_DIR_SEPARATOR (file_name[2]))
827 p = strchr (file_name + 2, G_DIR_SEPARATOR);
830 gchar *q = strchr (file_name + 2, '/');
831 if (p == NULL || (q != NULL && q < p))
841 while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
844 /* Possibly skip a backslash after the share name */
845 if (G_IS_DIR_SEPARATOR (file_name[0]))
848 return (gchar *)file_name;
853 /* Skip initial slashes */
854 if (G_IS_DIR_SEPARATOR (file_name[0]))
856 while (G_IS_DIR_SEPARATOR (file_name[0]))
858 return (gchar *)file_name;
863 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
864 return (gchar *)file_name + 3;
871 * g_path_get_dirname:
872 * @file_name: the name of the file.
874 * Gets the directory components of a file name. If the file name has no
875 * directory components "." is returned. The returned string should be
876 * freed when no longer needed.
878 * Returns: the directory components of the file.
881 g_path_get_dirname (const gchar *file_name)
883 register gchar *base;
886 g_return_val_if_fail (file_name != NULL, NULL);
888 base = strrchr (file_name, G_DIR_SEPARATOR);
891 gchar *q = strrchr (file_name, '/');
892 if (base == NULL || (q != NULL && q > base))
899 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
901 gchar drive_colon_dot[4];
903 drive_colon_dot[0] = file_name[0];
904 drive_colon_dot[1] = ':';
905 drive_colon_dot[2] = '.';
906 drive_colon_dot[3] = '\0';
908 return g_strdup (drive_colon_dot);
911 return g_strdup (".");
914 while (base > file_name && G_IS_DIR_SEPARATOR (*base))
918 /* base points to the char before the last slash.
920 * In case file_name is the root of a drive (X:\) or a child of the
921 * root of a drive (X:\foo), include the slash.
923 * In case file_name is the root share of an UNC path
924 * (\\server\share), add a slash, returning \\server\share\ .
926 * In case file_name is a direct child of a share in an UNC path
927 * (\\server\share\foo), include the slash after the share name,
928 * returning \\server\share\ .
930 if (base == file_name + 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
932 else if (G_IS_DIR_SEPARATOR (file_name[0]) &&
933 G_IS_DIR_SEPARATOR (file_name[1]) &&
935 !G_IS_DIR_SEPARATOR (file_name[2]) &&
936 base >= file_name + 2)
938 const gchar *p = file_name + 2;
939 while (*p && !G_IS_DIR_SEPARATOR (*p))
943 len = (guint) strlen (file_name) + 1;
944 base = g_new (gchar, len + 1);
945 strcpy (base, file_name);
946 base[len-1] = G_DIR_SEPARATOR;
950 if (G_IS_DIR_SEPARATOR (*p))
953 while (*p && !G_IS_DIR_SEPARATOR (*p))
961 len = (guint) 1 + base - file_name;
963 base = g_new (gchar, len + 1);
964 g_memmove (base, file_name, len);
973 * Gets the current directory.
974 * The returned string should be freed when no longer needed. The encoding
975 * of the returned string is system defined. On Windows, it is always UTF-8.
977 * Returns: the current directory.
980 g_get_current_dir (void)
985 wchar_t dummy[2], *wdir;
988 len = GetCurrentDirectoryW (2, dummy);
989 wdir = g_new (wchar_t, len);
991 if (GetCurrentDirectoryW (len, wdir) == len - 1)
992 dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
997 dir = g_strdup ("\\");
1003 gchar *buffer = NULL;
1005 static gulong max_len = 0;
1008 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
1010 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
1011 * and, if that wasn't bad enough, hangs in doing so.
1013 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
1014 buffer = g_new (gchar, max_len + 1);
1016 dir = getwd (buffer);
1017 #else /* !sun || !HAVE_GETCWD */
1018 while (max_len < G_MAXULONG / 2)
1021 buffer = g_new (gchar, max_len + 1);
1023 dir = getcwd (buffer, max_len);
1025 if (dir || errno != ERANGE)
1030 #endif /* !sun || !HAVE_GETCWD */
1032 if (!dir || !*buffer)
1034 /* hm, should we g_error() out here?
1035 * this can happen if e.g. "./" has mode \0000
1037 buffer[0] = G_DIR_SEPARATOR;
1041 dir = g_strdup (buffer);
1050 * @variable: the environment variable to get, in the GLib file name encoding.
1052 * Returns the value of an environment variable. The name and value
1053 * are in the GLib file name encoding. On UNIX, this means the actual
1054 * bytes which might or might not be in some consistent character set
1055 * and encoding. On Windows, it is in UTF-8. On Windows, in case the
1056 * environment variable's value contains references to other
1057 * environment variables, they are expanded.
1059 * Return value: the value of the environment variable, or %NULL if
1060 * the environment variable is not found. The returned string may be
1061 * overwritten by the next call to g_getenv(), g_setenv() or
1064 G_CONST_RETURN gchar*
1065 g_getenv (const gchar *variable)
1069 g_return_val_if_fail (variable != NULL, NULL);
1071 return getenv (variable);
1073 #else /* G_OS_WIN32 */
1077 wchar_t dummy[2], *wname, *wvalue;
1080 g_return_val_if_fail (variable != NULL, NULL);
1081 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
1083 /* On Windows NT, it is relatively typical that environment
1084 * variables contain references to other environment variables. If
1085 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
1086 * environment variables would be stored in the Registry as
1087 * REG_EXPAND_SZ type values, and would then get automatically
1088 * expanded before a program sees them. But there is broken software
1089 * that stores environment variables as REG_SZ values even if they
1090 * contain references to other environment variables.)
1093 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1095 len = GetEnvironmentVariableW (wname, dummy, 2);
1105 wvalue = g_new (wchar_t, len);
1107 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
1114 if (wcschr (wvalue, L'%') != NULL)
1116 wchar_t *tem = wvalue;
1118 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
1122 wvalue = g_new (wchar_t, len);
1124 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
1134 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
1139 quark = g_quark_from_string (value);
1142 return g_quark_to_string (quark);
1144 #endif /* G_OS_WIN32 */
1147 /* _g_getenv_nomalloc
1148 * this function does a getenv() without doing any kind of allocation
1149 * through glib. it's suitable for chars <= 127 only (both, for the
1150 * variable name and the contents) and for contents < 1024 chars in
1151 * length. also, it aliases "" to a NULL return value.
1154 _g_getenv_nomalloc (const gchar *variable,
1157 const gchar *retval = getenv (variable);
1158 if (retval && retval[0])
1160 gint l = strlen (retval);
1163 strncpy (buffer, retval, l);
1173 * @variable: the environment variable to set, must not contain '='.
1174 * @value: the value for to set the variable to.
1175 * @overwrite: whether to change the variable if it already exists.
1177 * Sets an environment variable. Both the variable's name and value
1178 * should be in the GLib file name encoding. On UNIX, this means that
1179 * they can be any sequence of bytes. On Windows, they should be in
1182 * Note that on some systems, when variables are overwritten, the memory
1183 * used for the previous variables and its value isn't reclaimed.
1185 * Returns: %FALSE if the environment variable couldn't be set.
1190 g_setenv (const gchar *variable,
1201 g_return_val_if_fail (variable != NULL, FALSE);
1202 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1205 result = setenv (variable, value, overwrite);
1207 if (!overwrite && getenv (variable) != NULL)
1210 /* This results in a leak when you overwrite existing
1211 * settings. It would be fairly easy to fix this by keeping
1212 * our own parallel array or hash table.
1214 string = g_strconcat (variable, "=", value, NULL);
1215 result = putenv (string);
1219 #else /* G_OS_WIN32 */
1222 wchar_t *wname, *wvalue, *wassignment;
1225 g_return_val_if_fail (variable != NULL, FALSE);
1226 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1227 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
1228 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
1230 if (!overwrite && g_getenv (variable) != NULL)
1233 /* We want to (if possible) set both the environment variable copy
1234 * kept by the C runtime and the one kept by the system.
1236 * We can't use only the C runtime's putenv or _wputenv() as that
1237 * won't work for arbitrary Unicode strings in a "non-Unicode" app
1238 * (with main() and not wmain()). In a "main()" app the C runtime
1239 * initializes the C runtime's environment table by converting the
1240 * real (wide char) environment variables to system codepage, thus
1241 * breaking those that aren't representable in the system codepage.
1243 * As the C runtime's putenv() will also set the system copy, we do
1244 * the putenv() first, then call SetEnvironmentValueW ourselves.
1247 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1248 wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
1249 tem = g_strconcat (variable, "=", value, NULL);
1250 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1253 _wputenv (wassignment);
1254 g_free (wassignment);
1256 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
1263 #endif /* G_OS_WIN32 */
1266 #ifdef HAVE__NSGETENVIRON
1267 #define environ (*_NSGetEnviron())
1268 #elif !defined(G_OS_WIN32)
1270 /* According to the Single Unix Specification, environ is not in
1271 * any system header, although unistd.h often declares it.
1273 extern char **environ;
1278 * @variable: the environment variable to remove, must not contain '='.
1280 * Removes an environment variable from the environment.
1282 * Note that on some systems, when variables are overwritten, the memory
1283 * used for the previous variables and its value isn't reclaimed.
1284 * Furthermore, this function can't be guaranteed to operate in a
1290 g_unsetenv (const gchar *variable)
1294 #ifdef HAVE_UNSETENV
1295 g_return_if_fail (variable != NULL);
1296 g_return_if_fail (strchr (variable, '=') == NULL);
1298 unsetenv (variable);
1299 #else /* !HAVE_UNSETENV */
1303 g_return_if_fail (variable != NULL);
1304 g_return_if_fail (strchr (variable, '=') == NULL);
1306 len = strlen (variable);
1308 /* Mess directly with the environ array.
1309 * This seems to be the only portable way to do this.
1311 * Note that we remove *all* environment entries for
1312 * the variable name, not just the first.
1317 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
1325 #endif /* !HAVE_UNSETENV */
1327 #else /* G_OS_WIN32 */
1329 wchar_t *wname, *wassignment;
1332 g_return_if_fail (variable != NULL);
1333 g_return_if_fail (strchr (variable, '=') == NULL);
1334 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
1336 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1337 tem = g_strconcat (variable, "=", NULL);
1338 wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1341 _wputenv (wassignment);
1342 g_free (wassignment);
1344 SetEnvironmentVariableW (wname, NULL);
1348 #endif /* G_OS_WIN32 */
1354 * Gets the names of all variables set in the environment.
1356 * Returns: a %NULL-terminated list of strings which must be freed
1357 * with g_strfreev().
1359 * Programs that want to be portable to Windows should typically use
1360 * this function and g_getenv() instead of using the environ array
1361 * from the C library directly. On Windows, the strings in the environ
1362 * array are in system codepage encoding, while in most of the typical
1363 * use cases for environment variables in GLib-using programs you want
1364 * the UTF-8 encoding that this function and g_getenv() provide.
1372 gchar **result, *eq;
1375 len = g_strv_length (environ);
1376 result = g_new0 (gchar *, len + 1);
1379 for (i = 0; i < len; i++)
1381 eq = strchr (environ[i], '=');
1383 result[j++] = g_strndup (environ[i], eq - environ[i]);
1390 gchar **result, *eq;
1394 p = (wchar_t *) GetEnvironmentStringsW ();
1400 q += wcslen (q) + 1;
1404 result = g_new0 (gchar *, len + 1);
1410 result[j] = g_utf16_to_utf8 (q, -1, NULL, NULL, NULL);
1411 if (result[j] != NULL)
1413 eq = strchr (result[j], '=');
1414 if (eq && eq > result[j])
1422 q += wcslen (q) + 1;
1425 FreeEnvironmentStringsW (p);
1431 G_LOCK_DEFINE_STATIC (g_utils_global);
1433 static gchar *g_tmp_dir = NULL;
1434 static gchar *g_user_name = NULL;
1435 static gchar *g_real_name = NULL;
1436 static gchar *g_home_dir = NULL;
1437 static gchar *g_host_name = NULL;
1440 /* System codepage versions of the above, kept at file level so that they,
1441 * too, are produced only once.
1443 static gchar *g_tmp_dir_cp = NULL;
1444 static gchar *g_user_name_cp = NULL;
1445 static gchar *g_real_name_cp = NULL;
1446 static gchar *g_home_dir_cp = NULL;
1449 static gchar *g_user_data_dir = NULL;
1450 static gchar **g_system_data_dirs = NULL;
1451 static gchar *g_user_cache_dir = NULL;
1452 static gchar *g_user_config_dir = NULL;
1453 static gchar **g_system_config_dirs = NULL;
1455 static gchar **g_user_special_dirs = NULL;
1457 /* fifteen minutes of fame for everybody */
1458 #define G_USER_DIRS_EXPIRE 15 * 60
1463 get_special_folder (int csidl)
1465 wchar_t path[MAX_PATH+1];
1467 LPITEMIDLIST pidl = NULL;
1469 gchar *retval = NULL;
1471 hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
1474 b = SHGetPathFromIDListW (pidl, path);
1476 retval = g_utf16_to_utf8 (path, -1, NULL, NULL, NULL);
1477 CoTaskMemFree (pidl);
1483 get_windows_directory_root (void)
1485 wchar_t wwindowsdir[MAX_PATH];
1487 if (GetWindowsDirectoryW (wwindowsdir, G_N_ELEMENTS (wwindowsdir)))
1489 /* Usually X:\Windows, but in terminal server environments
1490 * might be an UNC path, AFAIK.
1492 char *windowsdir = g_utf16_to_utf8 (wwindowsdir, -1, NULL, NULL, NULL);
1495 if (windowsdir == NULL)
1496 return g_strdup ("C:\\");
1498 p = (char *) g_path_skip_root (windowsdir);
1499 if (G_IS_DIR_SEPARATOR (p[-1]) && p[-2] != ':')
1505 return g_strdup ("C:\\");
1510 /* HOLDS: g_utils_global_lock */
1512 g_get_any_init_do (void)
1514 gchar hostname[100];
1516 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
1518 g_tmp_dir = g_strdup (g_getenv ("TMP"));
1520 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
1524 g_tmp_dir = get_windows_directory_root ();
1530 g_tmp_dir = g_strdup (P_tmpdir);
1531 k = strlen (g_tmp_dir);
1532 if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
1533 g_tmp_dir[k - 1] = '\0';
1539 g_tmp_dir = g_strdup ("/tmp");
1541 #endif /* !G_OS_WIN32 */
1544 /* We check $HOME first for Win32, though it is a last resort for Unix
1545 * where we prefer the results of getpwuid().
1547 g_home_dir = g_strdup (g_getenv ("HOME"));
1549 /* Only believe HOME if it is an absolute path and exists */
1552 if (!(g_path_is_absolute (g_home_dir) &&
1553 g_file_test (g_home_dir, G_FILE_TEST_IS_DIR)))
1555 g_free (g_home_dir);
1560 /* In case HOME is Unix-style (it happens), convert it to
1566 while ((p = strchr (g_home_dir, '/')) != NULL)
1572 /* USERPROFILE is probably the closest equivalent to $HOME? */
1573 if (g_getenv ("USERPROFILE") != NULL)
1574 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
1578 g_home_dir = get_special_folder (CSIDL_PROFILE);
1581 g_home_dir = get_windows_directory_root ();
1582 #endif /* G_OS_WIN32 */
1586 struct passwd *pw = NULL;
1587 gpointer buffer = NULL;
1591 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
1593 # ifdef _SC_GETPW_R_SIZE_MAX
1594 /* This reurns the maximum length */
1595 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
1599 # else /* _SC_GETPW_R_SIZE_MAX */
1601 # endif /* _SC_GETPW_R_SIZE_MAX */
1603 logname = (gchar *) g_getenv ("LOGNAME");
1608 /* we allocate 6 extra bytes to work around a bug in
1609 * Mac OS < 10.3. See #156446
1611 buffer = g_malloc (bufsize + 6);
1614 # ifdef HAVE_POSIX_GETPWUID_R
1616 error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
1617 if (!pw || (pw->pw_uid != getuid ())) {
1618 /* LOGNAME is lying, fall back to looking up the uid */
1619 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1622 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1624 error = error < 0 ? errno : error;
1625 # else /* HAVE_NONPOSIX_GETPWUID_R */
1626 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
1627 # if defined(_AIX) || defined(__hpux)
1628 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1629 pw = error == 0 ? &pwd : NULL;
1632 pw = getpwnam_r (logname, &pwd, buffer, bufsize);
1633 if (!pw || (pw->pw_uid != getuid ())) {
1634 /* LOGNAME is lying, fall back to looking up the uid */
1635 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1638 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1640 error = pw ? 0 : errno;
1642 # endif /* HAVE_NONPOSIX_GETPWUID_R */
1646 /* we bail out prematurely if the user id can't be found
1647 * (should be pretty rare case actually), or if the buffer
1648 * should be sufficiently big and lookups are still not
1651 if (error == 0 || error == ENOENT)
1653 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
1654 (gulong) getuid ());
1657 if (bufsize > 32 * 1024)
1659 g_warning ("getpwuid_r(): failed due to: %s.",
1660 g_strerror (error));
1668 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
1673 pw = getpwuid (getuid ());
1678 g_user_name = g_strdup (pw->pw_name);
1680 if (pw->pw_gecos && *pw->pw_gecos != '\0')
1682 gchar **gecos_fields;
1685 /* split the gecos field and substitute '&' */
1686 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
1687 name_parts = g_strsplit (gecos_fields[0], "&", 0);
1688 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
1689 g_real_name = g_strjoinv (pw->pw_name, name_parts);
1690 g_strfreev (gecos_fields);
1691 g_strfreev (name_parts);
1695 g_home_dir = g_strdup (pw->pw_dir);
1700 #else /* !HAVE_PWD_H */
1704 guint len = UNLEN+1;
1705 wchar_t buffer[UNLEN+1];
1707 if (GetUserNameW (buffer, (LPDWORD) &len))
1709 g_user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
1710 g_real_name = g_strdup (g_user_name);
1713 #endif /* G_OS_WIN32 */
1715 #endif /* !HAVE_PWD_H */
1719 g_home_dir = g_strdup (g_getenv ("HOME"));
1723 /* change '\\' in %HOME% to '/' */
1724 g_strdelimit (g_home_dir, "\\",'/');
1727 g_user_name = g_strdup ("somebody");
1729 g_real_name = g_strdup ("Unknown");
1733 gboolean hostname_fail = (gethostname (hostname, sizeof (hostname)) == -1);
1735 DWORD size = sizeof (hostname);
1736 gboolean hostname_fail = (!GetComputerName (hostname, &size));
1738 g_host_name = g_strdup (hostname_fail ? "localhost" : hostname);
1742 g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL);
1743 g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL);
1744 g_real_name_cp = g_locale_from_utf8 (g_real_name, -1, NULL, NULL, NULL);
1747 g_tmp_dir_cp = g_strdup ("\\");
1748 if (!g_user_name_cp)
1749 g_user_name_cp = g_strdup ("somebody");
1750 if (!g_real_name_cp)
1751 g_real_name_cp = g_strdup ("Unknown");
1753 /* home_dir might be NULL, unlike tmp_dir, user_name and
1757 g_home_dir_cp = g_locale_from_utf8 (g_home_dir, -1, NULL, NULL, NULL);
1759 g_home_dir_cp = NULL;
1760 #endif /* G_OS_WIN32 */
1764 g_get_any_init (void)
1767 g_get_any_init_do ();
1771 g_get_any_init_locked (void)
1773 G_LOCK (g_utils_global);
1775 G_UNLOCK (g_utils_global);
1782 * Gets the user name of the current user. The encoding of the returned
1783 * string is system-defined. On UNIX, it might be the preferred file name
1784 * encoding, or something else, and there is no guarantee that it is even
1785 * consistent on a machine. On Windows, it is always UTF-8.
1787 * Returns: the user name of the current user.
1789 G_CONST_RETURN gchar*
1790 g_get_user_name (void)
1792 g_get_any_init_locked ();
1799 * Gets the real name of the user. This usually comes from the user's entry
1800 * in the <filename>passwd</filename> file. The encoding of the returned
1801 * string is system-defined. (On Windows, it is, however, always UTF-8.)
1802 * If the real user name cannot be determined, the string "Unknown" is
1805 * Returns: the user's real name.
1807 G_CONST_RETURN gchar*
1808 g_get_real_name (void)
1810 g_get_any_init_locked ();
1817 * Gets the current user's home directory as defined in the
1818 * password database.
1820 * Note that in contrast to traditional UNIX tools, this function
1821 * prefers <filename>passwd</filename> entries over the <envar>HOME</envar>
1822 * environment variable.
1824 * One of the reasons for this decision is that applications in many
1825 * cases need special handling to deal with the case where
1826 * <envar>HOME</envar> is
1828 * <member>Not owned by the user</member>
1829 * <member>Not writeable</member>
1830 * <member>Not even readable</member>
1832 * Since applications are in general <emphasis>not</emphasis> written
1833 * to deal with these situations it was considered better to make
1834 * g_get_home_dir() not pay attention to <envar>HOME</envar> and to
1835 * return the real home directory for the user. If applications
1836 * want to pay attention to <envar>HOME</envar>, they can do:
1838 * const char *homedir = g_getenv ("HOME");
1840 * homedir = g_get_home_dir (<!-- -->);
1843 * Returns: the current user's home directory
1845 G_CONST_RETURN gchar*
1846 g_get_home_dir (void)
1848 g_get_any_init_locked ();
1855 * Gets the directory to use for temporary files. This is found from
1856 * inspecting the environment variables <envar>TMPDIR</envar>,
1857 * <envar>TMP</envar>, and <envar>TEMP</envar> in that order. If none
1858 * of those are defined "/tmp" is returned on UNIX and "C:\" on Windows.
1859 * The encoding of the returned string is system-defined. On Windows,
1860 * it is always UTF-8. The return value is never %NULL.
1862 * Returns: the directory to use for temporary files.
1864 G_CONST_RETURN gchar*
1865 g_get_tmp_dir (void)
1867 g_get_any_init_locked ();
1874 * Return a name for the machine.
1876 * The returned name is not necessarily a fully-qualified domain name,
1877 * or even present in DNS or some other name service at all. It need
1878 * not even be unique on your local network or site, but usually it
1879 * is. Callers should not rely on the return value having any specific
1880 * properties like uniqueness for security purposes. Even if the name
1881 * of the machine is changed while an application is running, the
1882 * return value from this function does not change. The returned
1883 * string is owned by GLib and should not be modified or freed. If no
1884 * name can be determined, a default fixed string "localhost" is
1887 * Returns: the host name of the machine.
1892 g_get_host_name (void)
1894 g_get_any_init_locked ();
1898 G_LOCK_DEFINE_STATIC (g_prgname);
1899 static gchar *g_prgname = NULL;
1904 * Gets the name of the program. This name should <emphasis>not</emphasis>
1905 * be localized, contrast with g_get_application_name().
1906 * (If you are using GDK or GTK+ the program name is set in gdk_init(),
1907 * which is called by gtk_init(). The program name is found by taking
1908 * the last component of <literal>argv[0]</literal>.)
1910 * Returns: the name of the program. The returned string belongs
1911 * to GLib and must not be modified or freed.
1914 g_get_prgname (void)
1920 if (g_prgname == NULL)
1922 static gboolean beenhere = FALSE;
1926 gchar *utf8_buf = NULL;
1927 wchar_t buf[MAX_PATH+1];
1930 if (GetModuleFileNameW (GetModuleHandle (NULL),
1931 buf, G_N_ELEMENTS (buf)) > 0)
1932 utf8_buf = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
1936 g_prgname = g_path_get_basename (utf8_buf);
1943 G_UNLOCK (g_prgname);
1950 * @prgname: the name of the program.
1952 * Sets the name of the program. This name should <emphasis>not</emphasis>
1953 * be localized, contrast with g_set_application_name(). Note that for
1954 * thread-safety reasons this function can only be called once.
1957 g_set_prgname (const gchar *prgname)
1961 g_prgname = g_strdup (prgname);
1962 G_UNLOCK (g_prgname);
1965 G_LOCK_DEFINE_STATIC (g_application_name);
1966 static gchar *g_application_name = NULL;
1969 * g_get_application_name:
1971 * Gets a human-readable name for the application, as set by
1972 * g_set_application_name(). This name should be localized if
1973 * possible, and is intended for display to the user. Contrast with
1974 * g_get_prgname(), which gets a non-localized name. If
1975 * g_set_application_name() has not been called, returns the result of
1976 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1979 * Return value: human-readable application name. may return %NULL
1983 G_CONST_RETURN gchar*
1984 g_get_application_name (void)
1988 G_LOCK (g_application_name);
1989 retval = g_application_name;
1990 G_UNLOCK (g_application_name);
1993 return g_get_prgname ();
1999 * g_set_application_name:
2000 * @application_name: localized name of the application
2002 * Sets a human-readable name for the application. This name should be
2003 * localized if possible, and is intended for display to the user.
2004 * Contrast with g_set_prgname(), which sets a non-localized name.
2005 * g_set_prgname() will be called automatically by gtk_init(),
2006 * but g_set_application_name() will not.
2008 * Note that for thread safety reasons, this function can only
2011 * The application name will be used in contexts such as error messages,
2012 * or when displaying an application's name in the task list.
2017 g_set_application_name (const gchar *application_name)
2019 gboolean already_set = FALSE;
2021 G_LOCK (g_application_name);
2022 if (g_application_name)
2025 g_application_name = g_strdup (application_name);
2026 G_UNLOCK (g_application_name);
2029 g_warning ("g_set_application() name called multiple times");
2033 * g_get_user_data_dir:
2035 * Returns a base directory in which to access application data such
2036 * as icons that is customized for a particular user.
2038 * On UNIX platforms this is determined using the mechanisms described in
2039 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2040 * XDG Base Directory Specification</ulink>
2042 * Return value: a string owned by GLib that must not be modified
2046 G_CONST_RETURN gchar*
2047 g_get_user_data_dir (void)
2051 G_LOCK (g_utils_global);
2053 if (!g_user_data_dir)
2056 data_dir = get_special_folder (CSIDL_PERSONAL);
2058 data_dir = (gchar *) g_getenv ("XDG_DATA_HOME");
2060 if (data_dir && data_dir[0])
2061 data_dir = g_strdup (data_dir);
2063 if (!data_dir || !data_dir[0])
2068 data_dir = g_build_filename (g_home_dir, ".local",
2071 data_dir = g_build_filename (g_tmp_dir, g_user_name, ".local",
2075 g_user_data_dir = data_dir;
2078 data_dir = g_user_data_dir;
2080 G_UNLOCK (g_utils_global);
2086 g_init_user_config_dir (void)
2090 if (!g_user_config_dir)
2093 config_dir = get_special_folder (CSIDL_APPDATA);
2095 config_dir = (gchar *) g_getenv ("XDG_CONFIG_HOME");
2097 if (config_dir && config_dir[0])
2098 config_dir = g_strdup (config_dir);
2100 if (!config_dir || !config_dir[0])
2105 config_dir = g_build_filename (g_home_dir, ".config", NULL);
2107 config_dir = g_build_filename (g_tmp_dir, g_user_name, ".config", NULL);
2110 g_user_config_dir = config_dir;
2115 * g_get_user_config_dir:
2117 * Returns a base directory in which to store user-specific application
2118 * configuration information such as user preferences and settings.
2120 * On UNIX platforms this is determined using the mechanisms described in
2121 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2122 * XDG Base Directory Specification</ulink>
2124 * Return value: a string owned by GLib that must not be modified
2128 G_CONST_RETURN gchar*
2129 g_get_user_config_dir (void)
2131 G_LOCK (g_utils_global);
2133 g_init_user_config_dir ();
2135 G_UNLOCK (g_utils_global);
2137 return g_user_config_dir;
2141 * g_get_user_cache_dir:
2143 * Returns a base directory in which to store non-essential, cached
2144 * data specific to particular user.
2146 * On UNIX platforms this is determined using the mechanisms described in
2147 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2148 * XDG Base Directory Specification</ulink>
2150 * Return value: a string owned by GLib that must not be modified
2154 G_CONST_RETURN gchar*
2155 g_get_user_cache_dir (void)
2159 G_LOCK (g_utils_global);
2161 if (!g_user_cache_dir)
2164 cache_dir = get_special_folder (CSIDL_INTERNET_CACHE); /* XXX correct? */
2166 cache_dir = (gchar *) g_getenv ("XDG_CACHE_HOME");
2168 if (cache_dir && cache_dir[0])
2169 cache_dir = g_strdup (cache_dir);
2171 if (!cache_dir || !cache_dir[0])
2176 cache_dir = g_build_filename (g_home_dir, ".cache", NULL);
2178 cache_dir = g_build_filename (g_tmp_dir, g_user_name, ".cache", NULL);
2180 g_user_cache_dir = cache_dir;
2183 cache_dir = g_user_cache_dir;
2185 G_UNLOCK (g_utils_global);
2193 find_folder (OSType type)
2195 gchar *filename = NULL;
2198 if (FSFindFolder (kUserDomain, type, kDontCreateFolder, &found) == noErr)
2200 CFURLRef url = CFURLCreateFromFSRef (kCFAllocatorSystemDefault, &found);
2204 CFStringRef path = CFURLCopyFileSystemPath (url, kCFURLPOSIXPathStyle);
2208 filename = g_strdup (CFStringGetCStringPtr (path, kCFStringEncodingUTF8));
2212 filename = g_new0 (gchar, CFStringGetLength (path) * 3 + 1);
2214 CFStringGetCString (path, filename,
2215 CFStringGetLength (path) * 3 + 1,
2216 kCFStringEncodingUTF8);
2230 load_user_special_dirs (void)
2232 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = find_folder (kDesktopFolderType);
2233 g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = find_folder (kDocumentsFolderType);
2234 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = find_folder (kDesktopFolderType); /* XXX correct ? */
2235 g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = find_folder (kMusicDocumentsFolderType);
2236 g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = find_folder (kPictureDocumentsFolderType);
2237 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = NULL;
2238 g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = NULL;
2239 g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = find_folder (kMovieDocumentsFolderType);
2242 #endif /* HAVE_CARBON */
2244 #if defined(G_OS_WIN32)
2246 load_user_special_dirs (void)
2248 typedef HRESULT (WINAPI *t_SHGetKnownFolderPath) (const GUID *rfid,
2252 t_SHGetKnownFolderPath p_SHGetKnownFolderPath;
2253 static const GUID FOLDERID_Downloads =
2254 { 0x374de290, 0x123f, 0x4565, { 0x91, 0x64, 0x39, 0xc4, 0x92, 0x5e, 0x46, 0x7b } };
2255 static const GUID FOLDERID_Public =
2256 { 0xDFDF76A2, 0xC82A, 0x4D63, { 0x90, 0x6A, 0x56, 0x44, 0xAC, 0x45, 0x73, 0x85 } };
2259 p_SHGetKnownFolderPath = (t_SHGetKnownFolderPath) GetProcAddress (LoadLibrary ("shell32.dll"),
2260 "SHGetKnownFolderPath");
2262 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2263 g_user_special_dirs[G_USER_DIRECTORY_DOCUMENTS] = get_special_folder (CSIDL_PERSONAL);
2265 if (p_SHGetKnownFolderPath == NULL)
2267 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2272 (*p_SHGetKnownFolderPath) (&FOLDERID_Downloads, 0, NULL, &wcp);
2273 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
2274 if (g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] == NULL)
2275 g_user_special_dirs[G_USER_DIRECTORY_DOWNLOAD] = get_special_folder (CSIDL_DESKTOPDIRECTORY);
2276 CoTaskMemFree (wcp);
2279 g_user_special_dirs[G_USER_DIRECTORY_MUSIC] = get_special_folder (CSIDL_MYMUSIC);
2280 g_user_special_dirs[G_USER_DIRECTORY_PICTURES] = get_special_folder (CSIDL_MYPICTURES);
2282 if (p_SHGetKnownFolderPath == NULL)
2285 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2290 (*p_SHGetKnownFolderPath) (&FOLDERID_Public, 0, NULL, &wcp);
2291 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = g_utf16_to_utf8 (wcp, -1, NULL, NULL, NULL);
2292 if (g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] == NULL)
2293 g_user_special_dirs[G_USER_DIRECTORY_PUBLIC_SHARE] = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2294 CoTaskMemFree (wcp);
2297 g_user_special_dirs[G_USER_DIRECTORY_TEMPLATES] = get_special_folder (CSIDL_TEMPLATES);
2298 g_user_special_dirs[G_USER_DIRECTORY_VIDEOS] = get_special_folder (CSIDL_MYVIDEO);
2300 #endif /* G_OS_WIN32 */
2302 static void g_init_user_config_dir (void);
2304 #if defined(G_OS_UNIX) && !defined(HAVE_CARBON)
2306 /* adapted from xdg-user-dir-lookup.c
2308 * Copyright (C) 2007 Red Hat Inc.
2310 * Permission is hereby granted, free of charge, to any person
2311 * obtaining a copy of this software and associated documentation files
2312 * (the "Software"), to deal in the Software without restriction,
2313 * including without limitation the rights to use, copy, modify, merge,
2314 * publish, distribute, sublicense, and/or sell copies of the Software,
2315 * and to permit persons to whom the Software is furnished to do so,
2316 * subject to the following conditions:
2318 * The above copyright notice and this permission notice shall be
2319 * included in all copies or substantial portions of the Software.
2321 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2322 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2323 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2324 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2325 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2326 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2327 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2331 load_user_special_dirs (void)
2338 g_init_user_config_dir ();
2339 config_file = g_build_filename (g_user_config_dir,
2343 if (!g_file_get_contents (config_file, &data, NULL, NULL))
2345 g_free (config_file);
2349 lines = g_strsplit (data, "\n", -1);
2350 n_lines = g_strv_length (lines);
2353 for (i = 0; i < n_lines; i++)
2355 gchar *buffer = lines[i];
2358 gboolean is_relative = FALSE;
2359 GUserDirectory directory;
2361 /* Remove newline at end */
2362 len = strlen (buffer);
2363 if (len > 0 && buffer[len - 1] == '\n')
2364 buffer[len - 1] = 0;
2367 while (*p == ' ' || *p == '\t')
2370 if (strncmp (p, "XDG_DESKTOP_DIR", strlen ("XDG_DESKTOP_DIR")) == 0)
2372 directory = G_USER_DIRECTORY_DESKTOP;
2373 p += strlen ("XDG_DESKTOP_DIR");
2375 else if (strncmp (p, "XDG_DOCUMENTS_DIR", strlen ("XDG_DOCUMENTS_DIR")) == 0)
2377 directory = G_USER_DIRECTORY_DOCUMENTS;
2378 p += strlen ("XDG_DOCUMENTS_DIR");
2380 else if (strncmp (p, "XDG_DOWNLOAD_DIR", strlen ("XDG_DOWNLOAD_DIR")) == 0)
2382 directory = G_USER_DIRECTORY_DOWNLOAD;
2383 p += strlen ("XDG_DOWNLOAD_DIR");
2385 else if (strncmp (p, "XDG_MUSIC_DIR", strlen ("XDG_MUSIC_DIR")) == 0)
2387 directory = G_USER_DIRECTORY_MUSIC;
2388 p += strlen ("XDG_MUSIC_DIR");
2390 else if (strncmp (p, "XDG_PICTURES_DIR", strlen ("XDG_PICTURES_DIR")) == 0)
2392 directory = G_USER_DIRECTORY_PICTURES;
2393 p += strlen ("XDG_PICTURES_DIR");
2395 else if (strncmp (p, "XDG_PUBLICSHARE_DIR", strlen ("XDG_PUBLICSHARE_DIR")) == 0)
2397 directory = G_USER_DIRECTORY_PUBLIC_SHARE;
2398 p += strlen ("XDG_PUBLICSHARE_DIR");
2400 else if (strncmp (p, "XDG_TEMPLATES_DIR", strlen ("XDG_TEMPLATES_DIR")) == 0)
2402 directory = G_USER_DIRECTORY_TEMPLATES;
2403 p += strlen ("XDG_TEMPLATES_DIR");
2405 else if (strncmp (p, "XDG_VIDEOS_DIR", strlen ("XDG_VIDEOS_DIR")) == 0)
2407 directory = G_USER_DIRECTORY_VIDEOS;
2408 p += strlen ("XDG_VIDEOS_DIR");
2413 while (*p == ' ' || *p == '\t')
2420 while (*p == ' ' || *p == '\t')
2427 if (strncmp (p, "$HOME", 5) == 0)
2435 d = strrchr (p, '"');
2442 /* remove trailing slashes */
2444 if (d[len - 1] == '/')
2450 g_user_special_dirs[directory] = g_build_filename (g_home_dir, d, NULL);
2453 g_user_special_dirs[directory] = g_strdup (d);
2457 g_free (config_file);
2460 #endif /* G_OS_UNIX && !HAVE_CARBON */
2463 * g_get_user_special_dir:
2464 * @directory: the logical id of special directory
2466 * Returns the full path of a special directory using its logical id.
2468 * On Unix this is done using the XDG special user directories.
2469 * For compatibility with existing practise, %G_USER_DIRECTORY_DESKTOP
2470 * falls back to <filename>$HOME/Desktop</filename> when XDG special
2471 * user directories have not been set up.
2473 * Depending on the platform, the user might be able to change the path
2474 * of the special directory without requiring the session to restart; GLib
2475 * will not reflect any change once the special directories are loaded.
2477 * Return value: the path to the specified special directory, or %NULL
2478 * if the logical id was not found. The returned string is owned by
2479 * GLib and should not be modified or freed.
2483 G_CONST_RETURN gchar *
2484 g_get_user_special_dir (GUserDirectory directory)
2486 g_return_val_if_fail (directory >= G_USER_DIRECTORY_DESKTOP &&
2487 directory < G_USER_N_DIRECTORIES, NULL);
2489 G_LOCK (g_utils_global);
2491 if (G_UNLIKELY (g_user_special_dirs == NULL))
2493 g_user_special_dirs = g_new0 (gchar *, G_USER_N_DIRECTORIES);
2495 load_user_special_dirs ();
2497 /* Special-case desktop for historical compatibility */
2498 if (g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] == NULL)
2502 g_user_special_dirs[G_USER_DIRECTORY_DESKTOP] =
2503 g_build_filename (g_home_dir, "Desktop", NULL);
2507 G_UNLOCK (g_utils_global);
2509 return g_user_special_dirs[directory];
2514 #undef g_get_system_data_dirs
2517 get_module_for_address (gconstpointer address)
2519 /* Holds the g_utils_global lock */
2521 static gboolean beenhere = FALSE;
2522 typedef BOOL (WINAPI *t_GetModuleHandleExA) (DWORD, LPCTSTR, HMODULE *);
2523 static t_GetModuleHandleExA p_GetModuleHandleExA = NULL;
2524 HMODULE hmodule = NULL;
2531 p_GetModuleHandleExA =
2532 (t_GetModuleHandleExA) GetProcAddress (LoadLibrary ("kernel32.dll"),
2533 "GetModuleHandleExA");
2537 if (p_GetModuleHandleExA == NULL ||
2538 !(*p_GetModuleHandleExA) (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
2539 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2542 MEMORY_BASIC_INFORMATION mbi;
2543 VirtualQuery (address, &mbi, sizeof (mbi));
2544 hmodule = (HMODULE) mbi.AllocationBase;
2551 get_module_share_dir (gconstpointer address)
2557 hmodule = get_module_for_address (address);
2558 if (hmodule == NULL)
2561 filename = g_win32_get_package_installation_directory_of_module (hmodule);
2562 retval = g_build_filename (filename, "share", NULL);
2568 G_CONST_RETURN gchar * G_CONST_RETURN *
2569 g_win32_get_system_data_dirs_for_module (gconstpointer address)
2573 static GHashTable *per_module_data_dirs = NULL;
2580 G_LOCK (g_utils_global);
2581 hmodule = get_module_for_address (address);
2582 if (hmodule != NULL)
2584 if (per_module_data_dirs == NULL)
2585 per_module_data_dirs = g_hash_table_new (NULL, NULL);
2588 retval = g_hash_table_lookup (per_module_data_dirs, hmodule);
2592 G_UNLOCK (g_utils_global);
2593 return (G_CONST_RETURN gchar * G_CONST_RETURN *) retval;
2599 data_dirs = g_array_new (TRUE, TRUE, sizeof (char *));
2601 /* Documents and Settings\All Users\Application Data */
2602 p = get_special_folder (CSIDL_COMMON_APPDATA);
2604 g_array_append_val (data_dirs, p);
2606 /* Documents and Settings\All Users\Documents */
2607 p = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2609 g_array_append_val (data_dirs, p);
2611 /* Using the above subfolders of Documents and Settings perhaps
2612 * makes sense from a Windows perspective.
2614 * But looking at the actual use cases of this function in GTK+
2615 * and GNOME software, what we really want is the "share"
2616 * subdirectory of the installation directory for the package
2617 * our caller is a part of.
2619 * The address parameter, if non-NULL, points to a function in the
2620 * calling module. Use that to determine that module's installation
2621 * folder, and use its "share" subfolder.
2623 * Additionally, also use the "share" subfolder of the installation
2624 * locations of GLib and the .exe file being run.
2626 * To guard against none of the above being what is really wanted,
2627 * callers of this function should have Win32-specific code to look
2628 * up their installation folder themselves, and handle a subfolder
2629 * "share" of it in the same way as the folders returned from this
2633 p = get_module_share_dir (address);
2635 g_array_append_val (data_dirs, p);
2637 if (glib_dll != NULL)
2639 gchar *glib_root = g_win32_get_package_installation_directory_of_module (glib_dll);
2640 p = g_build_filename (glib_root, "share", NULL);
2642 g_array_append_val (data_dirs, p);
2646 exe_root = g_win32_get_package_installation_directory_of_module (NULL);
2647 p = g_build_filename (exe_root, "share", NULL);
2649 g_array_append_val (data_dirs, p);
2652 retval = (gchar **) g_array_free (data_dirs, FALSE);
2656 if (hmodule != NULL)
2657 g_hash_table_insert (per_module_data_dirs, hmodule, retval);
2658 G_UNLOCK (g_utils_global);
2661 return (G_CONST_RETURN gchar * G_CONST_RETURN *) retval;
2667 * g_get_system_data_dirs:
2669 * Returns an ordered list of base directories in which to access
2670 * system-wide application data.
2672 * On UNIX platforms this is determined using the mechanisms described in
2673 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2674 * XDG Base Directory Specification</ulink>
2676 * On Windows the first elements in the list are the Application Data
2677 * and Documents folders for All Users. (These can be determined only
2678 * on Windows 2000 or later and are not present in the list on other
2679 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and
2680 * CSIDL_COMMON_DOCUMENTS.
2682 * Then follows the "share" subfolder in the installation folder for
2683 * the package containing the DLL that calls this function, if it can
2686 * Finally the list contains the "share" subfolder in the installation
2687 * folder for GLib, and in the installation folder for the package the
2688 * application's .exe file belongs to.
2690 * The installation folders above are determined by looking up the
2691 * folder where the module (DLL or EXE) in question is located. If the
2692 * folder's name is "bin", its parent is used, otherwise the folder
2695 * Note that on Windows the returned list can vary depending on where
2696 * this function is called.
2698 * Return value: a %NULL-terminated array of strings owned by GLib that must
2699 * not be modified or freed.
2702 G_CONST_RETURN gchar * G_CONST_RETURN *
2703 g_get_system_data_dirs (void)
2705 gchar **data_dir_vector;
2707 G_LOCK (g_utils_global);
2709 if (!g_system_data_dirs)
2712 data_dir_vector = (gchar **) g_win32_get_system_data_dirs_for_module (NULL);
2714 gchar *data_dirs = (gchar *) g_getenv ("XDG_DATA_DIRS");
2716 if (!data_dirs || !data_dirs[0])
2717 data_dirs = "/usr/local/share/:/usr/share/";
2719 data_dir_vector = g_strsplit (data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2722 g_system_data_dirs = data_dir_vector;
2725 data_dir_vector = g_system_data_dirs;
2727 G_UNLOCK (g_utils_global);
2729 return (G_CONST_RETURN gchar * G_CONST_RETURN *) data_dir_vector;
2733 * g_get_system_config_dirs:
2735 * Returns an ordered list of base directories in which to access
2736 * system-wide configuration information.
2738 * On UNIX platforms this is determined using the mechanisms described in
2739 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2740 * XDG Base Directory Specification</ulink>
2742 * Return value: a %NULL-terminated array of strings owned by GLib that must
2743 * not be modified or freed.
2746 G_CONST_RETURN gchar * G_CONST_RETURN *
2747 g_get_system_config_dirs (void)
2749 gchar *conf_dirs, **conf_dir_vector;
2751 G_LOCK (g_utils_global);
2753 if (!g_system_config_dirs)
2756 conf_dirs = get_special_folder (CSIDL_COMMON_APPDATA);
2759 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2764 /* Return empty list */
2765 conf_dir_vector = g_strsplit ("", G_SEARCHPATH_SEPARATOR_S, 0);
2768 conf_dirs = (gchar *) g_getenv ("XDG_CONFIG_DIRS");
2770 if (!conf_dirs || !conf_dirs[0])
2771 conf_dirs = "/etc/xdg";
2773 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2776 g_system_config_dirs = conf_dir_vector;
2779 conf_dir_vector = g_system_config_dirs;
2780 G_UNLOCK (g_utils_global);
2782 return (G_CONST_RETURN gchar * G_CONST_RETURN *) conf_dir_vector;
2787 static GHashTable *alias_table = NULL;
2789 /* read an alias file for the locales */
2791 read_aliases (gchar *file)
2797 alias_table = g_hash_table_new (g_str_hash, g_str_equal);
2798 fp = fopen (file,"r");
2801 while (fgets (buf, 256, fp))
2807 /* Line is a comment */
2808 if ((buf[0] == '#') || (buf[0] == '\0'))
2811 /* Reads first column */
2812 for (p = buf, q = NULL; *p; p++) {
2813 if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
2816 while ((*q == '\t') || (*q == ' ')) {
2822 /* The line only had one column */
2823 if (!q || *q == '\0')
2826 /* Read second column */
2827 for (p = q; *p; p++) {
2828 if ((*p == '\t') || (*p == ' ')) {
2834 /* Add to alias table if necessary */
2835 if (!g_hash_table_lookup (alias_table, buf)) {
2836 g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
2845 unalias_lang (char *lang)
2852 read_aliases ("/usr/share/locale/locale.alias");
2855 while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
2860 static gboolean said_before = FALSE;
2862 g_warning ("Too many alias levels for a locale, "
2863 "may indicate a loop");
2872 /* Mask for components of locale spec. The ordering here is from
2873 * least significant to most significant
2877 COMPONENT_CODESET = 1 << 0,
2878 COMPONENT_TERRITORY = 1 << 1,
2879 COMPONENT_MODIFIER = 1 << 2
2882 /* Break an X/Open style locale specification into components
2885 explode_locale (const gchar *locale,
2891 const gchar *uscore_pos;
2892 const gchar *at_pos;
2893 const gchar *dot_pos;
2897 uscore_pos = strchr (locale, '_');
2898 dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
2899 at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
2903 mask |= COMPONENT_MODIFIER;
2904 *modifier = g_strdup (at_pos);
2907 at_pos = locale + strlen (locale);
2911 mask |= COMPONENT_CODESET;
2912 *codeset = g_strndup (dot_pos, at_pos - dot_pos);
2919 mask |= COMPONENT_TERRITORY;
2920 *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
2923 uscore_pos = dot_pos;
2925 *language = g_strndup (locale, uscore_pos - locale);
2931 * Compute all interesting variants for a given locale name -
2932 * by stripping off different components of the value.
2934 * For simplicity, we assume that the locale is in
2935 * X/Open format: language[_territory][.codeset][@modifier]
2937 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
2938 * as well. We could just copy the code from glibc wholesale
2939 * but it is big, ugly, and complicated, so I'm reluctant
2940 * to do so when this should handle 99% of the time...
2943 _g_compute_locale_variants (const gchar *locale)
2945 GSList *retval = NULL;
2947 gchar *language = NULL;
2948 gchar *territory = NULL;
2949 gchar *codeset = NULL;
2950 gchar *modifier = NULL;
2955 g_return_val_if_fail (locale != NULL, NULL);
2957 mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
2959 /* Iterate through all possible combinations, from least attractive
2960 * to most attractive.
2962 for (i = 0; i <= mask; i++)
2963 if ((i & ~mask) == 0)
2965 gchar *val = g_strconcat (language,
2966 (i & COMPONENT_TERRITORY) ? territory : "",
2967 (i & COMPONENT_CODESET) ? codeset : "",
2968 (i & COMPONENT_MODIFIER) ? modifier : "",
2970 retval = g_slist_prepend (retval, val);
2974 if (mask & COMPONENT_CODESET)
2976 if (mask & COMPONENT_TERRITORY)
2978 if (mask & COMPONENT_MODIFIER)
2984 /* The following is (partly) taken from the gettext package.
2985 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */
2987 static const gchar *
2988 guess_category_value (const gchar *category_name)
2990 const gchar *retval;
2992 /* The highest priority value is the `LANGUAGE' environment
2993 variable. This is a GNU extension. */
2994 retval = g_getenv ("LANGUAGE");
2995 if ((retval != NULL) && (retval[0] != '\0'))
2998 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
2999 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
3000 systems this can be done by the `setlocale' function itself. */
3002 /* Setting of LC_ALL overwrites all other. */
3003 retval = g_getenv ("LC_ALL");
3004 if ((retval != NULL) && (retval[0] != '\0'))
3007 /* Next comes the name of the desired category. */
3008 retval = g_getenv (category_name);
3009 if ((retval != NULL) && (retval[0] != '\0'))
3012 /* Last possibility is the LANG environment variable. */
3013 retval = g_getenv ("LANG");
3014 if ((retval != NULL) && (retval[0] != '\0'))
3017 #ifdef G_PLATFORM_WIN32
3018 /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
3019 * LANG, which we already did above. Oh well. The main point of
3020 * calling g_win32_getlocale() is to get the thread's locale as used
3021 * by Windows and the Microsoft C runtime (in the "English_United
3022 * States" format) translated into the Unixish format.
3024 retval = g_win32_getlocale ();
3025 if ((retval != NULL) && (retval[0] != '\0'))
3032 typedef struct _GLanguageNamesCache GLanguageNamesCache;
3034 struct _GLanguageNamesCache {
3036 gchar **language_names;
3040 language_names_cache_free (gpointer data)
3042 GLanguageNamesCache *cache = data;
3043 g_free (cache->languages);
3044 g_strfreev (cache->language_names);
3049 * g_get_language_names:
3051 * Computes a list of applicable locale names, which can be used to
3052 * e.g. construct locale-dependent filenames or search paths. The returned
3053 * list is sorted from most desirable to least desirable and always contains
3054 * the default locale "C".
3056 * For example, if LANGUAGE=de:en_US, then the returned list is
3057 * "de", "en_US", "en", "C".
3059 * This function consults the environment variables <envar>LANGUAGE</envar>,
3060 * <envar>LC_ALL</envar>, <envar>LC_MESSAGES</envar> and <envar>LANG</envar>
3061 * to find the list of locales specified by the user.
3063 * Return value: a %NULL-terminated array of strings owned by GLib
3064 * that must not be modified or freed.
3068 G_CONST_RETURN gchar * G_CONST_RETURN *
3069 g_get_language_names (void)
3071 static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
3072 GLanguageNamesCache *cache = g_static_private_get (&cache_private);
3077 cache = g_new0 (GLanguageNamesCache, 1);
3078 g_static_private_set (&cache_private, cache, language_names_cache_free);
3081 value = guess_category_value ("LC_MESSAGES");
3085 if (!(cache->languages && strcmp (cache->languages, value) == 0))
3092 g_free (cache->languages);
3093 g_strfreev (cache->language_names);
3094 cache->languages = g_strdup (value);
3096 alist = g_strsplit (value, ":", 0);
3098 for (a = alist; *a; a++)
3100 gchar *b = unalias_lang (*a);
3101 list = g_slist_concat (list, _g_compute_locale_variants (b));
3104 list = g_slist_append (list, g_strdup ("C"));
3106 cache->language_names = languages = g_new (gchar *, g_slist_length (list) + 1);
3107 for (l = list, i = 0; l; l = l->next, i++)
3108 languages[i] = l->data;
3109 languages[i] = NULL;
3111 g_slist_free (list);
3114 return (G_CONST_RETURN gchar * G_CONST_RETURN *) cache->language_names;
3119 * @v: a #gpointer key
3121 * Converts a gpointer to a hash value.
3122 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3123 * when using pointers as keys in a #GHashTable.
3125 * Returns: a hash value corresponding to the key.
3128 g_direct_hash (gconstpointer v)
3130 return GPOINTER_TO_UINT (v);
3136 * @v2: a key to compare with @v1.
3138 * Compares two #gpointer arguments and returns %TRUE if they are equal.
3139 * It can be passed to g_hash_table_new() as the @key_equal_func
3140 * parameter, when using pointers as keys in a #GHashTable.
3142 * Returns: %TRUE if the two keys match.
3145 g_direct_equal (gconstpointer v1,
3153 * @v1: a pointer to a #gint key.
3154 * @v2: a pointer to a #gint key to compare with @v1.
3156 * Compares the two #gint values being pointed to and returns
3157 * %TRUE if they are equal.
3158 * It can be passed to g_hash_table_new() as the @key_equal_func
3159 * parameter, when using pointers to integers as keys in a #GHashTable.
3161 * Returns: %TRUE if the two keys match.
3164 g_int_equal (gconstpointer v1,
3167 return *((const gint*) v1) == *((const gint*) v2);
3172 * @v: a pointer to a #gint key
3174 * Converts a pointer to a #gint to a hash value.
3175 * It can be passed to g_hash_table_new() as the @hash_func parameter,
3176 * when using pointers to integers values as keys in a #GHashTable.
3178 * Returns: a hash value corresponding to the key.
3181 g_int_hash (gconstpointer v)
3183 return *(const gint*) v;
3187 * g_nullify_pointer:
3188 * @nullify_location: the memory address of the pointer.
3190 * Set the pointer at the specified location to %NULL.
3193 g_nullify_pointer (gpointer *nullify_location)
3195 g_return_if_fail (nullify_location != NULL);
3197 *nullify_location = NULL;
3203 * Get the codeset for the current locale.
3205 * Return value: a newly allocated string containing the name
3206 * of the codeset. This string must be freed with g_free().
3209 g_get_codeset (void)
3211 const gchar *charset;
3213 g_get_charset (&charset);
3215 return g_strdup (charset);
3218 /* This is called from g_thread_init(). It's used to
3219 * initialize some static data in a threadsafe way.
3222 _g_utils_thread_init (void)
3224 g_get_language_names ();
3230 * _glib_get_locale_dir:
3232 * Return the path to the share\locale or lib\locale subfolder of the
3233 * GLib installation folder. The path is in the system codepage. We
3234 * have to use system codepage as bindtextdomain() doesn't have a
3238 _glib_get_locale_dir (void)
3240 gchar *install_dir = NULL, *locale_dir;
3241 gchar *retval = NULL;
3243 if (glib_dll != NULL)
3244 install_dir = g_win32_get_package_installation_directory_of_module (glib_dll);
3249 * Append "/share/locale" or "/lib/locale" depending on whether
3250 * autoconfigury detected GNU gettext or not.
3252 const char *p = GLIB_LOCALE_DIR + strlen (GLIB_LOCALE_DIR);
3258 locale_dir = g_build_filename (install_dir, p, NULL);
3260 retval = g_win32_locale_filename_from_utf8 (locale_dir);
3262 g_free (install_dir);
3263 g_free (locale_dir);
3269 return g_strdup ("");
3272 #undef GLIB_LOCALE_DIR
3274 #endif /* G_OS_WIN32 */
3278 * @str: The string to be translated
3280 * Returns the translated string from the glib translations.
3281 * This is an internal function and should only be used by
3282 * the internals of glib (such as libgio).
3284 * Returns: the transation of @str to the current locale
3286 G_CONST_RETURN gchar *
3287 glib_gettext (const gchar *str)
3289 static gboolean _glib_gettext_initialized = FALSE;
3291 if (!_glib_gettext_initialized)
3294 gchar *tmp = _glib_get_locale_dir ();
3295 bindtextdomain (GETTEXT_PACKAGE, tmp);
3298 bindtextdomain (GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
3300 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
3301 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
3303 _glib_gettext_initialized = TRUE;
3306 return g_dgettext (GETTEXT_PACKAGE, str);
3309 #if defined (G_OS_WIN32) && !defined (_WIN64)
3311 /* Binary compatibility versions. Not for newly compiled code. */
3313 #undef g_find_program_in_path
3316 g_find_program_in_path (const gchar *program)
3318 gchar *utf8_program = g_locale_to_utf8 (program, -1, NULL, NULL, NULL);
3319 gchar *utf8_retval = g_find_program_in_path_utf8 (utf8_program);
3322 g_free (utf8_program);
3323 if (utf8_retval == NULL)
3325 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
3326 g_free (utf8_retval);
3331 #undef g_get_current_dir
3334 g_get_current_dir (void)
3336 gchar *utf8_dir = g_get_current_dir_utf8 ();
3337 gchar *dir = g_locale_from_utf8 (utf8_dir, -1, NULL, NULL, NULL);
3344 G_CONST_RETURN gchar*
3345 g_getenv (const gchar *variable)
3347 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3348 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
3352 g_free (utf8_variable);
3355 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
3356 quark = g_quark_from_string (value);
3359 return g_quark_to_string (quark);
3365 g_setenv (const gchar *variable,
3369 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3370 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
3371 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
3373 g_free (utf8_variable);
3374 g_free (utf8_value);
3382 g_unsetenv (const gchar *variable)
3384 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3386 g_unsetenv_utf8 (utf8_variable);
3388 g_free (utf8_variable);
3391 #undef g_get_user_name
3393 G_CONST_RETURN gchar*
3394 g_get_user_name (void)
3396 g_get_any_init_locked ();
3397 return g_user_name_cp;
3400 #undef g_get_real_name
3402 G_CONST_RETURN gchar*
3403 g_get_real_name (void)
3405 g_get_any_init_locked ();
3406 return g_real_name_cp;
3409 #undef g_get_home_dir
3411 G_CONST_RETURN gchar*
3412 g_get_home_dir (void)
3414 g_get_any_init_locked ();
3415 return g_home_dir_cp;
3418 #undef g_get_tmp_dir
3420 G_CONST_RETURN gchar*
3421 g_get_tmp_dir (void)
3423 g_get_any_init_locked ();
3424 return g_tmp_dir_cp;
3429 #define __G_UTILS_C__
3430 #include "galiasdef.c"