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);
384 static gchar *p = NULL;
388 g_return_val_if_fail (variable != NULL, NULL);
390 v = getenv (variable);
394 /* On Windows NT, it is relatively typical that environment variables
395 * contain references to other environment variables. Handle that by
396 * calling ExpandEnvironmentStrings.
399 /* First check how much space we need */
400 k = ExpandEnvironmentStrings (v, dummy, 2);
401 /* Then allocate that much, and actualy do the expansion */
409 p = g_realloc (p, k);
412 ExpandEnvironmentStrings (v, p, k);
418 G_LOCK_DEFINE_STATIC (g_utils_global);
420 static gchar *g_tmp_dir = NULL;
421 static gchar *g_user_name = NULL;
422 static gchar *g_real_name = NULL;
423 static gchar *g_home_dir = NULL;
425 /* HOLDS: g_utils_global_lock */
427 g_get_any_init (void)
431 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
433 g_tmp_dir = g_strdup (g_getenv ("TMP"));
435 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
441 g_tmp_dir = g_strdup (P_tmpdir);
442 k = strlen (g_tmp_dir);
443 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
444 g_tmp_dir[k-1] = '\0';
451 g_tmp_dir = g_strdup ("/tmp");
452 #else /* G_OS_WIN32 */
453 g_tmp_dir = g_strdup ("C:\\");
454 #endif /* G_OS_WIN32 */
458 g_home_dir = g_strdup (g_getenv ("HOME"));
463 /* The official way to specify a home directory on NT is
464 * the HOMEDRIVE and HOMEPATH environment variables.
466 * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
467 * HOME should be a POSIX style pathname.
470 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
472 gchar *homedrive, *homepath;
474 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
475 homepath = g_strdup (g_getenv ("HOMEPATH"));
477 g_home_dir = g_strconcat (homedrive, homepath, NULL);
482 #endif /* G_OS_WIN32 */
486 struct passwd *pw = NULL;
487 gpointer buffer = NULL;
489 # ifdef HAVE_GETPWUID_R
491 # ifdef _SC_GETPW_R_SIZE_MAX
492 /* This reurns the maximum length */
493 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
494 # else /* _SC_GETPW_R_SIZE_MAX */
496 # endif /* _SC_GETPW_R_SIZE_MAX */
502 buffer = g_malloc (bufsize);
505 # ifdef HAVE_GETPWUID_R_POSIX
506 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
507 error = error < 0 ? errno : error;
508 # else /* !HAVE_GETPWUID_R_POSIX */
510 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
511 pw = error == 0 ? &pwd : NULL;
513 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
514 error = pw ? 0 : errno;
516 # endif /* !HAVE_GETPWUID_R_POSIX */
520 /* we bail out prematurely if the user id can't be found
521 * (should be pretty rare case actually), or if the buffer
522 * should be sufficiently big and lookups are still not
525 if (error == 0 || error == ENOENT)
527 g_warning ("getpwuid_r(): failed due to unknown user id (%lu)",
531 if (bufsize > 32 * 1024)
533 g_warning ("getpwuid_r(): failed due to: %s.",
542 # endif /* !HAVE_GETPWUID_R */
547 pw = getpwuid (getuid ());
552 g_user_name = g_strdup (pw->pw_name);
553 g_real_name = g_strdup (pw->pw_gecos);
555 g_home_dir = g_strdup (pw->pw_dir);
560 #else /* !HAVE_PWD_H */
567 if (GetUserName (buffer, &len))
569 g_user_name = g_strdup (buffer);
570 g_real_name = g_strdup (buffer);
573 # endif /* G_OS_WIN32 */
575 #endif /* !HAVE_PWD_H */
578 /* change '\\' in %HOME% to '/' */
579 g_strdelimit (g_home_dir, "\\",'/');
582 g_user_name = g_strdup ("somebody");
584 g_real_name = g_strdup ("Unknown");
589 for (p = g_real_name; *p; p++)
593 p = g_strdup (g_real_name);
594 g_free (g_real_name);
603 g_get_user_name (void)
605 G_LOCK (g_utils_global);
608 G_UNLOCK (g_utils_global);
614 g_get_real_name (void)
616 G_LOCK (g_utils_global);
619 G_UNLOCK (g_utils_global);
624 /* Return the home directory of the user. If there is a HOME
625 * environment variable, its value is returned, otherwise use some
626 * system-dependent way of finding it out. If no home directory can be
627 * deduced, return NULL.
631 g_get_home_dir (void)
633 G_LOCK (g_utils_global);
636 G_UNLOCK (g_utils_global);
641 /* Return a directory to be used to store temporary files. This is the
642 * value of the TMPDIR, TMP or TEMP environment variables (they are
643 * checked in that order). If none of those exist, use P_tmpdir from
644 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
645 * and C:\ on Windows.
651 G_LOCK (g_utils_global);
654 G_UNLOCK (g_utils_global);
659 static gchar *g_prgname = NULL;
666 G_LOCK (g_utils_global);
668 G_UNLOCK (g_utils_global);
674 g_set_prgname (const gchar *prgname)
678 G_LOCK (g_utils_global);
680 g_prgname = g_strdup (prgname);
682 G_UNLOCK (g_utils_global);
686 g_direct_hash (gconstpointer v)
688 return GPOINTER_TO_UINT (v);
692 g_direct_equal (gconstpointer v1,
699 g_int_equal (gconstpointer v1,
702 return *((const gint*) v1) == *((const gint*) v2);
706 g_int_hash (gconstpointer v)
708 return *(const gint*) v;