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;
85 g_atexit (GVoidFunc func)
90 /* keep this in sync with glib.h */
92 #ifdef G_NATIVE_ATEXIT
93 result = ATEXIT (func);
95 error = g_strerror (errno);
96 #elif defined (HAVE_ATEXIT)
97 # ifdef NeXT /* @#%@! NeXTStep */
98 result = !atexit ((void (*)(void)) func);
100 error = g_strerror (errno);
102 result = atexit ((void (*)(void)) func);
104 error = g_strerror (errno);
106 #elif defined (HAVE_ON_EXIT)
107 result = on_exit ((void (*)(int, void *)) func, NULL);
109 error = g_strerror (errno);
112 error = "no implementation";
113 #endif /* G_NATIVE_ATEXIT */
116 g_error ("Could not register atexit() function: %s", error);
120 g_snprintf (gchar *str,
125 #ifdef HAVE_VSNPRINTF
129 va_start (args, fmt);
130 retval = vsnprintf (str, n, fmt, args);
134 #else /* !HAVE_VSNPRINTF */
138 va_start (args, fmt);
139 printed = g_strdup_vprintf (fmt, args);
142 strncpy (str, printed, n);
148 #endif /* !HAVE_VSNPRINTF */
152 g_vsnprintf (gchar *str,
157 #ifdef HAVE_VSNPRINTF
160 retval = vsnprintf (str, n, fmt, args);
163 #else /* !HAVE_VSNPRINTF */
166 printed = g_strdup_vprintf (fmt, args);
167 strncpy (str, printed, n);
173 #endif /* !HAVE_VSNPRINTF */
177 g_parse_debug_string (const gchar *string,
184 g_return_val_if_fail (string != NULL, 0);
186 if (!g_strcasecmp (string, "all"))
188 for (i=0; i<nkeys; i++)
189 result |= keys[i].value;
193 gchar *str = g_strdup (string);
196 gboolean done = FALSE;
209 for (i=0; i<nkeys; i++)
210 if (!g_strcasecmp(keys[i].key, p))
211 result |= keys[i].value;
223 g_basename (const gchar *file_name)
225 register gchar *base;
227 g_return_val_if_fail (file_name != NULL, NULL);
229 base = strrchr (file_name, G_DIR_SEPARATOR);
234 if (isalpha (file_name[0]) && file_name[1] == ':')
235 return (gchar*) file_name + 2;
236 #endif /* NATIVE_WIN32 */
238 return (gchar*) file_name;
242 g_path_is_absolute (const gchar *file_name)
244 g_return_val_if_fail (file_name != NULL, FALSE);
246 if (file_name[0] == G_DIR_SEPARATOR)
250 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
258 g_path_skip_root (gchar *file_name)
260 g_return_val_if_fail (file_name != NULL, NULL);
262 if (file_name[0] == G_DIR_SEPARATOR)
263 return file_name + 1;
266 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
267 return file_name + 3;
274 g_dirname (const gchar *file_name)
276 register gchar *base;
279 g_return_val_if_fail (file_name != NULL, NULL);
281 base = strrchr (file_name, G_DIR_SEPARATOR);
283 return g_strdup (".");
284 while (base > file_name && *base == G_DIR_SEPARATOR)
286 len = (guint) 1 + base - file_name;
288 base = g_new (gchar, len + 1);
289 g_memmove (base, file_name, len);
296 g_get_current_dir (void)
301 buffer = g_new (gchar, G_PATH_LENGTH);
304 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
305 * and, if that wasn't bad enough, hangs in doing so.
307 #if defined (sun) && !defined (__SVR4)
308 dir = getwd (buffer);
310 dir = getcwd (buffer, G_PATH_LENGTH - 1);
313 if (!dir || !*buffer)
315 /* hm, should we g_error() out here?
316 * this can happen if e.g. "./" has mode \0000
318 buffer[0] = G_DIR_SEPARATOR;
322 dir = g_strdup (buffer);
329 g_getenv (const gchar *variable)
332 g_return_val_if_fail (variable != NULL, NULL);
334 return getenv (variable);
338 static gchar *p = NULL;
342 g_return_val_if_fail (variable != NULL, NULL);
344 v = getenv (variable);
348 /* On Windows NT, it is relatively typical that environment variables
349 * contain references to other environment variables. Handle that by
350 * calling ExpandEnvironmentStrings.
353 /* First check how much space we need */
354 k = ExpandEnvironmentStrings (v, dummy, 2);
355 /* Then allocate that much, and actualy do the expansion */
363 p = g_realloc (p, k);
366 ExpandEnvironmentStrings (v, p, k);
372 G_LOCK_DEFINE_STATIC (g_utils_global);
374 static gchar *g_tmp_dir = NULL;
375 static gchar *g_user_name = NULL;
376 static gchar *g_real_name = NULL;
377 static gchar *g_home_dir = NULL;
379 /* HOLDS: g_utils_global_lock */
381 g_get_any_init (void)
385 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
387 g_tmp_dir = g_strdup (g_getenv ("TMP"));
389 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
395 g_tmp_dir = g_strdup (P_tmpdir);
396 k = strlen (g_tmp_dir);
397 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
398 g_tmp_dir[k-1] = '\0';
405 g_tmp_dir = g_strdup ("/tmp");
406 #else /* NATIVE_WIN32 */
407 g_tmp_dir = g_strdup ("C:\\");
408 #endif /* NATIVE_WIN32 */
412 g_home_dir = g_strdup (g_getenv ("HOME"));
417 /* The official way to specify a home directory on NT is
418 * the HOMEDRIVE and HOMEPATH environment variables.
420 * This is inside #ifdef NATIVE_WIN32 because with the cygwin dll,
421 * HOME should be a POSIX style pathname.
424 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
426 gchar *homedrive, *homepath;
428 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
429 homepath = g_strdup (g_getenv ("HOMEPATH"));
431 g_home_dir = g_strconcat (homedrive, homepath, NULL);
436 #endif /* !NATIVE_WIN32 */
440 struct passwd *pw = NULL;
441 gpointer buffer = NULL;
443 # ifdef HAVE_GETPWUID_R
445 # ifdef _SC_GETPW_R_SIZE_MAX
446 /* This reurns the maximum length */
447 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
448 # else /* _SC_GETPW_R_SIZE_MAX */
450 # endif /* _SC_GETPW_R_SIZE_MAX */
456 buffer = g_malloc (bufsize);
459 # ifdef HAVE_GETPWUID_R_POSIX
460 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
461 error = error < 0 ? errno : error;
462 # else /* !HAVE_GETPWUID_R_POSIX */
464 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
465 pw = error == 0 ? &pwd : NULL;
467 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
468 error = pw ? 0 : errno;
470 # endif /* !HAVE_GETPWUID_R_POSIX */
474 /* we bail out prematurely if the user id can't be found
475 * (should be pretty rare case actually), or if the buffer
476 * should be sufficiently big and lookups are still not
479 if (error == 0 || error == ENOENT)
481 g_warning ("getpwuid_r(): failed due to: No such user %d.",
485 if (bufsize > 32 * 1024)
487 g_warning ("getpwuid_r(): failed due to: %s.",
496 # endif /* !HAVE_GETPWUID_R */
501 pw = getpwuid (getuid ());
506 g_user_name = g_strdup (pw->pw_name);
507 g_real_name = g_strdup (pw->pw_gecos);
509 g_home_dir = g_strdup (pw->pw_dir);
514 #else /* !HAVE_PWD_H */
521 if (GetUserName (buffer, &len))
523 g_user_name = g_strdup (buffer);
524 g_real_name = g_strdup (buffer);
527 # endif /* NATIVE_WIN32 */
529 #endif /* !HAVE_PWD_H */
532 /* change '\\' in %HOME% to '/' */
533 g_strdelimit (g_home_dir, "\\",'/');
536 g_user_name = g_strdup ("somebody");
538 g_real_name = g_strdup ("Unknown");
543 for (p = g_real_name; *p; p++)
547 p = g_strdup (g_real_name);
548 g_free (g_real_name);
557 g_get_user_name (void)
559 G_LOCK (g_utils_global);
562 G_UNLOCK (g_utils_global);
568 g_get_real_name (void)
570 G_LOCK (g_utils_global);
573 G_UNLOCK (g_utils_global);
578 /* Return the home directory of the user. If there is a HOME
579 * environment variable, its value is returned, otherwise use some
580 * system-dependent way of finding it out. If no home directory can be
581 * deduced, return NULL.
585 g_get_home_dir (void)
587 G_LOCK (g_utils_global);
590 G_UNLOCK (g_utils_global);
595 /* Return a directory to be used to store temporary files. This is the
596 * value of the TMPDIR, TMP or TEMP environment variables (they are
597 * checked in that order). If none of those exist, use P_tmpdir from
598 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
599 * and C:\ on Windows.
605 G_LOCK (g_utils_global);
608 G_UNLOCK (g_utils_global);
613 static gchar *g_prgname = NULL;
620 G_LOCK (g_utils_global);
622 G_UNLOCK (g_utils_global);
628 g_set_prgname (const gchar *prgname)
632 G_LOCK (g_utils_global);
634 g_prgname = g_strdup (prgname);
636 G_UNLOCK (g_utils_global);
640 g_direct_hash (gconstpointer v)
642 return GPOINTER_TO_UINT (v);
646 g_direct_equal (gconstpointer v1,
653 g_int_equal (gconstpointer v1,
656 return *((const gint*) v1) == *((const gint*) v2);
660 g_int_hash (gconstpointer v)
662 return *(const gint*) v;
668 gwin_ftruncate (gint fd,
674 g_return_val_if_fail (fd >= 0, -1);
676 hfile = (HANDLE) _get_osfhandle (fd);
677 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
678 if (curpos == 0xFFFFFFFF
679 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
680 || !SetEndOfFile (hfile))
682 gint error = GetLastError ();
686 case ERROR_INVALID_HANDLE:
701 gwin_opendir (const char *dirname)
707 g_return_val_if_fail (dirname != NULL, NULL);
709 result = g_new0 (DIR, 1);
710 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
711 result->dir_name = g_strdup (dirname);
713 k = strlen (result->dir_name);
714 if (k && result->dir_name[k - 1] == '\\')
716 result->dir_name[k - 1] = '\0';
719 mask = g_strdup_printf ("%s\\*", result->dir_name);
721 result->find_file_handle = (guint) FindFirstFile (mask,
722 (LPWIN32_FIND_DATA) result->find_file_data);
725 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
727 int error = GetLastError ();
729 g_free (result->dir_name);
730 g_free (result->find_file_data);
739 result->just_opened = TRUE;
745 gwin_readdir (DIR *dir)
747 static struct dirent result;
749 g_return_val_if_fail (dir != NULL, NULL);
751 if (dir->just_opened)
752 dir->just_opened = FALSE;
755 if (!FindNextFile ((HANDLE) dir->find_file_handle,
756 (LPWIN32_FIND_DATA) dir->find_file_data))
758 int error = GetLastError ();
762 case ERROR_NO_MORE_FILES:
770 strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
776 gwin_rewinddir (DIR *dir)
780 g_return_if_fail (dir != NULL);
782 if (!FindClose ((HANDLE) dir->find_file_handle))
783 g_warning ("gwin_rewinddir(): FindClose() failed\n");
785 mask = g_strdup_printf ("%s\\*", dir->dir_name);
786 dir->find_file_handle = (guint) FindFirstFile (mask,
787 (LPWIN32_FIND_DATA) dir->find_file_data);
790 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
792 int error = GetLastError ();
801 dir->just_opened = TRUE;
805 gwin_closedir (DIR *dir)
807 g_return_val_if_fail (dir != NULL, -1);
809 if (!FindClose ((HANDLE) dir->find_file_handle))
811 int error = GetLastError ();
816 errno = EIO; return -1;
820 g_free (dir->dir_name);
821 g_free (dir->find_file_data);
827 #endif /* NATIVE_WIN32 */