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 (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);
160 #else /* !HAVE_VSNPRINTF */
164 va_start (args, fmt);
165 printed = g_strdup_vprintf (fmt, args);
168 strncpy (str, printed, n);
174 #endif /* !HAVE_VSNPRINTF */
178 g_vsnprintf (gchar *str,
183 #ifdef HAVE_VSNPRINTF
186 retval = vsnprintf (str, n, fmt, args);
189 #else /* !HAVE_VSNPRINTF */
192 printed = g_strdup_vprintf (fmt, args);
193 strncpy (str, printed, n);
199 #endif /* !HAVE_VSNPRINTF */
203 g_parse_debug_string (const gchar *string,
210 g_return_val_if_fail (string != NULL, 0);
212 if (!g_strcasecmp (string, "all"))
214 for (i=0; i<nkeys; i++)
215 result |= keys[i].value;
219 gchar *str = g_strdup (string);
222 gboolean done = FALSE;
235 for (i=0; i<nkeys; i++)
236 if (!g_strcasecmp(keys[i].key, p))
237 result |= keys[i].value;
249 g_basename (const gchar *file_name)
251 register gchar *base;
253 g_return_val_if_fail (file_name != NULL, NULL);
255 base = strrchr (file_name, G_DIR_SEPARATOR);
260 if (isalpha (file_name[0]) && file_name[1] == ':')
261 return (gchar*) file_name + 2;
262 #endif /* NATIVE_WIN32 */
264 return (gchar*) file_name;
268 g_path_is_absolute (const gchar *file_name)
270 g_return_val_if_fail (file_name != NULL, FALSE);
272 if (file_name[0] == G_DIR_SEPARATOR)
276 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
284 g_path_skip_root (gchar *file_name)
286 g_return_val_if_fail (file_name != NULL, NULL);
288 if (file_name[0] == G_DIR_SEPARATOR)
289 return file_name + 1;
292 if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
293 return file_name + 3;
300 g_dirname (const gchar *file_name)
302 register gchar *base;
305 g_return_val_if_fail (file_name != NULL, NULL);
307 base = strrchr (file_name, G_DIR_SEPARATOR);
309 return g_strdup (".");
310 while (base > file_name && *base == G_DIR_SEPARATOR)
312 len = (guint) 1 + base - file_name;
314 base = g_new (gchar, len + 1);
315 g_memmove (base, file_name, len);
322 g_get_current_dir (void)
327 buffer = g_new (gchar, G_PATH_LENGTH);
330 /* We don't use getcwd(3) on SUNOS, because, it does a popen("pwd")
331 * and, if that wasn't bad enough, hangs in doing so.
333 #if defined (sun) && !defined (__SVR4)
334 dir = getwd (buffer);
336 dir = getcwd (buffer, G_PATH_LENGTH - 1);
339 if (!dir || !*buffer)
341 /* hm, should we g_error() out here?
342 * this can happen if e.g. "./" has mode \0000
344 buffer[0] = G_DIR_SEPARATOR;
348 dir = g_strdup (buffer);
355 g_getenv (const gchar *variable)
358 g_return_val_if_fail (variable != NULL, NULL);
360 return getenv (variable);
364 static gchar *p = NULL;
368 g_return_val_if_fail (variable != NULL, NULL);
370 v = getenv (variable);
374 /* On Windows NT, it is relatively typical that environment variables
375 * contain references to other environment variables. Handle that by
376 * calling ExpandEnvironmentStrings.
379 /* First check how much space we need */
380 k = ExpandEnvironmentStrings (v, dummy, 2);
381 /* Then allocate that much, and actualy do the expansion */
389 p = g_realloc (p, k);
392 ExpandEnvironmentStrings (v, p, k);
398 G_LOCK_DEFINE_STATIC (g_utils_global);
400 static gchar *g_tmp_dir = NULL;
401 static gchar *g_user_name = NULL;
402 static gchar *g_real_name = NULL;
403 static gchar *g_home_dir = NULL;
405 /* HOLDS: g_utils_global_lock */
407 g_get_any_init (void)
411 g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
413 g_tmp_dir = g_strdup (g_getenv ("TMP"));
415 g_tmp_dir = g_strdup (g_getenv ("TEMP"));
421 g_tmp_dir = g_strdup (P_tmpdir);
422 k = strlen (g_tmp_dir);
423 if (g_tmp_dir[k-1] == G_DIR_SEPARATOR)
424 g_tmp_dir[k-1] = '\0';
431 g_tmp_dir = g_strdup ("/tmp");
432 #else /* NATIVE_WIN32 */
433 g_tmp_dir = g_strdup ("C:\\");
434 #endif /* NATIVE_WIN32 */
438 g_home_dir = g_strdup (g_getenv ("HOME"));
443 /* The official way to specify a home directory on NT is
444 * the HOMEDRIVE and HOMEPATH environment variables.
446 * This is inside #ifdef NATIVE_WIN32 because with the cygwin dll,
447 * HOME should be a POSIX style pathname.
450 if (getenv ("HOMEDRIVE") != NULL && getenv ("HOMEPATH") != NULL)
452 gchar *homedrive, *homepath;
454 homedrive = g_strdup (g_getenv ("HOMEDRIVE"));
455 homepath = g_strdup (g_getenv ("HOMEPATH"));
457 g_home_dir = g_strconcat (homedrive, homepath, NULL);
462 #endif /* !NATIVE_WIN32 */
466 struct passwd *pw = NULL;
467 gpointer buffer = NULL;
469 # ifdef HAVE_GETPWUID_R
471 # ifdef _SC_GETPW_R_SIZE_MAX
472 /* This reurns the maximum length */
473 guint bufsize = sysconf (_SC_GETPW_R_SIZE_MAX);
474 # else /* _SC_GETPW_R_SIZE_MAX */
476 # endif /* _SC_GETPW_R_SIZE_MAX */
482 buffer = g_malloc (bufsize);
485 # ifdef HAVE_GETPWUID_R_POSIX
486 error = getpwuid_r (getuid (), &pwd, buffer, bufsize, &pw);
487 error = error < 0 ? errno : error;
488 # else /* !HAVE_GETPWUID_R_POSIX */
490 error = getpwuid_r (getuid (), &pwd, buffer, bufsize);
491 pw = error == 0 ? &pwd : NULL;
493 pw = getpwuid_r (getuid (), &pwd, buffer, bufsize);
494 error = pw ? 0 : errno;
496 # endif /* !HAVE_GETPWUID_R_POSIX */
500 /* we bail out prematurely if the user id can't be found
501 * (should be pretty rare case actually), or if the buffer
502 * should be sufficiently big and lookups are still not
505 if (error == 0 || error == ENOENT)
507 g_warning ("getpwuid_r(): failed due to: "
508 "No such user: %lu.", (unsigned long)getuid ());
511 if (bufsize > 32 * 1024)
513 g_warning ("getpwuid_r(): failed due to: %s.",
522 # endif /* !HAVE_GETPWUID_R */
527 pw = getpwuid (getuid ());
532 g_user_name = g_strdup (pw->pw_name);
534 g_real_name = g_strdup (pw->pw_gecos);
536 g_real_name = g_strdup (g_user_name);
539 g_home_dir = g_strdup (pw->pw_dir);
544 #else /* !HAVE_PWD_H */
551 if (GetUserName (buffer, &len))
553 g_user_name = g_strdup (buffer);
554 g_real_name = g_strdup (buffer);
557 # endif /* NATIVE_WIN32 */
559 #endif /* !HAVE_PWD_H */
562 /* change '\\' in %HOME% to '/' */
563 g_strdelimit (g_home_dir, "\\",'/');
566 g_user_name = g_strdup ("somebody");
568 g_real_name = g_strdup ("Unknown");
573 for (p = g_real_name; *p; p++)
577 p = g_strdup (g_real_name);
578 g_free (g_real_name);
587 g_get_user_name (void)
589 G_LOCK (g_utils_global);
592 G_UNLOCK (g_utils_global);
598 g_get_real_name (void)
600 G_LOCK (g_utils_global);
603 G_UNLOCK (g_utils_global);
608 /* Return the home directory of the user. If there is a HOME
609 * environment variable, its value is returned, otherwise use some
610 * system-dependent way of finding it out. If no home directory can be
611 * deduced, return NULL.
615 g_get_home_dir (void)
617 G_LOCK (g_utils_global);
620 G_UNLOCK (g_utils_global);
625 /* Return a directory to be used to store temporary files. This is the
626 * value of the TMPDIR, TMP or TEMP environment variables (they are
627 * checked in that order). If none of those exist, use P_tmpdir from
628 * stdio.h. If that isn't defined, return "/tmp" on POSIXly systems,
629 * and C:\ on Windows.
635 G_LOCK (g_utils_global);
638 G_UNLOCK (g_utils_global);
643 static gchar *g_prgname = NULL;
650 G_LOCK (g_utils_global);
652 G_UNLOCK (g_utils_global);
658 g_set_prgname (const gchar *prgname)
662 G_LOCK (g_utils_global);
664 g_prgname = g_strdup (prgname);
666 G_UNLOCK (g_utils_global);
670 g_direct_hash (gconstpointer v)
672 return GPOINTER_TO_UINT (v);
676 g_direct_equal (gconstpointer v1,
683 g_int_equal (gconstpointer v1,
686 return *((const gint*) v1) == *((const gint*) v2);
690 g_int_hash (gconstpointer v)
692 return *(const gint*) v;
698 gwin_ftruncate (gint fd,
704 g_return_val_if_fail (fd >= 0, -1);
706 hfile = (HANDLE) _get_osfhandle (fd);
707 curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
708 if (curpos == 0xFFFFFFFF
709 || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
710 || !SetEndOfFile (hfile))
712 gint error = GetLastError ();
716 case ERROR_INVALID_HANDLE:
731 gwin_opendir (const char *dirname)
737 g_return_val_if_fail (dirname != NULL, NULL);
739 result = g_new0 (DIR, 1);
740 result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
741 result->dir_name = g_strdup (dirname);
743 k = strlen (result->dir_name);
744 if (k && result->dir_name[k - 1] == '\\')
746 result->dir_name[k - 1] = '\0';
749 mask = g_strdup_printf ("%s\\*", result->dir_name);
751 result->find_file_handle = (guint) FindFirstFile (mask,
752 (LPWIN32_FIND_DATA) result->find_file_data);
755 if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
757 int error = GetLastError ();
759 g_free (result->dir_name);
760 g_free (result->find_file_data);
769 result->just_opened = TRUE;
775 gwin_readdir (DIR *dir)
777 static struct dirent result;
779 g_return_val_if_fail (dir != NULL, NULL);
781 if (dir->just_opened)
782 dir->just_opened = FALSE;
785 if (!FindNextFile ((HANDLE) dir->find_file_handle,
786 (LPWIN32_FIND_DATA) dir->find_file_data))
788 int error = GetLastError ();
792 case ERROR_NO_MORE_FILES:
800 strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
806 gwin_rewinddir (DIR *dir)
810 g_return_if_fail (dir != NULL);
812 if (!FindClose ((HANDLE) dir->find_file_handle))
813 g_warning ("gwin_rewinddir(): FindClose() failed\n");
815 mask = g_strdup_printf ("%s\\*", dir->dir_name);
816 dir->find_file_handle = (guint) FindFirstFile (mask,
817 (LPWIN32_FIND_DATA) dir->find_file_data);
820 if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
822 int error = GetLastError ();
831 dir->just_opened = TRUE;
835 gwin_closedir (DIR *dir)
837 g_return_val_if_fail (dir != NULL, -1);
839 if (!FindClose ((HANDLE) dir->find_file_handle))
841 int error = GetLastError ();
846 errno = EIO; return -1;
850 g_free (dir->dir_name);
851 g_free (dir->find_file_data);
857 #endif /* NATIVE_WIN32 */