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.
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_PARAM_H
47 #include <sys/param.h>
49 #ifdef HAVE_CRT_EXTERNS_H
50 #include <crt_externs.h> /* for _NSGetEnviron */
53 /* implement gutils's inline functions
55 #define G_IMPLEMENT_INLINES 1
58 #include "gprintfint.h"
59 #include "gthreadinit.h"
63 #define G_PATH_LENGTH MAXPATHLEN
64 #elif defined (PATH_MAX)
65 #define G_PATH_LENGTH PATH_MAX
66 #elif defined (_PC_PATH_MAX)
67 #define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
69 #define G_PATH_LENGTH 2048
72 #ifdef G_PLATFORM_WIN32
73 # define STRICT /* Strict typing, please */
76 # ifndef GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
77 # define GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT 2
78 # define GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS 4
80 # include <lmcons.h> /* For UNLEN */
81 #endif /* G_PLATFORM_WIN32 */
86 /* older SDK (e.g. msvc 5.0) does not have these*/
87 # ifndef CSIDL_INTERNET_CACHE
88 # define CSIDL_INTERNET_CACHE 32
90 # ifndef CSIDL_COMMON_APPDATA
91 # define CSIDL_COMMON_APPDATA 35
93 # ifndef CSIDL_COMMON_DOCUMENTS
94 # define CSIDL_COMMON_DOCUMENTS 46
96 # ifndef CSIDL_PROFILE
97 # define CSIDL_PROFILE 40
103 #include <langinfo.h>
106 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
110 const guint glib_major_version = GLIB_MAJOR_VERSION;
111 const guint glib_minor_version = GLIB_MINOR_VERSION;
112 const guint glib_micro_version = GLIB_MICRO_VERSION;
113 const guint glib_interface_age = GLIB_INTERFACE_AGE;
114 const guint glib_binary_age = GLIB_BINARY_AGE;
116 #ifdef G_PLATFORM_WIN32
118 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
123 * glib_check_version:
124 * @required_major: the required major version.
125 * @required_minor: the required minor version.
126 * @required_micro: the required micro version.
128 * Checks that the GLib library in use is compatible with the
129 * given version. Generally you would pass in the constants
130 * #GLIB_MAJOR_VERSION, #GLIB_MINOR_VERSION, #GLIB_MICRO_VERSION
131 * as the three arguments to this function; that produces
132 * a check that the library in use is compatible with
133 * the version of GLib the application or module was compiled
136 * Compatibility is defined by two things: first the version
137 * of the running library is newer than the version
138 * @required_major.required_minor.@required_micro. Second
139 * the running library must be binary compatible with the
140 * version @required_major.required_minor.@required_micro
141 * (same major version.)
143 * Return value: %NULL if the GLib library is compatible with the
144 * given version, or a string describing the version mismatch.
145 * The returned string is owned by GLib and must not be modified
151 glib_check_version (guint required_major,
152 guint required_minor,
153 guint required_micro)
155 gint glib_effective_micro = 100 * GLIB_MINOR_VERSION + GLIB_MICRO_VERSION;
156 gint required_effective_micro = 100 * required_minor + required_micro;
158 if (required_major > GLIB_MAJOR_VERSION)
159 return "GLib version too old (major mismatch)";
160 if (required_major < GLIB_MAJOR_VERSION)
161 return "GLib version too new (major mismatch)";
162 if (required_effective_micro < glib_effective_micro - GLIB_BINARY_AGE)
163 return "GLib version too new (micro mismatch)";
164 if (required_effective_micro > glib_effective_micro)
165 return "GLib version too old (micro mismatch)";
169 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
172 * @dest: the destination address to copy the bytes to.
173 * @src: the source address to copy the bytes from.
174 * @len: the number of bytes to copy.
176 * Copies a block of memory @len bytes long, from @src to @dest.
177 * The source and destination areas may overlap.
179 * In order to use this function, you must include
180 * <filename>string.h</filename> yourself, because this macro will
181 * typically simply resolve to memmove() and GLib does not include
182 * <filename>string.h</filename> for you.
185 g_memmove (gpointer dest,
189 gchar* destptr = dest;
190 const gchar* srcptr = src;
191 if (src + len < dest || dest + len < src)
193 bcopy (src, dest, len);
196 else if (dest <= src)
199 *(destptr++) = *(srcptr++);
206 *(--destptr) = *(--srcptr);
209 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
217 * @func: the function to call on normal program termination.
219 * Specifies a function to be called at normal program termination.
221 * Since GLib 2.8.2, on Windows g_atexit() actually is a preprocessor
222 * macro that maps to a call to the atexit() function in the C
223 * library. This means that in case the code that calls g_atexit(),
224 * i.e. atexit(), is in a DLL, the function will be called when the
225 * DLL is detached from the program. This typically makes more sense
226 * than that the function is called when the GLib DLL is detached,
227 * which happened earlier when g_atexit() was a function in the GLib
230 * The behaviour of atexit() in the context of dynamically loaded
231 * modules is not formally specified and varies wildly.
233 * On POSIX systems, calling g_atexit() (or atexit()) in a dynamically
234 * loaded module which is unloaded before the program terminates might
235 * well cause a crash at program exit.
237 * Some POSIX systems implement atexit() like Windows, and have each
238 * dynamically loaded module maintain an own atexit chain that is
239 * called when the module is unloaded.
241 * On other POSIX systems, before a dynamically loaded module is
242 * unloaded, the registered atexit functions (if any) residing in that
243 * module are called, regardless where the code that registered them
244 * resided. This is presumably the most robust approach.
246 * As can be seen from the above, for portability it's best to avoid
247 * calling g_atexit() (or atexit()) except in the main executable of a
251 g_atexit (GVoidFunc func)
254 const gchar *error = NULL;
256 /* keep this in sync with glib.h */
258 #ifdef G_NATIVE_ATEXIT
259 result = ATEXIT (func);
261 error = g_strerror (errno);
262 #elif defined (HAVE_ATEXIT)
263 # ifdef NeXT /* @#%@! NeXTStep */
264 result = !atexit ((void (*)(void)) func);
266 error = g_strerror (errno);
268 result = atexit ((void (*)(void)) func);
270 error = g_strerror (errno);
272 #elif defined (HAVE_ON_EXIT)
273 result = on_exit ((void (*)(int, void *)) func, NULL);
275 error = g_strerror (errno);
278 error = "no implementation";
279 #endif /* G_NATIVE_ATEXIT */
282 g_error ("Could not register atexit() function: %s", error);
285 /* Based on execvp() from GNU Libc.
286 * Some of this code is cut-and-pasted into gspawn.c
290 my_strchrnul (const gchar *str,
293 gchar *p = (gchar*)str;
294 while (*p && (*p != c))
302 static gchar *inner_find_program_in_path (const gchar *program);
305 g_find_program_in_path (const gchar *program)
307 const gchar *last_dot = strrchr (program, '.');
309 if (last_dot == NULL ||
310 strchr (last_dot, '\\') != NULL ||
311 strchr (last_dot, '/') != NULL)
313 const gint program_length = strlen (program);
314 gchar *pathext = g_build_path (";",
315 ".exe;.cmd;.bat;.com",
316 g_getenv ("PATHEXT"),
319 gchar *decorated_program;
325 gchar *q = my_strchrnul (p, ';');
327 decorated_program = g_malloc (program_length + (q-p) + 1);
328 memcpy (decorated_program, program, program_length);
329 memcpy (decorated_program+program_length, p, q-p);
330 decorated_program [program_length + (q-p)] = '\0';
332 retval = inner_find_program_in_path (decorated_program);
333 g_free (decorated_program);
341 } while (*p++ != '\0');
346 return inner_find_program_in_path (program);
352 * g_find_program_in_path:
353 * @program: a program name in the GLib file name encoding
355 * Locates the first executable named @program in the user's path, in the
356 * same way that execvp() would locate it. Returns an allocated string
357 * with the absolute path name, or %NULL if the program is not found in
358 * the path. If @program is already an absolute path, returns a copy of
359 * @program if @program exists and is executable, and %NULL otherwise.
361 * On Windows, if @program does not have a file type suffix, tries
362 * with the suffixes .exe, .cmd, .bat and .com, and the suffixes in
363 * the <envar>PATHEXT</envar> environment variable.
365 * On Windows, it looks for the file in the same way as CreateProcess()
366 * would. This means first in the directory where the executing
367 * program was loaded from, then in the current directory, then in the
368 * Windows 32-bit system directory, then in the Windows directory, and
369 * finally in the directories in the <envar>PATH</envar> environment
370 * variable. If the program is found, the return value contains the
371 * full name including the type suffix.
373 * Return value: absolute path, or %NULL
377 inner_find_program_in_path (const gchar *program)
380 g_find_program_in_path (const gchar *program)
383 const gchar *path, *p;
384 gchar *name, *freeme;
386 const gchar *path_copy;
387 gchar *filename = NULL, *appdir = NULL;
388 gchar *sysdir = NULL, *windir = NULL;
393 g_return_val_if_fail (program != NULL, NULL);
395 /* If it is an absolute path, or a relative path including subdirectories,
396 * don't look in PATH.
398 if (g_path_is_absolute (program)
399 || strchr (program, G_DIR_SEPARATOR) != NULL
401 || strchr (program, '/') != NULL
405 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE) &&
406 !g_file_test (program, G_FILE_TEST_IS_DIR))
407 return g_strdup (program);
412 path = g_getenv ("PATH");
413 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
416 /* There is no `PATH' in the environment. The default
417 * search path in GNU libc is the current directory followed by
418 * the path `confstr' returns for `_CS_PATH'.
421 /* In GLib we put . last, for security, and don't use the
422 * unportable confstr(); UNIX98 does not actually specify
423 * what to search if PATH is unset. POSIX may, dunno.
426 path = "/bin:/usr/bin:.";
429 if (G_WIN32_HAVE_WIDECHAR_API ())
432 wchar_t wfilename[MAXPATHLEN], wsysdir[MAXPATHLEN],
435 n = GetModuleFileNameW (NULL, wfilename, MAXPATHLEN);
436 if (n > 0 && n < MAXPATHLEN)
437 filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
439 n = GetSystemDirectoryW (wsysdir, MAXPATHLEN);
440 if (n > 0 && n < MAXPATHLEN)
441 sysdir = g_utf16_to_utf8 (wsysdir, -1, NULL, NULL, NULL);
443 n = GetWindowsDirectoryW (wwindir, MAXPATHLEN);
444 if (n > 0 && n < MAXPATHLEN)
445 windir = g_utf16_to_utf8 (wwindir, -1, NULL, NULL, NULL);
450 gchar cpfilename[MAXPATHLEN], cpsysdir[MAXPATHLEN],
451 cpwindir[MAXPATHLEN];
453 n = GetModuleFileNameA (NULL, cpfilename, MAXPATHLEN);
454 if (n > 0 && n < MAXPATHLEN)
455 filename = g_locale_to_utf8 (cpfilename, -1, NULL, NULL, NULL);
457 n = GetSystemDirectoryA (cpsysdir, MAXPATHLEN);
458 if (n > 0 && n < MAXPATHLEN)
459 sysdir = g_locale_to_utf8 (cpsysdir, -1, NULL, NULL, NULL);
461 n = GetWindowsDirectoryA (cpwindir, MAXPATHLEN);
462 if (n > 0 && n < MAXPATHLEN)
463 windir = g_locale_to_utf8 (cpwindir, -1, NULL, NULL, NULL);
468 appdir = g_path_get_dirname (filename);
472 path = g_strdup (path);
476 const gchar *tem = path;
477 path = g_strconcat (windir, ";", path, NULL);
478 g_free ((gchar *) tem);
484 const gchar *tem = path;
485 path = g_strconcat (sysdir, ";", path, NULL);
486 g_free ((gchar *) tem);
491 const gchar *tem = path;
492 path = g_strconcat (".;", path, NULL);
493 g_free ((gchar *) tem);
498 const gchar *tem = path;
499 path = g_strconcat (appdir, ";", path, NULL);
500 g_free ((gchar *) tem);
507 len = strlen (program) + 1;
508 pathlen = strlen (path);
509 freeme = name = g_malloc (pathlen + len + 1);
511 /* Copy the file name at the top, including '\0' */
512 memcpy (name + pathlen + 1, program, len);
513 name = name + pathlen;
514 /* And add the slash before the filename */
515 *name = G_DIR_SEPARATOR;
523 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
526 /* Two adjacent colons, or a colon at the beginning or the end
527 * of `PATH' means to search the current directory.
531 startp = memcpy (name - (p - path), path, p - path);
533 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE) &&
534 !g_file_test (startp, G_FILE_TEST_IS_DIR))
537 ret = g_strdup (startp);
540 g_free ((gchar *) path_copy);
545 while (*p++ != '\0');
549 g_free ((gchar *) path_copy);
556 * g_parse_debug_string:
557 * @string: a list of debug options separated by ':' or "all"
559 * @keys: pointer to an array of #GDebugKey which associate
560 * strings with bit flags.
561 * @nkeys: the number of #GDebugKey<!-- -->s in the array.
563 * Parses a string containing debugging options separated
564 * by ':' into a %guint containing bit flags. This is used
565 * within GDK and GTK+ to parse the debug options passed on the
566 * command line or through environment variables.
568 * Returns: the combined set of bit flags.
571 g_parse_debug_string (const gchar *string,
572 const GDebugKey *keys,
578 g_return_val_if_fail (string != NULL, 0);
580 /* this function is used by gmem.c/gslice.c initialization code,
581 * so introducing malloc dependencies here would require adaptions
582 * of those code portions.
585 if (!g_ascii_strcasecmp (string, "all"))
587 for (i=0; i<nkeys; i++)
588 result |= keys[i].value;
592 const gchar *p = string;
594 gboolean done = FALSE;
605 for (i=0; i<nkeys; i++)
606 if (g_ascii_strncasecmp (keys[i].key, p, q - p) == 0 &&
607 keys[i].key[q - p] == '\0')
608 result |= keys[i].value;
619 * @file_name: the name of the file.
621 * Gets the name of the file without any leading directory components.
622 * It returns a pointer into the given file name string.
624 * Return value: the name of the file without any leading directory components.
626 * Deprecated:2.2: Use g_path_get_basename() instead, but notice that
627 * g_path_get_basename() allocates new memory for the returned string, unlike
628 * this function which returns a pointer into the argument.
630 G_CONST_RETURN gchar*
631 g_basename (const gchar *file_name)
633 register gchar *base;
635 g_return_val_if_fail (file_name != NULL, NULL);
637 base = strrchr (file_name, G_DIR_SEPARATOR);
641 gchar *q = strrchr (file_name, '/');
642 if (base == NULL || (q != NULL && q > base))
651 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
652 return (gchar*) file_name + 2;
653 #endif /* G_OS_WIN32 */
655 return (gchar*) file_name;
659 * g_path_get_basename:
660 * @file_name: the name of the file.
662 * Gets the last component of the filename. If @file_name ends with a
663 * directory separator it gets the component before the last slash. If
664 * @file_name consists only of directory separators (and on Windows,
665 * possibly a drive letter), a single separator is returned. If
666 * @file_name is empty, it gets ".".
668 * Return value: a newly allocated string containing the last component of
672 g_path_get_basename (const gchar *file_name)
674 register gssize base;
675 register gssize last_nonslash;
679 g_return_val_if_fail (file_name != NULL, NULL);
681 if (file_name[0] == '\0')
683 return g_strdup (".");
685 last_nonslash = strlen (file_name) - 1;
687 while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
690 if (last_nonslash == -1)
691 /* string only containing slashes */
692 return g_strdup (G_DIR_SEPARATOR_S);
695 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
696 /* string only containing slashes and a drive */
697 return g_strdup (G_DIR_SEPARATOR_S);
698 #endif /* G_OS_WIN32 */
700 base = last_nonslash;
702 while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
706 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
708 #endif /* G_OS_WIN32 */
710 len = last_nonslash - base;
711 retval = g_malloc (len + 1);
712 memcpy (retval, file_name + base + 1, len);
718 * g_path_is_absolute:
719 * @file_name: a file name.
721 * Returns %TRUE if the given @file_name is an absolute file name,
722 * i.e. it contains a full path from the root directory such as "/usr/local"
723 * on UNIX or "C:\windows" on Windows systems.
725 * Returns: %TRUE if @file_name is an absolute path.
728 g_path_is_absolute (const gchar *file_name)
730 g_return_val_if_fail (file_name != NULL, FALSE);
732 if (G_IS_DIR_SEPARATOR (file_name[0]))
736 /* Recognize drive letter on native Windows */
737 if (g_ascii_isalpha (file_name[0]) &&
738 file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
740 #endif /* G_OS_WIN32 */
747 * @file_name: a file name.
749 * Returns a pointer into @file_name after the root component, i.e. after
750 * the "/" in UNIX or "C:\" under Windows. If @file_name is not an absolute
751 * path it returns %NULL.
753 * Returns: a pointer into @file_name after the root component.
755 G_CONST_RETURN gchar*
756 g_path_skip_root (const gchar *file_name)
758 g_return_val_if_fail (file_name != NULL, NULL);
760 #ifdef G_PLATFORM_WIN32
761 /* Skip \\server\share or //server/share */
762 if (G_IS_DIR_SEPARATOR (file_name[0]) &&
763 G_IS_DIR_SEPARATOR (file_name[1]) &&
765 !G_IS_DIR_SEPARATOR (file_name[2]))
769 p = strchr (file_name + 2, G_DIR_SEPARATOR);
772 gchar *q = strchr (file_name + 2, '/');
773 if (p == NULL || (q != NULL && q < p))
783 while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
786 /* Possibly skip a backslash after the share name */
787 if (G_IS_DIR_SEPARATOR (file_name[0]))
790 return (gchar *)file_name;
795 /* Skip initial slashes */
796 if (G_IS_DIR_SEPARATOR (file_name[0]))
798 while (G_IS_DIR_SEPARATOR (file_name[0]))
800 return (gchar *)file_name;
805 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
806 return (gchar *)file_name + 3;
813 * g_path_get_dirname:
814 * @file_name: the name of the file.
816 * Gets the directory components of a file name. If the file name has no
817 * directory components "." is returned. The returned string should be
818 * freed when no longer needed.
820 * Returns: the directory components of the file.
823 g_path_get_dirname (const gchar *file_name)
825 register gchar *base;
828 g_return_val_if_fail (file_name != NULL, NULL);
830 base = strrchr (file_name, G_DIR_SEPARATOR);
833 gchar *q = strrchr (file_name, '/');
834 if (base == NULL || (q != NULL && q > base))
841 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
843 gchar drive_colon_dot[4];
845 drive_colon_dot[0] = file_name[0];
846 drive_colon_dot[1] = ':';
847 drive_colon_dot[2] = '.';
848 drive_colon_dot[3] = '\0';
850 return g_strdup (drive_colon_dot);
853 return g_strdup (".");
856 while (base > file_name && G_IS_DIR_SEPARATOR (*base))
860 /* base points to the char before the last slash.
862 * In case file_name is the root of a drive (X:\) or a child of the
863 * root of a drive (X:\foo), include the slash.
865 * In case file_name is the root share of an UNC path
866 * (\\server\share), add a slash, returning \\server\share\ .
868 * In case file_name is a direct child of a share in an UNC path
869 * (\\server\share\foo), include the slash after the share name,
870 * returning \\server\share\ .
872 if (base == file_name + 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
874 else if (G_IS_DIR_SEPARATOR (file_name[0]) &&
875 G_IS_DIR_SEPARATOR (file_name[1]) &&
877 !G_IS_DIR_SEPARATOR (file_name[2]) &&
878 base >= file_name + 2)
880 const gchar *p = file_name + 2;
881 while (*p && !G_IS_DIR_SEPARATOR (*p))
885 len = (guint) strlen (file_name) + 1;
886 base = g_new (gchar, len + 1);
887 strcpy (base, file_name);
888 base[len-1] = G_DIR_SEPARATOR;
892 if (G_IS_DIR_SEPARATOR (*p))
895 while (*p && !G_IS_DIR_SEPARATOR (*p))
903 len = (guint) 1 + base - file_name;
905 base = g_new (gchar, len + 1);
906 g_memmove (base, file_name, len);
915 * Gets the current directory.
916 * The returned string should be freed when no longer needed. The encoding
917 * of the returned string is system defined. On Windows, it is always UTF-8.
919 * Returns: the current directory.
922 g_get_current_dir (void)
928 if (G_WIN32_HAVE_WIDECHAR_API ())
930 wchar_t dummy[2], *wdir;
933 len = GetCurrentDirectoryW (2, dummy);
934 wdir = g_new (wchar_t, len);
936 if (GetCurrentDirectoryW (len, wdir) == len - 1)
937 dir = g_utf16_to_utf8 (wdir, -1, NULL, NULL, NULL);
943 gchar dummy[2], *cpdir;
946 len = GetCurrentDirectoryA (2, dummy);
947 cpdir = g_new (gchar, len);
949 if (GetCurrentDirectoryA (len, cpdir) == len - 1)
950 dir = g_locale_to_utf8 (cpdir, -1, NULL, NULL, NULL);
956 dir = g_strdup ("\\");
962 gchar *buffer = NULL;
964 static gulong max_len = 0;
967 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
969 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
970 * and, if that wasn't bad enough, hangs in doing so.
972 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
973 buffer = g_new (gchar, max_len + 1);
975 dir = getwd (buffer);
976 #else /* !sun || !HAVE_GETCWD */
977 while (max_len < G_MAXULONG / 2)
979 buffer = g_new (gchar, max_len + 1);
981 dir = getcwd (buffer, max_len);
983 if (dir || errno != ERANGE)
989 #endif /* !sun || !HAVE_GETCWD */
991 if (!dir || !*buffer)
993 /* hm, should we g_error() out here?
994 * this can happen if e.g. "./" has mode \0000
996 buffer[0] = G_DIR_SEPARATOR;
1000 dir = g_strdup (buffer);
1009 * @variable: the environment variable to get, in the GLib file name encoding.
1011 * Returns the value of an environment variable. The name and value
1012 * are in the GLib file name encoding. On UNIX, this means the actual
1013 * bytes which might or might not be in some consistent character set
1014 * and encoding. On Windows, it is in UTF-8. On Windows, in case the
1015 * environment variable's value contains references to other
1016 * environment variables, they are expanded.
1018 * Return value: the value of the environment variable, or %NULL if
1019 * the environment variable is not found. The returned string may be
1020 * overwritten by the next call to g_getenv(), g_setenv() or
1023 G_CONST_RETURN gchar*
1024 g_getenv (const gchar *variable)
1028 g_return_val_if_fail (variable != NULL, NULL);
1030 return getenv (variable);
1032 #else /* G_OS_WIN32 */
1037 g_return_val_if_fail (variable != NULL, NULL);
1038 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), NULL);
1040 /* On Windows NT, it is relatively typical that environment
1041 * variables contain references to other environment variables. If
1042 * so, use ExpandEnvironmentStrings(). (In an ideal world, such
1043 * environment variables would be stored in the Registry as
1044 * REG_EXPAND_SZ type values, and would then get automatically
1045 * expanded before a program sees them. But there is broken software
1046 * that stores environment variables as REG_SZ values even if they
1047 * contain references to other environment variables.)
1050 if (G_WIN32_HAVE_WIDECHAR_API ())
1052 wchar_t dummy[2], *wname, *wvalue;
1055 wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1057 len = GetEnvironmentVariableW (wname, dummy, 2);
1067 wvalue = g_new (wchar_t, len);
1069 if (GetEnvironmentVariableW (wname, wvalue, len) != len - 1)
1076 if (wcschr (wvalue, L'%') != NULL)
1078 wchar_t *tem = wvalue;
1080 len = ExpandEnvironmentStringsW (wvalue, dummy, 2);
1084 wvalue = g_new (wchar_t, len);
1086 if (ExpandEnvironmentStringsW (tem, wvalue, len) != len)
1096 value = g_utf16_to_utf8 (wvalue, -1, NULL, NULL, NULL);
1103 gchar dummy[3], *cpname, *cpvalue;
1106 cpname = g_locale_from_utf8 (variable, -1, NULL, NULL, NULL);
1108 g_return_val_if_fail (cpname != NULL, NULL);
1110 len = GetEnvironmentVariableA (cpname, dummy, 2);
1120 cpvalue = g_new (gchar, len);
1122 if (GetEnvironmentVariableA (cpname, cpvalue, len) != len - 1)
1129 if (strchr (cpvalue, '%') != NULL)
1131 gchar *tem = cpvalue;
1133 len = ExpandEnvironmentStringsA (cpvalue, dummy, 3);
1137 cpvalue = g_new (gchar, len);
1139 if (ExpandEnvironmentStringsA (tem, cpvalue, len) != len)
1149 value = g_locale_to_utf8 (cpvalue, -1, NULL, NULL, NULL);
1155 quark = g_quark_from_string (value);
1158 return g_quark_to_string (quark);
1160 #endif /* G_OS_WIN32 */
1163 /* _g_getenv_nomalloc
1164 * this function does a getenv() without doing any kind of allocation
1165 * through glib. it's suitable for chars <= 127 only (both, for the
1166 * variable name and the contents) and for contents < 1024 chars in
1167 * length. also, it aliases "" to a NULL return value.
1170 _g_getenv_nomalloc (const gchar *variable,
1173 const gchar *retval = getenv (variable);
1174 if (retval && retval[0])
1176 gint l = strlen (retval);
1179 strncpy (buffer, retval, l);
1189 * @variable: the environment variable to set, must not contain '='.
1190 * @value: the value for to set the variable to.
1191 * @overwrite: whether to change the variable if it already exists.
1193 * Sets an environment variable. Both the variable's name and value
1194 * should be in the GLib file name encoding. On UNIX, this means that
1195 * they can be any sequence of bytes. On Windows, they should be in
1198 * Note that on some systems, when variables are overwritten, the memory
1199 * used for the previous variables and its value isn't reclaimed.
1201 * Returns: %FALSE if the environment variable couldn't be set.
1206 g_setenv (const gchar *variable,
1217 g_return_val_if_fail (variable != NULL, FALSE);
1218 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1221 result = setenv (variable, value, overwrite);
1223 if (!overwrite && getenv (variable) != NULL)
1226 /* This results in a leak when you overwrite existing
1227 * settings. It would be fairly easy to fix this by keeping
1228 * our own parallel array or hash table.
1230 string = g_strconcat (variable, "=", value, NULL);
1231 result = putenv (string);
1235 #else /* G_OS_WIN32 */
1239 g_return_val_if_fail (variable != NULL, FALSE);
1240 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
1241 g_return_val_if_fail (g_utf8_validate (variable, -1, NULL), FALSE);
1242 g_return_val_if_fail (g_utf8_validate (value, -1, NULL), FALSE);
1244 if (!overwrite && g_getenv (variable) != NULL)
1247 /* We want to (if possible) set both the environment variable copy
1248 * kept by the C runtime and the one kept by the system.
1250 * We can't use only the C runtime's putenv or _wputenv() as that
1251 * won't work for arbitrary Unicode strings in a "non-Unicode" app
1252 * (with main() and not wmain()). In a "main()" app the C runtime
1253 * initializes the C runtime's environment table by converting the
1254 * real (wide char) environment variables to system codepage, thus
1255 * breaking those that aren't representable in the system codepage.
1257 * As the C runtime's putenv() will also set the system copy, we do
1258 * the putenv() first, then call SetEnvironmentValueW ourselves.
1261 if (G_WIN32_HAVE_WIDECHAR_API ())
1263 wchar_t *wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1264 wchar_t *wvalue = g_utf8_to_utf16 (value, -1, NULL, NULL, NULL);
1265 gchar *tem = g_strconcat (variable, "=", value, NULL);
1266 wchar_t *wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1269 _wputenv (wassignment);
1270 g_free (wassignment);
1272 retval = (SetEnvironmentVariableW (wname, wvalue) != 0);
1279 /* In the non-Unicode case (Win9x), just putenv() is good
1282 gchar *tem = g_strconcat (variable, "=", value, NULL);
1283 gchar *cpassignment = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
1287 retval = (putenv (cpassignment) == 0);
1289 g_free (cpassignment);
1294 #endif /* G_OS_WIN32 */
1297 #ifdef HAVE__NSGETENVIRON
1298 #define environ (*_NSGetEnviron())
1299 #elif !defined(G_OS_WIN32)
1301 /* According to the Single Unix Specification, environ is not in
1302 * any system header, although unistd.h often declares it.
1304 extern char **environ;
1309 * @variable: the environment variable to remove, must not contain '='.
1311 * Removes an environment variable from the environment.
1313 * Note that on some systems, when variables are overwritten, the memory
1314 * used for the previous variables and its value isn't reclaimed.
1315 * Furthermore, this function can't be guaranteed to operate in a
1321 g_unsetenv (const gchar *variable)
1325 #ifdef HAVE_UNSETENV
1326 g_return_if_fail (variable != NULL);
1327 g_return_if_fail (strchr (variable, '=') == NULL);
1329 unsetenv (variable);
1330 #else /* !HAVE_UNSETENV */
1334 g_return_if_fail (variable != NULL);
1335 g_return_if_fail (strchr (variable, '=') == NULL);
1337 len = strlen (variable);
1339 /* Mess directly with the environ array.
1340 * This seems to be the only portable way to do this.
1342 * Note that we remove *all* environment entries for
1343 * the variable name, not just the first.
1348 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
1356 #endif /* !HAVE_UNSETENV */
1358 #else /* G_OS_WIN32 */
1360 g_return_if_fail (variable != NULL);
1361 g_return_if_fail (strchr (variable, '=') == NULL);
1362 g_return_if_fail (g_utf8_validate (variable, -1, NULL));
1364 if (G_WIN32_HAVE_WIDECHAR_API ())
1366 wchar_t *wname = g_utf8_to_utf16 (variable, -1, NULL, NULL, NULL);
1367 gchar *tem = g_strconcat (variable, "=", NULL);
1368 wchar_t *wassignment = g_utf8_to_utf16 (tem, -1, NULL, NULL, NULL);
1371 _wputenv (wassignment);
1372 g_free (wassignment);
1374 SetEnvironmentVariableW (wname, NULL);
1380 /* In the non-Unicode case (Win9x), just putenv() is good
1383 gchar *tem = g_strconcat (variable, "=", NULL);
1384 gchar *cpassignment = g_locale_from_utf8 (tem, -1, NULL, NULL, NULL);
1388 putenv (cpassignment);
1390 g_free (cpassignment);
1393 #endif /* G_OS_WIN32 */
1399 * Gets the names of all variables set in the environment.
1401 * Returns: a %NULL-terminated list of strings which must be freed
1402 * with g_strfreev().
1409 gchar **result, *eq;
1412 len = g_strv_length (environ);
1413 result = g_new0 (gchar *, len + 1);
1416 for (i = 0; i < len; i++)
1418 eq = strchr (environ[i], '=');
1420 result[j++] = g_strndup (environ[i], eq - environ[i]);
1428 G_LOCK_DEFINE_STATIC (g_utils_global);
1430 static gchar *g_tmp_dir = NULL;
1431 static gchar *g_user_name = NULL;
1432 static gchar *g_real_name = NULL;
1433 static gchar *g_home_dir = NULL;
1434 static gchar *g_host_name = NULL;
1437 /* System codepage versions of the above, kept at file level so that they,
1438 * too, are produced only once.
1440 static gchar *g_tmp_dir_cp = NULL;
1441 static gchar *g_user_name_cp = NULL;
1442 static gchar *g_real_name_cp = NULL;
1443 static gchar *g_home_dir_cp = NULL;
1446 static gchar *g_user_data_dir = NULL;
1447 static gchar **g_system_data_dirs = NULL;
1448 static gchar *g_user_cache_dir = NULL;
1449 static gchar *g_user_config_dir = NULL;
1450 static gchar **g_system_config_dirs = NULL;
1455 get_special_folder (int csidl)
1459 wchar_t wc[MAX_PATH+1];
1462 LPITEMIDLIST pidl = NULL;
1464 gchar *retval = NULL;
1466 hr = SHGetSpecialFolderLocation (NULL, csidl, &pidl);
1469 if (G_WIN32_HAVE_WIDECHAR_API ())
1471 b = SHGetPathFromIDListW (pidl, path.wc);
1473 retval = g_utf16_to_utf8 (path.wc, -1, NULL, NULL, NULL);
1477 b = SHGetPathFromIDListA (pidl, path.c);
1479 retval = g_locale_to_utf8 (path.c, -1, NULL, NULL, NULL);
1481 CoTaskMemFree (pidl);
1487 get_windows_directory_root (void)
1489 char windowsdir[MAX_PATH];
1491 if (GetWindowsDirectory (windowsdir, sizeof (windowsdir)))
1493 /* Usually X:\Windows, but in terminal server environments
1494 * might be an UNC path, AFAIK.
1496 char *p = (char *) g_path_skip_root (windowsdir);
1497 if (G_IS_DIR_SEPARATOR (p[-1]) && p[-2] != ':')
1500 return g_strdup (windowsdir);
1503 return g_strdup ("C:\\");
1508 /* HOLDS: g_utils_global_lock */
1510 g_get_any_init_do (void)
1512 gchar hostname[100];
1514 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
1516 g_tmp_dir = g_strdup (g_getenv ("TMP"));
1518 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
1522 g_tmp_dir = get_windows_directory_root ();
1528 g_tmp_dir = g_strdup (P_tmpdir);
1529 k = strlen (g_tmp_dir);
1530 if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
1531 g_tmp_dir[k - 1] = '\0';
1537 g_tmp_dir = g_strdup ("/tmp");
1539 #endif /* !G_OS_WIN32 */
1542 /* We check $HOME first for Win32, though it is a last resort for Unix
1543 * where we prefer the results of getpwuid().
1545 g_home_dir = g_strdup (g_getenv ("HOME"));
1547 /* Only believe HOME if it is an absolute path and exists */
1550 if (!(g_path_is_absolute (g_home_dir) &&
1551 g_file_test (g_home_dir, G_FILE_TEST_IS_DIR)))
1553 g_free (g_home_dir);
1558 /* In case HOME is Unix-style (it happens), convert it to
1564 while ((p = strchr (g_home_dir, '/')) != NULL)
1570 /* USERPROFILE is probably the closest equivalent to $HOME? */
1571 if (g_getenv ("USERPROFILE") != NULL)
1572 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
1576 g_home_dir = get_special_folder (CSIDL_PROFILE);
1579 g_home_dir = get_windows_directory_root ();
1580 #endif /* G_OS_WIN32 */
1584 struct passwd *pw = NULL;
1585 gpointer buffer = NULL;
1589 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
1591 # ifdef _SC_GETPW_R_SIZE_MAX
1592 /* This reurns the maximum length */
1593 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
1597 # else /* _SC_GETPW_R_SIZE_MAX */
1599 # endif /* _SC_GETPW_R_SIZE_MAX */
1601 logname = (gchar *) g_getenv ("LOGNAME");
1606 /* we allocate 6 extra bytes to work around a bug in
1607 * Mac OS < 10.3. See #156446
1609 buffer = g_malloc (bufsize + 6);
1612 # ifdef HAVE_POSIX_GETPWUID_R
1614 error = getpwnam_r (logname, &pwd, buffer, bufsize, &pw);
1615 if (!pw || (pw->pw_uid != getuid ())) {
1616 /* LOGNAME is lying, fall back to looking up the uid */
1617 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1620 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
1622 error = error < 0 ? errno : error;
1623 # else /* HAVE_NONPOSIX_GETPWUID_R */
1624 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
1625 # if defined(_AIX) || defined(__hpux)
1626 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1627 pw = error == 0 ? &pwd : NULL;
1630 pw = getpwnam_r (logname, &pwd, buffer, bufsize);
1631 if (!pw || (pw->pw_uid != getuid ())) {
1632 /* LOGNAME is lying, fall back to looking up the uid */
1633 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1636 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1638 error = pw ? 0 : errno;
1640 # endif /* HAVE_NONPOSIX_GETPWUID_R */
1644 /* we bail out prematurely if the user id can't be found
1645 * (should be pretty rare case actually), or if the buffer
1646 * should be sufficiently big and lookups are still not
1649 if (error == 0 || error == ENOENT)
1651 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
1652 (gulong) getuid ());
1655 if (bufsize > 32 * 1024)
1657 g_warning ("getpwuid_r(): failed due to: %s.",
1658 g_strerror (error));
1666 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
1671 pw = getpwuid (getuid ());
1676 g_user_name = g_strdup (pw->pw_name);
1678 if (pw->pw_gecos && *pw->pw_gecos != '\0')
1680 gchar **gecos_fields;
1683 /* split the gecos field and substitute '&' */
1684 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
1685 name_parts = g_strsplit (gecos_fields[0], "&", 0);
1686 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
1687 g_real_name = g_strjoinv (pw->pw_name, name_parts);
1688 g_strfreev (gecos_fields);
1689 g_strfreev (name_parts);
1693 g_home_dir = g_strdup (pw->pw_dir);
1698 #else /* !HAVE_PWD_H */
1701 if (G_WIN32_HAVE_WIDECHAR_API ())
1703 guint len = UNLEN+1;
1704 wchar_t buffer[UNLEN+1];
1706 if (GetUserNameW (buffer, (LPDWORD) &len))
1708 g_user_name = g_utf16_to_utf8 (buffer, -1, NULL, NULL, NULL);
1709 g_real_name = g_strdup (g_user_name);
1714 guint len = UNLEN+1;
1715 char buffer[UNLEN+1];
1717 if (GetUserNameA (buffer, (LPDWORD) &len))
1719 g_user_name = g_locale_to_utf8 (buffer, -1, NULL, NULL, NULL);
1720 g_real_name = g_strdup (g_user_name);
1723 #endif /* G_OS_WIN32 */
1725 #endif /* !HAVE_PWD_H */
1729 g_home_dir = g_strdup (g_getenv ("HOME"));
1733 /* change '\\' in %HOME% to '/' */
1734 g_strdelimit (g_home_dir, "\\",'/');
1737 g_user_name = g_strdup ("somebody");
1739 g_real_name = g_strdup ("Unknown");
1743 gboolean hostname_fail = (gethostname (hostname, sizeof (hostname)) == -1);
1745 DWORD size = sizeof (hostname);
1746 gboolean hostname_fail = (!GetComputerName (hostname, &size));
1748 g_host_name = g_strdup (hostname_fail ? "localhost" : hostname);
1752 g_tmp_dir_cp = g_locale_from_utf8 (g_tmp_dir, -1, NULL, NULL, NULL);
1753 g_user_name_cp = g_locale_from_utf8 (g_user_name, -1, NULL, NULL, NULL);
1754 g_real_name_cp = g_locale_from_utf8 (g_real_name, -1, NULL, NULL, NULL);
1757 g_tmp_dir_cp = g_strdup ("\\");
1758 if (!g_user_name_cp)
1759 g_user_name_cp = g_strdup ("somebody");
1760 if (!g_real_name_cp)
1761 g_real_name_cp = g_strdup ("Unknown");
1763 /* home_dir might be NULL, unlike tmp_dir, user_name and
1767 g_home_dir_cp = g_locale_from_utf8 (g_home_dir, -1, NULL, NULL, NULL);
1769 g_home_dir_cp = NULL;
1770 #endif /* G_OS_WIN32 */
1774 g_get_any_init (void)
1777 g_get_any_init_do ();
1781 g_get_any_init_locked (void)
1783 G_LOCK (g_utils_global);
1785 G_UNLOCK (g_utils_global);
1792 * Gets the user name of the current user. The encoding of the returned
1793 * string is system-defined. On UNIX, it might be the preferred file name
1794 * encoding, or something else, and there is no guarantee that it is even
1795 * consistent on a machine. On Windows, it is always UTF-8.
1797 * Returns: the user name of the current user.
1799 G_CONST_RETURN gchar*
1800 g_get_user_name (void)
1802 g_get_any_init_locked ();
1809 * Gets the real name of the user. This usually comes from the user's entry
1810 * in the <filename>passwd</filename> file. The encoding of the returned
1811 * string is system-defined. (On Windows, it is, however, always UTF-8.)
1812 * If the real user name cannot be determined, the string "Unknown" is
1815 * Returns: the user's real name.
1817 G_CONST_RETURN gchar*
1818 g_get_real_name (void)
1820 g_get_any_init_locked ();
1827 * Gets the current user's home directory.
1829 * Note that in contrast to traditional UNIX tools, this function
1830 * prefers <filename>passwd</filename> entries over the <envar>HOME</envar>
1831 * environment variable.
1833 * Returns: the current user's home directory.
1835 G_CONST_RETURN gchar*
1836 g_get_home_dir (void)
1838 g_get_any_init_locked ();
1845 * Gets the directory to use for temporary files. This is found from
1846 * inspecting the environment variables <envar>TMPDIR</envar>,
1847 * <envar>TMP</envar>, and <envar>TEMP</envar> in that order. If none
1848 * of those are defined "/tmp" is returned on UNIX and "C:\" on Windows.
1849 * The encoding of the returned string is system-defined. On Windows,
1850 * it is always UTF-8. The return value is never %NULL.
1852 * Returns: the directory to use for temporary files.
1854 G_CONST_RETURN gchar*
1855 g_get_tmp_dir (void)
1857 g_get_any_init_locked ();
1864 * Return a name for the machine.
1866 * The returned name is not necessarily a fully-qualified domain name,
1867 * or even present in DNS or some other name service at all. It need
1868 * not even be unique on your local network or site, but usually it
1869 * is. Callers should not rely on the return value having any specific
1870 * properties like uniqueness for security purposes. Even if the name
1871 * of the machine is changed while an application is running, the
1872 * return value from this function does not change. The returned
1873 * string is owned by GLib and should not be modified or freed. If no
1874 * name can be determined, a default fixed string "localhost" is
1877 * Returns: the host name of the machine.
1882 g_get_host_name (void)
1884 g_get_any_init_locked ();
1888 G_LOCK_DEFINE_STATIC (g_prgname);
1889 static gchar *g_prgname = NULL;
1894 * Gets the name of the program. This name should <emphasis>not</emphasis>
1895 * be localized, contrast with g_get_application_name().
1896 * (If you are using GDK or GTK+ the program name is set in gdk_init(),
1897 * which is called by gtk_init(). The program name is found by taking
1898 * the last component of <literal>argv[0]</literal>.)
1900 * Returns: the name of the program. The returned string belongs
1901 * to GLib and must not be modified or freed.
1904 g_get_prgname (void)
1910 if (g_prgname == NULL)
1912 static gboolean beenhere = FALSE;
1916 gchar *utf8_buf = NULL;
1919 if (G_WIN32_HAVE_WIDECHAR_API ())
1921 wchar_t buf[MAX_PATH+1];
1922 if (GetModuleFileNameW (GetModuleHandle (NULL),
1923 buf, G_N_ELEMENTS (buf)) > 0)
1924 utf8_buf = g_utf16_to_utf8 (buf, -1, NULL, NULL, NULL);
1928 gchar buf[MAX_PATH+1];
1929 if (GetModuleFileNameA (GetModuleHandle (NULL),
1930 buf, G_N_ELEMENTS (buf)) > 0)
1931 utf8_buf = g_locale_to_utf8 (buf, -1, NULL, NULL, NULL);
1935 g_prgname = g_path_get_basename (utf8_buf);
1942 G_UNLOCK (g_prgname);
1949 * @prgname: the name of the program.
1951 * Sets the name of the program. This name should <emphasis>not</emphasis>
1952 * be localized, contrast with g_set_application_name(). Note that for
1953 * thread-safety reasons this function can only be called once.
1956 g_set_prgname (const gchar *prgname)
1960 g_prgname = g_strdup (prgname);
1961 G_UNLOCK (g_prgname);
1964 G_LOCK_DEFINE_STATIC (g_application_name);
1965 static gchar *g_application_name = NULL;
1968 * g_get_application_name:
1970 * Gets a human-readable name for the application, as set by
1971 * g_set_application_name(). This name should be localized if
1972 * possible, and is intended for display to the user. Contrast with
1973 * g_get_prgname(), which gets a non-localized name. If
1974 * g_set_application_name() has not been called, returns the result of
1975 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1978 * Return value: human-readable application name. may return %NULL
1982 G_CONST_RETURN gchar*
1983 g_get_application_name (void)
1987 G_LOCK (g_application_name);
1988 retval = g_application_name;
1989 G_UNLOCK (g_application_name);
1992 return g_get_prgname ();
1998 * g_set_application_name:
1999 * @application_name: localized name of the application
2001 * Sets a human-readable name for the application. This name should be
2002 * localized if possible, and is intended for display to the user.
2003 * Contrast with g_set_prgname(), which sets a non-localized name.
2004 * g_set_prgname() will be called automatically by gtk_init(),
2005 * but g_set_application_name() will not.
2007 * Note that for thread safety reasons, this function can only
2010 * The application name will be used in contexts such as error messages,
2011 * or when displaying an application's name in the task list.
2015 g_set_application_name (const gchar *application_name)
2017 gboolean already_set = FALSE;
2019 G_LOCK (g_application_name);
2020 if (g_application_name)
2023 g_application_name = g_strdup (application_name);
2024 G_UNLOCK (g_application_name);
2027 g_warning ("g_set_application() name called multiple times");
2031 * g_get_user_data_dir:
2033 * Returns a base directory in which to access application data such
2034 * as icons that is customized for a particular user.
2036 * On UNIX platforms this is determined using the mechanisms described in
2037 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2038 * XDG Base Directory Specification</ulink>
2040 * Return value: a string owned by GLib that must not be modified
2044 G_CONST_RETURN gchar*
2045 g_get_user_data_dir (void)
2049 G_LOCK (g_utils_global);
2051 if (!g_user_data_dir)
2054 data_dir = get_special_folder (CSIDL_PERSONAL);
2056 data_dir = (gchar *) g_getenv ("XDG_DATA_HOME");
2058 if (data_dir && data_dir[0])
2059 data_dir = g_strdup (data_dir);
2061 if (!data_dir || !data_dir[0])
2066 data_dir = g_build_filename (g_home_dir, ".local",
2069 data_dir = g_build_filename (g_tmp_dir, g_user_name, ".local",
2073 g_user_data_dir = data_dir;
2076 data_dir = g_user_data_dir;
2078 G_UNLOCK (g_utils_global);
2084 * g_get_user_config_dir:
2086 * Returns a base directory in which to store user-specific application
2087 * configuration information such as user preferences and settings.
2089 * On UNIX platforms this is determined using the mechanisms described in
2090 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2091 * XDG Base Directory Specification</ulink>
2093 * Return value: a string owned by GLib that must not be modified
2097 G_CONST_RETURN gchar*
2098 g_get_user_config_dir (void)
2102 G_LOCK (g_utils_global);
2104 if (!g_user_config_dir)
2107 config_dir = get_special_folder (CSIDL_APPDATA);
2109 config_dir = (gchar *) g_getenv ("XDG_CONFIG_HOME");
2111 if (config_dir && config_dir[0])
2112 config_dir = g_strdup (config_dir);
2114 if (!config_dir || !config_dir[0])
2119 config_dir = g_build_filename (g_home_dir, ".config", NULL);
2121 config_dir = g_build_filename (g_tmp_dir, g_user_name, ".config", NULL);
2123 g_user_config_dir = config_dir;
2126 config_dir = g_user_config_dir;
2128 G_UNLOCK (g_utils_global);
2134 * g_get_user_cache_dir:
2136 * Returns a base directory in which to store non-essential, cached
2137 * data specific to particular user.
2139 * On UNIX platforms this is determined using the mechanisms described in
2140 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2141 * XDG Base Directory Specification</ulink>
2143 * Return value: a string owned by GLib that must not be modified
2147 G_CONST_RETURN gchar*
2148 g_get_user_cache_dir (void)
2152 G_LOCK (g_utils_global);
2154 if (!g_user_cache_dir)
2157 cache_dir = get_special_folder (CSIDL_INTERNET_CACHE); /* XXX correct? */
2159 cache_dir = (gchar *) g_getenv ("XDG_CACHE_HOME");
2161 if (cache_dir && cache_dir[0])
2162 cache_dir = g_strdup (cache_dir);
2164 if (!cache_dir || !cache_dir[0])
2169 cache_dir = g_build_filename (g_home_dir, ".cache", NULL);
2171 cache_dir = g_build_filename (g_tmp_dir, g_user_name, ".cache", NULL);
2173 g_user_cache_dir = cache_dir;
2176 cache_dir = g_user_cache_dir;
2178 G_UNLOCK (g_utils_global);
2185 #undef g_get_system_data_dirs
2188 get_module_for_address (gconstpointer address)
2190 /* Holds the g_utils_global lock */
2192 static gboolean beenhere = FALSE;
2193 typedef BOOL (WINAPI *t_GetModuleHandleExA) (DWORD, LPCTSTR, HMODULE *);
2194 static t_GetModuleHandleExA p_GetModuleHandleExA = NULL;
2202 p_GetModuleHandleExA =
2203 (t_GetModuleHandleExA) GetProcAddress (LoadLibrary ("kernel32.dll"),
2204 "GetModuleHandleExA");
2208 if (p_GetModuleHandleExA == NULL ||
2209 !(*p_GetModuleHandleExA) (GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT |
2210 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
2213 MEMORY_BASIC_INFORMATION mbi;
2214 VirtualQuery (address, &mbi, sizeof (mbi));
2215 hmodule = (HMODULE) mbi.AllocationBase;
2222 get_module_share_dir (gconstpointer address)
2225 gchar *filename = NULL;
2228 hmodule = get_module_for_address (address);
2229 if (hmodule == NULL)
2232 if (G_WIN32_IS_NT_BASED ())
2234 wchar_t wfilename[MAX_PATH];
2235 if (GetModuleFileNameW (hmodule, wfilename, G_N_ELEMENTS (wfilename)))
2236 filename = g_utf16_to_utf8 (wfilename, -1, NULL, NULL, NULL);
2240 char cpfilename[MAX_PATH];
2241 if (GetModuleFileNameA (hmodule, cpfilename, G_N_ELEMENTS (cpfilename)))
2242 filename = g_locale_to_utf8 (cpfilename, -1, NULL, NULL, NULL);
2245 if (filename == NULL)
2248 if ((p = strrchr (filename, G_DIR_SEPARATOR)) != NULL)
2251 p = strrchr (filename, G_DIR_SEPARATOR);
2252 if (p && (g_ascii_strcasecmp (p + 1, "bin") == 0))
2255 retval = g_build_filename (filename, "share", NULL);
2261 G_CONST_RETURN gchar * G_CONST_RETURN *
2262 g_win32_get_system_data_dirs_for_module (gconstpointer address)
2266 static GHashTable *per_module_data_dirs = NULL;
2272 G_LOCK (g_utils_global);
2273 hmodule = get_module_for_address (address);
2274 if (hmodule != NULL)
2276 if (per_module_data_dirs == NULL)
2277 per_module_data_dirs = g_hash_table_new (NULL, NULL);
2280 retval = g_hash_table_lookup (per_module_data_dirs, hmodule);
2284 G_UNLOCK (g_utils_global);
2285 return (G_CONST_RETURN gchar * G_CONST_RETURN *) retval;
2291 data_dirs = g_array_new (TRUE, TRUE, sizeof (char *));
2293 /* Documents and Settings\All Users\Application Data */
2294 p = get_special_folder (CSIDL_COMMON_APPDATA);
2296 g_array_append_val (data_dirs, p);
2298 /* Documents and Settings\All Users\Documents */
2299 p = get_special_folder (CSIDL_COMMON_DOCUMENTS);
2301 g_array_append_val (data_dirs, p);
2303 /* Using the above subfolders of Documents and Settings perhaps
2304 * makes sense from a Windows perspective.
2306 * But looking at the actual use cases of this function in GTK+
2307 * and GNOME software, what we really want is the "share"
2308 * subdirectory of the installation directory for the package
2309 * our caller is a part of.
2311 * The address parameter, if non-NULL, points to a function in the
2312 * calling module. Use that to determine that module's installation
2313 * folder, and use its "share" subfolder.
2315 * Additionally, also use the "share" subfolder of the installation
2316 * locations of GLib and the .exe file being run.
2318 * To guard against none of the above being what is really wanted,
2319 * callers of this function should have Win32-specific code to look
2320 * up their installation folder themselves, and handle a subfolder
2321 * "share" of it in the same way as the folders returned from this
2325 p = get_module_share_dir (address);
2327 g_array_append_val (data_dirs, p);
2329 p = g_win32_get_package_installation_subdirectory (NULL, dll_name, "share");
2331 g_array_append_val (data_dirs, p);
2333 p = g_win32_get_package_installation_subdirectory (NULL, NULL, "share");
2335 g_array_append_val (data_dirs, p);
2337 retval = (gchar **) g_array_free (data_dirs, FALSE);
2341 if (hmodule != NULL)
2342 g_hash_table_insert (per_module_data_dirs, hmodule, retval);
2343 G_UNLOCK (g_utils_global);
2346 return (G_CONST_RETURN gchar * G_CONST_RETURN *) retval;
2352 * g_get_system_data_dirs:
2354 * Returns an ordered list of base directories in which to access
2355 * system-wide application data.
2357 * On UNIX platforms this is determined using the mechanisms described in
2358 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2359 * XDG Base Directory Specification</ulink>
2361 * On Windows the first elements in the list are the Application Data
2362 * and Documents folders for All Users. (These can be determined only
2363 * on Windows 2000 or later and are not present in the list on other
2364 * Windows versions.) See documentation for CSIDL_COMMON_APPDATA and
2365 * CSIDL_COMMON_DOCUMENTS.
2367 * Then follows the "share" subfolder in the installation folder for
2368 * the package containing the DLL that calls this function, if it can
2371 * Finally the list contains the "share" subfolder in the installation
2372 * folder for GLib, and in the installation folder for the package the
2373 * application's .exe file belongs to.
2375 * The installation folders above are determined by looking up the
2376 * folder where the module (DLL or EXE) in question is located. If the
2377 * folder's name is "bin", its parent is used, otherwise the folder
2380 * Note that on Windows the returned list can vary depending on where
2381 * this function is called.
2383 * Return value: a %NULL-terminated array of strings owned by GLib that must
2384 * not be modified or freed.
2387 G_CONST_RETURN gchar * G_CONST_RETURN *
2388 g_get_system_data_dirs (void)
2390 gchar **data_dir_vector;
2392 G_LOCK (g_utils_global);
2394 if (!g_system_data_dirs)
2397 data_dir_vector = (gchar **) g_win32_get_system_data_dirs_for_module (NULL);
2399 gchar *data_dirs = (gchar *) g_getenv ("XDG_DATA_DIRS");
2401 if (!data_dirs || !data_dirs[0])
2402 data_dirs = "/usr/local/share/:/usr/share/";
2404 data_dir_vector = g_strsplit (data_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2407 g_system_data_dirs = data_dir_vector;
2410 data_dir_vector = g_system_data_dirs;
2412 G_UNLOCK (g_utils_global);
2414 return (G_CONST_RETURN gchar * G_CONST_RETURN *) data_dir_vector;
2418 * g_get_system_config_dirs:
2420 * Returns an ordered list of base directories in which to access
2421 * system-wide configuration information.
2423 * On UNIX platforms this is determined using the mechanisms described in
2424 * the <ulink url="http://www.freedesktop.org/Standards/basedir-spec">
2425 * XDG Base Directory Specification</ulink>
2427 * Return value: a %NULL-terminated array of strings owned by GLib that must
2428 * not be modified or freed.
2431 G_CONST_RETURN gchar * G_CONST_RETURN *
2432 g_get_system_config_dirs (void)
2434 gchar *conf_dirs, **conf_dir_vector;
2436 G_LOCK (g_utils_global);
2438 if (!g_system_config_dirs)
2441 conf_dirs = get_special_folder (CSIDL_COMMON_APPDATA);
2444 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2449 /* Return empty list */
2450 conf_dir_vector = g_strsplit ("", G_SEARCHPATH_SEPARATOR_S, 0);
2453 conf_dirs = (gchar *) g_getenv ("XDG_CONFIG_DIRS");
2455 if (!conf_dirs || !conf_dirs[0])
2456 conf_dirs = "/etc/xdg";
2458 conf_dir_vector = g_strsplit (conf_dirs, G_SEARCHPATH_SEPARATOR_S, 0);
2461 g_system_config_dirs = conf_dir_vector;
2464 conf_dir_vector = g_system_config_dirs;
2465 G_UNLOCK (g_utils_global);
2467 return (G_CONST_RETURN gchar * G_CONST_RETURN *) conf_dir_vector;
2472 static GHashTable *alias_table = NULL;
2474 /* read an alias file for the locales */
2476 read_aliases (gchar *file)
2482 alias_table = g_hash_table_new (g_str_hash, g_str_equal);
2483 fp = fopen (file,"r");
2486 while (fgets (buf, 256, fp))
2492 /* Line is a comment */
2493 if ((buf[0] == '#') || (buf[0] == '\0'))
2496 /* Reads first column */
2497 for (p = buf, q = NULL; *p; p++) {
2498 if ((*p == '\t') || (*p == ' ') || (*p == ':')) {
2501 while ((*q == '\t') || (*q == ' ')) {
2507 /* The line only had one column */
2508 if (!q || *q == '\0')
2511 /* Read second column */
2512 for (p = q; *p; p++) {
2513 if ((*p == '\t') || (*p == ' ')) {
2519 /* Add to alias table if necessary */
2520 if (!g_hash_table_lookup (alias_table, buf)) {
2521 g_hash_table_insert (alias_table, g_strdup (buf), g_strdup (q));
2530 unalias_lang (char *lang)
2537 read_aliases ("/usr/share/locale/locale.alias");
2540 while ((p = g_hash_table_lookup (alias_table, lang)) && (strcmp (p, lang) != 0))
2545 static gboolean said_before = FALSE;
2547 g_warning ("Too many alias levels for a locale, "
2548 "may indicate a loop");
2557 /* Mask for components of locale spec. The ordering here is from
2558 * least significant to most significant
2562 COMPONENT_CODESET = 1 << 0,
2563 COMPONENT_TERRITORY = 1 << 1,
2564 COMPONENT_MODIFIER = 1 << 2
2567 /* Break an X/Open style locale specification into components
2570 explode_locale (const gchar *locale,
2576 const gchar *uscore_pos;
2577 const gchar *at_pos;
2578 const gchar *dot_pos;
2582 uscore_pos = strchr (locale, '_');
2583 dot_pos = strchr (uscore_pos ? uscore_pos : locale, '.');
2584 at_pos = strchr (dot_pos ? dot_pos : (uscore_pos ? uscore_pos : locale), '@');
2588 mask |= COMPONENT_MODIFIER;
2589 *modifier = g_strdup (at_pos);
2592 at_pos = locale + strlen (locale);
2596 mask |= COMPONENT_CODESET;
2597 *codeset = g_strndup (dot_pos, at_pos - dot_pos);
2604 mask |= COMPONENT_TERRITORY;
2605 *territory = g_strndup (uscore_pos, dot_pos - uscore_pos);
2608 uscore_pos = dot_pos;
2610 *language = g_strndup (locale, uscore_pos - locale);
2616 * Compute all interesting variants for a given locale name -
2617 * by stripping off different components of the value.
2619 * For simplicity, we assume that the locale is in
2620 * X/Open format: language[_territory][.codeset][@modifier]
2622 * TODO: Extend this to handle the CEN format (see the GNUlibc docs)
2623 * as well. We could just copy the code from glibc wholesale
2624 * but it is big, ugly, and complicated, so I'm reluctant
2625 * to do so when this should handle 99% of the time...
2628 _g_compute_locale_variants (const gchar *locale)
2630 GSList *retval = NULL;
2632 gchar *language = NULL;
2633 gchar *territory = NULL;
2634 gchar *codeset = NULL;
2635 gchar *modifier = NULL;
2640 g_return_val_if_fail (locale != NULL, NULL);
2642 mask = explode_locale (locale, &language, &territory, &codeset, &modifier);
2644 /* Iterate through all possible combinations, from least attractive
2645 * to most attractive.
2647 for (i = 0; i <= mask; i++)
2648 if ((i & ~mask) == 0)
2650 gchar *val = g_strconcat (language,
2651 (i & COMPONENT_TERRITORY) ? territory : "",
2652 (i & COMPONENT_CODESET) ? codeset : "",
2653 (i & COMPONENT_MODIFIER) ? modifier : "",
2655 retval = g_slist_prepend (retval, val);
2659 if (mask & COMPONENT_CODESET)
2661 if (mask & COMPONENT_TERRITORY)
2663 if (mask & COMPONENT_MODIFIER)
2669 /* The following is (partly) taken from the gettext package.
2670 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc. */
2672 static const gchar *
2673 guess_category_value (const gchar *category_name)
2675 const gchar *retval;
2677 /* The highest priority value is the `LANGUAGE' environment
2678 variable. This is a GNU extension. */
2679 retval = g_getenv ("LANGUAGE");
2680 if ((retval != NULL) && (retval[0] != '\0'))
2683 /* `LANGUAGE' is not set. So we have to proceed with the POSIX
2684 methods of looking to `LC_ALL', `LC_xxx', and `LANG'. On some
2685 systems this can be done by the `setlocale' function itself. */
2687 /* Setting of LC_ALL overwrites all other. */
2688 retval = g_getenv ("LC_ALL");
2689 if ((retval != NULL) && (retval[0] != '\0'))
2692 /* Next comes the name of the desired category. */
2693 retval = g_getenv (category_name);
2694 if ((retval != NULL) && (retval[0] != '\0'))
2697 /* Last possibility is the LANG environment variable. */
2698 retval = g_getenv ("LANG");
2699 if ((retval != NULL) && (retval[0] != '\0'))
2702 #ifdef G_PLATFORM_WIN32
2703 /* g_win32_getlocale() first checks for LC_ALL, LC_MESSAGES and
2704 * LANG, which we already did above. Oh well. The main point of
2705 * calling g_win32_getlocale() is to get the thread's locale as used
2706 * by Windows and the Microsoft C runtime (in the "English_United
2707 * States" format) translated into the Unixish format.
2709 retval = g_win32_getlocale ();
2710 if ((retval != NULL) && (retval[0] != '\0'))
2717 typedef struct _GLanguageNamesCache GLanguageNamesCache;
2719 struct _GLanguageNamesCache {
2721 gchar **language_names;
2725 language_names_cache_free (gpointer data)
2727 GLanguageNamesCache *cache = data;
2728 g_free (cache->languages);
2729 g_strfreev (cache->language_names);
2734 * g_get_language_names:
2736 * Computes a list of applicable locale names, which can be used to
2737 * e.g. construct locale-dependent filenames or search paths. The returned
2738 * list is sorted from most desirable to least desirable and always contains
2739 * the default locale "C".
2741 * For example, if LANGUAGE=de:en_US, then the returned list is
2742 * "de", "en_US", "en", "C".
2744 * This function consults the environment variables <envar>LANGUAGE</envar>,
2745 * <envar>LC_ALL</envar>, <envar>LC_MESSAGES</envar> and <envar>LANG</envar>
2746 * to find the list of locales specified by the user.
2748 * Return value: a %NULL-terminated array of strings owned by GLib
2749 * that must not be modified or freed.
2753 G_CONST_RETURN gchar * G_CONST_RETURN *
2754 g_get_language_names (void)
2756 static GStaticPrivate cache_private = G_STATIC_PRIVATE_INIT;
2757 GLanguageNamesCache *cache = g_static_private_get (&cache_private);
2762 cache = g_new0 (GLanguageNamesCache, 1);
2763 g_static_private_set (&cache_private, cache, language_names_cache_free);
2766 value = guess_category_value ("LC_MESSAGES");
2770 if (!(cache->languages && strcmp (cache->languages, value) == 0))
2777 g_free (cache->languages);
2778 g_strfreev (cache->language_names);
2779 cache->languages = g_strdup (value);
2781 alist = g_strsplit (value, ":", 0);
2783 for (a = alist; *a; a++)
2785 gchar *b = unalias_lang (*a);
2786 list = g_slist_concat (list, _g_compute_locale_variants (b));
2789 list = g_slist_append (list, g_strdup ("C"));
2791 cache->language_names = languages = g_new (gchar *, g_slist_length (list) + 1);
2792 for (l = list, i = 0; l; l = l->next, i++)
2793 languages[i] = l->data;
2794 languages[i] = NULL;
2796 g_slist_free (list);
2799 return (G_CONST_RETURN gchar * G_CONST_RETURN *) cache->language_names;
2804 * @v: a #gpointer key
2806 * Converts a gpointer to a hash value.
2807 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2808 * when using pointers as keys in a #GHashTable.
2810 * Returns: a hash value corresponding to the key.
2813 g_direct_hash (gconstpointer v)
2815 return GPOINTER_TO_UINT (v);
2821 * @v2: a key to compare with @v1.
2823 * Compares two #gpointer arguments and returns %TRUE if they are equal.
2824 * It can be passed to g_hash_table_new() as the @key_equal_func
2825 * parameter, when using pointers as keys in a #GHashTable.
2827 * Returns: %TRUE if the two keys match.
2830 g_direct_equal (gconstpointer v1,
2838 * @v1: a pointer to a #gint key.
2839 * @v2: a pointer to a #gint key to compare with @v1.
2841 * Compares the two #gint values being pointed to and returns
2842 * %TRUE if they are equal.
2843 * It can be passed to g_hash_table_new() as the @key_equal_func
2844 * parameter, when using pointers to integers as keys in a #GHashTable.
2846 * Returns: %TRUE if the two keys match.
2849 g_int_equal (gconstpointer v1,
2852 return *((const gint*) v1) == *((const gint*) v2);
2857 * @v: a pointer to a #gint key
2859 * Converts a pointer to a #gint to a hash value.
2860 * It can be passed to g_hash_table_new() as the @hash_func parameter,
2861 * when using pointers to integers values as keys in a #GHashTable.
2863 * Returns: a hash value corresponding to the key.
2866 g_int_hash (gconstpointer v)
2868 return *(const gint*) v;
2872 * g_nullify_pointer:
2873 * @nullify_location: the memory address of the pointer.
2875 * Set the pointer at the specified location to %NULL.
2878 g_nullify_pointer (gpointer *nullify_location)
2880 g_return_if_fail (nullify_location != NULL);
2882 *nullify_location = NULL;
2888 * Get the codeset for the current locale.
2890 * Return value: a newly allocated string containing the name
2891 * of the codeset. This string must be freed with g_free().
2894 g_get_codeset (void)
2896 const gchar *charset;
2898 g_get_charset (&charset);
2900 return g_strdup (charset);
2903 /* This is called from g_thread_init(). It's used to
2904 * initialize some static data in a threadsafe way.
2907 _g_utils_thread_init (void)
2909 g_get_language_names ();
2914 #include <libintl.h>
2919 * _glib_get_locale_dir:
2921 * Return the path to the lib\locale subfolder of the GLib
2922 * installation folder. The path is in the system codepage. We have to
2923 * use system codepage as bindtextdomain() doesn't have a UTF-8
2926 static const gchar *
2927 _glib_get_locale_dir (void)
2929 gchar *dir, *cp_dir;
2930 gchar *retval = NULL;
2932 dir = g_win32_get_package_installation_directory (GETTEXT_PACKAGE, dll_name);
2933 cp_dir = g_win32_locale_filename_from_utf8 (dir);
2938 /* Don't use g_build_filename() on pathnames in the system
2939 * codepage. In CJK locales cp_dir might end with a double-byte
2940 * character whose trailing byte is a backslash.
2942 retval = g_strconcat (cp_dir, "\\lib\\locale", NULL);
2949 return g_strdup ("");
2952 #undef GLIB_LOCALE_DIR
2953 #define GLIB_LOCALE_DIR _glib_get_locale_dir ()
2955 #endif /* G_OS_WIN32 */
2957 G_CONST_RETURN gchar *
2958 _glib_gettext (const gchar *str)
2960 static gboolean _glib_gettext_initialized = FALSE;
2962 if (!_glib_gettext_initialized)
2964 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
2965 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
2966 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
2968 _glib_gettext_initialized = TRUE;
2971 return dgettext (GETTEXT_PACKAGE, str);
2974 #endif /* ENABLE_NLS */
2978 /* Binary compatibility versions. Not for newly compiled code. */
2980 #undef g_find_program_in_path
2983 g_find_program_in_path (const gchar *program)
2985 gchar *utf8_program = g_locale_to_utf8 (program, -1, NULL, NULL, NULL);
2986 gchar *utf8_retval = g_find_program_in_path_utf8 (utf8_program);
2989 g_free (utf8_program);
2990 if (utf8_retval == NULL)
2992 retval = g_locale_from_utf8 (utf8_retval, -1, NULL, NULL, NULL);
2993 g_free (utf8_retval);
2998 #undef g_get_current_dir
3001 g_get_current_dir (void)
3003 gchar *utf8_dir = g_get_current_dir_utf8 ();
3004 gchar *dir = g_locale_from_utf8 (utf8_dir, -1, NULL, NULL, NULL);
3011 G_CONST_RETURN gchar*
3012 g_getenv (const gchar *variable)
3014 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3015 const gchar *utf8_value = g_getenv_utf8 (utf8_variable);
3019 g_free (utf8_variable);
3022 value = g_locale_from_utf8 (utf8_value, -1, NULL, NULL, NULL);
3023 quark = g_quark_from_string (value);
3026 return g_quark_to_string (quark);
3032 g_setenv (const gchar *variable,
3036 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3037 gchar *utf8_value = g_locale_to_utf8 (value, -1, NULL, NULL, NULL);
3038 gboolean retval = g_setenv_utf8 (utf8_variable, utf8_value, overwrite);
3040 g_free (utf8_variable);
3041 g_free (utf8_value);
3049 g_unsetenv (const gchar *variable)
3051 gchar *utf8_variable = g_locale_to_utf8 (variable, -1, NULL, NULL, NULL);
3053 g_unsetenv_utf8 (utf8_variable);
3055 g_free (utf8_variable);
3058 #undef g_get_user_name
3060 G_CONST_RETURN gchar*
3061 g_get_user_name (void)
3063 g_get_any_init_locked ();
3064 return g_user_name_cp;
3067 #undef g_get_real_name
3069 G_CONST_RETURN gchar*
3070 g_get_real_name (void)
3072 g_get_any_init_locked ();
3073 return g_real_name_cp;
3076 #undef g_get_home_dir
3078 G_CONST_RETURN gchar*
3079 g_get_home_dir (void)
3081 g_get_any_init_locked ();
3082 return g_home_dir_cp;
3085 #undef g_get_tmp_dir
3087 G_CONST_RETURN gchar*
3088 g_get_tmp_dir (void)
3090 g_get_any_init_locked ();
3091 return g_tmp_dir_cp;
3096 #define __G_UTILS_C__
3097 #include "galiasdef.c"