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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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>
52 # define STRICT /* Strict typing, please */
58 # endif /* _MSC_VER */
59 #endif /* G_OS_WIN32 */
61 /* implement Glib's inline functions
63 #define G_INLINE_FUNC extern
64 #define G_CAN_INLINE 1
68 #define G_PATH_LENGTH MAXPATHLEN
69 #elif defined (PATH_MAX)
70 #define G_PATH_LENGTH PATH_MAX
71 #elif defined (_PC_PATH_MAX)
72 #define G_PATH_LENGTH sysconf(_PC_PATH_MAX)
74 #define G_PATH_LENGTH 2048
77 const guint glib_major_version = GLIB_MAJOR_VERSION;
78 const guint glib_minor_version = GLIB_MINOR_VERSION;
79 const guint glib_micro_version = GLIB_MICRO_VERSION;
80 const guint glib_interface_age = GLIB_INTERFACE_AGE;
81 const guint glib_binary_age = GLIB_BINARY_AGE;
83 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
85 g_memmove (gpointer dest, gconstpointer src, gulong len)
87 gchar* destptr = dest;
88 const gchar* srcptr = src;
89 if (src + len < dest || dest + len < src)
91 bcopy (src, dest, len);
97 *(destptr++) = *(srcptr++);
104 *(--destptr) = *(--srcptr);
107 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
110 g_atexit (GVoidFunc func)
115 /* keep this in sync with glib.h */
117 #ifdef G_NATIVE_ATEXIT
118 result = ATEXIT (func);
120 error = g_strerror (errno);
121 #elif defined (HAVE_ATEXIT)
122 # ifdef NeXT /* @#%@! NeXTStep */
123 result = !atexit ((void (*)(void)) func);
125 error = g_strerror (errno);
127 result = atexit ((void (*)(void)) func);
129 error = g_strerror (errno);
131 #elif defined (HAVE_ON_EXIT)
132 result = on_exit ((void (*)(int, void *)) func, NULL);
134 error = g_strerror (errno);
137 error = "no implementation";
138 #endif /* G_NATIVE_ATEXIT */
141 g_error ("Could not register atexit() function: %s", error);
145 g_snprintf (gchar *str,
150 #ifdef HAVE_VSNPRINTF
154 va_start (args, fmt);
155 retval = vsnprintf (str, n, fmt, args);
161 retval = strlen (str);
165 #else /* !HAVE_VSNPRINTF */
169 va_start (args, fmt);
170 printed = g_strdup_vprintf (fmt, args);
173 strncpy (str, printed, n);
179 #endif /* !HAVE_VSNPRINTF */
183 g_vsnprintf (gchar *str,
188 #ifdef HAVE_VSNPRINTF
191 retval = vsnprintf (str, n, fmt, args);
196 retval = strlen (str);
200 #else /* !HAVE_VSNPRINTF */
203 printed = g_strdup_vprintf (fmt, args);
204 strncpy (str, printed, n);
210 #endif /* !HAVE_VSNPRINTF */
214 g_parse_debug_string (const gchar *string,
221 g_return_val_if_fail (string != NULL, 0);
223 if (!g_strcasecmp (string, "all"))
225 for (i=0; i<nkeys; i++)
226 result |= keys[i].value;
230 gchar *str = g_strdup (string);
233 gboolean done = FALSE;
246 for (i=0; i<nkeys; i++)
247 if (!g_strcasecmp(keys[i].key, p))
248 result |= keys[i].value;
260 g_basename (const gchar *file_name)
262 register gchar *base;
264 g_return_val_if_fail (file_name != NULL, NULL);
266 base = strrchr (file_name, G_DIR_SEPARATOR);
271 if (isalpha (file_name[0]) && file_name[1] == ':')
272 return (gchar*) file_name + 2;
273 #endif /* G_OS_WIN32 */
275 return (gchar*) file_name;
279 g_path_is_absolute (const gchar *file_name)
281 g_return_val_if_fail (file_name != NULL, FALSE);
283 if (file_name[0] == G_DIR_SEPARATOR)
287 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
295 g_path_skip_root (gchar *file_name)
297 g_return_val_if_fail (file_name != NULL, NULL);
299 if (file_name[0] == G_DIR_SEPARATOR)
300 return file_name + 1;
303 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
304 return file_name + 3;
311 g_dirname (const gchar *file_name)
313 register gchar *base;
316 g_return_val_if_fail (file_name != NULL, NULL);
318 base = strrchr (file_name, G_DIR_SEPARATOR);
320 return g_strdup (".");
321 while (base > file_name && *base == G_DIR_SEPARATOR)
323 len = (guint) 1 + base - file_name;
325 base = g_new (gchar, len + 1);
326 g_memmove (base, file_name, len);
333 g_get_current_dir (void)
335 gchar *buffer = NULL;
337 static gulong max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
339 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
340 * and, if that wasn't bad enough, hangs in doing so.
342 #if defined (sun) && !defined (__SVR4)
343 buffer = g_new (gchar, max_len + 1);
345 dir = getwd (buffer);
347 while (max_len < 128 * 1024)
349 buffer = g_new (gchar, max_len + 1);
351 dir = getcwd (buffer, max_len);
353 if (dir || errno != ERANGE)
361 if (!dir || !*buffer)
363 /* hm, should we g_error() out here?
364 * this can happen if e.g. "./" has mode \0000
366 buffer[0] = G_DIR_SEPARATOR;
370 dir = g_strdup (buffer);
377 g_getenv (const gchar *variable)
380 g_return_val_if_fail (variable != NULL, NULL);
382 return getenv (variable);
386 static gchar *p = NULL;
390 g_return_val_if_fail (variable != NULL, NULL);
392 v = getenv (variable);
396 /* On Windows NT, it is relatively typical that environment variables
397 * contain references to other environment variables. Handle that by
398 * calling ExpandEnvironmentStrings.
401 /* First check how much space we need */
402 k = ExpandEnvironmentStrings (v, dummy, 2);
403 /* Then allocate that much, and actualy do the expansion */
411 p = g_realloc (p, k);
414 ExpandEnvironmentStrings (v, p, k);
420 G_LOCK_DEFINE_STATIC (g_utils_global);
422 static gchar *g_tmp_dir = NULL;
423 static gchar *g_user_name = NULL;
424 static gchar *g_real_name = NULL;
425 static gchar *g_home_dir = NULL;
427 /* HOLDS: g_utils_global_lock */
429 g_get_any_init (void)
433 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
435 g_tmp_dir = g_strdup (g_getenv ("TMP"));
437 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
443 g_tmp_dir = g_strdup (P_tmpdir);
444 k = strlen (g_tmp_dir);
445 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
446 g_tmp_dir[k-1] = '\0';
453 g_tmp_dir = g_strdup ("/tmp");
454 #else /* G_OS_WIN32 */
455 g_tmp_dir = g_strdup ("C:\\");
456 #endif /* G_OS_WIN32 */
460 g_home_dir = g_strdup (g_getenv ("HOME"));
465 /* The official way to specify a home directory on NT is
466 * the HOMEDRIVE and HOMEPATH environment variables.
468 * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
469 * HOME should be a POSIX style pathname.
472 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
474 gchar *homedrive, *homepath;
476 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
477 homepath = g_strdup (g_getenv ("HOMEPATH"));
479 g_home_dir = g_strconcat (homedrive, homepath, NULL);
484 #endif /* !G_OS_WIN32 */
488 struct passwd *pw = NULL;
489 gpointer buffer = NULL;
491 # ifdef HAVE_GETPWUID_R
493 # ifdef _SC_GETPW_R_SIZE_MAX
494 /* This reurns the maximum length */
495 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
496 # else /* _SC_GETPW_R_SIZE_MAX */
498 # endif /* _SC_GETPW_R_SIZE_MAX */
504 buffer = g_malloc (bufsize);
507 # ifdef HAVE_GETPWUID_R_POSIX
508 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
509 error = error < 0 ? errno : error;
510 # else /* !HAVE_GETPWUID_R_POSIX */
512 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
513 pw = error == 0 ? &pwd : NULL;
515 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
516 error = pw ? 0 : errno;
518 # endif /* !HAVE_GETPWUID_R_POSIX */
522 /* we bail out prematurely if the user id can't be found
523 * (should be pretty rare case actually), or if the buffer
524 * should be sufficiently big and lookups are still not
527 if (error == 0 || error == ENOENT)
529 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
533 if (bufsize > 32 * 1024)
535 g_warning ("getpwuid_r(): failed due to: %s.",
544 # endif /* !HAVE_GETPWUID_R */
549 pw = getpwuid (getuid ());
554 g_user_name = g_strdup (pw->pw_name);
555 g_real_name = g_strdup (pw->pw_gecos);
557 g_home_dir = g_strdup (pw->pw_dir);
562 #else /* !HAVE_PWD_H */
569 if (GetUserName (buffer, &len))
571 g_user_name = g_strdup (buffer);
572 g_real_name = g_strdup (buffer);
575 # endif /* G_OS_WIN32 */
577 #endif /* !HAVE_PWD_H */
580 /* change '\\' in %HOME% to '/' */
581 g_strdelimit (g_home_dir, "\\",'/');
584 g_user_name = g_strdup ("somebody");
586 g_real_name = g_strdup ("Unknown");
591 for (p = g_real_name; *p; p++)
595 p = g_strdup (g_real_name);
596 g_free (g_real_name);
605 g_get_user_name (void)
607 G_LOCK (g_utils_global);
610 G_UNLOCK (g_utils_global);
616 g_get_real_name (void)
618 G_LOCK (g_utils_global);
621 G_UNLOCK (g_utils_global);
626 /* Return the home directory of the user. If there is a HOME
627 * environment variable, its value is returned, otherwise use some
628 * system-dependent way of finding it out. If no home directory can be
629 * deduced, return NULL.
633 g_get_home_dir (void)
635 G_LOCK (g_utils_global);
638 G_UNLOCK (g_utils_global);
643 /* Return a directory to be used to store temporary files. This is the
644 * value of the TMPDIR, TMP or TEMP environment variables (they are
645 * checked in that order). If none of those exist, use P_tmpdir from
646 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
647 * and C:\ on Windows.
653 G_LOCK (g_utils_global);
656 G_UNLOCK (g_utils_global);
661 static gchar *g_prgname = NULL;
668 G_LOCK (g_utils_global);
670 G_UNLOCK (g_utils_global);
676 g_set_prgname (const gchar *prgname)
680 G_LOCK (g_utils_global);
682 g_prgname = g_strdup (prgname);
684 G_UNLOCK (g_utils_global);
688 g_direct_hash (gconstpointer v)
690 return GPOINTER_TO_UINT (v);
694 g_direct_equal (gconstpointer v1,
701 g_int_equal (gconstpointer v1,
704 return *((const gint*) v1) == *((const gint*) v2);
708 g_int_hash (gconstpointer v)
710 return *(const gint*) v;