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.
19 #include "glibconfig.h"
32 #include <sys/types.h>
33 #ifdef HAVE_SYS_PARAM_H
34 #include <sys/param.h>
38 # define STRICT /* Strict typing, please */
45 # endif /* _MSC_VER */
46 #endif /* NATIVE_WIN32 */
48 /* implement Glib's inline functions
50 #define G_INLINE_FUNC extern
51 #define G_CAN_INLINE 1
55 #define G_PATH_LENGTH (MAXPATHLEN + 1)
56 #elif defined (PATH_MAX)
57 #define G_PATH_LENGTH (PATH_MAX + 1)
58 #else /* !MAXPATHLEN */
59 #define G_PATH_LENGTH (2048 + 1)
60 #endif /* !MAXPATHLEN && !PATH_MAX */
62 const guint glib_major_version = GLIB_MAJOR_VERSION;
63 const guint glib_minor_version = GLIB_MINOR_VERSION;
64 const guint glib_micro_version = GLIB_MICRO_VERSION;
65 const guint glib_interface_age = GLIB_INTERFACE_AGE;
66 const guint glib_binary_age = GLIB_BINARY_AGE;
68 #if defined (NATIVE_WIN32) && defined (__LCC__)
70 LibMain (void *hinstDll,
71 unsigned long dwReason,
76 #endif /* NATIVE_WIN32 && __LCC__ */
79 g_atexit (GVoidFunc func)
84 /* keep this in sync with glib.h */
86 #ifdef G_NATIVE_ATEXIT
87 result = ATEXIT (func);
89 error = g_strerror (errno);
90 #elif defined (HAVE_ATEXIT)
91 # ifdef NeXT /* @#%@! NeXTStep */
92 result = !atexit ((void (*)(void)) func);
94 error = g_strerror (errno);
96 result = atexit ((void (*)(void)) func);
98 error = g_strerror (errno);
100 #elif defined (HAVE_ON_EXIT)
101 result = on_exit ((void (*)(int, void *)) func, NULL);
103 error = g_strerror (errno);
106 error = "no implementation";
107 #endif /* G_NATIVE_ATEXIT */
110 g_error ("Could not register atexit() function: %s", error);
114 g_snprintf (gchar *str,
119 #ifdef HAVE_VSNPRINTF
123 va_start (args, fmt);
124 retval = vsnprintf (str, n, fmt, args);
128 #else /* !HAVE_VSNPRINTF */
132 va_start (args, fmt);
133 printed = g_strdup_vprintf (fmt, args);
136 strncpy (str, printed, n);
142 #endif /* !HAVE_VSNPRINTF */
146 g_vsnprintf (gchar *str,
151 #ifdef HAVE_VSNPRINTF
154 retval = vsnprintf (str, n, fmt, args);
157 #else /* !HAVE_VSNPRINTF */
160 printed = g_strdup_vprintf (fmt, args);
161 strncpy (str, printed, n);
167 #endif /* !HAVE_VSNPRINTF */
171 g_parse_debug_string (const gchar *string,
178 g_return_val_if_fail (string != NULL, 0);
180 if (!g_strcasecmp (string, "all"))
182 for (i=0; i<nkeys; i++)
183 result |= keys[i].value;
187 gchar *str = g_strdup (string);
190 gboolean done = FALSE;
203 for (i=0; i<nkeys; i++)
204 if (!g_strcasecmp(keys[i].key, p))
205 result |= keys[i].value;
217 g_basename (const gchar *file_name)
219 register gchar *base;
221 g_return_val_if_fail (file_name != NULL, NULL);
223 base = strrchr (file_name, G_DIR_SEPARATOR);
228 if (isalpha (file_name[0]) && file_name[1] == ':')
229 return (gchar*) file_name + 2;
230 #endif /* NATIVE_WIN32 */
232 return (gchar*) file_name;
236 g_path_is_absolute (const gchar *file_name)
238 g_return_val_if_fail (file_name != NULL, FALSE);
240 if (file_name[0] == G_DIR_SEPARATOR)
244 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
252 g_path_skip_root (gchar *file_name)
254 g_return_val_if_fail (file_name != NULL, NULL);
256 if (file_name[0] == G_DIR_SEPARATOR)
257 return file_name + 1;
260 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
261 return file_name + 3;
268 g_dirname (const gchar *file_name)
270 register gchar *base;
273 g_return_val_if_fail (file_name != NULL, NULL);
275 base = strrchr (file_name, G_DIR_SEPARATOR);
277 return g_strdup (".");
278 while (base > file_name && *base == G_DIR_SEPARATOR)
280 len = (guint) 1 + base - file_name;
282 base = g_new (gchar, len + 1);
283 g_memmove (base, file_name, len);
290 g_get_current_dir (void)
295 buffer = g_new (gchar, G_PATH_LENGTH);
298 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
299 * and, if that wasn't bad enough, hangs in doing so.
301 #if defined (sun) && !defined (__SVR4)
302 dir = getwd (buffer);
304 dir = getcwd (buffer, G_PATH_LENGTH - 1);
307 if (!dir || !*buffer)
309 /* hm, should we g_error() out here?
310 * this can happen if e.g. "./" has mode \0000
312 buffer[0] = G_DIR_SEPARATOR;
316 dir = g_strdup (buffer);
323 g_getenv (const gchar *variable)
325 g_return_val_if_fail (variable != NULL, NULL);
328 return getenv (variable);
334 v = getenv (variable);
338 /* On Windows NT, it is relatively typical that environment variables
339 * contain references to other environment variables. Handle that by
340 * calling ExpandEnvironmentStrings.
347 p = g_new (gchar, l);
349 k = ExpandEnvironmentStrings (v, p, l);
364 static gchar *g_tmp_dir = NULL;
365 static gchar *g_user_name = NULL;
366 static gchar *g_real_name = NULL;
367 static gchar *g_home_dir = NULL;
370 g_get_any_init (void)
378 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
380 g_tmp_dir = g_strdup (g_getenv ("TMP"));
382 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
387 g_tmp_dir = g_strdup (G_DIR_SEPARATOR_S "tmp");
388 #else /* !NATIVE_WIN32 */
389 g_tmp_dir = g_strdup (".");
390 #endif /* !NATIVE_WIN32 */
393 g_home_dir = g_strdup (g_getenv ("HOME"));
397 pw = getpwuid (getuid ());
402 g_user_name = g_strdup (pw->pw_name);
403 g_real_name = g_strdup (pw->pw_gecos);
405 g_home_dir = g_strdup (pw->pw_dir);
407 #else /* !HAVE_PWD_H */
412 g_user_name = g_new (gchar, len);
414 if (!GetUserName (g_user_name, &len))
416 g_free (g_user_name);
417 g_user_name = g_strdup ("somebody");
418 g_real_name = g_strdup ("Unknown");
421 g_real_name = g_strdup (g_user_name);
424 # else /* !NATIVE_WIN32 */
425 g_user_name = g_strdup ("somebody");
426 g_real_name = g_strdup ("Unknown");
428 # endif /* !NATIVE_WIN32 */
429 #endif /* !HAVE_PWD_H */
433 g_home_dir = g_strdup (g_tmp_dir);
437 g_get_user_name (void)
446 g_get_real_name (void)
455 g_get_home_dir (void)
472 static gchar *g_prgname = NULL;
481 g_set_prgname (const gchar *prgname)
483 gchar *c = g_prgname;
485 g_prgname = g_strdup (prgname);
490 g_direct_hash (gconstpointer v)
492 return GPOINTER_TO_UINT (v);
496 g_direct_equal (gconstpointer v1,
499 return GPOINTER_TO_UINT (v1) == GPOINTER_TO_UINT (v2);
503 g_int_equal (gconstpointer v1,
506 return *((const gint*) v1) == *((const gint*) v2);
510 g_int_hash (gconstpointer v)
512 return *(const gint*) v;
516 g_iochannel_new (gint fd)
518 GIOChannel *channel = g_new0 (GIOChannel, 1);
524 channel->peer_fd = 0;
526 channel->peer_offset = 0;
527 #endif /* NATIVE_WIN32 */
533 g_iochannel_free (GIOChannel *channel)
535 g_return_if_fail (channel != NULL);
541 g_iochannel_close_and_free (GIOChannel *channel)
543 g_return_if_fail (channel != NULL);
547 g_iochannel_free (channel);
550 #undef g_iochannel_wakeup_peer
553 g_iochannel_wakeup_peer (GIOChannel *channel)
555 static guint message = 0;
557 g_return_if_fail (channel != NULL);
561 message = RegisterWindowMessage ("gdk-pipe-readable");
564 g_print ("g_iochannel_wakeup_peer: calling PostThreadMessage (%#x, %d, %d, %d)\n",
565 channel->peer, message, channel->peer_fd, channel->offset);
567 PostThreadMessage (channel->peer, message,
568 channel->peer_fd, channel->offset);
569 #endif /* NATIVE_WIN32 */
577 gwin_ftruncate (gint fd,
583 g_return_val_if_fail (fd >= 0, -1);
585 hfile = (HANDLE) _get_osfhandle (fd);
586 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
587 if (curpos == 0xFFFFFFFF
588 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
589 || !SetEndOfFile (hfile))
591 gint error = GetLastError ();
595 case ERROR_INVALID_HANDLE:
610 gwin_opendir (const char *dirname)
616 g_return_val_if_fail (dirname != NULL, NULL);
618 result = g_new0 (DIR, 1);
619 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
620 result->dir_name = g_strdup (dirname);
622 k = strlen (result->dir_name);
623 if (k && result->dir_name[k - 1] == '\\')
625 result->dir_name[k - 1] = '\0';
628 mask = g_strdup_printf ("%s\\*", result->dir_name);
630 result->find_file_handle = (guint) FindFirstFile (mask,
631 (LPWIN32_FIND_DATA) result->find_file_data);
634 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
636 int error = GetLastError ();
638 g_free (result->dir_name);
639 g_free (result->find_file_data);
648 result->just_opened = TRUE;
654 gwin_readdir (DIR *dir)
656 static struct dirent result;
658 g_return_val_if_fail (dir != NULL, NULL);
660 if (dir->just_opened)
661 dir->just_opened = FALSE;
664 if (!FindNextFile ((HANDLE) dir->find_file_handle,
665 (LPWIN32_FIND_DATA) dir->find_file_data))
667 int error = GetLastError ();
671 case ERROR_NO_MORE_FILES:
679 strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
685 gwin_rewinddir (DIR *dir)
689 g_return_if_fail (dir != NULL);
691 if (!FindClose ((HANDLE) dir->find_file_handle))
692 g_warning ("gwin_rewinddir(): FindClose() failed\n");
694 mask = g_strdup_printf ("%s\\*", dir->dir_name);
695 dir->find_file_handle = (guint) FindFirstFile (mask,
696 (LPWIN32_FIND_DATA) dir->find_file_data);
699 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
701 int error = GetLastError ();
710 dir->just_opened = TRUE;
714 gwin_closedir (DIR *dir)
716 g_return_val_if_fail (dir != NULL, -1);
718 if (!FindClose ((HANDLE) dir->find_file_handle))
720 int error = GetLastError ();
725 errno = EIO; return -1;
729 g_free (dir->dir_name);
730 g_free (dir->find_file_data);
736 #endif /* _MSC_VER */
738 #endif /* NATIVE_WIN32 */