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.
24 #include "glibconfig.h"
37 #include <sys/types.h>
38 #ifdef HAVE_SYS_PARAM_H
39 #include <sys/param.h>
43 # define STRICT /* Strict typing, please */
50 # endif /* _MSC_VER */
51 #endif /* NATIVE_WIN32 */
53 /* implement Glib's inline functions
55 #define G_INLINE_FUNC extern
56 #define G_CAN_INLINE 1
60 #define G_PATH_LENGTH (MAXPATHLEN + 1)
61 #elif defined (PATH_MAX)
62 #define G_PATH_LENGTH (PATH_MAX + 1)
63 #else /* !MAXPATHLEN */
64 #define G_PATH_LENGTH (2048 + 1)
65 #endif /* !MAXPATHLEN && !PATH_MAX */
67 const guint glib_major_version = GLIB_MAJOR_VERSION;
68 const guint glib_minor_version = GLIB_MINOR_VERSION;
69 const guint glib_micro_version = GLIB_MICRO_VERSION;
70 const guint glib_interface_age = GLIB_INTERFACE_AGE;
71 const guint glib_binary_age = GLIB_BINARY_AGE;
73 #if defined (NATIVE_WIN32) && defined (__LCC__)
75 LibMain (void *hinstDll,
76 unsigned long dwReason,
81 #endif /* NATIVE_WIN32 && __LCC__ */
84 g_atexit (GVoidFunc func)
89 /* keep this in sync with glib.h */
91 #ifdef G_NATIVE_ATEXIT
92 result = ATEXIT (func);
94 error = g_strerror (errno);
95 #elif defined (HAVE_ATEXIT)
96 # ifdef NeXT /* @#%@! NeXTStep */
97 result = !atexit ((void (*)(void)) func);
99 error = g_strerror (errno);
101 result = atexit ((void (*)(void)) func);
103 error = g_strerror (errno);
105 #elif defined (HAVE_ON_EXIT)
106 result = on_exit ((void (*)(int, void *)) func, NULL);
108 error = g_strerror (errno);
111 error = "no implementation";
112 #endif /* G_NATIVE_ATEXIT */
115 g_error ("Could not register atexit() function: %s", error);
119 g_snprintf (gchar *str,
124 #ifdef HAVE_VSNPRINTF
128 va_start (args, fmt);
129 retval = vsnprintf (str, n, fmt, args);
133 #else /* !HAVE_VSNPRINTF */
137 va_start (args, fmt);
138 printed = g_strdup_vprintf (fmt, args);
141 strncpy (str, printed, n);
147 #endif /* !HAVE_VSNPRINTF */
151 g_vsnprintf (gchar *str,
156 #ifdef HAVE_VSNPRINTF
159 retval = vsnprintf (str, n, fmt, args);
162 #else /* !HAVE_VSNPRINTF */
165 printed = g_strdup_vprintf (fmt, args);
166 strncpy (str, printed, n);
172 #endif /* !HAVE_VSNPRINTF */
176 g_parse_debug_string (const gchar *string,
183 g_return_val_if_fail (string != NULL, 0);
185 if (!g_strcasecmp (string, "all"))
187 for (i=0; i<nkeys; i++)
188 result |= keys[i].value;
192 gchar *str = g_strdup (string);
195 gboolean done = FALSE;
208 for (i=0; i<nkeys; i++)
209 if (!g_strcasecmp(keys[i].key, p))
210 result |= keys[i].value;
222 g_basename (const gchar *file_name)
224 register gchar *base;
226 g_return_val_if_fail (file_name != NULL, NULL);
228 base = strrchr (file_name, G_DIR_SEPARATOR);
233 if (isalpha (file_name[0]) && file_name[1] == ':')
234 return (gchar*) file_name + 2;
235 #endif /* NATIVE_WIN32 */
237 return (gchar*) file_name;
241 g_path_is_absolute (const gchar *file_name)
243 g_return_val_if_fail (file_name != NULL, FALSE);
245 if (file_name[0] == G_DIR_SEPARATOR)
249 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
257 g_path_skip_root (gchar *file_name)
259 g_return_val_if_fail (file_name != NULL, NULL);
261 if (file_name[0] == G_DIR_SEPARATOR)
262 return file_name + 1;
265 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
266 return file_name + 3;
273 g_dirname (const gchar *file_name)
275 register gchar *base;
278 g_return_val_if_fail (file_name != NULL, NULL);
280 base = strrchr (file_name, G_DIR_SEPARATOR);
282 return g_strdup (".");
283 while (base > file_name && *base == G_DIR_SEPARATOR)
285 len = (guint) 1 + base - file_name;
287 base = g_new (gchar, len + 1);
288 g_memmove (base, file_name, len);
295 g_get_current_dir (void)
300 buffer = g_new (gchar, G_PATH_LENGTH);
303 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
304 * and, if that wasn't bad enough, hangs in doing so.
306 #if defined (sun) && !defined (__SVR4)
307 dir = getwd (buffer);
309 dir = getcwd (buffer, G_PATH_LENGTH - 1);
312 if (!dir || !*buffer)
314 /* hm, should we g_error() out here?
315 * this can happen if e.g. "./" has mode \0000
317 buffer[0] = G_DIR_SEPARATOR;
321 dir = g_strdup (buffer);
328 g_getenv (const gchar *variable)
331 g_return_val_if_fail (variable != NULL, NULL);
333 return getenv (variable);
337 static gchar *p = NULL;
341 g_return_val_if_fail (variable != NULL, NULL);
343 v = getenv (variable);
347 /* On Windows NT, it is relatively typical that environment variables
348 * contain references to other environment variables. Handle that by
349 * calling ExpandEnvironmentStrings.
352 /* First check how much space we need */
353 k = ExpandEnvironmentStrings (v, dummy, 2);
354 /* Then allocate that much, and actualy do the expansion */
362 p = g_realloc (p, k);
365 ExpandEnvironmentStrings (v, p, k);
370 static gchar *g_tmp_dir = NULL;
371 static gchar *g_user_name = NULL;
372 static gchar *g_real_name = NULL;
373 static gchar *g_home_dir = NULL;
376 g_get_any_init (void)
384 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
386 g_tmp_dir = g_strdup (g_getenv ("TMP"));
388 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
394 g_tmp_dir = g_strdup (P_tmpdir);
395 k = strlen (g_tmp_dir);
396 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
397 g_tmp_dir[k-1] = '\0';
403 g_tmp_dir = g_strdup ("/tmp");
404 #else /* NATIVE_WIN32 */
405 g_tmp_dir = g_strdup ("C:\\");
406 #endif /* NATIVE_WIN32 */
409 g_home_dir = g_strdup (g_getenv ("HOME"));
413 pw = getpwuid (getuid ());
418 g_user_name = g_strdup (pw->pw_name);
419 g_real_name = g_strdup (pw->pw_gecos);
421 g_home_dir = g_strdup (pw->pw_dir);
423 #else /* !HAVE_PWD_H */
428 g_user_name = g_new (gchar, len);
430 if (!GetUserName (g_user_name, &len))
432 g_free (g_user_name);
433 g_user_name = g_strdup ("somebody");
434 g_real_name = g_strdup ("Unknown");
437 g_real_name = g_strdup (g_user_name);
439 # else /* !NATIVE_WIN32 */
440 g_user_name = g_strdup ("somebody");
441 g_real_name = g_strdup ("Unknown");
443 # endif /* !NATIVE_WIN32 */
444 #endif /* !HAVE_PWD_H */
449 g_get_user_name (void)
458 g_get_real_name (void)
466 /* Return the home directory of the user. If there is a HOME
467 * environment variable, its value is returned, otherwise use some
468 * system-dependent way of finding it out. If no home directory can be
469 * deduced, return NULL.
473 g_get_home_dir (void)
481 /* Return a directory to be used to store temporary files. This is the
482 * value of the TMPDIR, TMP or TEMP environment variables (they are
483 * checked in that order). If none of those exist, use P_tmpdir from
484 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
485 * and C:\ on Windows.
497 static gchar *g_prgname = NULL;
506 g_set_prgname (const gchar *prgname)
508 gchar *c = g_prgname;
510 g_prgname = g_strdup (prgname);
515 g_direct_hash (gconstpointer v)
517 return GPOINTER_TO_UINT (v);
521 g_direct_equal (gconstpointer v1,
524 return GPOINTER_TO_UINT (v1) == GPOINTER_TO_UINT (v2);
528 g_int_equal (gconstpointer v1,
531 return *((const gint*) v1) == *((const gint*) v2);
535 g_int_hash (gconstpointer v)
537 return *(const gint*) v;
541 g_iochannel_new (gint fd)
543 GIOChannel *channel = g_new (GIOChannel, 1);
549 channel->peer_fd = 0;
551 channel->need_wakeups = 0;
552 #endif /* NATIVE_WIN32 */
558 g_iochannel_free (GIOChannel *channel)
560 g_return_if_fail (channel != NULL);
566 g_iochannel_close_and_free (GIOChannel *channel)
568 g_return_if_fail (channel != NULL);
572 g_iochannel_free (channel);
575 #undef g_iochannel_wakeup_peer
578 g_iochannel_wakeup_peer (GIOChannel *channel)
581 static guint message = 0;
584 g_return_if_fail (channel != NULL);
588 message = RegisterWindowMessage ("gdk-pipe-readable");
591 g_print ("g_iochannel_wakeup_peer: calling PostThreadMessage (%#x, %d, %d, %d)\n",
592 channel->peer, message, channel->peer_fd, channel->offset);
594 PostThreadMessage (channel->peer, message,
595 channel->peer_fd, channel->offset);
596 #endif /* NATIVE_WIN32 */
604 gwin_ftruncate (gint fd,
610 g_return_val_if_fail (fd >= 0, -1);
612 hfile = (HANDLE) _get_osfhandle (fd);
613 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
614 if (curpos == 0xFFFFFFFF
615 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
616 || !SetEndOfFile (hfile))
618 gint error = GetLastError ();
622 case ERROR_INVALID_HANDLE:
637 gwin_opendir (const char *dirname)
643 g_return_val_if_fail (dirname != NULL, NULL);
645 result = g_new0 (DIR, 1);
646 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
647 result->dir_name = g_strdup (dirname);
649 k = strlen (result->dir_name);
650 if (k && result->dir_name[k - 1] == '\\')
652 result->dir_name[k - 1] = '\0';
655 mask = g_strdup_printf ("%s\\*", result->dir_name);
657 result->find_file_handle = (guint) FindFirstFile (mask,
658 (LPWIN32_FIND_DATA) result->find_file_data);
661 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
663 int error = GetLastError ();
665 g_free (result->dir_name);
666 g_free (result->find_file_data);
675 result->just_opened = TRUE;
681 gwin_readdir (DIR *dir)
683 static struct dirent result;
685 g_return_val_if_fail (dir != NULL, NULL);
687 if (dir->just_opened)
688 dir->just_opened = FALSE;
691 if (!FindNextFile ((HANDLE) dir->find_file_handle,
692 (LPWIN32_FIND_DATA) dir->find_file_data))
694 int error = GetLastError ();
698 case ERROR_NO_MORE_FILES:
706 strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
712 gwin_rewinddir (DIR *dir)
716 g_return_if_fail (dir != NULL);
718 if (!FindClose ((HANDLE) dir->find_file_handle))
719 g_warning ("gwin_rewinddir(): FindClose() failed\n");
721 mask = g_strdup_printf ("%s\\*", dir->dir_name);
722 dir->find_file_handle = (guint) FindFirstFile (mask,
723 (LPWIN32_FIND_DATA) dir->find_file_data);
726 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
728 int error = GetLastError ();
737 dir->just_opened = TRUE;
741 gwin_closedir (DIR *dir)
743 g_return_val_if_fail (dir != NULL, -1);
745 if (!FindClose ((HANDLE) dir->find_file_handle))
747 int error = GetLastError ();
752 errno = EIO; return -1;
756 g_free (dir->dir_name);
757 g_free (dir->find_file_data);
763 #endif /* _MSC_VER */
765 #endif /* NATIVE_WIN32 */