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>
51 /* implement Glib's inline functions
53 #define G_INLINE_FUNC extern
54 #define G_CAN_INLINE 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 */
75 const guint glib_major_version = GLIB_MAJOR_VERSION;
76 const guint glib_minor_version = GLIB_MINOR_VERSION;
77 const guint glib_micro_version = GLIB_MICRO_VERSION;
78 const guint glib_interface_age = GLIB_INTERFACE_AGE;
79 const guint glib_binary_age = GLIB_BINARY_AGE;
81 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
83 g_memmove (gpointer dest, gconstpointer src, gulong len)
85 gchar* destptr = dest;
86 const gchar* srcptr = src;
87 if (src + len < dest || dest + len < src)
89 bcopy (src, dest, len);
95 *(destptr++) = *(srcptr++);
102 *(--destptr) = *(--srcptr);
105 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
108 g_atexit (GVoidFunc func)
113 /* keep this in sync with glib.h */
115 #ifdef G_NATIVE_ATEXIT
116 result = ATEXIT (func);
118 error = g_strerror (errno);
119 #elif defined (HAVE_ATEXIT)
120 # ifdef NeXT /* @#%@! NeXTStep */
121 result = !atexit ((void (*)(void)) func);
123 error = g_strerror (errno);
125 result = atexit ((void (*)(void)) func);
127 error = g_strerror (errno);
129 #elif defined (HAVE_ON_EXIT)
130 result = on_exit ((void (*)(int, void *)) func, NULL);
132 error = g_strerror (errno);
135 error = "no implementation";
136 #endif /* G_NATIVE_ATEXIT */
139 g_error ("Could not register atexit() function: %s", error);
143 g_snprintf (gchar *str,
148 #ifdef HAVE_VSNPRINTF
152 va_start (args, fmt);
153 retval = vsnprintf (str, n, fmt, args);
159 retval = strlen (str);
163 #else /* !HAVE_VSNPRINTF */
167 va_start (args, fmt);
168 printed = g_strdup_vprintf (fmt, args);
171 strncpy (str, printed, n);
177 #endif /* !HAVE_VSNPRINTF */
181 g_vsnprintf (gchar *str,
186 #ifdef HAVE_VSNPRINTF
189 retval = vsnprintf (str, n, fmt, args);
194 retval = strlen (str);
198 #else /* !HAVE_VSNPRINTF */
201 printed = g_strdup_vprintf (fmt, args);
202 strncpy (str, printed, n);
208 #endif /* !HAVE_VSNPRINTF */
212 g_parse_debug_string (const gchar *string,
219 g_return_val_if_fail (string != NULL, 0);
221 if (!g_strcasecmp (string, "all"))
223 for (i=0; i<nkeys; i++)
224 result |= keys[i].value;
228 gchar *str = g_strdup (string);
231 gboolean done = FALSE;
244 for (i=0; i<nkeys; i++)
245 if (!g_strcasecmp(keys[i].key, p))
246 result |= keys[i].value;
258 g_basename (const gchar *file_name)
260 register gchar *base;
262 g_return_val_if_fail (file_name != NULL, NULL);
264 base = strrchr (file_name, G_DIR_SEPARATOR);
269 if (isalpha (file_name[0]) && file_name[1] == ':')
270 return (gchar*) file_name + 2;
271 #endif /* G_OS_WIN32 */
273 return (gchar*) file_name;
277 g_path_is_absolute (const gchar *file_name)
279 g_return_val_if_fail (file_name != NULL, FALSE);
281 if (file_name[0] == G_DIR_SEPARATOR)
285 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
293 g_path_skip_root (gchar *file_name)
295 g_return_val_if_fail (file_name != NULL, NULL);
297 if (file_name[0] == G_DIR_SEPARATOR)
298 return file_name + 1;
301 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
302 return file_name + 3;
309 g_dirname (const gchar *file_name)
311 register gchar *base;
314 g_return_val_if_fail (file_name != NULL, NULL);
316 base = strrchr (file_name, G_DIR_SEPARATOR);
318 return g_strdup (".");
319 while (base > file_name && *base == G_DIR_SEPARATOR)
321 len = (guint) 1 + base - file_name;
323 base = g_new (gchar, len + 1);
324 g_memmove (base, file_name, len);
331 g_get_current_dir (void)
333 gchar *buffer = NULL;
335 static gulong max_len = (G_PATH_LENGTH == -1) ? 2048 : G_PATH_LENGTH;
337 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
338 * and, if that wasn't bad enough, hangs in doing so.
340 #if defined (sun) && !defined (__SVR4)
341 buffer = g_new (gchar, max_len + 1);
343 dir = getwd (buffer);
345 while (max_len < G_MAXULONG / 2)
347 buffer = g_new (gchar, max_len + 1);
349 dir = getcwd (buffer, max_len);
351 if (dir || errno != ERANGE)
359 if (!dir || !*buffer)
361 /* hm, should we g_error() out here?
362 * this can happen if e.g. "./" has mode \0000
364 buffer[0] = G_DIR_SEPARATOR;
368 dir = g_strdup (buffer);
375 g_getenv (const gchar *variable)
378 g_return_val_if_fail (variable != NULL, NULL);
380 return getenv (variable);
382 G_LOCK_DEFINE_STATIC (getenv);
388 static GArray *environs = NULL;
393 g_return_val_if_fail (variable != NULL, NULL);
398 environs = g_array_new (FALSE, FALSE, sizeof (struct env_struct));
400 /* First we try to find the envinronment variable inside the already
404 for (i = 0; i < environs->len; i++)
406 env = &g_array_index (environs, struct env_struct, i);
407 if (strcmp (env->key, variable) == 0)
409 g_assert (env->value);
415 /* If not found, we ask the system */
417 system_env = getenv (variable);
424 /* On Windows NT, it is relatively typical that environment variables
425 * contain references to other environment variables. Handle that by
426 * calling ExpandEnvironmentStrings.
429 g_array_set_size (environs, environs->len + 1);
431 env = &g_array_index (environs, struct env_struct, environs->len - 1);
433 /* First check how much space we need */
434 length = ExpandEnvironmentStrings (system_env, dummy, 2);
436 /* Then allocate that much, and actualy do the expansion and insert
437 * the new found pair into our buffer
440 env->value = g_malloc (length);
441 env->key = g_strdup (variable);
443 ExpandEnvironmentStrings (system_env, env->value, length);
451 G_LOCK_DEFINE_STATIC (g_utils_global);
453 static gchar *g_tmp_dir = NULL;
454 static gchar *g_user_name = NULL;
455 static gchar *g_real_name = NULL;
456 static gchar *g_home_dir = NULL;
458 /* HOLDS: g_utils_global_lock */
460 g_get_any_init (void)
464 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
466 g_tmp_dir = g_strdup (g_getenv ("TMP"));
468 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
474 g_tmp_dir = g_strdup (P_tmpdir);
475 k = strlen (g_tmp_dir);
476 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
477 g_tmp_dir[k-1] = '\0';
484 g_tmp_dir = g_strdup ("/tmp");
485 #else /* G_OS_WIN32 */
486 g_tmp_dir = g_strdup ("C:\\");
487 #endif /* G_OS_WIN32 */
491 g_home_dir = g_strdup (g_getenv ("HOME"));
496 /* The official way to specify a home directory on NT is
497 * the HOMEDRIVE and HOMEPATH environment variables.
499 * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
500 * HOME should be a POSIX style pathname.
503 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
505 gchar *homedrive, *homepath;
507 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
508 homepath = g_strdup (g_getenv ("HOMEPATH"));
510 g_home_dir = g_strconcat (homedrive, homepath, NULL);
515 #endif /* G_OS_WIN32 */
519 struct passwd *pw = NULL;
520 gpointer buffer = NULL;
522 # ifdef HAVE_GETPWUID_R
524 # ifdef _SC_GETPW_R_SIZE_MAX
525 /* This reurns the maximum length */
526 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
527 # else /* _SC_GETPW_R_SIZE_MAX */
529 # endif /* _SC_GETPW_R_SIZE_MAX */
535 buffer = g_malloc (bufsize);
538 # ifdef HAVE_GETPWUID_R_POSIX
539 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
540 error = error < 0 ? errno : error;
541 # else /* !HAVE_GETPWUID_R_POSIX */
543 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
544 pw = error == 0 ? &pwd : NULL;
546 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
547 error = pw ? 0 : errno;
549 # endif /* !HAVE_GETPWUID_R_POSIX */
553 /* we bail out prematurely if the user id can't be found
554 * (should be pretty rare case actually), or if the buffer
555 * should be sufficiently big and lookups are still not
558 if (error == 0 || error == ENOENT)
560 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
564 if (bufsize > 32 * 1024)
566 g_warning ("getpwuid_r(): failed due to: %s.",
575 # endif /* !HAVE_GETPWUID_R */
580 pw = getpwuid (getuid ());
585 g_user_name = g_strdup (pw->pw_name);
586 g_real_name = g_strdup (pw->pw_gecos);
588 g_home_dir = g_strdup (pw->pw_dir);
593 #else /* !HAVE_PWD_H */
600 if (GetUserName (buffer, &len))
602 g_user_name = g_strdup (buffer);
603 g_real_name = g_strdup (buffer);
606 # endif /* G_OS_WIN32 */
608 #endif /* !HAVE_PWD_H */
611 /* change '\\' in %HOME% to '/' */
612 g_strdelimit (g_home_dir, "\\",'/');
615 g_user_name = g_strdup ("somebody");
617 g_real_name = g_strdup ("Unknown");
622 for (p = g_real_name; *p; p++)
626 p = g_strdup (g_real_name);
627 g_free (g_real_name);
636 g_get_user_name (void)
638 G_LOCK (g_utils_global);
641 G_UNLOCK (g_utils_global);
647 g_get_real_name (void)
649 G_LOCK (g_utils_global);
652 G_UNLOCK (g_utils_global);
657 /* Return the home directory of the user. If there is a HOME
658 * environment variable, its value is returned, otherwise use some
659 * system-dependent way of finding it out. If no home directory can be
660 * deduced, return NULL.
664 g_get_home_dir (void)
666 G_LOCK (g_utils_global);
669 G_UNLOCK (g_utils_global);
674 /* Return a directory to be used to store temporary files. This is the
675 * value of the TMPDIR, TMP or TEMP environment variables (they are
676 * checked in that order). If none of those exist, use P_tmpdir from
677 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
678 * and C:\ on Windows.
684 G_LOCK (g_utils_global);
687 G_UNLOCK (g_utils_global);
692 static gchar *g_prgname = NULL;
699 G_LOCK (g_utils_global);
701 G_UNLOCK (g_utils_global);
707 g_set_prgname (const gchar *prgname)
711 G_LOCK (g_utils_global);
713 g_prgname = g_strdup (prgname);
715 G_UNLOCK (g_utils_global);
719 g_direct_hash (gconstpointer v)
721 return GPOINTER_TO_UINT (v);
725 g_direct_equal (gconstpointer v1,
732 g_int_equal (gconstpointer v1,
735 return *((const gint*) v1) == *((const gint*) v2);
739 g_int_hash (gconstpointer v)
741 return *(const gint*) v;