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 /* NATIVE_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 (NATIVE_WIN32) && defined (__LCC__)
86 LibMain (void *hinstDll,
87 unsigned long dwReason,
92 #endif /* NATIVE_WIN32 && __LCC__ */
95 g_atexit (GVoidFunc func)
100 /* keep this in sync with glib.h */
102 #ifdef G_NATIVE_ATEXIT
103 result = ATEXIT (func);
105 error = g_strerror (errno);
106 #elif defined (HAVE_ATEXIT)
107 # ifdef NeXT /* @#%@! NeXTStep */
108 result = !atexit ((void (*)(void)) func);
110 error = g_strerror (errno);
112 result = atexit ((void (*)(void)) func);
114 error = g_strerror (errno);
116 #elif defined (HAVE_ON_EXIT)
117 result = on_exit ((void (*)(int, void *)) func, NULL);
119 error = g_strerror (errno);
122 error = "no implementation";
123 #endif /* G_NATIVE_ATEXIT */
126 g_error ("Could not register atexit() function: %s", error);
130 g_snprintf (gchar *str,
135 #ifdef HAVE_VSNPRINTF
139 va_start (args, fmt);
140 retval = vsnprintf (str, n, fmt, args);
144 #else /* !HAVE_VSNPRINTF */
148 va_start (args, fmt);
149 printed = g_strdup_vprintf (fmt, args);
152 strncpy (str, printed, n);
158 #endif /* !HAVE_VSNPRINTF */
162 g_vsnprintf (gchar *str,
167 #ifdef HAVE_VSNPRINTF
170 retval = vsnprintf (str, n, fmt, args);
173 #else /* !HAVE_VSNPRINTF */
176 printed = g_strdup_vprintf (fmt, args);
177 strncpy (str, printed, n);
183 #endif /* !HAVE_VSNPRINTF */
187 g_parse_debug_string (const gchar *string,
194 g_return_val_if_fail (string != NULL, 0);
196 if (!g_strcasecmp (string, "all"))
198 for (i=0; i<nkeys; i++)
199 result |= keys[i].value;
203 gchar *str = g_strdup (string);
206 gboolean done = FALSE;
219 for (i=0; i<nkeys; i++)
220 if (!g_strcasecmp(keys[i].key, p))
221 result |= keys[i].value;
233 g_basename (const gchar *file_name)
235 register gchar *base;
237 g_return_val_if_fail (file_name != NULL, NULL);
239 base = strrchr (file_name, G_DIR_SEPARATOR);
244 if (isalpha (file_name[0]) && file_name[1] == ':')
245 return (gchar*) file_name + 2;
246 #endif /* NATIVE_WIN32 */
248 return (gchar*) file_name;
252 g_path_is_absolute (const gchar *file_name)
254 g_return_val_if_fail (file_name != NULL, FALSE);
256 if (file_name[0] == G_DIR_SEPARATOR)
260 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
268 g_path_skip_root (gchar *file_name)
270 g_return_val_if_fail (file_name != NULL, NULL);
272 if (file_name[0] == G_DIR_SEPARATOR)
273 return file_name + 1;
276 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
277 return file_name + 3;
284 g_dirname (const gchar *file_name)
286 register gchar *base;
289 g_return_val_if_fail (file_name != NULL, NULL);
291 base = strrchr (file_name, G_DIR_SEPARATOR);
293 return g_strdup (".");
294 while (base > file_name && *base == G_DIR_SEPARATOR)
296 len = (guint) 1 + base - file_name;
298 base = g_new (gchar, len + 1);
299 g_memmove (base, file_name, len);
306 g_get_current_dir (void)
311 buffer = g_new (gchar, G_PATH_LENGTH);
314 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
315 * and, if that wasn't bad enough, hangs in doing so.
317 #if defined (sun) && !defined (__SVR4)
318 dir = getwd (buffer);
320 dir = getcwd (buffer, G_PATH_LENGTH - 1);
323 if (!dir || !*buffer)
325 /* hm, should we g_error() out here?
326 * this can happen if e.g. "./" has mode \0000
328 buffer[0] = G_DIR_SEPARATOR;
332 dir = g_strdup (buffer);
339 g_getenv (const gchar *variable)
342 g_return_val_if_fail (variable != NULL, NULL);
344 return getenv (variable);
348 static gchar *p = NULL;
352 g_return_val_if_fail (variable != NULL, NULL);
354 v = getenv (variable);
358 /* On Windows NT, it is relatively typical that environment variables
359 * contain references to other environment variables. Handle that by
360 * calling ExpandEnvironmentStrings.
363 /* First check how much space we need */
364 k = ExpandEnvironmentStrings (v, dummy, 2);
365 /* Then allocate that much, and actualy do the expansion */
373 p = g_realloc (p, k);
376 ExpandEnvironmentStrings (v, p, k);
382 G_LOCK_DEFINE_STATIC (g_utils_global);
384 static gchar *g_tmp_dir = NULL;
385 static gchar *g_user_name = NULL;
386 static gchar *g_real_name = NULL;
387 static gchar *g_home_dir = NULL;
389 /* HOLDS: g_utils_global_lock */
391 g_get_any_init (void)
395 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
397 g_tmp_dir = g_strdup (g_getenv ("TMP"));
399 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
405 g_tmp_dir = g_strdup (P_tmpdir);
406 k = strlen (g_tmp_dir);
407 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
408 g_tmp_dir[k-1] = '\0';
415 g_tmp_dir = g_strdup ("/tmp");
416 #else /* NATIVE_WIN32 */
417 g_tmp_dir = g_strdup ("C:\\");
418 #endif /* NATIVE_WIN32 */
422 g_home_dir = g_strdup (g_getenv ("HOME"));
427 /* The official way to specify a home directory on NT is
428 * the HOMEDRIVE and HOMEPATH environment variables.
430 * This is inside #ifdef NATIVE_WIN32 because with the cygwin dll,
431 * HOME should be a POSIX style pathname.
434 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
436 gchar *homedrive, *homepath;
438 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
439 homepath = g_strdup (g_getenv ("HOMEPATH"));
441 g_home_dir = g_strconcat (homedrive, homepath, NULL);
446 #endif /* !NATIVE_WIN32 */
450 struct passwd *pw = NULL;
451 gpointer buffer = NULL;
453 # ifdef HAVE_GETPWUID_R
461 buffer = g_malloc (bufsize);
464 # ifdef HAVE_GETPWUID_R_POSIX
465 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
466 error = error < 0 ? errno : error;
467 # else /* !HAVE_GETPWUID_R_POSIX */
469 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
470 pw = error == 0 ? &pwd : NULL;
472 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
473 error = pw ? 0 : errno;
475 # endif /* !HAVE_GETPWUID_R_POSIX */
479 /* we bail out prematurely if the user id can't be found
480 * (should be pretty rare case actually), or if the buffer
481 * should be sufficiently big and lookups are still not
484 if (error == 0 || error == ENOENT)
486 g_warning ("getpwuid_r(): failed due to: No such user %d.",
490 if (bufsize > 32 * 1024)
492 g_warning ("getpwuid_r(): failed due to: %s.",
501 # endif /* !HAVE_GETPWUID_R */
506 pw = getpwuid (getuid ());
511 g_user_name = g_strdup (pw->pw_name);
512 g_real_name = g_strdup (pw->pw_gecos);
514 g_home_dir = g_strdup (pw->pw_dir);
519 #else /* !HAVE_PWD_H */
526 if (GetUserName (buffer, &len))
528 g_user_name = g_strdup (buffer);
529 g_real_name = g_strdup (buffer);
532 # endif /* NATIVE_WIN32 */
534 #endif /* !HAVE_PWD_H */
537 /* change '\\' in %HOME% to '/' */
538 g_strdelimit (g_home_dir, "\\",'/');
541 g_user_name = g_strdup ("somebody");
543 g_real_name = g_strdup ("Unknown");
548 for (p = g_real_name; *p; p++)
552 p = g_strdup (g_real_name);
553 g_free (g_real_name);
562 g_get_user_name (void)
564 G_LOCK (g_utils_global);
567 G_UNLOCK (g_utils_global);
573 g_get_real_name (void)
575 G_LOCK (g_utils_global);
578 G_UNLOCK (g_utils_global);
583 /* Return the home directory of the user. If there is a HOME
584 * environment variable, its value is returned, otherwise use some
585 * system-dependent way of finding it out. If no home directory can be
586 * deduced, return NULL.
590 g_get_home_dir (void)
592 G_LOCK (g_utils_global);
595 G_UNLOCK (g_utils_global);
600 /* Return a directory to be used to store temporary files. This is the
601 * value of the TMPDIR, TMP or TEMP environment variables (they are
602 * checked in that order). If none of those exist, use P_tmpdir from
603 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
604 * and C:\ on Windows.
610 G_LOCK (g_utils_global);
613 G_UNLOCK (g_utils_global);
618 static gchar *g_prgname = NULL;
625 G_LOCK (g_utils_global);
627 G_UNLOCK (g_utils_global);
633 g_set_prgname (const gchar *prgname)
637 G_LOCK (g_utils_global);
639 g_prgname = g_strdup (prgname);
641 G_UNLOCK (g_utils_global);
645 g_direct_hash (gconstpointer v)
647 return GPOINTER_TO_UINT (v);
651 g_direct_equal (gconstpointer v1,
658 g_int_equal (gconstpointer v1,
661 return *((const gint*) v1) == *((const gint*) v2);
665 g_int_hash (gconstpointer v)
667 return *(const gint*) v;
670 #if 0 /* Old IO Channels */
673 g_iochannel_new (gint fd)
675 GIOChannel *channel = g_new (GIOChannel, 1);
681 channel->peer_fd = 0;
683 channel->need_wakeups = 0;
684 #endif /* NATIVE_WIN32 */
690 g_iochannel_free (GIOChannel *channel)
692 g_return_if_fail (channel != NULL);
698 g_iochannel_close_and_free (GIOChannel *channel)
700 g_return_if_fail (channel != NULL);
704 g_iochannel_free (channel);
707 #undef g_iochannel_wakeup_peer
710 g_iochannel_wakeup_peer (GIOChannel *channel)
713 static guint message = 0;
716 g_return_if_fail (channel != NULL);
720 message = RegisterWindowMessage ("gdk-pipe-readable");
723 g_print ("g_iochannel_wakeup_peer: calling PostThreadMessage (%#x, %d, %d, %d)\n",
724 channel->peer, message, channel->peer_fd, channel->offset);
726 PostThreadMessage (channel->peer, message,
727 channel->peer_fd, channel->offset);
728 #endif /* NATIVE_WIN32 */
731 #endif /* Old IO Channels */
737 gwin_ftruncate (gint fd,
743 g_return_val_if_fail (fd >= 0, -1);
745 hfile = (HANDLE) _get_osfhandle (fd);
746 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
747 if (curpos == 0xFFFFFFFF
748 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
749 || !SetEndOfFile (hfile))
751 gint error = GetLastError ();
755 case ERROR_INVALID_HANDLE:
770 gwin_opendir (const char *dirname)
776 g_return_val_if_fail (dirname != NULL, NULL);
778 result = g_new0 (DIR, 1);
779 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
780 result->dir_name = g_strdup (dirname);
782 k = strlen (result->dir_name);
783 if (k && result->dir_name[k - 1] == '\\')
785 result->dir_name[k - 1] = '\0';
788 mask = g_strdup_printf ("%s\\*", result->dir_name);
790 result->find_file_handle = (guint) FindFirstFile (mask,
791 (LPWIN32_FIND_DATA) result->find_file_data);
794 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
796 int error = GetLastError ();
798 g_free (result->dir_name);
799 g_free (result->find_file_data);
808 result->just_opened = TRUE;
814 gwin_readdir (DIR *dir)
816 static struct dirent result;
818 g_return_val_if_fail (dir != NULL, NULL);
820 if (dir->just_opened)
821 dir->just_opened = FALSE;
824 if (!FindNextFile ((HANDLE) dir->find_file_handle,
825 (LPWIN32_FIND_DATA) dir->find_file_data))
827 int error = GetLastError ();
831 case ERROR_NO_MORE_FILES:
839 strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
845 gwin_rewinddir (DIR *dir)
849 g_return_if_fail (dir != NULL);
851 if (!FindClose ((HANDLE) dir->find_file_handle))
852 g_warning ("gwin_rewinddir(): FindClose() failed\n");
854 mask = g_strdup_printf ("%s\\*", dir->dir_name);
855 dir->find_file_handle = (guint) FindFirstFile (mask,
856 (LPWIN32_FIND_DATA) dir->find_file_data);
859 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
861 int error = GetLastError ();
870 dir->just_opened = TRUE;
874 gwin_closedir (DIR *dir)
876 g_return_val_if_fail (dir != NULL, -1);
878 if (!FindClose ((HANDLE) dir->find_file_handle))
880 int error = GetLastError ();
885 errno = EIO; return -1;
889 g_free (dir->dir_name);
890 g_free (dir->find_file_data);
896 #endif /* _MSC_VER */
898 #endif /* NATIVE_WIN32 */