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;
100 * glib_check_version:
101 * @required_major: the required major version.
102 * @required_minor: the required major version.
103 * @required_micro: the required major version.
105 * Checks that the GLib library in use is compatible with the
106 * given version. Generally you would pass in the constants
107 * #GLIB_MAJOR_VERSION, #GLIB_MINOR_VERSION, #GLIB_MICRO_VERSION
108 * as the three arguments to this function; that produces
109 * a check that the library in use is compatible with
110 * the version of GLib the application or module was compiled
113 * Compatibility is defined by two things: first the version
114 * of the running library is newer than the version
115 * @required_major.required_minor.@required_micro. Second
116 * the running library must be binary compatible with the
117 * version @required_major.required_minor.@required_micro
118 * (same major version.)
120 * Return value: %NULL if the GLib library is compatible with the
121 * given version, or a string describing the version mismatch.
122 * The returned string is owned by GLib and must not be modified
128 glib_check_version (guint required_major,
129 guint required_minor,
130 guint required_micro)
132 gint glib_effective_micro = 100 * GLIB_MINOR_VERSION + GLIB_MICRO_VERSION;
133 gint required_effective_micro = 100 * required_minor + required_micro;
135 if (required_major > GLIB_MAJOR_VERSION)
136 return "GLib version too old (major mismatch)";
137 if (required_major < GLIB_MAJOR_VERSION)
138 return "GLib version too new (major mismatch)";
139 if (required_effective_micro < glib_effective_micro - GLIB_BINARY_AGE)
140 return "GLib version too new (micro mismatch)";
141 if (required_effective_micro > glib_effective_micro)
142 return "GLib version too old (micro mismatch)";
146 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
148 g_memmove (gpointer dest, gconstpointer src, gulong len)
150 gchar* destptr = dest;
151 const gchar* srcptr = src;
152 if (src + len < dest || dest + len < src)
154 bcopy (src, dest, len);
157 else if (dest <= src)
160 *(destptr++) = *(srcptr++);
167 *(--destptr) = *(--srcptr);
170 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
173 g_atexit (GVoidFunc func)
176 const gchar *error = NULL;
178 /* keep this in sync with glib.h */
180 #ifdef G_NATIVE_ATEXIT
181 result = ATEXIT (func);
183 error = g_strerror (errno);
184 #elif defined (HAVE_ATEXIT)
185 # ifdef NeXT /* @#%@! NeXTStep */
186 result = !atexit ((void (*)(void)) func);
188 error = g_strerror (errno);
190 result = atexit ((void (*)(void)) func);
192 error = g_strerror (errno);
194 #elif defined (HAVE_ON_EXIT)
195 result = on_exit ((void (*)(int, void *)) func, NULL);
197 error = g_strerror (errno);
200 error = "no implementation";
201 #endif /* G_NATIVE_ATEXIT */
204 g_error ("Could not register atexit() function: %s", error);
207 /* Based on execvp() from GNU Libc.
208 * Some of this code is cut-and-pasted into gspawn.c
212 my_strchrnul (const gchar *str, gchar c)
214 gchar *p = (gchar*)str;
215 while (*p && (*p != c))
223 static gchar *inner_find_program_in_path (const gchar *program);
226 g_find_program_in_path (const gchar *program)
228 const gchar *last_dot = strrchr (program, '.');
230 if (last_dot == NULL || strchr (last_dot, '\\') != NULL)
232 const gint program_length = strlen (program);
233 const gchar *pathext = getenv ("PATHEXT");
235 gchar *decorated_program;
239 pathext = ".com;.exe;.bat";
245 p = my_strchrnul (pathext, ';');
247 decorated_program = g_malloc (program_length + (p-pathext) + 1);
248 memcpy (decorated_program, program, program_length);
249 memcpy (decorated_program+program_length, pathext, p-pathext);
250 decorated_program [program_length + (p-pathext)] = '\0';
252 retval = inner_find_program_in_path (decorated_program);
253 g_free (decorated_program);
257 } while (*p++ != '\0');
261 return inner_find_program_in_path (program);
264 #define g_find_program_in_path inner_find_program_in_path
268 * g_find_program_in_path:
269 * @program: a program name
271 * Locates the first executable named @program in the user's path, in the
272 * same way that execvp() would locate it. Returns an allocated string
273 * with the absolute path name, or NULL if the program is not found in
274 * the path. If @program is already an absolute path, returns a copy of
275 * @program if @program exists and is executable, and NULL otherwise.
277 * On Windows, if @program does not have a file type suffix, tries to
278 * append the suffixes in the PATHEXT environment variable (if that
279 * doesn't exists, the suffixes .com, .exe, and .bat) in turn, and
280 * then look for the resulting file name in the same way as
281 * CreateProcess() would. This means first in the directory where the
282 * program was loaded from, then in the current directory, then in the
283 * Windows 32-bit system directory, then in the Windows directory, and
284 * finally in the directories in the PATH environment variable. If
285 * the program is found, the return value contains the full name
286 * including the type suffix.
288 * Return value: absolute path, or NULL
294 g_find_program_in_path (const gchar *program)
296 const gchar *path, *p;
297 gchar *name, *freeme;
304 g_return_val_if_fail (program != NULL, NULL);
306 /* If it is an absolute path, or a relative path including subdirectories,
307 * don't look in PATH.
309 if (g_path_is_absolute (program)
310 || strchr (program, G_DIR_SEPARATOR) != NULL
312 || strchr (program, '/') != NULL
316 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
317 return g_strdup (program);
322 path = g_getenv ("PATH");
326 /* There is no `PATH' in the environment. The default
327 * search path in GNU libc is the current directory followed by
328 * the path `confstr' returns for `_CS_PATH'.
331 /* In GLib we put . last, for security, and don't use the
332 * unportable confstr(); UNIX98 does not actually specify
333 * what to search if PATH is unset. POSIX may, dunno.
336 path = "/bin:/usr/bin:.";
341 gchar moddir[MAXPATHLEN], sysdir[MAXPATHLEN], windir[MAXPATHLEN];
343 GetModuleFileName (NULL, moddir, sizeof (moddir));
344 tmp = g_path_get_dirname (moddir);
345 GetSystemDirectory (sysdir, sizeof (sysdir));
346 GetWindowsDirectory (windir, sizeof (windir));
347 path_tmp = g_strconcat (tmp, ";.;", sysdir, ";", windir,
348 (path != NULL ? ";" : NULL),
349 (path != NULL ? path : NULL),
356 len = strlen (program) + 1;
357 pathlen = strlen (path);
358 freeme = name = g_malloc (pathlen + len + 1);
360 /* Copy the file name at the top, including '\0' */
361 memcpy (name + pathlen + 1, program, len);
362 name = name + pathlen;
363 /* And add the slash before the filename */
364 *name = G_DIR_SEPARATOR;
372 p = my_strchrnul (path, G_SEARCHPATH_SEPARATOR);
375 /* Two adjacent colons, or a colon at the beginning or the end
376 * of `PATH' means to search the current directory.
380 startp = memcpy (name - (p - path), path, p - path);
382 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
385 ret = g_strdup (startp);
393 while (*p++ != '\0');
404 g_parse_debug_string (const gchar *string,
405 const GDebugKey *keys,
411 g_return_val_if_fail (string != NULL, 0);
413 if (!g_ascii_strcasecmp (string, "all"))
415 for (i=0; i<nkeys; i++)
416 result |= keys[i].value;
420 const gchar *p = string;
422 gboolean done = FALSE;
433 for (i=0; i<nkeys; i++)
434 if (g_ascii_strncasecmp(keys[i].key, p, q - p) == 0 &&
435 keys[i].key[q - p] == '\0')
436 result |= keys[i].value;
447 * @file_name: the name of the file.
449 * Gets the name of the file without any leading directory components.
450 * It returns a pointer into the given file name string.
452 * Return value: the name of the file without any leading directory components.
454 * Deprecated: Use g_path_get_basename() instead, but notice that
455 * g_path_get_basename() allocates new memory for the returned string, unlike
456 * this function which returns a pointer into the argument.
458 G_CONST_RETURN gchar*
459 g_basename (const gchar *file_name)
461 register gchar *base;
463 g_return_val_if_fail (file_name != NULL, NULL);
465 base = strrchr (file_name, G_DIR_SEPARATOR);
469 gchar *q = strrchr (file_name, '/');
470 if (base == NULL || (q != NULL && q > base))
479 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
480 return (gchar*) file_name + 2;
481 #endif /* G_OS_WIN32 */
483 return (gchar*) file_name;
487 * g_path_get_basename:
488 * @file_name: the name of the file.
490 * Gets the last component of the filename. If @file_name ends with a
491 * directory separator it gets the component before the last slash. If
492 * @file_name consists only of directory separators (and on Windows,
493 * possibly a drive letter), a single separator is returned. If
494 * @file_name is empty, it gets ".".
496 * Return value: a newly allocated string containing the last component of
500 g_path_get_basename (const gchar *file_name)
502 register gssize base;
503 register gssize last_nonslash;
507 g_return_val_if_fail (file_name != NULL, NULL);
509 if (file_name[0] == '\0')
511 return g_strdup (".");
513 last_nonslash = strlen (file_name) - 1;
515 while (last_nonslash >= 0 && G_IS_DIR_SEPARATOR (file_name [last_nonslash]))
518 if (last_nonslash == -1)
519 /* string only containing slashes */
520 return g_strdup (G_DIR_SEPARATOR_S);
523 if (last_nonslash == 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
524 /* string only containing slashes and a drive */
525 return g_strdup (G_DIR_SEPARATOR_S);
526 #endif /* G_OS_WIN32 */
528 base = last_nonslash;
530 while (base >=0 && !G_IS_DIR_SEPARATOR (file_name [base]))
534 if (base == -1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
536 #endif /* G_OS_WIN32 */
538 len = last_nonslash - base;
539 retval = g_malloc (len + 1);
540 memcpy (retval, file_name + base + 1, len);
546 g_path_is_absolute (const gchar *file_name)
548 g_return_val_if_fail (file_name != NULL, FALSE);
550 if (G_IS_DIR_SEPARATOR (file_name[0]))
554 /* Recognize drive letter on native Windows */
555 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
557 #endif /* G_OS_WIN32 */
562 G_CONST_RETURN gchar*
563 g_path_skip_root (const gchar *file_name)
565 g_return_val_if_fail (file_name != NULL, NULL);
567 #ifdef G_PLATFORM_WIN32
568 /* Skip \\server\share or //server/share */
569 if (G_IS_DIR_SEPARATOR (file_name[0]) &&
570 G_IS_DIR_SEPARATOR (file_name[1]) &&
575 p = strchr (file_name + 2, G_DIR_SEPARATOR);
578 gchar *q = strchr (file_name + 2, '/');
579 if (p == NULL || (q != NULL && q < p))
589 while (file_name[0] && !G_IS_DIR_SEPARATOR (file_name[0]))
592 /* Possibly skip a backslash after the share name */
593 if (G_IS_DIR_SEPARATOR (file_name[0]))
596 return (gchar *)file_name;
601 /* Skip initial slashes */
602 if (G_IS_DIR_SEPARATOR (file_name[0]))
604 while (G_IS_DIR_SEPARATOR (file_name[0]))
606 return (gchar *)file_name;
611 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':' && G_IS_DIR_SEPARATOR (file_name[2]))
612 return (gchar *)file_name + 3;
619 g_path_get_dirname (const gchar *file_name)
621 register gchar *base;
624 g_return_val_if_fail (file_name != NULL, NULL);
626 base = strrchr (file_name, G_DIR_SEPARATOR);
629 gchar *q = strrchr (file_name, '/');
630 if (base == NULL || (q != NULL && q > base))
637 if (g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
639 gchar drive_colon_dot[4];
641 drive_colon_dot[0] = file_name[0];
642 drive_colon_dot[1] = ':';
643 drive_colon_dot[2] = '.';
644 drive_colon_dot[3] = '\0';
646 return g_strdup (drive_colon_dot);
649 return g_strdup (".");
652 while (base > file_name && G_IS_DIR_SEPARATOR (*base))
656 if (base == file_name + 1 && g_ascii_isalpha (file_name[0]) && file_name[1] == ':')
660 len = (guint) 1 + base - file_name;
662 base = g_new (gchar, len + 1);
663 g_memmove (base, file_name, len);
670 g_get_current_dir (void)
672 gchar *buffer = NULL;
674 static gulong max_len = 0;
677 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
679 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
680 * and, if that wasn't bad enough, hangs in doing so.
682 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
683 buffer = g_new (gchar, max_len + 1);
685 dir = getwd (buffer);
686 #else /* !sun || !HAVE_GETCWD */
687 while (max_len < G_MAXULONG / 2)
689 buffer = g_new (gchar, max_len + 1);
691 dir = getcwd (buffer, max_len);
693 if (dir || errno != ERANGE)
699 #endif /* !sun || !HAVE_GETCWD */
701 if (!dir || !*buffer)
703 /* hm, should we g_error() out here?
704 * this can happen if e.g. "./" has mode \0000
706 buffer[0] = G_DIR_SEPARATOR;
710 dir = g_strdup (buffer);
718 * @variable: the environment variable to get.
720 * Returns an environment variable.
722 * Return value: the value of the environment variable, or %NULL if the environment
723 * variable is not found. The returned string may be overwritten by the next call to g_getenv(),
724 * g_setenv() or g_unsetenv().
726 G_CONST_RETURN gchar*
727 g_getenv (const gchar *variable)
730 g_return_val_if_fail (variable != NULL, NULL);
732 return getenv (variable);
740 g_return_val_if_fail (variable != NULL, NULL);
742 system_env = getenv (variable);
746 /* On Windows NT, it is relatively typical that environment
747 * variables contain references to other environment variables. If
748 * so, use ExpandEnvironmentStrings(). (If all software was written
749 * in the best possible way, such environment variables would be
750 * stored in the Registry as REG_EXPAND_SZ type values, and would
751 * then get automatically expanded before the program sees them. But
752 * there is broken software that stores environment variables as
753 * REG_SZ values even if they contain references to other
754 * environment variables.
757 if (strchr (system_env, '%') == NULL)
759 /* No reference to other variable(s), return value as such. */
763 /* First check how much space we need */
764 length = ExpandEnvironmentStrings (system_env, dummy, 2);
766 expanded_env = g_malloc (length);
768 ExpandEnvironmentStrings (system_env, expanded_env, length);
770 quark = g_quark_from_string (expanded_env);
771 g_free (expanded_env);
773 return g_quark_to_string (quark);
779 * @variable: the environment variable to set, must not contain '='.
780 * @value: the value for to set the variable to.
781 * @overwrite: whether to change the variable if it already exists.
783 * Sets an environment variable.
785 * Note that on some systems, the memory used for the variable and its value
786 * can't be reclaimed later.
788 * Returns: %FALSE if the environment variable couldn't be set.
793 g_setenv (const gchar *variable,
802 g_return_val_if_fail (strchr (variable, '=') == NULL, FALSE);
805 result = setenv (variable, value, overwrite);
807 if (!overwrite && g_getenv (variable) != NULL)
810 /* This results in a leak when you overwrite existing
811 * settings. It would be fairly easy to fix this by keeping
812 * our own parallel array or hash table.
814 string = g_strconcat (variable, "=", value, NULL);
815 result = putenv (string);
820 #ifndef HAVE_UNSETENV
821 /* According to the Single Unix Specification, environ is not in
822 * any system header, although unistd.h often declares it.
826 * Win32 - at least msvc headers declare it so let's avoid
827 * warning C4273: '__p__environ' : inconsistent dll linkage. dllexport assumed.
829 extern char **environ;
835 * @variable: the environment variable to remove, must not contain '='.
837 * Removes an environment variable from the environment.
839 * Note that on some systems, the memory used for the variable and its value
840 * can't be reclaimed. Furthermore, this function can't be guaranteed to operate in a
846 g_unsetenv (const gchar *variable)
849 g_return_if_fail (strchr (variable, '=') == NULL);
856 g_return_if_fail (strchr (variable, '=') == NULL);
858 len = strlen (variable);
860 /* Mess directly with the environ array.
861 * This seems to be the only portable way to do this.
863 * Note that we remove *all* environment entries for
864 * the variable name, not just the first.
869 if (strncmp (*e, variable, len) != 0 || (*e)[len] != '=')
880 G_LOCK_DEFINE_STATIC (g_utils_global);
882 static gchar *g_tmp_dir = NULL;
883 static gchar *g_user_name = NULL;
884 static gchar *g_real_name = NULL;
885 static gchar *g_home_dir = NULL;
887 /* HOLDS: g_utils_global_lock */
889 g_get_any_init (void)
893 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
895 g_tmp_dir = g_strdup (g_getenv ("TMP"));
897 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
903 g_tmp_dir = g_strdup (P_tmpdir);
904 k = strlen (g_tmp_dir);
905 if (k > 1 && G_IS_DIR_SEPARATOR (g_tmp_dir[k - 1]))
906 g_tmp_dir[k - 1] = '\0';
913 g_tmp_dir = g_strdup ("/tmp");
914 #else /* G_OS_WIN32 */
915 g_tmp_dir = g_strdup ("C:\\");
916 #endif /* G_OS_WIN32 */
920 /* We check $HOME first for Win32, though it is a last resort for Unix
921 * where we prefer the results of getpwuid().
924 gchar *home = g_getenv ("HOME");
926 /* Only believe HOME if it is an absolute path and exists */
927 if (home && g_path_is_absolute (home) && g_file_test (home, G_FILE_TEST_IS_DIR))
928 g_home_dir = g_strdup (home);
931 /* In case HOME is Unix-style (it happens), convert it to
937 while ((p = strchr (g_home_dir, '/')) != NULL)
943 /* USERPROFILE is probably the closest equivalent to $HOME? */
944 if (getenv ("USERPROFILE") != NULL)
945 g_home_dir = g_strdup (g_getenv ("USERPROFILE"));
950 /* At least at some time, HOMEDRIVE and HOMEPATH were used
951 * to point to the home directory, I think. But on Windows
952 * 2000 HOMEDRIVE seems to be equal to SYSTEMDRIVE, and
953 * HOMEPATH is its root "\"?
955 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
957 gchar *homedrive, *homepath;
959 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
960 homepath = g_strdup (g_getenv ("HOMEPATH"));
962 g_home_dir = g_strconcat (homedrive, homepath, NULL);
967 #endif /* G_OS_WIN32 */
971 struct passwd *pw = NULL;
972 gpointer buffer = NULL;
975 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
977 # ifdef _SC_GETPW_R_SIZE_MAX
978 /* This reurns the maximum length */
979 glong bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
983 # else /* _SC_GETPW_R_SIZE_MAX */
985 # endif /* _SC_GETPW_R_SIZE_MAX */
990 buffer = g_malloc (bufsize);
993 # ifdef HAVE_POSIX_GETPWUID_R
994 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
995 error = error < 0 ? errno : error;
996 # else /* HAVE_NONPOSIX_GETPWUID_R */
997 /* HPUX 11 falls into the HAVE_POSIX_GETPWUID_R case */
998 # if defined(_AIX) || defined(__hpux)
999 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1000 pw = error == 0 ? &pwd : NULL;
1002 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
1003 error = pw ? 0 : errno;
1005 # endif /* HAVE_NONPOSIX_GETPWUID_R */
1009 /* we bail out prematurely if the user id can't be found
1010 * (should be pretty rare case actually), or if the buffer
1011 * should be sufficiently big and lookups are still not
1014 if (error == 0 || error == ENOENT)
1016 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
1017 (gulong) getuid ());
1020 if (bufsize > 32 * 1024)
1022 g_warning ("getpwuid_r(): failed due to: %s.",
1023 g_strerror (error));
1031 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
1036 pw = getpwuid (getuid ());
1041 g_user_name = g_strdup (pw->pw_name);
1043 if (pw->pw_gecos && *pw->pw_gecos != '\0')
1045 gchar **gecos_fields;
1048 /* split the gecos field and substitute '&' */
1049 gecos_fields = g_strsplit (pw->pw_gecos, ",", 0);
1050 name_parts = g_strsplit (gecos_fields[0], "&", 0);
1051 pw->pw_name[0] = g_ascii_toupper (pw->pw_name[0]);
1052 g_real_name = g_strjoinv (pw->pw_name, name_parts);
1053 g_strfreev (gecos_fields);
1054 g_strfreev (name_parts);
1058 g_home_dir = g_strdup (pw->pw_dir);
1063 #else /* !HAVE_PWD_H */
1067 guint len = UNLEN+1;
1068 gchar buffer[UNLEN+1];
1070 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
1072 g_user_name = g_strdup (buffer);
1073 g_real_name = g_strdup (buffer);
1076 # endif /* G_OS_WIN32 */
1078 #endif /* !HAVE_PWD_H */
1081 g_home_dir = g_strdup (g_getenv ("HOME"));
1084 /* change '\\' in %HOME% to '/' */
1085 g_strdelimit (g_home_dir, "\\",'/');
1088 g_user_name = g_strdup ("somebody");
1090 g_real_name = g_strdup ("Unknown");
1094 G_CONST_RETURN gchar*
1095 g_get_user_name (void)
1097 G_LOCK (g_utils_global);
1100 G_UNLOCK (g_utils_global);
1105 G_CONST_RETURN gchar*
1106 g_get_real_name (void)
1108 G_LOCK (g_utils_global);
1111 G_UNLOCK (g_utils_global);
1116 /* Return the home directory of the user. If there is a HOME
1117 * environment variable, its value is returned, otherwise use some
1118 * system-dependent way of finding it out. If no home directory can be
1119 * deduced, return NULL.
1122 G_CONST_RETURN gchar*
1123 g_get_home_dir (void)
1125 G_LOCK (g_utils_global);
1128 G_UNLOCK (g_utils_global);
1133 /* Return a directory to be used to store temporary files. This is the
1134 * value of the TMPDIR, TMP or TEMP environment variables (they are
1135 * checked in that order). If none of those exist, use P_tmpdir from
1136 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
1137 * and C:\ on Windows.
1140 G_CONST_RETURN gchar*
1141 g_get_tmp_dir (void)
1143 G_LOCK (g_utils_global);
1146 G_UNLOCK (g_utils_global);
1151 G_LOCK_DEFINE_STATIC (g_prgname);
1152 static gchar *g_prgname = NULL;
1155 g_get_prgname (void)
1161 G_UNLOCK (g_prgname);
1167 g_set_prgname (const gchar *prgname)
1171 g_prgname = g_strdup (prgname);
1172 G_UNLOCK (g_prgname);
1175 G_LOCK_DEFINE_STATIC (g_application_name);
1176 static gchar *g_application_name = NULL;
1179 * g_get_application_name:
1181 * Gets a human-readable name for the application, as set by
1182 * g_set_application_name(). This name should be localized if
1183 * possible, and is intended for display to the user. Contrast with
1184 * g_get_prgname(), which gets a non-localized name. If
1185 * g_set_application_name() has not been called, returns the result of
1186 * g_get_prgname() (which may be %NULL if g_set_prgname() has also not
1189 * Return value: human-readable application name. may return %NULL
1193 G_CONST_RETURN gchar*
1194 g_get_application_name (void)
1198 G_LOCK (g_application_name);
1199 retval = g_application_name;
1200 G_UNLOCK (g_application_name);
1203 return g_get_prgname ();
1209 * g_set_application_name:
1210 * @application_name: localized name of the application
1212 * Sets a human-readable name for the application. This name should be
1213 * localized if possible, and is intended for display to the user.
1214 * Contrast with g_set_prgname(), which sets a non-localized name.
1215 * g_set_prgname() will be called automatically by gtk_init(),
1216 * but g_set_application_name() will not.
1218 * Note that for thread safety reasons, this function can only
1221 * The application name will be used in contexts such as error messages,
1222 * or when displaying an application's name in the task list.
1226 g_set_application_name (const gchar *application_name)
1228 gboolean already_set = FALSE;
1230 G_LOCK (g_application_name);
1231 if (g_application_name)
1234 g_application_name = g_strdup (application_name);
1235 G_UNLOCK (g_application_name);
1238 g_warning ("g_set_application() name called multiple times");
1242 g_direct_hash (gconstpointer v)
1244 return GPOINTER_TO_UINT (v);
1248 g_direct_equal (gconstpointer v1,
1255 g_int_equal (gconstpointer v1,
1258 return *((const gint*) v1) == *((const gint*) v2);
1262 g_int_hash (gconstpointer v)
1264 return *(const gint*) v;
1268 * g_nullify_pointer:
1269 * @nullify_location: the memory address of the pointer.
1271 * Set the pointer at the specified location to %NULL.
1274 g_nullify_pointer (gpointer *nullify_location)
1276 g_return_if_fail (nullify_location != NULL);
1278 *nullify_location = NULL;
1284 * Get the codeset for the current locale.
1286 * Return value: a newly allocated string containing the name
1287 * of the codeset. This string must be freed with g_free().
1290 g_get_codeset (void)
1292 const gchar *charset;
1294 g_get_charset (&charset);
1296 return g_strdup (charset);
1301 #include <libintl.h>
1303 #ifdef G_PLATFORM_WIN32
1305 G_WIN32_DLLMAIN_FOR_DLL_NAME (static, dll_name)
1307 static const gchar *
1308 _glib_get_locale_dir (void)
1310 static const gchar *cache = NULL;
1312 cache = g_win32_get_package_installation_subdirectory
1313 (GETTEXT_PACKAGE, dll_name, "lib\\locale");
1318 #undef GLIB_LOCALE_DIR
1319 #define GLIB_LOCALE_DIR _glib_get_locale_dir ()
1321 #endif /* G_PLATFORM_WIN32 */
1323 G_CONST_RETURN gchar *
1324 _glib_gettext (const gchar *str)
1326 static gboolean _glib_gettext_initialized = FALSE;
1328 if (!_glib_gettext_initialized)
1330 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1331 # ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1332 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1334 _glib_gettext_initialized = TRUE;
1337 return dgettext (GETTEXT_PACKAGE, str);
1340 #endif /* ENABLE_NLS */