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.
44 #include <sys/types.h>
45 #ifdef HAVE_SYS_PARAM_H
46 #include <sys/param.h>
49 /* implement gutils's inline functions
51 #define G_IMPLEMENT_INLINES 1
54 #include "gprintfint.h"
57 #define G_PATH_LENGTH MAXPATHLEN
58 #elif defined (PATH_MAX)
59 #define G_PATH_LENGTH PATH_MAX
60 #elif defined (_PC_PATH_MAX)
61 #define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
63 #define G_PATH_LENGTH 2048
66 #ifdef G_PLATFORM_WIN32
67 # define STRICT /* Strict typing, please */
70 # include <lmcons.h> /* For UNLEN */
72 #endif /* G_PLATFORM_WIN32 */
82 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
86 const guint glib_major_version = GLIB_MAJOR_VERSION;
87 const guint glib_minor_version = GLIB_MINOR_VERSION;
88 const guint glib_micro_version = GLIB_MICRO_VERSION;
89 const guint glib_interface_age = GLIB_INTERFACE_AGE;
90 const guint glib_binary_age = GLIB_BINARY_AGE;
92 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
94 g_memmove (gpointer dest, gconstpointer src, gulong len)
96 gchar* destptr = dest;
97 const gchar* srcptr = src;
98 if (src + len < dest || dest + len < src)
100 bcopy (src, dest, len);
103 else if (dest <= src)
106 *(destptr++) = *(srcptr++);
113 *(--destptr) = *(--srcptr);
116 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
119 g_atexit (GVoidFunc func)
122 const gchar *error = NULL;
124 /* keep this in sync with glib.h */
126 #ifdef G_NATIVE_ATEXIT
127 result = ATEXIT (func);
129 error = g_strerror (errno);
130 #elif defined (HAVE_ATEXIT)
131 # ifdef NeXT /* @#%@! NeXTStep */
132 result = !atexit ((void (*)(void)) func);
134 error = g_strerror (errno);
136 result = atexit ((void (*)(void)) func);
138 error = g_strerror (errno);
140 #elif defined (HAVE_ON_EXIT)
141 result = on_exit ((void (*)(int, void *)) func, NULL);
143 error = g_strerror (errno);
146 error = "no implementation";
147 #endif /* G_NATIVE_ATEXIT */
150 g_error ("Could not register atexit() function: %s", error);
153 /* Based on execvp() from GNU Libc.
154 * Some of this code is cut-and-pasted into gspawn.c
158 my_strchrnul (const gchar *str, gchar c)
160 gchar *p = (gchar*)str;
161 while (*p && (*p != c))
169 gchar *inner_find_program_in_path (const gchar *program);
172 g_find_program_in_path (const gchar *program)
174 const gchar *last_dot = strrchr (program, '.');
176 if (last_dot == NULL || strchr (last_dot, '\\') != NULL)
178 const gint program_length = strlen (program);
179 const gchar *pathext = getenv ("PATHEXT");
181 gchar *decorated_program;
185 pathext = ".com;.exe;.bat";
191 p = my_strchrnul (pathext, ';');
193 decorated_program = g_malloc (program_length + (p-pathext) + 1);
194 memcpy (decorated_program, program, program_length);
195 memcpy (decorated_program+program_length, pathext, p-pathext);
196 decorated_program [program_length + (p-pathext)] = '\0';
198 retval = inner_find_program_in_path (decorated_program);
199 g_free (decorated_program);
203 } while (*p++ != '\0');
207 return inner_find_program_in_path (program);
210 #define g_find_program_in_path inner_find_program_in_path
214 * g_find_program_in_path:
215 * @program: a program name
217 * Locates the first executable named @program in the user's path, in the
218 * same way that execvp() would locate it. Returns an allocated string
219 * with the absolute path name, or NULL if the program is not found in
220 * the path. If @program is already an absolute path, returns a copy of
221 * @program if @program exists and is executable, and NULL otherwise.
223 * On Windows, if @program does not have a file type suffix, tries to
224 * append the suffixes in the PATHEXT environment variable (if that
225 * doesn't exists, the suffixes .com, .exe, and .bat) in turn, and
226 * then look for the resulting file name in the same way as
227 * CreateProcess() would. This means first in the directory where the
228 * program was loaded from, then in the current directory, then in the
229 * Windows 32-bit system directory, then in the Windows directory, and
230 * finally in the directories in the PATH environment variable. If
231 * the program is found, the return value contains the full name
232 * including the type suffix.
234 * Return value: absolute path, or NULL
237 g_find_program_in_path (const gchar *program)
239 const gchar *path, *p;
240 gchar *name, *freeme;
247 g_return_val_if_fail (program != NULL, NULL);
249 /* If it is an absolute path, or a relative path including subdirectories,
250 * don't look in PATH.
252 if (g_path_is_absolute (program)
253 || strchr (program, G_DIR_SEPARATOR) != NULL)
255 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
256 return g_strdup (program);
261 path = g_getenv ("PATH");
265 /* There is no `PATH' in the environment. The default
266 * search path in GNU libc is the current directory followed by
267 * the path `confstr' returns for `_CS_PATH'.
270 /* In GLib we put . last, for security, and don't use the
271 * unportable confstr(); UNIX98 does not actually specify
272 * what to search if PATH is unset. POSIX may, dunno.
275 path = "/bin:/usr/bin:.";
280 gchar moddir[MAXPATHLEN], sysdir[MAXPATHLEN], windir[MAXPATHLEN];
282 GetModuleFileName (NULL, moddir, sizeof (moddir));
283 tmp = g_path_get_dirname (moddir);
284 GetSystemDirectory (sysdir, sizeof (sysdir));
285 GetWindowsDirectory (windir, sizeof (windir));
286 path_tmp = g_strconcat (tmp, ";.;", sysdir, ";", windir,
287 (path != NULL ? ";" : NULL),
288 (path != NULL ? path : NULL),
295 len = strlen (program) + 1;
296 pathlen = strlen (path);
297 freeme = name = g_malloc (pathlen + len + 1);
299 /* Copy the file name at the top, including '\0' */
300 memcpy (name + pathlen + 1, program, len);
301 name = name + pathlen;
302 /* And add the slash before the filename */
303 *name = G_DIR_SEPARATOR;
311 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
314 /* Two adjacent colons, or a colon at the beginning or the end
315 * of `PATH' means to search the current directory.
319 startp = memcpy (name - (p - path), path, p - path);
321 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
324 ret = g_strdup (startp);
332 while (*p++ != '\0');
343 g_parse_debug_string (const gchar *string,
344 const GDebugKey *keys,
350 g_return_val_if_fail (string != NULL, 0);
352 if (!g_ascii_strcasecmp (string, "all"))
354 for (i=0; i<nkeys; i++)
355 result |= keys[i].value;
359 const gchar *p = string;
361 gboolean done = FALSE;
372 for (i=0; i<nkeys; i++)
373 if (g_ascii_strncasecmp(keys[i].key, p, q - p) == 0 &&
374 keys[i].key[q - p] == '\0')
375 result |= keys[i].value;
386 * @file_name: the name of the file.
388 * Gets the name of the file without any leading directory components.
389 * It returns a pointer into the given file name string.
391 * Return value: the name of the file without any leading directory components.
393 * Deprecated: Use g_path_get_basename() instead, but notice that
394 * g_path_get_basename() allocates new memory for the returned string, unlike
395 * this function which returns a pointer into the argument.
397 G_CONST_RETURN gchar*
398 g_basename (const gchar *file_name)
400 register gchar *base;
402 g_return_val_if_fail (file_name != NULL, NULL);
404 base = strrchr (file_name, G_DIR_SEPARATOR);
409 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
410 return (gchar*) file_name + 2;
411 #endif /* G_OS_WIN32 */
413 return (gchar*) file_name;
417 * g_path_get_basename:
418 * @file_name: the name of the file.
420 * Gets the last component of the filename. If @file_name ends with a
421 * directory separator it gets the component before the last slash. If
422 * @file_name consists only of directory separators (and on Windows,
423 * possibly a drive letter), a single separator is returned. If
424 * @file_name is empty, it gets ".".
426 * Return value: a newly allocated string containing the last component of
430 g_path_get_basename (const gchar *file_name)
432 register gssize base;
433 register gssize last_nonslash;
437 g_return_val_if_fail (file_name != NULL, NULL);
439 if (file_name[0] == '\0')
441 return g_strdup (".");
443 last_nonslash = strlen (file_name) - 1;
445 while (last_nonslash >= 0 && file_name [last_nonslash] == G_DIR_SEPARATOR)
448 if (last_nonslash == -1)
449 /* string only containing slashes */
450 return g_strdup (G_DIR_SEPARATOR_S);
453 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
454 /* string only containing slashes and a drive */
455 return g_strdup (G_DIR_SEPARATOR_S);
456 #endif /* G_OS_WIN32 */
458 base = last_nonslash;
460 while (base >=0 && file_name [base] != G_DIR_SEPARATOR)
464 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
466 #endif /* G_OS_WIN32 */
468 len = last_nonslash - base;
469 retval = g_malloc (len + 1);
470 memcpy (retval, file_name + base + 1, len);
476 g_path_is_absolute (const gchar *file_name)
478 g_return_val_if_fail (file_name != NULL, FALSE);
480 if (file_name[0] == G_DIR_SEPARATOR
482 || file_name[0] == '/'
488 /* Recognize drive letter on native Windows */
489 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && (file_name[2] == G_DIR_SEPARATOR || file_name[2] == '/'))
491 #endif /* G_OS_WIN32 */
496 G_CONST_RETURN gchar*
497 g_path_skip_root (const gchar *file_name)
499 g_return_val_if_fail (file_name != NULL, NULL);
501 #ifdef G_PLATFORM_WIN32
502 /* Skip \\server\share (Win32) or //server/share (Cygwin) */
503 if (file_name[0] == G_DIR_SEPARATOR &&
504 file_name[1] == G_DIR_SEPARATOR &&
509 if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 &&
514 while (file_name[0] && file_name[0] != G_DIR_SEPARATOR)
517 /* Possibly skip a backslash after the share name */
518 if (file_name[0] == G_DIR_SEPARATOR)
521 return (gchar *)file_name;
526 /* Skip initial slashes */
527 if (file_name[0] == G_DIR_SEPARATOR)
529 while (file_name[0] == G_DIR_SEPARATOR)
531 return (gchar *)file_name;
536 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
537 return (gchar *)file_name + 3;
544 g_path_get_dirname (const gchar *file_name)
546 register gchar *base;
549 g_return_val_if_fail (file_name != NULL, NULL);
551 base = strrchr (file_name, G_DIR_SEPARATOR);
553 return g_strdup (".");
554 while (base > file_name && *base == G_DIR_SEPARATOR)
556 len = (guint) 1 + base - file_name;
558 base = g_new (gchar, len + 1);
559 g_memmove (base, file_name, len);
566 g_get_current_dir (void)
568 gchar *buffer = NULL;
570 static gulong max_len = 0;
573 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
575 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
576 * and, if that wasn't bad enough, hangs in doing so.
578 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
579 buffer = g_new (gchar, max_len + 1);
581 dir = getwd (buffer);
582 #else /* !sun || !HAVE_GETCWD */
583 while (max_len < G_MAXULONG / 2)
585 buffer = g_new (gchar, max_len + 1);
587 dir = getcwd (buffer, max_len);
589 if (dir || errno != ERANGE)
595 #endif /* !sun || !HAVE_GETCWD */
597 if (!dir || !*buffer)
599 /* hm, should we g_error() out here?
600 * this can happen if e.g. "./" has mode \0000
602 buffer[0] = G_DIR_SEPARATOR;
606 dir = g_strdup (buffer);
614 * @variable: the environment variable to get.
616 * Returns an environment variable.
618 * Return value: the value of the environment variable, or %NULL if the environment
619 * variable is not found. The returned string may be overwritten by the next call to g_getenv(),
620 * g_setenv() or g_unsetenv().
622 G_CONST_RETURN gchar*
623 g_getenv (const gchar *variable)
626 g_return_val_if_fail (variable != NULL, NULL);
628 return getenv (variable);
630 G_LOCK_DEFINE_STATIC (getenv);
636 static GArray *environs = NULL;
641 g_return_val_if_fail (variable != NULL, NULL);
646 environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
648 /* First we try to find the environment variable inside the already
652 for (i = 0; i < environs->len; i++)
654 env = &g_array_index (environs, struct env_struct, i);
655 if (strcmp (env->key, variable) == 0)
657 g_assert (env->value);
663 /* If not found, we ask the system */
665 system_env = getenv (variable);
672 /* On Windows NT, it is relatively typical that environment variables
673 * contain references to other environment variables. Handle that by
674 * calling ExpandEnvironmentStrings.
677 g_array_set_size (environs, environs->len + 1);
679 env = &g_array_index (environs, struct env_struct, environs->len - 1);
681 /* First check how much space we need */
682 length = ExpandEnvironmentStrings (system_env, dummy, 2);
684 /* Then allocate that much, and actualy do the expansion and insert
685 * the new found pair into our buffer
688 env->value = g_malloc (length);
689 env->key = g_strdup (variable);
691 ExpandEnvironmentStrings (system_env, env->value, length);
700 * @variable: the environment variable to set, must not contain '='.
701 * @value: the value for to set the variable to.
702 * @overwrite: whether to change the variable if it already exists.
704 * Sets an environment variable.
706 * Note that on some systems, the memory used for the variable and its value
707 * can't be reclaimed later.
709 * Returns: %FALSE if the environment variable couldn't be set.
714 g_setenv (const gchar *variable,
719 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
721 result = setenv (variable, value, overwrite);
725 if (!overwrite && g_getenv (variable) != NULL)
728 /* This results in a leak when you overwrite existing
729 * settings. It would be fairly easy to fix this by keeping
730 * our own parallel array or hash table.
732 string = g_strconcat (variable, "=", value, NULL);
733 result = putenv (string);
738 #ifndef HAVE_UNSETENV
739 /* According to the Single Unix Specification, environ is not in
740 * any system header, although unistd.h often declares it.
742 extern char **environ;
747 * @variable: the environment variable to remove, must not contain '='.
749 * Removes an environment variable from the environment.
751 * Note that on some systems, the memory used for the variable and its value
752 * can't be reclaimed. Furthermore, this function can't be guaranteed to operate in a
758 g_unsetenv (const gchar *variable)
761 g_return_if_fail (strchr (variable, '=') == NULL);
768 g_return_if_fail (strchr (variable, '=') == NULL);
770 len = strlen (variable);
772 /* Mess directly with the environ array.
773 * This seems to be the only portable way to do this.
775 * Note that we remove *all* environment entries for
776 * the variable name, not just the first.
781 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
792 G_LOCK_DEFINE_STATIC (g_utils_global);
794 static gchar *g_tmp_dir = NULL;
795 static gchar *g_user_name = NULL;
796 static gchar *g_real_name = NULL;
797 static gchar *g_home_dir = NULL;
799 /* HOLDS: g_utils_global_lock */
801 g_get_any_init (void)
805 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
807 g_tmp_dir = g_strdup (g_getenv ("TMP"));
809 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
815 g_tmp_dir = g_strdup (P_tmpdir);
816 k = strlen (g_tmp_dir);
817 if (k > 1 && g_tmp_dir[k - 1] == G_DIR_SEPARATOR)
818 g_tmp_dir[k - 1] = '\0';
825 g_tmp_dir = g_strdup ("/tmp");
826 #else /* G_OS_WIN32 */
827 g_tmp_dir = g_strdup ("C:\\");
828 #endif /* G_OS_WIN32 */
832 /* We check $HOME first for Win32, though it is a last resort for Unix
833 * where we prefer the results of getpwuid().
835 g_home_dir = g_strdup (g_getenv ("HOME"));
837 /* In case HOME is Unix-style (it happens), convert it to
843 while ((p = strchr (g_home_dir, '/')) != NULL)
849 /* USERPROFILE is probably the closest equivalent to $HOME? */
850 if (getenv ("USERPROFILE") != NULL)
851 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
856 /* At least at some time, HOMEDRIVE and HOMEPATH were used
857 * to point to the home directory, I think. But on Windows
858 * 2000 HOMEDRIVE seems to be equal to SYSTEMDRIVE, and
859 * HOMEPATH is its root "\"?
861 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
863 gchar *homedrive, *homepath;
865 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
866 homepath = g_strdup (g_getenv ("HOMEPATH"));
868 g_home_dir = g_strconcat (homedrive, homepath, NULL);
873 #endif /* G_OS_WIN32 */
877 struct passwd *pw = NULL;
878 gpointer buffer = NULL;
881 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
883 # ifdef _SC_GETPW_R_SIZE_MAX
884 /* This reurns the maximum length */
885 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
889 # else /* _SC_GETPW_R_SIZE_MAX */
891 # endif /* _SC_GETPW_R_SIZE_MAX */
896 buffer = g_malloc (bufsize);
899 # ifdef HAVE_POSIX_GETPWUID_R
900 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
901 error = error < 0 ? errno : error;
902 # else /* HAVE_NONPOSIX_GETPWUID_R */
903 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
904 # if defined(_AIX) || defined(__hpux)
905 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
906 pw = error == 0 ? &pwd : NULL;
908 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
909 error = pw ? 0 : errno;
911 # endif /* HAVE_NONPOSIX_GETPWUID_R */
915 /* we bail out prematurely if the user id can't be found
916 * (should be pretty rare case actually), or if the buffer
917 * should be sufficiently big and lookups are still not
920 if (error == 0 || error == ENOENT)
922 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
926 if (bufsize > 32 * 1024)
928 g_warning ("getpwuid_r(): failed due to: %s.",
937 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
942 pw = getpwuid (getuid ());
947 g_user_name = g_strdup (pw->pw_name);
948 g_real_name = g_strdup (pw->pw_gecos);
950 g_home_dir = g_strdup (pw->pw_dir);
955 #else /* !HAVE_PWD_H */
960 gchar buffer[UNLEN+1];
962 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
964 g_user_name = g_strdup (buffer);
965 g_real_name = g_strdup (buffer);
968 # endif /* G_OS_WIN32 */
970 #endif /* !HAVE_PWD_H */
973 g_home_dir = g_strdup (g_getenv ("HOME"));
976 /* change '\\' in %HOME% to '/' */
977 g_strdelimit (g_home_dir, "\\",'/');
980 g_user_name = g_strdup ("somebody");
982 g_real_name = g_strdup ("Unknown");
987 for (p = g_real_name; *p; p++)
991 p = g_strdup (g_real_name);
992 g_free (g_real_name);
1000 G_CONST_RETURN gchar*
1001 g_get_user_name (void)
1003 G_LOCK (g_utils_global);
1006 G_UNLOCK (g_utils_global);
1011 G_CONST_RETURN gchar*
1012 g_get_real_name (void)
1014 G_LOCK (g_utils_global);
1017 G_UNLOCK (g_utils_global);
1022 /* Return the home directory of the user. If there is a HOME
1023 * environment variable, its value is returned, otherwise use some
1024 * system-dependent way of finding it out. If no home directory can be
1025 * deduced, return NULL.
1028 G_CONST_RETURN gchar*
1029 g_get_home_dir (void)
1031 G_LOCK (g_utils_global);
1034 G_UNLOCK (g_utils_global);
1039 /* Return a directory to be used to store temporary files. This is the
1040 * value of the TMPDIR, TMP or TEMP environment variables (they are
1041 * checked in that order). If none of those exist, use P_tmpdir from
1042 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
1043 * and C:\ on Windows.
1046 G_CONST_RETURN gchar*
1047 g_get_tmp_dir (void)
1049 G_LOCK (g_utils_global);
1052 G_UNLOCK (g_utils_global);
1057 G_LOCK_DEFINE (g_prgname);
1058 static gchar *g_prgname = NULL;
1061 g_get_prgname (void)
1067 G_UNLOCK (g_prgname);
1073 g_set_prgname (const gchar *prgname)
1077 g_prgname = g_strdup (prgname);
1078 G_UNLOCK (g_prgname);
1081 G_LOCK_DEFINE (g_application_name);
1082 static gchar *g_application_name = NULL;
1085 * g_get_application_name:
1087 * Gets a human-readable name for the application, as set by
1088 * g_set_application_name(). This name should be localized if
1089 * possible, and is intended for display to the user. Contrast with
1090 * g_get_prgname(), which gets a non-localized name. If
1091 * g_set_application_name() has not been called, returns the result of
1092 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1095 * Return value: human-readable application name. may return %NULL
1099 G_CONST_RETURN gchar*
1100 g_get_application_name (void)
1104 G_LOCK (g_application_name);
1105 retval = g_application_name;
1106 G_UNLOCK (g_application_name);
1109 return g_get_prgname ();
1115 * g_set_application_name:
1116 * @application_name: localized name of the application
1118 * Sets a human-readable name for the application. This name should be
1119 * localized if possible, and is intended for display to the user.
1120 * Contrast with g_set_prgname(), which sets a non-localized name.
1121 * g_set_prgname() will be called automatically by gtk_init(),
1122 * but g_set_application_name() will not.
1124 * Note that for thread safety reasons, this function can only
1127 * The application name will be used in contexts such as error messages,
1128 * or when displaying an application's name in the task list.
1132 g_set_application_name (const gchar *application_name)
1134 gboolean already_set = FALSE;
1136 G_LOCK (g_application_name);
1137 if (g_application_name)
1140 g_application_name = g_strdup (application_name);
1141 G_UNLOCK (g_application_name);
1144 g_warning ("g_set_application() name called multiple times");
1148 g_direct_hash (gconstpointer v)
1150 return GPOINTER_TO_UINT (v);
1154 g_direct_equal (gconstpointer v1,
1161 g_int_equal (gconstpointer v1,
1164 return *((const gint*) v1) == *((const gint*) v2);
1168 g_int_hash (gconstpointer v)
1170 return *(const gint*) v;
1174 * g_nullify_pointer:
1175 * @nullify_location: the memory address of the pointer.
1177 * Set the pointer at the specified location to %NULL.
1180 g_nullify_pointer (gpointer *nullify_location)
1182 g_return_if_fail (nullify_location != NULL);
1184 *nullify_location = NULL;
1190 * Get the codeset for the current locale.
1192 * Return value: a newly allocated string containing the name
1193 * of the codeset. This string must be freed with g_free().
1196 g_get_codeset (void)
1199 char *result = nl_langinfo (CODESET);
1200 return g_strdup (result);
1202 #ifdef G_PLATFORM_WIN32
1203 return g_strdup_printf ("CP%d", GetACP ());
1205 /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
1207 return g_strdup ("ISO-8859-1");
1214 #include <libintl.h>
1216 #ifdef G_PLATFORM_WIN32
1218 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
1220 static const gchar *
1221 _glib_get_locale_dir (void)
1223 static const gchar *cache = NULL;
1225 cache = g_win32_get_package_installation_subdirectory
1226 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
1231 #undef GLIB_LOCALE_DIR
1232 #define GLIB_LOCALE_DIR _glib_get_locale_dir ()
1234 #endif /* G_PLATFORM_WIN32 */
1236 G_CONST_RETURN gchar *
1237 _glib_gettext (const gchar *str)
1239 static gboolean _glib_gettext_initialized = FALSE;
1241 if (!_glib_gettext_initialized)
1243 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1244 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1245 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1247 _glib_gettext_initialized = TRUE;
1250 return dgettext (GETTEXT_PACKAGE, str);
1253 #endif /* ENABLE_NLS */