summaryrefslogtreecommitdiff
path: root/gutils.c
diff options
context:
space:
mode:
authorTor Lillqvist <tml@iki.fi>1998-10-27 04:11:34 +0000
committerTim Janik <timj@src.gnome.org>1998-10-27 04:11:34 +0000
commita6149403deed931b8c78b26aeb0e9c952cf3ef93 (patch)
tree4af17a36f5c19446cd177757b6fd2ef3dbe8ae03 /gutils.c
parent57d59041f844dc58ce6f70dce449bf03c801de85 (diff)
downloadglib-a6149403deed931b8c78b26aeb0e9c952cf3ef93.tar.gz
removed dummy structure definitions for struct _GCache, _GTree, _GTimer,
Tue Oct 27 03:00:50 1998 Tim Janik <timj@gtk.org> * glib.h: removed dummy structure definitions for struct _GCache, _GTree, _GTimer, _GMemChunk, _GListAllocator and _GStringChunk. * gutils.c: implement glib's inline functions _after_ all include statements have been processed. removed Tor's MAXPATHLEN check since there already was one supplied further down in this file. (LibMain): special cased the #ifdef __LCC__ case for NATIVE_WIN32, since lcc maybe used on other platforms as well. why in hell is this stuff required? (g_get_any_init): for windows, if the user name is supplied, use it as realname also. in general, if there is no homedir specified, use the tmpdir that we already figured. * gtimer.c (g_timer_elapsed): changed a g_assert() statement to g_return_if_fail(). * applied glib-tml-981020-0.patch for WIN32 portability, added some comments and g_return_if_fail() statements, minor indentation fixes. ChangeLog entry from Tor Lillqvist is appended. * glib.h (struct dirent): use lower case structure members. * glib.h: * makefile.lcc: * makefile.msc: s/COMPILING_GLIB/GLIB_COMPILATION/ 1998-10-20: Tor Lillqvist <tml@iki.fi> * README.win32 glib.def gmodule.def * glibconfig.h.win32 gmodule/gmoduleconf.h.win32: New files for the Windows port. The .def files list exported symbols for the Microsoft linker and compatibles. * configure.in: Added checks for some platform-dependent headers: pwd.h sys/param.h sys/select.h sys/time.h sys/times.h unistd.h, and the function lstat. * gerror.c: Conditionalized inclusion of system-dependent headers. Changes for Windows: no gdb to do a stack trace. Just call abort(). * glib.h: Changes for Windows: Added macros G_DIR_SEPARATOR, G_DIR_SEPARATOR_S for platform-dependent file name syntax elements. Added macros G_SEARCHPATH_SEPARATOR, G_SEARCHPATH_SEPARATOR_S for platform-dependent search path syntax conventions. Added pragmas for Microsoft C to make it more pedantic. Marked GLib's global variables for export from DLL. Added the function g_strescape that escapes backslashes. Added functions g_path_is_absolute and g_path_skip_root to handle platform-dependent file name syntax. Added the function g_getenv that expands environment variables that contain references to other environment variables, as is typical on Windows NT. Added the GIOChannel structure which is used to encapsulate the IPC mechanism used by the GIMP's plug-ins, and possibly other things later. On Unix a GIOChannel encapsulates just a file descriptor. On Windows it contains a file handle from _pipe() and a few other things related to the implementation of gdk_input_add and GIMP plug-in communication. Subject to change. Removed duplicate declarations of the version variables. For the Microsoft compiler, declare own implementation of ftruncate and the <dirent.h> functions. * gmem.c: Define a symbolic name for the profiling table size. * gmessages.c: Conditionalized inclusion of unistd.h. On Windows, output using stdio to stdout. * gscanner.c: Conditionalized inclusion of unistd.h. Added changes for Microsoft C. Added CR to the skipped character set. Added small workaround for MSC compiler bug in g_scanner_cur_value. * gstrfuncs.c: Added the function g_strescape, which escapes the backslash character. Needed especially when printing Windows filenames. * gtimer.c: Conditionalized inclusion of unistd.h and sys/time.h. Added implementations for Windows. * gutils.c: Conditionalized inclusion of platform-dependent headers. Use the platform-independent file name syntax macros. Conditionalize code on platform-dependent features. Added the functions g_path_is_absolute g_path_skip_root and g_getenv. Added the GIOChannel-related functions. Added compiler-dependent Unix compatibility functions for Windows. * makefile.lcc makefile.msc: New files. Compiler-specific makefiles for LCC-Win32 and Microsoft C. Only Microsoft C is actually supported currently. * testglib.c: Added pathname check cases for Windows. Added workaround for bug in the Microsoft runtime library. Improved some tests a bit. Tue Oct 27 04:00:11 1998 Tim Janik <timj@gtk.org> * testgmodule.c (main): changed the #ifdef WIN32 test to NATIVE_WIN32, this needs to be more constistent throughout the code, do we go for NATIVE_WIN32 or WIN32? * gmodule.c (LibMain): special cased the #ifdef __LCC__ case for NATIVE_WIN32, since lcc maybe used on other platforms as well. * libgplugin_a.c (LibMain): * libgplugin_b.c (LibMain): likewise. not sure i like this special requirement for lcc in here. * gmodule-dl.c (_g_module_build_path): feature empty "" directories and prepend the module name with "lib". * gmodule-dld.c (_g_module_build_path): * gmodule-win32.c (_g_module_build_path): feature empty "" directories. * we need some more magic in the _g_module_build_path variants so we don't append/prepend lib and .so, .sl or .dll for those names that already contain it. * applied patch from Tor Lillqvist for g_module_build_path() and windows support. 1998-10-20: Tor Lillqvist <tml@iki.fi> * gmodule/gmodule-win32.c: New file. * gmodule/gmodule.c gmodule/gmodule.h: Added the funcion g_module_build_path that builds the path to a module file, decorating the name according to the system's conventions. Added the Windows implementation. * gmodule/libgplugin_a.c gmodule/libgplugin_b.c: Added LibMain for LCC-Win32. * gmodule/testgmodule.c: Handle Windows dll names.
Diffstat (limited to 'gutils.c')
-rw-r--r--gutils.c416
1 files changed, 393 insertions, 23 deletions
diff --git a/gutils.c b/gutils.c
index 6c2d4cfeb..dd42ef850 100644
--- a/gutils.c
+++ b/gutils.c
@@ -16,15 +16,34 @@
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
+#include "glibconfig.h"
+
+#ifdef HAVE_UNISTD_H
#include <unistd.h>
+#endif
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
+#ifdef HAVE_PWD_H
#include <pwd.h>
+#endif
#include <sys/types.h>
+#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
+#endif
+
+#ifdef NATIVE_WIN32
+# define STRICT /* Strict typing, please */
+# include <windows.h>
+# include <direct.h>
+# include <errno.h>
+# include <ctype.h>
+# ifdef _MSC_VER
+# include <io.h>
+# endif /* _MSC_VER */
+#endif /* NATIVE_WIN32 */
/* implement Glib's inline functions
*/
@@ -32,12 +51,29 @@
#define G_CAN_INLINE 1
#include "glib.h"
+#ifdef MAXPATHLEN
+#define G_PATH_LENGTH (MAXPATHLEN + 1)
+#elif defined (PATH_MAX)
+#define G_PATH_LENGTH (PATH_MAX + 1)
+#else /* !MAXPATHLEN */
+#define G_PATH_LENGTH (2048 + 1)
+#endif /* !MAXPATHLEN && !PATH_MAX */
+
const guint glib_major_version = GLIB_MAJOR_VERSION;
const guint glib_minor_version = GLIB_MINOR_VERSION;
const guint glib_micro_version = GLIB_MICRO_VERSION;
const guint glib_interface_age = GLIB_INTERFACE_AGE;
const guint glib_binary_age = GLIB_BINARY_AGE;
+#if defined (NATIVE_WIN32) && defined (__LCC__)
+int __stdcall
+LibMain (void *hinstDll,
+ unsigned long dwReason,
+ void *reserved)
+{
+ return 1;
+}
+#endif /* NATIVE_WIN32 && __LCC__ */
void
g_atexit (GVoidFunc func)
@@ -184,13 +220,50 @@ g_basename (const gchar *file_name)
g_return_val_if_fail (file_name != NULL, NULL);
- base = strrchr (file_name, '/');
+ base = strrchr (file_name, G_DIR_SEPARATOR);
if (base)
return base + 1;
+
+#ifdef NATIVE_WIN32
+ if (isalpha (file_name[0]) && file_name[1] == ':')
+ return (gchar*) file_name + 2;
+#endif /* NATIVE_WIN32 */
return (gchar*) file_name;
}
+gboolean
+g_path_is_absolute (const gchar *file_name)
+{
+ g_return_val_if_fail (file_name != NULL, FALSE);
+
+ if (file_name[0] == G_DIR_SEPARATOR)
+ return TRUE;
+
+#ifdef NATIVE_WIN32
+ if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
+ return TRUE;
+#endif
+
+ return FALSE;
+}
+
+gchar*
+g_path_skip_root (gchar *file_name)
+{
+ g_return_val_if_fail (file_name != NULL, NULL);
+
+ if (file_name[0] == G_DIR_SEPARATOR)
+ return file_name + 1;
+
+#ifdef NATIVE_WIN32
+ if (isalpha (file_name[0]) && file_name[1] == ':' && file_name[2] == G_DIR_SEPARATOR)
+ return file_name + 3;
+#endif
+
+ return NULL;
+}
+
gchar*
g_dirname (const gchar *file_name)
{
@@ -199,10 +272,10 @@ g_dirname (const gchar *file_name)
g_return_val_if_fail (file_name != NULL, NULL);
- base = strrchr (file_name, '/');
+ base = strrchr (file_name, G_DIR_SEPARATOR);
if (!base)
return g_strdup (".");
- while (base > file_name && *base == '/')
+ while (base > file_name && *base == G_DIR_SEPARATOR)
base--;
len = (guint) 1 + base - file_name;
@@ -213,14 +286,6 @@ g_dirname (const gchar *file_name)
return base;
}
-#ifdef MAXPATHLEN
-#define G_PATH_LENGTH (MAXPATHLEN + 1)
-#elif defined (PATH_MAX)
-#define G_PATH_LENGTH (PATH_MAX + 1)
-#else /* !MAXPATHLEN */
-#define G_PATH_LENGTH (2048 + 1)
-#endif /* !MAXPATHLEN && !PATH_MAX */
-
gchar*
g_get_current_dir (void)
{
@@ -244,7 +309,7 @@ g_get_current_dir (void)
/* hm, should we g_error() out here?
* this can happen if e.g. "./" has mode \0000
*/
- buffer[0] = '/';
+ buffer[0] = G_DIR_SEPARATOR;
buffer[1] = 0;
}
@@ -254,6 +319,48 @@ g_get_current_dir (void)
return dir;
}
+gchar*
+g_getenv (const gchar *variable)
+{
+ g_return_val_if_fail (variable != NULL, NULL);
+
+#ifndef NATIVE_WIN32
+ return getenv (variable);
+#else
+ gchar *v;
+ guint l, k;
+ gchar *p;
+
+ v = getenv (variable);
+ if (!v)
+ return NULL;
+
+ /* On Windows NT, it is relatively typical that environment variables
+ * contain references to other environment variables. Handle that by
+ * calling ExpandEnvironmentStrings.
+ */
+
+ v = g_strdup (v);
+ l = 16;
+ do
+ {
+ p = g_new (gchar, l);
+
+ k = ExpandEnvironmentStrings (v, p, l);
+ if (k > l)
+ {
+ g_free (p);
+ l *= 2;
+ }
+ }
+ while (k > l);
+
+ g_free (v);
+
+ return p;
+#endif
+}
+
static gchar *g_tmp_dir = NULL;
static gchar *g_user_name = NULL;
static gchar *g_real_name = NULL;
@@ -264,18 +371,28 @@ g_get_any_init (void)
{
if (!g_tmp_dir)
{
+#ifdef HAVE_PWD_H
struct passwd *pw;
-
- g_tmp_dir = g_strdup (getenv ("TMPDIR"));
+#endif
+
+ g_tmp_dir = g_strdup (g_getenv ("TMPDIR"));
if (!g_tmp_dir)
- g_tmp_dir = g_strdup (getenv ("TMP"));
+ g_tmp_dir = g_strdup (g_getenv ("TMP"));
if (!g_tmp_dir)
- g_tmp_dir = g_strdup (getenv ("TEMP"));
+ g_tmp_dir = g_strdup (g_getenv ("TEMP"));
+
if (!g_tmp_dir)
- g_tmp_dir = g_strdup ("/tmp");
+ {
+#ifndef NATIVE_WIN32
+ g_tmp_dir = g_strdup (G_DIR_SEPARATOR_S "tmp");
+#else /* !NATIVE_WIN32 */
+ g_tmp_dir = g_strdup (".");
+#endif /* !NATIVE_WIN32 */
+ }
- g_home_dir = g_strdup (getenv ("HOME"));
+ g_home_dir = g_strdup (g_getenv ("HOME"));
+#ifdef HAVE_PWD_H
setpwent ();
pw = getpwuid (getuid ());
endpwent ();
@@ -287,7 +404,33 @@ g_get_any_init (void)
if (!g_home_dir)
g_home_dir = g_strdup (pw->pw_dir);
}
+#else /* !HAVE_PWD_H */
+# ifdef NATIVE_WIN32
+ {
+ guint len = 17;
+
+ g_user_name = g_new (gchar, len);
+
+ if (!GetUserName (g_user_name, &len))
+ {
+ g_free (g_user_name);
+ g_user_name = g_strdup ("somebody");
+ g_real_name = g_strdup ("Unknown");
+ }
+ else
+ g_real_name = g_strdup (g_user_name);
+ g_home_dir = NULL;
+ }
+# else /* !NATIVE_WIN32 */
+ g_user_name = g_strdup ("somebody");
+ g_real_name = g_strdup ("Unknown");
+ g_home_dir = NULL;
+# endif /* !NATIVE_WIN32 */
+#endif /* !HAVE_PWD_H */
}
+
+ if (!g_home_dir)
+ g_home_dir = g_strdup (g_tmp_dir);
}
gchar*
@@ -344,21 +487,23 @@ g_set_prgname (const gchar *prgname)
}
guint
-g_direct_hash(gconstpointer v)
+g_direct_hash (gconstpointer v)
{
return GPOINTER_TO_UINT (v);
}
gint
-g_direct_equal(gconstpointer v, gconstpointer v2)
+g_direct_equal (gconstpointer v1,
+ gconstpointer v2)
{
- return GPOINTER_TO_UINT (v) == GPOINTER_TO_UINT (v2);
+ return GPOINTER_TO_UINT (v1) == GPOINTER_TO_UINT (v2);
}
gint
-g_int_equal (gconstpointer v, gconstpointer v2)
+g_int_equal (gconstpointer v1,
+ gconstpointer v2)
{
- return *((const gint*) v) == *((const gint*) v2);
+ return *((const gint*) v1) == *((const gint*) v2);
}
guint
@@ -366,3 +511,228 @@ g_int_hash (gconstpointer v)
{
return *(const gint*) v;
}
+
+GIOChannel*
+g_iochannel_new (gint fd)
+{
+ GIOChannel *channel = g_new0 (GIOChannel, 1);
+
+ channel->fd = fd;
+
+#ifdef NATIVE_WIN32
+ channel->peer = 0;
+ channel->peer_fd = 0;
+ channel->offset = 0;
+ channel->peer_offset = 0;
+#endif /* NATIVE_WIN32 */
+
+ return channel;
+}
+
+void
+g_iochannel_free (GIOChannel *channel)
+{
+ g_return_if_fail (channel != NULL);
+
+ g_free (channel);
+}
+
+void
+g_iochannel_close_and_free (GIOChannel *channel)
+{
+ g_return_if_fail (channel != NULL);
+
+ close (channel->fd);
+
+ g_iochannel_free (channel);
+}
+
+#undef g_iochannel_wakeup_peer
+
+void
+g_iochannel_wakeup_peer (GIOChannel *channel)
+{
+ static guint message = 0;
+
+ g_return_if_fail (channel != NULL);
+
+#ifdef NATIVE_WIN32
+ if (message == 0)
+ message = RegisterWindowMessage ("gdk-pipe-readable");
+
+# if 0
+ g_print ("g_iochannel_wakeup_peer: calling PostThreadMessage (%#x, %d, %d, %d)\n",
+ channel->peer, message, channel->peer_fd, channel->offset);
+# endif
+ PostThreadMessage (channel->peer, message,
+ channel->peer_fd, channel->offset);
+#endif /* NATIVE_WIN32 */
+}
+
+
+#ifdef NATIVE_WIN32
+#ifdef _MSC_VER
+
+int
+gwin_ftruncate (gint fd,
+ guint size)
+{
+ HANDLE hfile;
+ guint curpos;
+
+ g_return_val_if_fail (fd >= 0, -1);
+
+ hfile = (HANDLE) _get_osfhandle (fd);
+ curpos = SetFilePointer (hfile, 0, NULL, FILE_CURRENT);
+ if (curpos == 0xFFFFFFFF
+ || SetFilePointer (hfile, size, NULL, FILE_BEGIN) == 0xFFFFFFFF
+ || !SetEndOfFile (hfile))
+ {
+ gint error = GetLastError ();
+
+ switch (error)
+ {
+ case ERROR_INVALID_HANDLE:
+ errno = EBADF;
+ break;
+ default:
+ errno = EIO;
+ break;
+ }
+
+ return -1;
+ }
+
+ return 0;
+}
+
+DIR*
+gwin_opendir (const char *dirname)
+{
+ DIR *result;
+ gchar *mask;
+ guint k;
+
+ g_return_val_if_fail (dirname != NULL, NULL);
+
+ result = g_new0 (DIR, 1);
+ result->find_file_data = g_new0 (WIN32_FIND_DATA, 1);
+ result->dir_name = g_strdup (dirname);
+
+ k = strlen (result->dir_name);
+ if (k && result->dir_name[k - 1] == '\\')
+ {
+ result->dir_name[k - 1] = '\0';
+ k--;
+ }
+ mask = g_strdup_printf ("%s\\*", result->dir_name);
+
+ result->find_file_handle = (guint) FindFirstFile (mask,
+ (LPWIN32_FIND_DATA) result->find_file_data);
+ g_free (mask);
+
+ if (result->find_file_handle == (guint) INVALID_HANDLE_VALUE)
+ {
+ int error = GetLastError ();
+
+ g_free (result->dir_name);
+ g_free (result->find_file_data);
+ g_free (result);
+ switch (error)
+ {
+ default:
+ errno = EIO;
+ return NULL;
+ }
+ }
+ result->just_opened = TRUE;
+
+ return result;
+}
+
+struct dirent*
+gwin_readdir (DIR *dir)
+{
+ static struct dirent result;
+
+ g_return_val_if_fail (dir != NULL, NULL);
+
+ if (dir->just_opened)
+ dir->just_opened = FALSE;
+ else
+ {
+ if (!FindNextFile ((HANDLE) dir->find_file_handle,
+ (LPWIN32_FIND_DATA) dir->find_file_data))
+ {
+ int error = GetLastError ();
+
+ switch (error)
+ {
+ case ERROR_NO_MORE_FILES:
+ return NULL;
+ default:
+ errno = EIO;
+ return NULL;
+ }
+ }
+ }
+ strcpy (result.d_name, g_basename (((LPWIN32_FIND_DATA) dir->find_file_data)->cFileName));
+
+ return &result;
+}
+
+void
+gwin_rewinddir (DIR *dir)
+{
+ gchar *mask;
+
+ g_return_if_fail (dir != NULL);
+
+ if (!FindClose ((HANDLE) dir->find_file_handle))
+ g_warning ("gwin_rewinddir(): FindClose() failed\n");
+
+ mask = g_strdup_printf ("%s\\*", dir->dir_name);
+ dir->find_file_handle = (guint) FindFirstFile (mask,
+ (LPWIN32_FIND_DATA) dir->find_file_data);
+ g_free (mask);
+
+ if (dir->find_file_handle == (guint) INVALID_HANDLE_VALUE)
+ {
+ int error = GetLastError ();
+
+ switch (error)
+ {
+ default:
+ errno = EIO;
+ return;
+ }
+ }
+ dir->just_opened = TRUE;
+}
+
+gint
+gwin_closedir (DIR *dir)
+{
+ g_return_val_if_fail (dir != NULL, -1);
+
+ if (!FindClose ((HANDLE) dir->find_file_handle))
+ {
+ int error = GetLastError ();
+
+ switch (error)
+ {
+ default:
+ errno = EIO; return -1;
+ }
+ }
+
+ g_free (dir->dir_name);
+ g_free (dir->find_file_data);
+ g_free (dir);
+
+ return 0;
+}
+
+#endif /* _MSC_VER */
+
+#endif /* NATIVE_WIN32 */