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.
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_PARAM_H
48 #include <sys/param.h>
51 /* implement Glib's inline functions
53 #define G_IMPLEMENT_INLINES 1
58 #define G_PATH_LENGTH MAXPATHLEN
59 #elif defined (PATH_MAX)
60 #define G_PATH_LENGTH PATH_MAX
61 #elif defined (_PC_PATH_MAX)
62 #define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
64 #define G_PATH_LENGTH 2048
68 # define STRICT /* Strict typing, please */
72 #endif /* G_OS_WIN32 */
78 const guint glib_major_version = GLIB_MAJOR_VERSION;
79 const guint glib_minor_version = GLIB_MINOR_VERSION;
80 const guint glib_micro_version = GLIB_MICRO_VERSION;
81 const guint glib_interface_age = GLIB_INTERFACE_AGE;
82 const guint glib_binary_age = GLIB_BINARY_AGE;
84 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
86 g_memmove (gpointer dest, gconstpointer src, gulong len)
88 gchar* destptr = dest;
89 const gchar* srcptr = src;
90 if (src + len < dest || dest + len < src)
92 bcopy (src, dest, len);
98 *(destptr++) = *(srcptr++);
105 *(--destptr) = *(--srcptr);
108 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
111 g_atexit (GVoidFunc func)
114 const gchar *error = NULL;
116 /* keep this in sync with glib.h */
118 #ifdef G_NATIVE_ATEXIT
119 result = ATEXIT (func);
121 error = g_strerror (errno);
122 #elif defined (HAVE_ATEXIT)
123 # ifdef NeXT /* @#%@! NeXTStep */
124 result = !atexit ((void (*)(void)) func);
126 error = g_strerror (errno);
128 result = atexit ((void (*)(void)) func);
130 error = g_strerror (errno);
132 #elif defined (HAVE_ON_EXIT)
133 result = on_exit ((void (*)(int, void *)) func, NULL);
135 error = g_strerror (errno);
138 error = "no implementation";
139 #endif /* G_NATIVE_ATEXIT */
142 g_error ("Could not register atexit() function: %s", error);
145 /* Based on execvp() from GNU Libc.
146 * Some of this code is cut-and-pasted into gspawn.c
150 my_strchrnul (const gchar *str, gchar c)
152 gchar *p = (gchar*)str;
153 while (*p && (*p != c))
160 * g_find_program_in_path:
161 * @program: a program name
163 * Locates the first executable named @program in the user's path, in the
164 * same way that execvp() would locate it. Returns an allocated string
165 * with the absolute path name, or NULL if the program is not found in
166 * the path. If @program is already an absolute path, returns a copy of
167 * @program if @program exists and is executable, and NULL otherwise.
169 * Return value: absolute path, or NULL
172 g_find_program_in_path (const gchar *program)
174 const gchar *path, *p;
175 gchar *name, *freeme;
179 g_return_val_if_fail (program != NULL, NULL);
183 if (g_file_test (program, G_FILE_TEST_IS_EXECUTABLE))
184 return g_strdup (program);
189 path = g_getenv ("PATH");
192 /* There is no `PATH' in the environment. The default
193 * search path in GNU libc is the current directory followed by
194 * the path `confstr' returns for `_CS_PATH'.
197 /* In GLib we put . last, for security, and don't use the
198 * unportable confstr(); UNIX98 does not actually specify
199 * what to search if PATH is unset. POSIX may, dunno.
202 path = "/bin:/usr/bin:.";
205 len = strlen (program) + 1;
206 pathlen = strlen (path);
207 freeme = name = g_malloc (pathlen + len + 1);
209 /* Copy the file name at the top, including '\0' */
210 memcpy (name + pathlen + 1, program, len);
211 name = name + pathlen;
212 /* And add the slash before the filename */
221 p = my_strchrnul (path, ':');
224 /* Two adjacent colons, or a colon at the beginning or the end
225 * of `PATH' means to search the current directory.
229 startp = memcpy (name - (p - path), path, p - path);
231 if (g_file_test (startp, G_FILE_TEST_IS_EXECUTABLE))
234 ret = g_strdup (startp);
239 while (*p++ != '\0');
247 g_snprintf (gchar *str,
252 #ifdef HAVE_VSNPRINTF
256 g_return_val_if_fail (str != NULL, 0);
257 g_return_val_if_fail (n > 0, 0);
258 g_return_val_if_fail (fmt != NULL, 0);
260 va_start (args, fmt);
261 retval = vsnprintf (str, n, fmt, args);
267 retval = strlen (str);
271 #else /* !HAVE_VSNPRINTF */
275 g_return_val_if_fail (str != NULL, 0);
276 g_return_val_if_fail (n > 0, 0);
277 g_return_val_if_fail (fmt != NULL, 0);
279 va_start (args, fmt);
280 printed = g_strdup_vprintf (fmt, args);
283 strncpy (str, printed, n);
289 #endif /* !HAVE_VSNPRINTF */
293 g_vsnprintf (gchar *str,
298 #ifdef HAVE_VSNPRINTF
301 g_return_val_if_fail (str != NULL, 0);
302 g_return_val_if_fail (n > 0, 0);
303 g_return_val_if_fail (fmt != NULL, 0);
305 retval = vsnprintf (str, n, fmt, args);
310 retval = strlen (str);
314 #else /* !HAVE_VSNPRINTF */
317 g_return_val_if_fail (str != NULL, 0);
318 g_return_val_if_fail (n > 0, 0);
319 g_return_val_if_fail (fmt != NULL, 0);
321 printed = g_strdup_vprintf (fmt, args);
322 strncpy (str, printed, n);
328 #endif /* !HAVE_VSNPRINTF */
332 g_parse_debug_string (const gchar *string,
339 g_return_val_if_fail (string != NULL, 0);
341 if (!g_strcasecmp (string, "all"))
343 for (i=0; i<nkeys; i++)
344 result |= keys[i].value;
348 gchar *str = g_strdup (string);
351 gboolean done = FALSE;
364 for (i=0; i<nkeys; i++)
365 if (!g_strcasecmp(keys[i].key, p))
366 result |= keys[i].value;
377 G_CONST_RETURN gchar*
378 g_basename (const gchar *file_name)
380 register gchar *base;
381 #if defined(G_ENABLE_DEBUG) && !defined(G_OS_WIN32)
382 static gboolean first_call = TRUE;
386 g_message ("g_basename is deprecated. Use g_path_get_basename instead. "
387 "Beware that the string returned by g_path_get_basename() has "
388 " to be g_free()ed.");
391 #endif /* G_ENABLE_DEBUG */
393 g_return_val_if_fail (file_name != NULL, NULL);
395 base = strrchr (file_name, G_DIR_SEPARATOR);
400 if (isalpha (file_name[0]) && file_name[1] == ':')
401 return (gchar*) file_name + 2;
402 #endif /* G_OS_WIN32 */
404 return (gchar*) file_name;
408 g_path_get_basename (const gchar *file_name)
411 register gint last_nonslash;
415 g_return_val_if_fail (file_name != NULL, NULL);
417 if (file_name[0] == '\0')
419 return g_strdup (".");
421 last_nonslash = strlen (file_name) - 1;
423 while (last_nonslash >= 0 && file_name [last_nonslash] == G_DIR_SEPARATOR)
426 if (last_nonslash == -1)
427 /* string only containing slashes */
428 return g_strdup (G_DIR_SEPARATOR_S);
431 if (last_nonslash == 1 && isalpha (file_name[0]) && file_name[1] == ':')
432 /* string only containing slashes and a drive */
433 return g_strdup (G_DIR_SEPARATOR_S);
434 #endif /* G_OS_WIN32 */
436 base = last_nonslash;
438 while (base >=0 && file_name [base] != G_DIR_SEPARATOR)
442 if (base == -1 && isalpha (file_name[0]) && file_name[1] == ':')
444 #endif /* G_OS_WIN32 */
446 len = last_nonslash - base;
447 retval = g_malloc (len + 1);
448 memcpy (retval, file_name + base + 1, len);
454 g_path_is_absolute (const gchar *file_name)
456 g_return_val_if_fail (file_name != NULL, FALSE);
458 if (file_name[0] == G_DIR_SEPARATOR)
462 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
465 if (file_name[0] == G_DIR_SEPARATOR && file_name[1] == G_DIR_SEPARATOR)
472 G_CONST_RETURN gchar*
473 g_path_skip_root (const gchar *file_name)
475 g_return_val_if_fail (file_name != NULL, NULL);
478 /* Skip \\server\share */
479 if (file_name[0] == G_DIR_SEPARATOR &&
480 file_name[1] == G_DIR_SEPARATOR &&
485 if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 &&
490 while (file_name[0] && file_name[0] != G_DIR_SEPARATOR)
493 /* Possibly skip a backslash after the share name */
494 if (file_name[0] == G_DIR_SEPARATOR)
497 return (gchar *)file_name;
502 /* Skip initial slashes */
503 if (file_name[0] == G_DIR_SEPARATOR)
505 while (file_name[0] == G_DIR_SEPARATOR)
507 return (gchar *)file_name;
512 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
513 return (gchar *)file_name + 3;
520 g_path_get_dirname (const gchar *file_name)
522 register gchar *base;
525 g_return_val_if_fail (file_name != NULL, NULL);
527 base = strrchr (file_name, G_DIR_SEPARATOR);
529 return g_strdup (".");
530 while (base > file_name && *base == G_DIR_SEPARATOR)
532 len = (guint) 1 + base - file_name;
534 base = g_new (gchar, len + 1);
535 g_memmove (base, file_name, len);
542 g_dirname (const gchar *file_name)
544 #if defined(G_ENABLE_DEBUG) && !defined(G_OS_WIN32)
545 static gboolean first_call = TRUE;
549 g_message ("g_dirname() is deprecated. Use g_path_get_dirname() instead.");
552 #endif /* G_ENABLE_DEBUG */
554 return g_path_get_dirname (file_name);
558 g_get_current_dir (void)
560 gchar *buffer = NULL;
562 static gulong max_len = 0;
565 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
567 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
568 * and, if that wasn't bad enough, hangs in doing so.
570 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
571 buffer = g_new (gchar, max_len + 1);
573 dir = getwd (buffer);
574 #else /* !sun || !HAVE_GETCWD */
575 while (max_len < G_MAXULONG / 2)
577 buffer = g_new (gchar, max_len + 1);
579 dir = getcwd (buffer, max_len);
581 if (dir || errno != ERANGE)
587 #endif /* !sun || !HAVE_GETCWD */
589 if (!dir || !*buffer)
591 /* hm, should we g_error() out here?
592 * this can happen if e.g. "./" has mode \0000
594 buffer[0] = G_DIR_SEPARATOR;
598 dir = g_strdup (buffer);
604 G_CONST_RETURN gchar*
605 g_getenv (const gchar *variable)
608 g_return_val_if_fail (variable != NULL, NULL);
610 return getenv (variable);
612 G_LOCK_DEFINE_STATIC (getenv);
618 static GArray *environs = NULL;
623 g_return_val_if_fail (variable != NULL, NULL);
628 environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
630 /* First we try to find the envinronment variable inside the already
634 for (i = 0; i < environs->len; i++)
636 env = &g_array_index (environs, struct env_struct, i);
637 if (strcmp (env->key, variable) == 0)
639 g_assert (env->value);
645 /* If not found, we ask the system */
647 system_env = getenv (variable);
654 /* On Windows NT, it is relatively typical that environment variables
655 * contain references to other environment variables. Handle that by
656 * calling ExpandEnvironmentStrings.
659 g_array_set_size (environs, environs->len + 1);
661 env = &g_array_index (environs, struct env_struct, environs->len - 1);
663 /* First check how much space we need */
664 length = ExpandEnvironmentStrings (system_env, dummy, 2);
666 /* Then allocate that much, and actualy do the expansion and insert
667 * the new found pair into our buffer
670 env->value = g_malloc (length);
671 env->key = g_strdup (variable);
673 ExpandEnvironmentStrings (system_env, env->value, length);
681 G_LOCK_DEFINE_STATIC (g_utils_global);
683 static gchar *g_tmp_dir = NULL;
684 static gchar *g_user_name = NULL;
685 static gchar *g_real_name = NULL;
686 static gchar *g_home_dir = NULL;
688 /* HOLDS: g_utils_global_lock */
690 g_get_any_init (void)
694 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
696 g_tmp_dir = g_strdup (g_getenv ("TMP"));
698 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
704 g_tmp_dir = g_strdup (P_tmpdir);
705 k = strlen (g_tmp_dir);
706 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
707 g_tmp_dir[k-1] = '\0';
714 g_tmp_dir = g_strdup ("/tmp");
715 #else /* G_OS_WIN32 */
716 g_tmp_dir = g_strdup ("C:\\");
717 #endif /* G_OS_WIN32 */
721 g_home_dir = g_strdup (g_getenv ("HOME"));
724 /* In case HOME is Unix-style (it happens), convert it to
730 while ((p = strchr (g_home_dir, '/')) != NULL)
736 /* USERPROFILE is probably the closest equivalent to $HOME? */
737 if (getenv ("USERPROFILE") != NULL)
738 g_home_dir = g_getenv ("USERPROFILE");
743 /* At least at some time, HOMEDRIVE and HOMEPATH were used
744 * to point to the home directory, I think. But on Windows
745 * 2000 HOMEDRIVE seems to be equal to SYSTEMDRIVE, and
746 * HOMEPATH is its root "\"?
748 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
750 gchar *homedrive, *homepath;
752 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
753 homepath = g_strdup (g_getenv ("HOMEPATH"));
755 g_home_dir = g_strconcat (homedrive, homepath, NULL);
760 #endif /* G_OS_WIN32 */
764 struct passwd *pw = NULL;
765 gpointer buffer = NULL;
767 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
769 # ifdef _SC_GETPW_R_SIZE_MAX
770 /* This reurns the maximum length */
771 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
772 # else /* _SC_GETPW_R_SIZE_MAX */
774 # endif /* _SC_GETPW_R_SIZE_MAX */
780 buffer = g_malloc (bufsize);
783 # ifdef HAVE_POSIX_GETPWUID_R
784 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
785 error = error < 0 ? errno : error;
786 # else /* HAVE_NONPOSIX_GETPWUID_R */
788 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
789 pw = error == 0 ? &pwd : NULL;
791 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
792 error = pw ? 0 : errno;
794 # endif /* HAVE_NONPOSIX_GETPWUID_R */
798 /* we bail out prematurely if the user id can't be found
799 * (should be pretty rare case actually), or if the buffer
800 * should be sufficiently big and lookups are still not
803 if (error == 0 || error == ENOENT)
805 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
809 if (bufsize > 32 * 1024)
811 g_warning ("getpwuid_r(): failed due to: %s.",
820 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
825 pw = getpwuid (getuid ());
830 g_user_name = g_strdup (pw->pw_name);
831 g_real_name = g_strdup (pw->pw_gecos);
833 g_home_dir = g_strdup (pw->pw_dir);
838 #else /* !HAVE_PWD_H */
845 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
847 g_user_name = g_strdup (buffer);
848 g_real_name = g_strdup (buffer);
851 # endif /* G_OS_WIN32 */
853 #endif /* !HAVE_PWD_H */
856 /* change '\\' in %HOME% to '/' */
857 g_strdelimit (g_home_dir, "\\",'/');
860 g_user_name = g_strdup ("somebody");
862 g_real_name = g_strdup ("Unknown");
867 for (p = g_real_name; *p; p++)
871 p = g_strdup (g_real_name);
872 g_free (g_real_name);
880 G_CONST_RETURN gchar*
881 g_get_user_name (void)
883 G_LOCK (g_utils_global);
886 G_UNLOCK (g_utils_global);
891 G_CONST_RETURN gchar*
892 g_get_real_name (void)
894 G_LOCK (g_utils_global);
897 G_UNLOCK (g_utils_global);
902 /* Return the home directory of the user. If there is a HOME
903 * environment variable, its value is returned, otherwise use some
904 * system-dependent way of finding it out. If no home directory can be
905 * deduced, return NULL.
908 G_CONST_RETURN gchar*
909 g_get_home_dir (void)
911 G_LOCK (g_utils_global);
914 G_UNLOCK (g_utils_global);
919 /* Return a directory to be used to store temporary files. This is the
920 * value of the TMPDIR, TMP or TEMP environment variables (they are
921 * checked in that order). If none of those exist, use P_tmpdir from
922 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
923 * and C:\ on Windows.
926 G_CONST_RETURN gchar*
929 G_LOCK (g_utils_global);
932 G_UNLOCK (g_utils_global);
937 static gchar *g_prgname = NULL;
944 G_LOCK (g_utils_global);
946 G_UNLOCK (g_utils_global);
952 g_set_prgname (const gchar *prgname)
956 G_LOCK (g_utils_global);
958 g_prgname = g_strdup (prgname);
960 G_UNLOCK (g_utils_global);
964 g_direct_hash (gconstpointer v)
966 return GPOINTER_TO_UINT (v);
970 g_direct_equal (gconstpointer v1,
977 g_int_equal (gconstpointer v1,
980 return *((const gint*) v1) == *((const gint*) v2);
984 g_int_hash (gconstpointer v)
986 return *(const gint*) v;
992 * Get the codeset for the current locale.
994 * Return value: a newly allocated string containing the name
995 * of the codeset. This string must be freed with g_free().
1001 char *result = nl_langinfo (CODESET);
1002 return g_strdup (result);
1005 /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
1007 return g_strdup ("ISO-8859-1");
1009 return g_strdup_printf ("CP%d", GetACP ());
1016 #include <libintl.h>
1021 #define GLIB_LOCALE_DIR \
1022 g_win32_get_package_installation_subdirectory \
1023 (GETTEXT_PACKAGE, g_strdup_printf ("glib-%d.%d.dll", \
1024 GLIB_MAJOR_VERSION, \
1025 GLIB_MINOR_VERSION), \
1028 #endif /* G_OS_WIN32 */
1030 G_CONST_RETURN gchar *
1031 _glib_gettext (const gchar *str)
1033 gboolean _glib_gettext_initialized = FALSE;
1035 if (!_glib_gettext_initialized)
1037 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1038 _glib_gettext_initialized = TRUE;
1041 return dgettext (GETTEXT_PACKAGE, str);
1044 #endif /* ENABLE_NLS */