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 /* G_IS_DIR_SEPARATOR probably should be made public in GLib 2.4 */
88 #define G_IS_DIR_SEPARATOR(c) (c == G_DIR_SEPARATOR || c == '/')
90 #define G_IS_DIR_SEPARATOR(c) (c == G_DIR_SEPARATOR)
93 const guint glib_major_version = GLIB_MAJOR_VERSION;
94 const guint glib_minor_version = GLIB_MINOR_VERSION;
95 const guint glib_micro_version = GLIB_MICRO_VERSION;
96 const guint glib_interface_age = GLIB_INTERFACE_AGE;
97 const guint glib_binary_age = GLIB_BINARY_AGE;
99 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
101 g_memmove (gpointer dest, gconstpointer src, gulong len)
103 gchar* destptr = dest;
104 const gchar* srcptr = src;
105 if (src + len < dest || dest + len < src)
107 bcopy (src, dest, len);
110 else if (dest <= src)
113 *(destptr++) = *(srcptr++);
120 *(--destptr) = *(--srcptr);
123 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
126 g_atexit (GVoidFunc func)
129 const gchar *error = NULL;
131 /* keep this in sync with glib.h */
133 #ifdef G_NATIVE_ATEXIT
134 result = ATEXIT (func);
136 error = g_strerror (errno);
137 #elif defined (HAVE_ATEXIT)
138 # ifdef NeXT /* @#%@! NeXTStep */
139 result = !atexit ((void (*)(void)) func);
141 error = g_strerror (errno);
143 result = atexit ((void (*)(void)) func);
145 error = g_strerror (errno);
147 #elif defined (HAVE_ON_EXIT)
148 result = on_exit ((void (*)(int, void *)) func, NULL);
150 error = g_strerror (errno);
153 error = "no implementation";
154 #endif /* G_NATIVE_ATEXIT */
157 g_error ("Could not register atexit() function: %s", error);
160 /* Based on execvp() from GNU Libc.
161 * Some of this code is cut-and-pasted into gspawn.c
165 my_strchrnul (const gchar *str, gchar c)
167 gchar *p = (gchar*)str;
168 while (*p && (*p != c))
176 static gchar *inner_find_program_in_path (const gchar *program);
179 g_find_program_in_path (const gchar *program)
181 const gchar *last_dot = strrchr (program, '.');
183 if (last_dot == NULL || strchr (last_dot, '\\') != NULL)
185 const gint program_length = strlen (program);
186 const gchar *pathext = getenv ("PATHEXT");
188 gchar *decorated_program;
192 pathext = ".com;.exe;.bat";
198 p = my_strchrnul (pathext, ';');
200 decorated_program = g_malloc (program_length + (p-pathext) + 1);
201 memcpy (decorated_program, program, program_length);
202 memcpy (decorated_program+program_length, pathext, p-pathext);
203 decorated_program [program_length + (p-pathext)] = '\0';
205 retval = inner_find_program_in_path (decorated_program);
206 g_free (decorated_program);
210 } while (*p++ != '\0');
214 return inner_find_program_in_path (program);
217 #define g_find_program_in_path inner_find_program_in_path
221 * g_find_program_in_path:
222 * @program: a program name
224 * Locates the first executable named @program in the user's path, in the
225 * same way that execvp() would locate it. Returns an allocated string
226 * with the absolute path name, or NULL if the program is not found in
227 * the path. If @program is already an absolute path, returns a copy of
228 * @program if @program exists and is executable, and NULL otherwise.
230 * On Windows, if @program does not have a file type suffix, tries to
231 * append the suffixes in the PATHEXT environment variable (if that
232 * doesn't exists, the suffixes .com, .exe, and .bat) in turn, and
233 * then look for the resulting file name in the same way as
234 * CreateProcess() would. This means first in the directory where the
235 * program was loaded from, then in the current directory, then in the
236 * Windows 32-bit system directory, then in the Windows directory, and
237 * finally in the directories in the PATH environment variable. If
238 * the program is found, the return value contains the full name
239 * including the type suffix.
241 * Return value: absolute path, or NULL
247 g_find_program_in_path (const gchar *program)
249 const gchar *path, *p;
250 gchar *name, *freeme;
257 g_return_val_if_fail (program != NULL, NULL);
259 /* If it is an absolute path, or a relative path including subdirectories,
260 * don't look in PATH.
262 if (g_path_is_absolute (program)
263 || strchr (program, G_DIR_SEPARATOR) != NULL
265 || strchr (program, '/') != NULL
269 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
270 return g_strdup (program);
275 path = g_getenv ("PATH");
279 /* There is no `PATH' in the environment. The default
280 * search path in GNU libc is the current directory followed by
281 * the path `confstr' returns for `_CS_PATH'.
284 /* In GLib we put . last, for security, and don't use the
285 * unportable confstr(); UNIX98 does not actually specify
286 * what to search if PATH is unset. POSIX may, dunno.
289 path = "/bin:/usr/bin:.";
294 gchar moddir[MAXPATHLEN], sysdir[MAXPATHLEN], windir[MAXPATHLEN];
296 GetModuleFileName (NULL, moddir, sizeof (moddir));
297 tmp = g_path_get_dirname (moddir);
298 GetSystemDirectory (sysdir, sizeof (sysdir));
299 GetWindowsDirectory (windir, sizeof (windir));
300 path_tmp = g_strconcat (tmp, ";.;", sysdir, ";", windir,
301 (path != NULL ? ";" : NULL),
302 (path != NULL ? path : NULL),
309 len = strlen (program) + 1;
310 pathlen = strlen (path);
311 freeme = name = g_malloc (pathlen + len + 1);
313 /* Copy the file name at the top, including '\0' */
314 memcpy (name + pathlen + 1, program, len);
315 name = name + pathlen;
316 /* And add the slash before the filename */
317 *name = G_DIR_SEPARATOR;
325 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
328 /* Two adjacent colons, or a colon at the beginning or the end
329 * of `PATH' means to search the current directory.
333 startp = memcpy (name - (p - path), path, p - path);
335 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
338 ret = g_strdup (startp);
346 while (*p++ != '\0');
357 g_parse_debug_string (const gchar *string,
358 const GDebugKey *keys,
364 g_return_val_if_fail (string != NULL, 0);
366 if (!g_ascii_strcasecmp (string, "all"))
368 for (i=0; i<nkeys; i++)
369 result |= keys[i].value;
373 const gchar *p = string;
375 gboolean done = FALSE;
386 for (i=0; i<nkeys; i++)
387 if (g_ascii_strncasecmp(keys[i].key, p, q - p) == 0 &&
388 keys[i].key[q - p] == '\0')
389 result |= keys[i].value;
400 * @file_name: the name of the file.
402 * Gets the name of the file without any leading directory components.
403 * It returns a pointer into the given file name string.
405 * Return value: the name of the file without any leading directory components.
407 * Deprecated: Use g_path_get_basename() instead, but notice that
408 * g_path_get_basename() allocates new memory for the returned string, unlike
409 * this function which returns a pointer into the argument.
411 G_CONST_RETURN gchar*
412 g_basename (const gchar *file_name)
414 register gchar *base;
416 g_return_val_if_fail (file_name != NULL, NULL);
418 base = strrchr (file_name, G_DIR_SEPARATOR);
422 gchar *q = strrchr (file_name, '/');
423 if (base == NULL || (q != NULL && q > base))
432 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
433 return (gchar*) file_name + 2;
434 #endif /* G_OS_WIN32 */
436 return (gchar*) file_name;
440 * g_path_get_basename:
441 * @file_name: the name of the file.
443 * Gets the last component of the filename. If @file_name ends with a
444 * directory separator it gets the component before the last slash. If
445 * @file_name consists only of directory separators (and on Windows,
446 * possibly a drive letter), a single separator is returned. If
447 * @file_name is empty, it gets ".".
449 * Return value: a newly allocated string containing the last component of
453 g_path_get_basename (const gchar *file_name)
455 register gssize base;
456 register gssize last_nonslash;
460 g_return_val_if_fail (file_name != NULL, NULL);
462 if (file_name[0] == '\0')
464 return g_strdup (".");
466 last_nonslash = strlen (file_name) - 1;
468 while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
471 if (last_nonslash == -1)
472 /* string only containing slashes */
473 return g_strdup (G_DIR_SEPARATOR_S);
476 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
477 /* string only containing slashes and a drive */
478 return g_strdup (G_DIR_SEPARATOR_S);
479 #endif /* G_OS_WIN32 */
481 base = last_nonslash;
483 while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
487 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
489 #endif /* G_OS_WIN32 */
491 len = last_nonslash - base;
492 retval = g_malloc (len + 1);
493 memcpy (retval, file_name + base + 1, len);
499 g_path_is_absolute (const gchar *file_name)
501 g_return_val_if_fail (file_name != NULL, FALSE);
503 if (G_IS_DIR_SEPARATOR (file_name[0]))
507 /* Recognize drive letter on native Windows */
508 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
510 #endif /* G_OS_WIN32 */
515 G_CONST_RETURN gchar*
516 g_path_skip_root (const gchar *file_name)
518 g_return_val_if_fail (file_name != NULL, NULL);
520 #ifdef G_PLATFORM_WIN32
521 /* Skip \\server\share or //server/share */
522 if (G_IS_DIR_SEPARATOR (file_name[0]) &&
523 G_IS_DIR_SEPARATOR (file_name[1]) &&
528 p = strchr (file_name + 2, G_DIR_SEPARATOR);
531 gchar *q = strchr (file_name + 2, '/');
532 if (p == NULL || (q != NULL && q < p))
542 while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
545 /* Possibly skip a backslash after the share name */
546 if (G_IS_DIR_SEPARATOR (file_name[0]))
549 return (gchar *)file_name;
554 /* Skip initial slashes */
555 if (G_IS_DIR_SEPARATOR (file_name[0]))
557 while (G_IS_DIR_SEPARATOR (file_name[0]))
559 return (gchar *)file_name;
564 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
565 return (gchar *)file_name + 3;
572 g_path_get_dirname (const gchar *file_name)
574 register gchar *base;
577 g_return_val_if_fail (file_name != NULL, NULL);
579 base = strrchr (file_name, G_DIR_SEPARATOR);
582 gchar *q = strrchr (file_name, '/');
583 if (base == NULL || (q != NULL && q > base))
590 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
592 gchar drive_colon_dot[4];
594 drive_colon_dot[0] = file_name[0];
595 drive_colon_dot[1] = ':';
596 drive_colon_dot[2] = '.';
597 drive_colon_dot[3] = '\0';
599 return g_strdup (drive_colon_dot);
602 return g_strdup (".");
605 while (base > file_name && G_IS_DIR_SEPARATOR (*base))
609 if (base == file_name + 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
613 len = (guint) 1 + base - file_name;
615 base = g_new (gchar, len + 1);
616 g_memmove (base, file_name, len);
623 g_get_current_dir (void)
625 gchar *buffer = NULL;
627 static gulong max_len = 0;
630 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
632 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
633 * and, if that wasn't bad enough, hangs in doing so.
635 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
636 buffer = g_new (gchar, max_len + 1);
638 dir = getwd (buffer);
639 #else /* !sun || !HAVE_GETCWD */
640 while (max_len < G_MAXULONG / 2)
642 buffer = g_new (gchar, max_len + 1);
644 dir = getcwd (buffer, max_len);
646 if (dir || errno != ERANGE)
652 #endif /* !sun || !HAVE_GETCWD */
654 if (!dir || !*buffer)
656 /* hm, should we g_error() out here?
657 * this can happen if e.g. "./" has mode \0000
659 buffer[0] = G_DIR_SEPARATOR;
663 dir = g_strdup (buffer);
671 * @variable: the environment variable to get.
673 * Returns an environment variable.
675 * Return value: the value of the environment variable, or %NULL if the environment
676 * variable is not found. The returned string may be overwritten by the next call to g_getenv(),
677 * g_setenv() or g_unsetenv().
679 G_CONST_RETURN gchar*
680 g_getenv (const gchar *variable)
683 g_return_val_if_fail (variable != NULL, NULL);
685 return getenv (variable);
693 g_return_val_if_fail (variable != NULL, NULL);
695 system_env = getenv (variable);
699 /* On Windows NT, it is relatively typical that environment
700 * variables contain references to other environment variables. If
701 * so, use ExpandEnvironmentStrings(). (If all software was written
702 * in the best possible way, such environment variables would be
703 * stored in the Registry as REG_EXPAND_SZ type values, and would
704 * then get automatically expanded before the program sees them. But
705 * there is broken software that stores environment variables as
706 * REG_SZ values even if they contain references to other
707 * environment variables.
710 if (strchr (system_env, '%') == NULL)
712 /* No reference to other variable(s), return value as such. */
716 /* First check how much space we need */
717 length = ExpandEnvironmentStrings (system_env, dummy, 2);
719 expanded_env = g_malloc (length);
721 ExpandEnvironmentStrings (system_env, expanded_env, length);
723 quark = g_quark_from_string (expanded_env);
724 g_free (expanded_env);
726 return g_quark_to_string (quark);
732 * @variable: the environment variable to set, must not contain '='.
733 * @value: the value for to set the variable to.
734 * @overwrite: whether to change the variable if it already exists.
736 * Sets an environment variable.
738 * Note that on some systems, the memory used for the variable and its value
739 * can't be reclaimed later.
741 * Returns: %FALSE if the environment variable couldn't be set.
746 g_setenv (const gchar *variable,
755 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
758 result = setenv (variable, value, overwrite);
760 if (!overwrite && g_getenv (variable) != NULL)
763 /* This results in a leak when you overwrite existing
764 * settings. It would be fairly easy to fix this by keeping
765 * our own parallel array or hash table.
767 string = g_strconcat (variable, "=", value, NULL);
768 result = putenv (string);
773 #ifndef HAVE_UNSETENV
774 /* According to the Single Unix Specification, environ is not in
775 * any system header, although unistd.h often declares it.
777 extern char **environ;
782 * @variable: the environment variable to remove, must not contain '='.
784 * Removes an environment variable from the environment.
786 * Note that on some systems, the memory used for the variable and its value
787 * can't be reclaimed. Furthermore, this function can't be guaranteed to operate in a
793 g_unsetenv (const gchar *variable)
796 g_return_if_fail (strchr (variable, '=') == NULL);
803 g_return_if_fail (strchr (variable, '=') == NULL);
805 len = strlen (variable);
807 /* Mess directly with the environ array.
808 * This seems to be the only portable way to do this.
810 * Note that we remove *all* environment entries for
811 * the variable name, not just the first.
816 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
827 G_LOCK_DEFINE_STATIC (g_utils_global);
829 static gchar *g_tmp_dir = NULL;
830 static gchar *g_user_name = NULL;
831 static gchar *g_real_name = NULL;
832 static gchar *g_home_dir = NULL;
834 /* HOLDS: g_utils_global_lock */
836 g_get_any_init (void)
840 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
842 g_tmp_dir = g_strdup (g_getenv ("TMP"));
844 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
850 g_tmp_dir = g_strdup (P_tmpdir);
851 k = strlen (g_tmp_dir);
852 if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
853 g_tmp_dir[k - 1] = '\0';
860 g_tmp_dir = g_strdup ("/tmp");
861 #else /* G_OS_WIN32 */
862 g_tmp_dir = g_strdup ("C:\\");
863 #endif /* G_OS_WIN32 */
867 /* We check $HOME first for Win32, though it is a last resort for Unix
868 * where we prefer the results of getpwuid().
870 g_home_dir = g_strdup (g_getenv ("HOME"));
872 /* In case HOME is Unix-style (it happens), convert it to
878 while ((p = strchr (g_home_dir, '/')) != NULL)
884 /* USERPROFILE is probably the closest equivalent to $HOME? */
885 if (getenv ("USERPROFILE") != NULL)
886 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
891 /* At least at some time, HOMEDRIVE and HOMEPATH were used
892 * to point to the home directory, I think. But on Windows
893 * 2000 HOMEDRIVE seems to be equal to SYSTEMDRIVE, and
894 * HOMEPATH is its root "\"?
896 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
898 gchar *homedrive, *homepath;
900 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
901 homepath = g_strdup (g_getenv ("HOMEPATH"));
903 g_home_dir = g_strconcat (homedrive, homepath, NULL);
908 #endif /* G_OS_WIN32 */
912 struct passwd *pw = NULL;
913 gpointer buffer = NULL;
916 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
918 # ifdef _SC_GETPW_R_SIZE_MAX
919 /* This reurns the maximum length */
920 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
924 # else /* _SC_GETPW_R_SIZE_MAX */
926 # endif /* _SC_GETPW_R_SIZE_MAX */
931 buffer = g_malloc (bufsize);
934 # ifdef HAVE_POSIX_GETPWUID_R
935 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
936 error = error < 0 ? errno : error;
937 # else /* HAVE_NONPOSIX_GETPWUID_R */
938 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
939 # if defined(_AIX) || defined(__hpux)
940 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
941 pw = error == 0 ? &pwd : NULL;
943 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
944 error = pw ? 0 : errno;
946 # endif /* HAVE_NONPOSIX_GETPWUID_R */
950 /* we bail out prematurely if the user id can't be found
951 * (should be pretty rare case actually), or if the buffer
952 * should be sufficiently big and lookups are still not
955 if (error == 0 || error == ENOENT)
957 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
961 if (bufsize > 32 * 1024)
963 g_warning ("getpwuid_r(): failed due to: %s.",
972 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
977 pw = getpwuid (getuid ());
982 g_user_name = g_strdup (pw->pw_name);
984 if (pw->pw_gecos && *pw->pw_gecos != '\0')
986 gchar **gecos_fields;
989 /* split the gecos field and substitute '&' */
990 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
991 name_parts = g_strsplit (gecos_fields[0], "&", 0);
992 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
993 g_real_name = g_strjoinv (pw->pw_name, name_parts);
994 g_strfreev (gecos_fields);
995 g_strfreev (name_parts);
999 g_home_dir = g_strdup (pw->pw_dir);
1004 #else /* !HAVE_PWD_H */
1008 guint len = UNLEN+1;
1009 gchar buffer[UNLEN+1];
1011 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
1013 g_user_name = g_strdup (buffer);
1014 g_real_name = g_strdup (buffer);
1017 # endif /* G_OS_WIN32 */
1019 #endif /* !HAVE_PWD_H */
1022 g_home_dir = g_strdup (g_getenv ("HOME"));
1025 /* change '\\' in %HOME% to '/' */
1026 g_strdelimit (g_home_dir, "\\",'/');
1029 g_user_name = g_strdup ("somebody");
1031 g_real_name = g_strdup ("Unknown");
1035 G_CONST_RETURN gchar*
1036 g_get_user_name (void)
1038 G_LOCK (g_utils_global);
1041 G_UNLOCK (g_utils_global);
1046 G_CONST_RETURN gchar*
1047 g_get_real_name (void)
1049 G_LOCK (g_utils_global);
1052 G_UNLOCK (g_utils_global);
1057 /* Return the home directory of the user. If there is a HOME
1058 * environment variable, its value is returned, otherwise use some
1059 * system-dependent way of finding it out. If no home directory can be
1060 * deduced, return NULL.
1063 G_CONST_RETURN gchar*
1064 g_get_home_dir (void)
1066 G_LOCK (g_utils_global);
1069 G_UNLOCK (g_utils_global);
1074 /* Return a directory to be used to store temporary files. This is the
1075 * value of the TMPDIR, TMP or TEMP environment variables (they are
1076 * checked in that order). If none of those exist, use P_tmpdir from
1077 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
1078 * and C:\ on Windows.
1081 G_CONST_RETURN gchar*
1082 g_get_tmp_dir (void)
1084 G_LOCK (g_utils_global);
1087 G_UNLOCK (g_utils_global);
1092 G_LOCK_DEFINE_STATIC (g_prgname);
1093 static gchar *g_prgname = NULL;
1096 g_get_prgname (void)
1102 G_UNLOCK (g_prgname);
1108 g_set_prgname (const gchar *prgname)
1112 g_prgname = g_strdup (prgname);
1113 G_UNLOCK (g_prgname);
1116 G_LOCK_DEFINE_STATIC (g_application_name);
1117 static gchar *g_application_name = NULL;
1120 * g_get_application_name:
1122 * Gets a human-readable name for the application, as set by
1123 * g_set_application_name(). This name should be localized if
1124 * possible, and is intended for display to the user. Contrast with
1125 * g_get_prgname(), which gets a non-localized name. If
1126 * g_set_application_name() has not been called, returns the result of
1127 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1130 * Return value: human-readable application name. may return %NULL
1134 G_CONST_RETURN gchar*
1135 g_get_application_name (void)
1139 G_LOCK (g_application_name);
1140 retval = g_application_name;
1141 G_UNLOCK (g_application_name);
1144 return g_get_prgname ();
1150 * g_set_application_name:
1151 * @application_name: localized name of the application
1153 * Sets a human-readable name for the application. This name should be
1154 * localized if possible, and is intended for display to the user.
1155 * Contrast with g_set_prgname(), which sets a non-localized name.
1156 * g_set_prgname() will be called automatically by gtk_init(),
1157 * but g_set_application_name() will not.
1159 * Note that for thread safety reasons, this function can only
1162 * The application name will be used in contexts such as error messages,
1163 * or when displaying an application's name in the task list.
1167 g_set_application_name (const gchar *application_name)
1169 gboolean already_set = FALSE;
1171 G_LOCK (g_application_name);
1172 if (g_application_name)
1175 g_application_name = g_strdup (application_name);
1176 G_UNLOCK (g_application_name);
1179 g_warning ("g_set_application() name called multiple times");
1183 g_direct_hash (gconstpointer v)
1185 return GPOINTER_TO_UINT (v);
1189 g_direct_equal (gconstpointer v1,
1196 g_int_equal (gconstpointer v1,
1199 return *((const gint*) v1) == *((const gint*) v2);
1203 g_int_hash (gconstpointer v)
1205 return *(const gint*) v;
1209 * g_nullify_pointer:
1210 * @nullify_location: the memory address of the pointer.
1212 * Set the pointer at the specified location to %NULL.
1215 g_nullify_pointer (gpointer *nullify_location)
1217 g_return_if_fail (nullify_location != NULL);
1219 *nullify_location = NULL;
1225 * Get the codeset for the current locale.
1227 * Return value: a newly allocated string containing the name
1228 * of the codeset. This string must be freed with g_free().
1231 g_get_codeset (void)
1235 g_get_charset (&charset);
1242 #include <libintl.h>
1244 #ifdef G_PLATFORM_WIN32
1246 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
1248 static const gchar *
1249 _glib_get_locale_dir (void)
1251 static const gchar *cache = NULL;
1253 cache = g_win32_get_package_installation_subdirectory
1254 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
1259 #undef GLIB_LOCALE_DIR
1260 #define GLIB_LOCALE_DIR _glib_get_locale_dir ()
1262 #endif /* G_PLATFORM_WIN32 */
1264 G_CONST_RETURN gchar *
1265 _glib_gettext (const gchar *str)
1267 static gboolean _glib_gettext_initialized = FALSE;
1269 if (!_glib_gettext_initialized)
1271 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1272 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1273 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1275 _glib_gettext_initialized = TRUE;
1278 return dgettext (GETTEXT_PACKAGE, str);
1281 #endif /* ENABLE_NLS */