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.
35 #include "glibconfig.h"
48 #include <sys/types.h>
49 #ifdef HAVE_SYS_PARAM_H
50 #include <sys/param.h>
54 # define STRICT /* Strict typing, please */
60 # endif /* _MSC_VER */
61 #endif /* G_OS_WIN32 */
63 /* implement Glib's inline functions
65 #define G_INLINE_FUNC extern
66 #define G_CAN_INLINE 1
70 #define G_PATH_LENGTH (MAXPATHLEN + 1)
71 #elif defined (PATH_MAX)
72 #define G_PATH_LENGTH (PATH_MAX + 1)
73 #else /* !MAXPATHLEN */
74 #define G_PATH_LENGTH (2048 + 1)
75 #endif /* !MAXPATHLEN && !PATH_MAX */
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)
338 buffer = g_new (gchar, G_PATH_LENGTH);
341 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
342 * and, if that wasn't bad enough, hangs in doing so.
344 #if defined (sun) && !defined (__SVR4)
345 dir = getwd (buffer);
347 dir = getcwd (buffer, G_PATH_LENGTH - 1);
350 if (!dir || !*buffer)
352 /* hm, should we g_error() out here?
353 * this can happen if e.g. "./" has mode \0000
355 buffer[0] = G_DIR_SEPARATOR;
359 dir = g_strdup (buffer);
366 g_getenv (const gchar *variable)
369 g_return_val_if_fail (variable != NULL, NULL);
371 return getenv (variable);
375 static gchar *p = NULL;
379 g_return_val_if_fail (variable != NULL, NULL);
381 v = getenv (variable);
385 /* On Windows NT, it is relatively typical that environment variables
386 * contain references to other environment variables. Handle that by
387 * calling ExpandEnvironmentStrings.
390 /* First check how much space we need */
391 k = ExpandEnvironmentStrings (v, dummy, 2);
392 /* Then allocate that much, and actualy do the expansion */
400 p = g_realloc (p, k);
403 ExpandEnvironmentStrings (v, p, k);
409 G_LOCK_DEFINE_STATIC (g_utils_global);
411 static gchar *g_tmp_dir = NULL;
412 static gchar *g_user_name = NULL;
413 static gchar *g_real_name = NULL;
414 static gchar *g_home_dir = NULL;
416 /* HOLDS: g_utils_global_lock */
418 g_get_any_init (void)
422 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
424 g_tmp_dir = g_strdup (g_getenv ("TMP"));
426 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
432 g_tmp_dir = g_strdup (P_tmpdir);
433 k = strlen (g_tmp_dir);
434 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
435 g_tmp_dir[k-1] = '\0';
442 g_tmp_dir = g_strdup ("/tmp");
443 #else /* G_OS_WIN32 */
444 g_tmp_dir = g_strdup ("C:\\");
445 #endif /* G_OS_WIN32 */
449 g_home_dir = g_strdup (g_getenv ("HOME"));
454 /* The official way to specify a home directory on NT is
455 * the HOMEDRIVE and HOMEPATH environment variables.
457 * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
458 * HOME should be a POSIX style pathname.
461 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
463 gchar *homedrive, *homepath;
465 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
466 homepath = g_strdup (g_getenv ("HOMEPATH"));
468 g_home_dir = g_strconcat (homedrive, homepath, NULL);
473 #endif /* !G_OS_WIN32 */
477 struct passwd *pw = NULL;
478 gpointer buffer = NULL;
480 # ifdef HAVE_GETPWUID_R
482 # ifdef _SC_GETPW_R_SIZE_MAX
483 /* This reurns the maximum length */
484 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
485 # else /* _SC_GETPW_R_SIZE_MAX */
487 # endif /* _SC_GETPW_R_SIZE_MAX */
493 buffer = g_malloc (bufsize);
496 # ifdef HAVE_GETPWUID_R_POSIX
497 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
498 error = error < 0 ? errno : error;
499 # else /* !HAVE_GETPWUID_R_POSIX */
501 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
502 pw = error == 0 ? &pwd : NULL;
504 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
505 error = pw ? 0 : errno;
507 # endif /* !HAVE_GETPWUID_R_POSIX */
511 /* we bail out prematurely if the user id can't be found
512 * (should be pretty rare case actually), or if the buffer
513 * should be sufficiently big and lookups are still not
516 if (error == 0 || error == ENOENT)
518 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
522 if (bufsize > 32 * 1024)
524 g_warning ("getpwuid_r(): failed due to: %s.",
533 # endif /* !HAVE_GETPWUID_R */
538 pw = getpwuid (getuid ());
543 g_user_name = g_strdup (pw->pw_name);
544 g_real_name = g_strdup (pw->pw_gecos);
546 g_home_dir = g_strdup (pw->pw_dir);
551 #else /* !HAVE_PWD_H */
558 if (GetUserName (buffer, &len))
560 g_user_name = g_strdup (buffer);
561 g_real_name = g_strdup (buffer);
564 # endif /* G_OS_WIN32 */
566 #endif /* !HAVE_PWD_H */
569 /* change '\\' in %HOME% to '/' */
570 g_strdelimit (g_home_dir, "\\",'/');
573 g_user_name = g_strdup ("somebody");
575 g_real_name = g_strdup ("Unknown");
580 for (p = g_real_name; *p; p++)
584 p = g_strdup (g_real_name);
585 g_free (g_real_name);
594 g_get_user_name (void)
596 G_LOCK (g_utils_global);
599 G_UNLOCK (g_utils_global);
605 g_get_real_name (void)
607 G_LOCK (g_utils_global);
610 G_UNLOCK (g_utils_global);
615 /* Return the home directory of the user. If there is a HOME
616 * environment variable, its value is returned, otherwise use some
617 * system-dependent way of finding it out. If no home directory can be
618 * deduced, return NULL.
622 g_get_home_dir (void)
624 G_LOCK (g_utils_global);
627 G_UNLOCK (g_utils_global);
632 /* Return a directory to be used to store temporary files. This is the
633 * value of the TMPDIR, TMP or TEMP environment variables (they are
634 * checked in that order). If none of those exist, use P_tmpdir from
635 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
636 * and C:\ on Windows.
642 G_LOCK (g_utils_global);
645 G_UNLOCK (g_utils_global);
650 static gchar *g_prgname = NULL;
657 G_LOCK (g_utils_global);
659 G_UNLOCK (g_utils_global);
665 g_set_prgname (const gchar *prgname)
669 G_LOCK (g_utils_global);
671 g_prgname = g_strdup (prgname);
673 G_UNLOCK (g_utils_global);
677 g_direct_hash (gconstpointer v)
679 return GPOINTER_TO_UINT (v);
683 g_direct_equal (gconstpointer v1,
690 g_int_equal (gconstpointer v1,
693 return *((const gint*) v1) == *((const gint*) v2);
697 g_int_hash (gconstpointer v)
699 return *(const gint*) v;