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 */
73 #endif /* G_OS_WIN32 */
79 const guint glib_major_version = GLIB_MAJOR_VERSION;
80 const guint glib_minor_version = GLIB_MINOR_VERSION;
81 const guint glib_micro_version = GLIB_MICRO_VERSION;
82 const guint glib_interface_age = GLIB_INTERFACE_AGE;
83 const guint glib_binary_age = GLIB_BINARY_AGE;
85 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
87 g_memmove (gpointer dest, gconstpointer src, gulong len)
89 gchar* destptr = dest;
90 const gchar* srcptr = src;
91 if (src + len < dest || dest + len < src)
93 bcopy (src, dest, len);
99 *(destptr++) = *(srcptr++);
106 *(--destptr) = *(--srcptr);
109 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
112 g_atexit (GVoidFunc func)
117 /* keep this in sync with glib.h */
119 #ifdef G_NATIVE_ATEXIT
120 result = ATEXIT (func);
122 error = g_strerror (errno);
123 #elif defined (HAVE_ATEXIT)
124 # ifdef NeXT /* @#%@! NeXTStep */
125 result = !atexit ((void (*)(void)) func);
127 error = g_strerror (errno);
129 result = atexit ((void (*)(void)) func);
131 error = g_strerror (errno);
133 #elif defined (HAVE_ON_EXIT)
134 result = on_exit ((void (*)(int, void *)) func, NULL);
136 error = g_strerror (errno);
139 error = "no implementation";
140 #endif /* G_NATIVE_ATEXIT */
143 g_error ("Could not register atexit() function: %s", error);
146 /* Based on execvp() from GNU Libc.
147 * Some of this code is cut-and-pasted into gspawn.c
151 my_strchrnul (const gchar *str, gchar c)
153 gchar *p = (gchar*)str;
154 while (*p && (*p != c))
161 * g_find_program_in_path:
162 * @program: a program name
164 * Locates the first executable named @program in the user's path, in the
165 * same way that execvp() would locate it. Returns an allocated string
166 * with the absolute path name, or NULL if the program is not found in
167 * the path. If @program is already an absolute path, returns a copy of
168 * @program if @program exists and is executable, and NULL otherwise.
170 * Return value: absolute path, or NULL
173 g_find_program_in_path (const gchar *program)
175 gchar *path, *p, *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;
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)
470 g_path_skip_root (gchar *file_name)
472 g_return_val_if_fail (file_name != NULL, NULL);
475 /* Skip \\server\share */
476 if (file_name[0] == G_DIR_SEPARATOR &&
477 file_name[1] == G_DIR_SEPARATOR &&
482 if ((p = strchr (file_name + 2, G_DIR_SEPARATOR)) > file_name + 2 &&
487 while (file_name[0] && file_name[0] != G_DIR_SEPARATOR)
490 /* Possibly skip a backslash after the share name */
491 if (file_name[0] == G_DIR_SEPARATOR)
499 /* Skip initial slashes */
500 if (file_name[0] == G_DIR_SEPARATOR)
502 while (file_name[0] == G_DIR_SEPARATOR)
509 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
510 return file_name + 3;
517 g_path_get_dirname (const gchar *file_name)
519 register gchar *base;
522 g_return_val_if_fail (file_name != NULL, NULL);
524 base = strrchr (file_name, G_DIR_SEPARATOR);
526 return g_strdup (".");
527 while (base > file_name && *base == G_DIR_SEPARATOR)
529 len = (guint) 1 + base - file_name;
531 base = g_new (gchar, len + 1);
532 g_memmove (base, file_name, len);
539 g_dirname (const gchar *file_name)
541 #if defined(G_ENABLE_DEBUG) && !defined(G_OS_WIN32)
542 static gboolean first_call = TRUE;
546 g_message ("g_dirname() is deprecated. Use g_path_get_dirname() instead.");
549 #endif /* G_ENABLE_DEBUG */
551 return g_path_get_dirname (file_name);
555 g_get_current_dir (void)
557 gchar *buffer = NULL;
559 static gulong max_len = 0;
562 max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
564 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
565 * and, if that wasn't bad enough, hangs in doing so.
567 #if (defined (sun) && !defined (__SVR4)) || !defined(HAVE_GETCWD)
568 buffer = g_new (gchar, max_len + 1);
570 dir = getwd (buffer);
571 #else /* !sun || !HAVE_GETCWD */
572 while (max_len < G_MAXULONG / 2)
574 buffer = g_new (gchar, max_len + 1);
576 dir = getcwd (buffer, max_len);
578 if (dir || errno != ERANGE)
584 #endif /* !sun || !HAVE_GETCWD */
586 if (!dir || !*buffer)
588 /* hm, should we g_error() out here?
589 * this can happen if e.g. "./" has mode \0000
591 buffer[0] = G_DIR_SEPARATOR;
595 dir = g_strdup (buffer);
602 g_getenv (const gchar *variable)
605 g_return_val_if_fail (variable != NULL, NULL);
607 return getenv (variable);
609 G_LOCK_DEFINE_STATIC (getenv);
615 static GArray *environs = NULL;
620 g_return_val_if_fail (variable != NULL, NULL);
625 environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
627 /* First we try to find the envinronment variable inside the already
631 for (i = 0; i < environs->len; i++)
633 env = &g_array_index (environs, struct env_struct, i);
634 if (strcmp (env->key, variable) == 0)
636 g_assert (env->value);
642 /* If not found, we ask the system */
644 system_env = getenv (variable);
651 /* On Windows NT, it is relatively typical that environment variables
652 * contain references to other environment variables. Handle that by
653 * calling ExpandEnvironmentStrings.
656 g_array_set_size (environs, environs->len + 1);
658 env = &g_array_index (environs, struct env_struct, environs->len - 1);
660 /* First check how much space we need */
661 length = ExpandEnvironmentStrings (system_env, dummy, 2);
663 /* Then allocate that much, and actualy do the expansion and insert
664 * the new found pair into our buffer
667 env->value = g_malloc (length);
668 env->key = g_strdup (variable);
670 ExpandEnvironmentStrings (system_env, env->value, length);
678 G_LOCK_DEFINE_STATIC (g_utils_global);
680 static gchar *g_tmp_dir = NULL;
681 static gchar *g_user_name = NULL;
682 static gchar *g_real_name = NULL;
683 static gchar *g_home_dir = NULL;
685 /* HOLDS: g_utils_global_lock */
687 g_get_any_init (void)
691 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
693 g_tmp_dir = g_strdup (g_getenv ("TMP"));
695 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
701 g_tmp_dir = g_strdup (P_tmpdir);
702 k = strlen (g_tmp_dir);
703 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
704 g_tmp_dir[k-1] = '\0';
711 g_tmp_dir = g_strdup ("/tmp");
712 #else /* G_OS_WIN32 */
713 g_tmp_dir = g_strdup ("C:\\");
714 #endif /* G_OS_WIN32 */
718 g_home_dir = g_strdup (g_getenv ("HOME"));
721 /* In case HOME is Unix-style (it happens), convert it to
727 while ((p = strchr (g_home_dir, '/')) != NULL)
732 /* The official way to specify a home directory on NT is
733 * the HOMEDRIVE and HOMEPATH environment variables. At least
734 * it was at some time.
736 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
738 gchar *homedrive, *homepath;
740 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
741 homepath = g_strdup (g_getenv ("HOMEPATH"));
743 g_home_dir = g_strconcat (homedrive, homepath, NULL);
748 #endif /* G_OS_WIN32 */
752 struct passwd *pw = NULL;
753 gpointer buffer = NULL;
755 # if defined (HAVE_POSIX_GETPWUID_R) || defined (HAVE_NONPOSIX_GETPWUID_R)
757 # ifdef _SC_GETPW_R_SIZE_MAX
758 /* This reurns the maximum length */
759 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
760 # else /* _SC_GETPW_R_SIZE_MAX */
762 # endif /* _SC_GETPW_R_SIZE_MAX */
768 buffer = g_malloc (bufsize);
771 # ifdef HAVE_POSIX_GETPWUID_R
772 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
773 error = error < 0 ? errno : error;
774 # else /* HAVE_NONPOSIX_GETPWUID_R */
776 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
777 pw = error == 0 ? &pwd : NULL;
779 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
780 error = pw ? 0 : errno;
782 # endif /* HAVE_NONPOSIX_GETPWUID_R */
786 /* we bail out prematurely if the user id can't be found
787 * (should be pretty rare case actually), or if the buffer
788 * should be sufficiently big and lookups are still not
791 if (error == 0 || error == ENOENT)
793 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
797 if (bufsize > 32 * 1024)
799 g_warning ("getpwuid_r(): failed due to: %s.",
808 # endif /* HAVE_POSIX_GETPWUID_R || HAVE_NONPOSIX_GETPWUID_R */
813 pw = getpwuid (getuid ());
818 g_user_name = g_strdup (pw->pw_name);
819 g_real_name = g_strdup (pw->pw_gecos);
821 g_home_dir = g_strdup (pw->pw_dir);
826 #else /* !HAVE_PWD_H */
833 if (GetUserName ((LPTSTR) buffer, (LPDWORD) &len))
835 g_user_name = g_strdup (buffer);
836 g_real_name = g_strdup (buffer);
839 # endif /* G_OS_WIN32 */
841 #endif /* !HAVE_PWD_H */
844 /* change '\\' in %HOME% to '/' */
845 g_strdelimit (g_home_dir, "\\",'/');
848 g_user_name = g_strdup ("somebody");
850 g_real_name = g_strdup ("Unknown");
855 for (p = g_real_name; *p; p++)
859 p = g_strdup (g_real_name);
860 g_free (g_real_name);
869 g_get_user_name (void)
871 G_LOCK (g_utils_global);
874 G_UNLOCK (g_utils_global);
880 g_get_real_name (void)
882 G_LOCK (g_utils_global);
885 G_UNLOCK (g_utils_global);
890 /* Return the home directory of the user. If there is a HOME
891 * environment variable, its value is returned, otherwise use some
892 * system-dependent way of finding it out. If no home directory can be
893 * deduced, return NULL.
897 g_get_home_dir (void)
899 G_LOCK (g_utils_global);
902 G_UNLOCK (g_utils_global);
907 /* Return a directory to be used to store temporary files. This is the
908 * value of the TMPDIR, TMP or TEMP environment variables (they are
909 * checked in that order). If none of those exist, use P_tmpdir from
910 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
911 * and C:\ on Windows.
917 G_LOCK (g_utils_global);
920 G_UNLOCK (g_utils_global);
925 static gchar *g_prgname = NULL;
932 G_LOCK (g_utils_global);
934 G_UNLOCK (g_utils_global);
940 g_set_prgname (const gchar *prgname)
944 G_LOCK (g_utils_global);
946 g_prgname = g_strdup (prgname);
948 G_UNLOCK (g_utils_global);
952 g_direct_hash (gconstpointer v)
954 return GPOINTER_TO_UINT (v);
958 g_direct_equal (gconstpointer v1,
965 g_int_equal (gconstpointer v1,
968 return *((const gint*) v1) == *((const gint*) v2);
972 g_int_hash (gconstpointer v)
974 return *(const gint*) v;
980 * Get the codeset for the current locale.
982 * Return value: a newly allocated string containing the name
983 * of the codeset. This string must be freed with g_free().
989 char *result = nl_langinfo (CODESET);
990 return g_strdup (result);
993 /* FIXME: Do something more intelligent based on setlocale (LC_CTYPE, NULL)
995 return g_strdup ("ISO-8859-1");
997 /* On Win32 we always use UTF-8. At least in GDK. SO should we
998 * therefore return that?
1000 return g_strdup ("UTF-8");
1007 #include <libintl.h>
1012 #define GLIB_LOCALE_DIR \
1013 g_strconcat (g_win32_get_package_installation_directory (GETTEXT_PACKAGE), \
1014 G_DIR_SEPARATOR_S, \
1018 #endif /* G_OS_WIN32 */
1021 _glib_gettext (const gchar *str)
1023 gboolean _glib_gettext_initialized = FALSE;
1025 if (!_glib_gettext_initialized)
1027 bindtextdomain(GETTEXT_PACKAGE, GLIB_LOCALE_DIR);
1028 _glib_gettext_initialized = TRUE;
1031 return dgettext (GETTEXT_PACKAGE, str);
1034 #endif /* ENABLE_NLS */