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 */
61 # endif /* _MSC_VER */
62 #endif /* G_OS_WIN32 */
64 /* implement Glib's inline functions
66 #define G_INLINE_FUNC extern
67 #define G_CAN_INLINE 1
71 #define G_PATH_LENGTH (MAXPATHLEN + 1)
72 #elif defined (PATH_MAX)
73 #define G_PATH_LENGTH (PATH_MAX + 1)
74 #else /* !MAXPATHLEN */
75 #define G_PATH_LENGTH (2048 + 1)
76 #endif /* !MAXPATHLEN && !PATH_MAX */
78 const guint glib_major_version = GLIB_MAJOR_VERSION;
79 const guint glib_minor_version = GLIB_MINOR_VERSION;
80 const guint glib_micro_version = GLIB_MICRO_VERSION;
81 const guint glib_interface_age = GLIB_INTERFACE_AGE;
82 const guint glib_binary_age = GLIB_BINARY_AGE;
84 #if !defined (HAVE_MEMMOVE) && !defined (HAVE_WORKING_BCOPY)
86 g_memmove (gpointer dest, gconstpointer src, gulong len)
88 gchar* destptr = dest;
89 const gchar* srcptr = src;
90 if (src + len < dest || dest + len < src)
92 bcopy (src, dest, len);
98 *(destptr++) = *(srcptr++);
105 *(--destptr) = *(--srcptr);
108 #endif /* !HAVE_MEMMOVE && !HAVE_WORKING_BCOPY */
111 g_atexit (GVoidFunc func)
116 /* keep this in sync with glib.h */
118 #ifdef G_NATIVE_ATEXIT
119 result = ATEXIT (func);
121 error = g_strerror (errno);
122 #elif defined (HAVE_ATEXIT)
123 # ifdef NeXT /* @#%@! NeXTStep */
124 result = !atexit ((void (*)(void)) func);
126 error = g_strerror (errno);
128 result = atexit ((void (*)(void)) func);
130 error = g_strerror (errno);
132 #elif defined (HAVE_ON_EXIT)
133 result = on_exit ((void (*)(int, void *)) func, NULL);
135 error = g_strerror (errno);
138 error = "no implementation";
139 #endif /* G_NATIVE_ATEXIT */
142 g_error ("Could not register atexit() function: %s", error);
146 g_snprintf (gchar *str,
151 #ifdef HAVE_VSNPRINTF
155 va_start (args, fmt);
156 retval = vsnprintf (str, n, fmt, args);
162 retval = strlen (str);
166 #else /* !HAVE_VSNPRINTF */
170 va_start (args, fmt);
171 printed = g_strdup_vprintf (fmt, args);
174 strncpy (str, printed, n);
180 #endif /* !HAVE_VSNPRINTF */
184 g_vsnprintf (gchar *str,
189 #ifdef HAVE_VSNPRINTF
192 retval = vsnprintf (str, n, fmt, args);
197 retval = strlen (str);
201 #else /* !HAVE_VSNPRINTF */
204 printed = g_strdup_vprintf (fmt, args);
205 strncpy (str, printed, n);
211 #endif /* !HAVE_VSNPRINTF */
215 g_parse_debug_string (const gchar *string,
222 g_return_val_if_fail (string != NULL, 0);
224 if (!g_strcasecmp (string, "all"))
226 for (i=0; i<nkeys; i++)
227 result |= keys[i].value;
231 gchar *str = g_strdup (string);
234 gboolean done = FALSE;
247 for (i=0; i<nkeys; i++)
248 if (!g_strcasecmp(keys[i].key, p))
249 result |= keys[i].value;
261 g_basename (const gchar *file_name)
263 register gchar *base;
265 g_return_val_if_fail (file_name != NULL, NULL);
267 base = strrchr (file_name, G_DIR_SEPARATOR);
272 if (isalpha (file_name[0]) && file_name[1] == ':')
273 return (gchar*) file_name + 2;
274 #endif /* G_OS_WIN32 */
276 return (gchar*) file_name;
280 g_path_is_absolute (const gchar *file_name)
282 g_return_val_if_fail (file_name != NULL, FALSE);
284 if (file_name[0] == G_DIR_SEPARATOR)
288 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
296 g_path_skip_root (gchar *file_name)
298 g_return_val_if_fail (file_name != NULL, NULL);
300 if (file_name[0] == G_DIR_SEPARATOR)
301 return file_name + 1;
304 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
305 return file_name + 3;
312 g_dirname (const gchar *file_name)
314 register gchar *base;
317 g_return_val_if_fail (file_name != NULL, NULL);
319 base = strrchr (file_name, G_DIR_SEPARATOR);
321 return g_strdup (".");
322 while (base > file_name && *base == G_DIR_SEPARATOR)
324 len = (guint) 1 + base - file_name;
326 base = g_new (gchar, len + 1);
327 g_memmove (base, file_name, len);
334 g_get_current_dir (void)
339 buffer = g_new (gchar, G_PATH_LENGTH);
342 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
343 * and, if that wasn't bad enough, hangs in doing so.
345 #if defined (sun) && !defined (__SVR4)
346 dir = getwd (buffer);
348 dir = getcwd (buffer, G_PATH_LENGTH - 1);
351 if (!dir || !*buffer)
353 /* hm, should we g_error() out here?
354 * this can happen if e.g. "./" has mode \0000
356 buffer[0] = G_DIR_SEPARATOR;
360 dir = g_strdup (buffer);
367 g_getenv (const gchar *variable)
370 g_return_val_if_fail (variable != NULL, NULL);
372 return getenv (variable);
376 static gchar *p = NULL;
380 g_return_val_if_fail (variable != NULL, NULL);
382 v = getenv (variable);
386 /* On Windows NT, it is relatively typical that environment variables
387 * contain references to other environment variables. Handle that by
388 * calling ExpandEnvironmentStrings.
391 /* First check how much space we need */
392 k = ExpandEnvironmentStrings (v, dummy, 2);
393 /* Then allocate that much, and actualy do the expansion */
401 p = g_realloc (p, k);
404 ExpandEnvironmentStrings (v, p, k);
410 G_LOCK_DEFINE_STATIC (g_utils_global);
412 static gchar *g_tmp_dir = NULL;
413 static gchar *g_user_name = NULL;
414 static gchar *g_real_name = NULL;
415 static gchar *g_home_dir = NULL;
417 /* HOLDS: g_utils_global_lock */
419 g_get_any_init (void)
423 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
425 g_tmp_dir = g_strdup (g_getenv ("TMP"));
427 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
433 g_tmp_dir = g_strdup (P_tmpdir);
434 k = strlen (g_tmp_dir);
435 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
436 g_tmp_dir[k-1] = '\0';
443 g_tmp_dir = g_strdup ("/tmp");
444 #else /* G_OS_WIN32 */
445 g_tmp_dir = g_strdup ("C:\\");
446 #endif /* G_OS_WIN32 */
450 g_home_dir = g_strdup (g_getenv ("HOME"));
455 /* The official way to specify a home directory on NT is
456 * the HOMEDRIVE and HOMEPATH environment variables.
458 * This is inside #ifdef G_OS_WIN32 because with the cygwin dll,
459 * HOME should be a POSIX style pathname.
462 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
464 gchar *homedrive, *homepath;
466 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
467 homepath = g_strdup (g_getenv ("HOMEPATH"));
469 g_home_dir = g_strconcat (homedrive, homepath, NULL);
474 #endif /* !G_OS_WIN32 */
478 struct passwd *pw = NULL;
479 gpointer buffer = NULL;
481 # ifdef HAVE_GETPWUID_R
483 # ifdef _SC_GETPW_R_SIZE_MAX
484 /* This reurns the maximum length */
485 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
486 # else /* _SC_GETPW_R_SIZE_MAX */
488 # endif /* _SC_GETPW_R_SIZE_MAX */
494 buffer = g_malloc (bufsize);
497 # ifdef HAVE_GETPWUID_R_POSIX
498 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
499 error = error < 0 ? errno : error;
500 # else /* !HAVE_GETPWUID_R_POSIX */
502 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
503 pw = error == 0 ? &pwd : NULL;
505 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
506 error = pw ? 0 : errno;
508 # endif /* !HAVE_GETPWUID_R_POSIX */
512 /* we bail out prematurely if the user id can't be found
513 * (should be pretty rare case actually), or if the buffer
514 * should be sufficiently big and lookups are still not
517 if (error == 0 || error == ENOENT)
519 g_warning ("getpwuid_r(): failed due to: "
520 "No such user: %lu.", (unsigned long)getuid ());
523 if (bufsize > 32 * 1024)
525 g_warning ("getpwuid_r(): failed due to: %s.",
534 # endif /* !HAVE_GETPWUID_R */
539 pw = getpwuid (getuid ());
544 g_user_name = g_strdup (pw->pw_name);
546 g_real_name = g_strdup (pw->pw_gecos);
548 g_real_name = g_strdup (g_user_name);
551 g_home_dir = g_strdup (pw->pw_dir);
556 #else /* !HAVE_PWD_H */
563 if (GetUserName (buffer, &len))
565 g_user_name = g_strdup (buffer);
566 g_real_name = g_strdup (buffer);
569 # endif /* G_OS_WIN32 */
571 #endif /* !HAVE_PWD_H */
574 /* change '\\' in %HOME% to '/' */
575 g_strdelimit (g_home_dir, "\\",'/');
578 g_user_name = g_strdup ("somebody");
580 g_real_name = g_strdup ("Unknown");
585 for (p = g_real_name; *p; p++)
589 p = g_strdup (g_real_name);
590 g_free (g_real_name);
599 g_get_user_name (void)
601 G_LOCK (g_utils_global);
604 G_UNLOCK (g_utils_global);
610 g_get_real_name (void)
612 G_LOCK (g_utils_global);
615 G_UNLOCK (g_utils_global);
620 /* Return the home directory of the user. If there is a HOME
621 * environment variable, its value is returned, otherwise use some
622 * system-dependent way of finding it out. If no home directory can be
623 * deduced, return NULL.
627 g_get_home_dir (void)
629 G_LOCK (g_utils_global);
632 G_UNLOCK (g_utils_global);
637 /* Return a directory to be used to store temporary files. This is the
638 * value of the TMPDIR, TMP or TEMP environment variables (they are
639 * checked in that order). If none of those exist, use P_tmpdir from
640 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
641 * and C:\ on Windows.
647 G_LOCK (g_utils_global);
650 G_UNLOCK (g_utils_global);
655 static gchar *g_prgname = NULL;
662 G_LOCK (g_utils_global);
664 G_UNLOCK (g_utils_global);
670 g_set_prgname (const gchar *prgname)
674 G_LOCK (g_utils_global);
676 g_prgname = g_strdup (prgname);
678 G_UNLOCK (g_utils_global);
682 g_direct_hash (gconstpointer v)
684 return GPOINTER_TO_UINT (v);
688 g_direct_equal (gconstpointer v1,
695 g_int_equal (gconstpointer v1,
698 return *((const gint*) v1) == *((const gint*) v2);
702 g_int_hash (gconstpointer v)
704 return *(const gint*) v;
710 gwin_ftruncate (gint fd,
716 g_return_val_if_fail (fd >= 0, -1);
718 hfile = (HANDLE) _get_osfhandle (fd);
719 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
720 if (curpos == 0xFFFFFFFF
721 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
722 || !SetEndOfFile (hfile))
724 gint error = GetLastError ();
728 case ERROR_INVALID_HANDLE:
743 gwin_opendir (const char *dirname)
749 g_return_val_if_fail (dirname != NULL, NULL);
751 result = g_new0 (DIR, 1);
752 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
753 result->dir_name = g_strdup (dirname);
755 k = strlen (result->dir_name);
756 if (k && result->dir_name[k - 1] == '\\')
758 result->dir_name[k - 1] = '\0';
761 mask = g_strdup_printf ("%s\\*", result->dir_name);
763 result->find_file_handle = (guint) FindFirstFile (mask,
764 (LPWIN32_FIND_DATA) result->find_file_data);
767 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
769 int error = GetLastError ();
771 g_free (result->dir_name);
772 g_free (result->find_file_data);
781 result->just_opened = TRUE;
787 gwin_readdir (DIR *dir)
789 static struct dirent result;
791 g_return_val_if_fail (dir != NULL, NULL);
793 if (dir->just_opened)
794 dir->just_opened = FALSE;
797 if (!FindNextFile ((HANDLE) dir->find_file_handle,
798 (LPWIN32_FIND_DATA) dir->find_file_data))
800 int error = GetLastError ();
804 case ERROR_NO_MORE_FILES:
812 strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
818 gwin_rewinddir (DIR *dir)
822 g_return_if_fail (dir != NULL);
824 if (!FindClose ((HANDLE) dir->find_file_handle))
825 g_warning ("gwin_rewinddir(): FindClose() failed\n");
827 mask = g_strdup_printf ("%s\\*", dir->dir_name);
828 dir->find_file_handle = (guint) FindFirstFile (mask,
829 (LPWIN32_FIND_DATA) dir->find_file_data);
832 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
834 int error = GetLastError ();
843 dir->just_opened = TRUE;
847 gwin_closedir (DIR *dir)
849 g_return_val_if_fail (dir != NULL, -1);
851 if (!FindClose ((HANDLE) dir->find_file_handle))
853 int error = GetLastError ();
858 errno = EIO; return -1;
862 g_free (dir->dir_name);
863 g_free (dir->find_file_data);
869 #endif /* G_OS_WIN32 */