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))
588 return g_strdup (".");
589 while (base > file_name && G_IS_DIR_SEPARATOR (*base))
591 len = (guint) 1 + base - file_name;
593 base = g_new (gchar, len + 1);
594 g_memmove (base, file_name, len);
601 g_get_current_dir (void)
603 gchar *buffer = NULL;
605 static gulong max_len = 0;
608 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
610 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
611 * and, if that wasn't bad enough, hangs in doing so.
613 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
614 buffer = g_new (gchar, max_len + 1);
616 dir = getwd (buffer);
617 #else /* !sun || !HAVE_GETCWD */
618 while (max_len < G_MAXULONG / 2)
620 buffer = g_new (gchar, max_len + 1);
622 dir = getcwd (buffer, max_len);
624 if (dir || errno != ERANGE)
630 #endif /* !sun || !HAVE_GETCWD */
632 if (!dir || !*buffer)
634 /* hm, should we g_error() out here?
635 * this can happen if e.g. "./" has mode \0000
637 buffer[0] = G_DIR_SEPARATOR;
641 dir = g_strdup (buffer);
649 * @variable: the environment variable to get.
651 * Returns an environment variable.
653 * Return value: the value of the environment variable, or %NULL if the environment
654 * variable is not found. The returned string may be overwritten by the next call to g_getenv(),
655 * g_setenv() or g_unsetenv().
657 G_CONST_RETURN gchar*
658 g_getenv (const gchar *variable)
661 g_return_val_if_fail (variable != NULL, NULL);
663 return getenv (variable);
671 g_return_val_if_fail (variable != NULL, NULL);
673 system_env = getenv (variable);
677 /* On Windows NT, it is relatively typical that environment
678 * variables contain references to other environment variables. If
679 * so, use ExpandEnvironmentStrings(). (If all software was written
680 * in the best possible way, such environment variables would be
681 * stored in the Registry as REG_EXPAND_SZ type values, and would
682 * then get automatically expanded before the program sees them. But
683 * there is broken software that stores environment variables as
684 * REG_SZ values even if they contain references to other
685 * environment variables.
688 if (strchr (system_env, '%') == NULL)
690 /* No reference to other variable(s), return value as such. */
694 /* First check how much space we need */
695 length = ExpandEnvironmentStrings (system_env, dummy, 2);
697 expanded_env = g_malloc (length);
699 ExpandEnvironmentStrings (system_env, expanded_env, length);
701 quark = g_quark_from_string (expanded_env);
702 g_free (expanded_env);
704 return g_quark_to_string (quark);
710 * @variable: the environment variable to set, must not contain '='.
711 * @value: the value for to set the variable to.
712 * @overwrite: whether to change the variable if it already exists.
714 * Sets an environment variable.
716 * Note that on some systems, the memory used for the variable and its value
717 * can't be reclaimed later.
719 * Returns: %FALSE if the environment variable couldn't be set.
724 g_setenv (const gchar *variable,
733 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
736 result = setenv (variable, value, overwrite);
738 if (!overwrite && g_getenv (variable) != NULL)
741 /* This results in a leak when you overwrite existing
742 * settings. It would be fairly easy to fix this by keeping
743 * our own parallel array or hash table.
745 string = g_strconcat (variable, "=", value, NULL);
746 result = putenv (string);
751 #ifndef HAVE_UNSETENV
752 /* According to the Single Unix Specification, environ is not in
753 * any system header, although unistd.h often declares it.
755 extern char **environ;
760 * @variable: the environment variable to remove, must not contain '='.
762 * Removes an environment variable from the environment.
764 * Note that on some systems, the memory used for the variable and its value
765 * can't be reclaimed. Furthermore, this function can't be guaranteed to operate in a
771 g_unsetenv (const gchar *variable)
774 g_return_if_fail (strchr (variable, '=') == NULL);
781 g_return_if_fail (strchr (variable, '=') == NULL);
783 len = strlen (variable);
785 /* Mess directly with the environ array.
786 * This seems to be the only portable way to do this.
788 * Note that we remove *all* environment entries for
789 * the variable name, not just the first.
794 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
805 G_LOCK_DEFINE_STATIC (g_utils_global);
807 static gchar *g_tmp_dir = NULL;
808 static gchar *g_user_name = NULL;
809 static gchar *g_real_name = NULL;
810 static gchar *g_home_dir = NULL;
812 /* HOLDS: g_utils_global_lock */
814 g_get_any_init (void)
818 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
820 g_tmp_dir = g_strdup (g_getenv ("TMP"));
822 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
828 g_tmp_dir = g_strdup (P_tmpdir);
829 k = strlen (g_tmp_dir);
830 if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
831 g_tmp_dir[k - 1] = '\0';
838 g_tmp_dir = g_strdup ("/tmp");
839 #else /* G_OS_WIN32 */
840 g_tmp_dir = g_strdup ("C:\\");
841 #endif /* G_OS_WIN32 */
845 /* We check $HOME first for Win32, though it is a last resort for Unix
846 * where we prefer the results of getpwuid().
848 g_home_dir = g_strdup (g_getenv ("HOME"));
850 /* In case HOME is Unix-style (it happens), convert it to
856 while ((p = strchr (g_home_dir, '/')) != NULL)
862 /* USERPROFILE is probably the closest equivalent to $HOME? */
863 if (getenv ("USERPROFILE") != NULL)
864 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
869 /* At least at some time, HOMEDRIVE and HOMEPATH were used
870 * to point to the home directory, I think. But on Windows
871 * 2000 HOMEDRIVE seems to be equal to SYSTEMDRIVE, and
872 * HOMEPATH is its root "\"?
874 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
876 gchar *homedrive, *homepath;
878 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
879 homepath = g_strdup (g_getenv ("HOMEPATH"));
881 g_home_dir = g_strconcat (homedrive, homepath, NULL);
886 #endif /* G_OS_WIN32 */
890 struct passwd *pw = NULL;
891 gpointer buffer = NULL;
894 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
896 # ifdef _SC_GETPW_R_SIZE_MAX
897 /* This reurns the maximum length */
898 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
902 # else /* _SC_GETPW_R_SIZE_MAX */
904 # endif /* _SC_GETPW_R_SIZE_MAX */
909 buffer = g_malloc (bufsize);
912 # ifdef HAVE_POSIX_GETPWUID_R
913 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
914 error = error < 0 ? errno : error;
915 # else /* HAVE_NONPOSIX_GETPWUID_R */
916 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
917 # if defined(_AIX) || defined(__hpux)
918 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
919 pw = error == 0 ? &pwd : NULL;
921 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
922 error = pw ? 0 : errno;
924 # endif /* HAVE_NONPOSIX_GETPWUID_R */
928 /* we bail out prematurely if the user id can't be found
929 * (should be pretty rare case actually), or if the buffer
930 * should be sufficiently big and lookups are still not
933 if (error == 0 || error == ENOENT)
935 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
939 if (bufsize > 32 * 1024)
941 g_warning ("getpwuid_r(): failed due to: %s.",
950 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
955 pw = getpwuid (getuid ());
960 g_user_name = g_strdup (pw->pw_name);
964 gchar **gecos_fields;
967 /* split the gecos field and substitute '&' */
968 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
969 name_parts = g_strsplit (gecos_fields[0], "&", 0);
970 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
971 g_real_name = g_strjoinv (pw->pw_name, name_parts);
972 g_strfreev (gecos_fields);
973 g_strfreev (name_parts);
977 g_home_dir = g_strdup (pw->pw_dir);
982 #else /* !HAVE_PWD_H */
987 gchar buffer[UNLEN+1];
989 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
991 g_user_name = g_strdup (buffer);
992 g_real_name = g_strdup (buffer);
995 # endif /* G_OS_WIN32 */
997 #endif /* !HAVE_PWD_H */
1000 g_home_dir = g_strdup (g_getenv ("HOME"));
1003 /* change '\\' in %HOME% to '/' */
1004 g_strdelimit (g_home_dir, "\\",'/');
1007 g_user_name = g_strdup ("somebody");
1009 g_real_name = g_strdup ("Unknown");
1013 G_CONST_RETURN gchar*
1014 g_get_user_name (void)
1016 G_LOCK (g_utils_global);
1019 G_UNLOCK (g_utils_global);
1024 G_CONST_RETURN gchar*
1025 g_get_real_name (void)
1027 G_LOCK (g_utils_global);
1030 G_UNLOCK (g_utils_global);
1035 /* Return the home directory of the user. If there is a HOME
1036 * environment variable, its value is returned, otherwise use some
1037 * system-dependent way of finding it out. If no home directory can be
1038 * deduced, return NULL.
1041 G_CONST_RETURN gchar*
1042 g_get_home_dir (void)
1044 G_LOCK (g_utils_global);
1047 G_UNLOCK (g_utils_global);
1052 /* Return a directory to be used to store temporary files. This is the
1053 * value of the TMPDIR, TMP or TEMP environment variables (they are
1054 * checked in that order). If none of those exist, use P_tmpdir from
1055 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
1056 * and C:\ on Windows.
1059 G_CONST_RETURN gchar*
1060 g_get_tmp_dir (void)
1062 G_LOCK (g_utils_global);
1065 G_UNLOCK (g_utils_global);
1070 G_LOCK_DEFINE_STATIC (g_prgname);
1071 static gchar *g_prgname = NULL;
1074 g_get_prgname (void)
1080 G_UNLOCK (g_prgname);
1086 g_set_prgname (const gchar *prgname)
1090 g_prgname = g_strdup (prgname);
1091 G_UNLOCK (g_prgname);
1094 G_LOCK_DEFINE_STATIC (g_application_name);
1095 static gchar *g_application_name = NULL;
1098 * g_get_application_name:
1100 * Gets a human-readable name for the application, as set by
1101 * g_set_application_name(). This name should be localized if
1102 * possible, and is intended for display to the user. Contrast with
1103 * g_get_prgname(), which gets a non-localized name. If
1104 * g_set_application_name() has not been called, returns the result of
1105 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1108 * Return value: human-readable application name. may return %NULL
1112 G_CONST_RETURN gchar*
1113 g_get_application_name (void)
1117 G_LOCK (g_application_name);
1118 retval = g_application_name;
1119 G_UNLOCK (g_application_name);
1122 return g_get_prgname ();
1128 * g_set_application_name:
1129 * @application_name: localized name of the application
1131 * Sets a human-readable name for the application. This name should be
1132 * localized if possible, and is intended for display to the user.
1133 * Contrast with g_set_prgname(), which sets a non-localized name.
1134 * g_set_prgname() will be called automatically by gtk_init(),
1135 * but g_set_application_name() will not.
1137 * Note that for thread safety reasons, this function can only
1140 * The application name will be used in contexts such as error messages,
1141 * or when displaying an application's name in the task list.
1145 g_set_application_name (const gchar *application_name)
1147 gboolean already_set = FALSE;
1149 G_LOCK (g_application_name);
1150 if (g_application_name)
1153 g_application_name = g_strdup (application_name);
1154 G_UNLOCK (g_application_name);
1157 g_warning ("g_set_application() name called multiple times");
1161 g_direct_hash (gconstpointer v)
1163 return GPOINTER_TO_UINT (v);
1167 g_direct_equal (gconstpointer v1,
1174 g_int_equal (gconstpointer v1,
1177 return *((const gint*) v1) == *((const gint*) v2);
1181 g_int_hash (gconstpointer v)
1183 return *(const gint*) v;
1187 * g_nullify_pointer:
1188 * @nullify_location: the memory address of the pointer.
1190 * Set the pointer at the specified location to %NULL.
1193 g_nullify_pointer (gpointer *nullify_location)
1195 g_return_if_fail (nullify_location != NULL);
1197 *nullify_location = NULL;
1203 * Get the codeset for the current locale.
1205 * Return value: a newly allocated string containing the name
1206 * of the codeset. This string must be freed with g_free().
1209 g_get_codeset (void)
1212 char *result = nl_langinfo (CODESET);
1213 return g_strdup (result);
1215 #ifdef G_PLATFORM_WIN32
1216 return g_strdup_printf ("CP%d", GetACP ());
1218 /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
1220 return g_strdup ("ISO-8859-1");
1227 #include <libintl.h>
1229 #ifdef G_PLATFORM_WIN32
1231 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
1233 static const gchar *
1234 _glib_get_locale_dir (void)
1236 static const gchar *cache = NULL;
1238 cache = g_win32_get_package_installation_subdirectory
1239 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
1244 #undef GLIB_LOCALE_DIR
1245 #define GLIB_LOCALE_DIR _glib_get_locale_dir ()
1247 #endif /* G_PLATFORM_WIN32 */
1249 G_CONST_RETURN gchar *
1250 _glib_gettext (const gchar *str)
1252 static gboolean _glib_gettext_initialized = FALSE;
1254 if (!_glib_gettext_initialized)
1256 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1257 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1258 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1260 _glib_gettext_initialized = TRUE;
1263 return dgettext (GETTEXT_PACKAGE, str);
1266 #endif /* ENABLE_NLS */