From 04b80a26e7d0f21b38e177c159fdab1901d15386 Mon Sep 17 00:00:00 2001 From: Yevgen Muntyan <17531749+muntyan@users.noreply.github.com> Date: Fri, 25 Dec 2015 21:30:53 -0800 Subject: [PATCH] Do not use errno directly --- m4/moo-flags.m4 | 2 +- moo/Makefile.am | 2 +- moo/eggsmclient/eggdesktopfile.c | 2 + moo/eggsmclient/eggsmclient-xsmp.c | 2 + moo/gtksourceview/gtksourcecontextengine.c | 7 +- moo/mooapp/mooapp.c | 4 +- moo/mooapp/mooappabout.h | 6 +- moo/mooedit/mooedit-fileops.c | 1 - moo/mooedit/mooeditor-tests.c | 12 +- moo/mooedit/mootextprint.c | 21 +-- moo/moofileview/moobookmarkmgr.c | 1 - moo/moofileview/moofile.c | 27 ++- moo/moofileview/moofilesystem.c | 27 +-- moo/moofileview/moofolder.c | 1 - moo/mooglib/moo-glib.c | 57 ------ moo/mooglib/moo-glib.cpp | 191 +++++++++++++++++++++ moo/mooglib/moo-glib.h | 74 +++++++- moo/mooglib/moo-stat.h | 4 +- moo/mooglib/moo-time.h | 2 + moo/mooutils/mooapp-ipc.c | 7 +- moo/mooutils/mooappinput.c | 5 + moo/mooutils/moofilewatch.c | 48 +++--- moo/mooutils/moofilewriter.c | 31 ++-- moo/mooutils/mooglade.c | 22 +-- moo/mooutils/moohistorymgr.c | 3 +- moo/mooutils/mooprefs.c | 8 +- moo/mooutils/mooutils-fs.c | 154 ++++++++--------- moo/mooutils/mooutils-fs.h | 20 +-- moo/mooutils/mooutils-gobject.c | 14 +- moo/mooutils/mooutils-misc.c | 7 +- moo/mooutils/mooutils-script.c | 3 +- moo/mooutils/mooutils-thread.c | 1 - moo/mooutils/mooutils-win32.c | 15 +- moo/plugins/ctags/ctags-doc.c | 1 + moo/plugins/moofilelist.c | 3 +- moo/plugins/moofileselector.c | 9 +- moo/plugins/moofind.c | 7 +- moo/plugins/usertools/moocommand.c | 15 +- 38 files changed, 501 insertions(+), 315 deletions(-) delete mode 100644 moo/mooglib/moo-glib.c create mode 100644 moo/mooglib/moo-glib.cpp diff --git a/m4/moo-flags.m4 b/m4/moo-flags.m4 index 95761f81..36e0067e 100644 --- a/m4/moo-flags.m4 +++ b/m4/moo-flags.m4 @@ -95,7 +95,7 @@ _MOO_AC_CHECK_COMPILER_OPTIONS([dnl -Wno-format-y2k -Wno-overlength-strings dnl ]) _MOO_AC_CHECK_CXX_COMPILER_OPTIONS([dnl --std=c++98 -fno-rtti dnl +-std=c++11 -fno-rtti dnl ]) if test "x$MOO_DEBUG_ENABLED" = "xyes"; then diff --git a/moo/Makefile.am b/moo/Makefile.am index 80d4d880..4533de67 100644 --- a/moo/Makefile.am +++ b/moo/Makefile.am @@ -24,7 +24,7 @@ moo_sources += \ mooglib/moo-glib.h \ mooglib/moo-stat.h \ mooglib/moo-time.h \ - mooglib/moo-glib.c + mooglib/moo-glib.cpp EXTRA_DIST += marshals.list built_moo_sources += marshals.h diff --git a/moo/eggsmclient/eggdesktopfile.c b/moo/eggsmclient/eggdesktopfile.c index 443d5ec6..fa9b9b9d 100644 --- a/moo/eggsmclient/eggdesktopfile.c +++ b/moo/eggsmclient/eggdesktopfile.c @@ -25,6 +25,8 @@ #include "config.h" #endif +#define MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS + #include "eggdesktopfile.h" #include diff --git a/moo/eggsmclient/eggsmclient-xsmp.c b/moo/eggsmclient/eggsmclient-xsmp.c index fd66f325..e67fa4a3 100644 --- a/moo/eggsmclient/eggsmclient-xsmp.c +++ b/moo/eggsmclient/eggsmclient-xsmp.c @@ -23,6 +23,8 @@ #include "config.h" +#define MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS + #include "eggsmclient.h" #include "eggsmclient-private.h" diff --git a/moo/gtksourceview/gtksourcecontextengine.c b/moo/gtksourceview/gtksourcecontextengine.c index 4a3010a6..7fcb39cb 100644 --- a/moo/gtksourceview/gtksourcecontextengine.c +++ b/moo/gtksourceview/gtksourcecontextengine.c @@ -26,7 +26,6 @@ #include "gtksourcebuffer.h" #include "gtksourcestyle-private.h" #include -#include #include #undef DEBUG @@ -2682,14 +2681,14 @@ sub_pattern_to_int (const gchar *name) { guint64 number; gchar *end_name; + mgw_errno_t err; if (*name == 0) return -1; - errno = 0; - number = g_ascii_strtoull (name, &end_name, 10); + number = mgw_ascii_strtoull (name, &end_name, 10, &err); - if (errno !=0 || number > G_MAXINT || *end_name != 0) + if (mgw_errno_is_set (err) || number > G_MAXINT || *end_name != 0) return -1; return number; diff --git a/moo/mooapp/mooapp.c b/moo/mooapp/mooapp.c index 35dfc33f..8761622c 100644 --- a/moo/mooapp/mooapp.c +++ b/moo/mooapp/mooapp.c @@ -49,7 +49,6 @@ #include #include #include -#include #include #ifdef MOO_USE_QUARTZ @@ -1105,7 +1104,8 @@ moo_app_write_session (MooApp *app) if (!app->priv->session) { - _moo_unlink (filename); + mgw_errno_t err; + mgw_unlink (filename, &err); g_free (filename); return; } diff --git a/moo/mooapp/mooappabout.h b/moo/mooapp/mooappabout.h index 813464c9..a410561f 100644 --- a/moo/mooapp/mooappabout.h +++ b/moo/mooapp/mooappabout.h @@ -28,6 +28,7 @@ #include #endif +#include #include #include @@ -121,8 +122,9 @@ get_system_name (void) if (uname (&name) != 0) { - int err = errno; - g_critical ("%s", g_strerror (err)); + MGW_ERROR_IF_NOT_SHARED_LIBC + mgw_errno_t err = { errno }; + g_critical ("%s", mgw_strerror (err)); return g_strdup ("unknown"); } diff --git a/moo/mooedit/mooedit-fileops.c b/moo/mooedit/mooedit-fileops.c index 9612c4a4..3c2f83e6 100644 --- a/moo/mooedit/mooedit-fileops.c +++ b/moo/mooedit/mooedit-fileops.c @@ -39,7 +39,6 @@ #include #endif #include -#include #include diff --git a/moo/mooedit/mooeditor-tests.c b/moo/mooedit/mooeditor-tests.c index 87518509..2118c4df 100644 --- a/moo/mooedit/mooeditor-tests.c +++ b/moo/mooedit/mooeditor-tests.c @@ -186,6 +186,7 @@ test_encodings (void) GDir *dir; const char *name; char *working_dir; + mgw_errno_t err; dir = g_dir_open (test_data.encodings_dir, 0, NULL); @@ -197,7 +198,7 @@ test_encodings (void) } working_dir = g_build_filename (test_data.working_dir, "encodings", (char*)0); - _moo_mkdir_with_parents (working_dir); + _moo_mkdir_with_parents (working_dir, &err); while ((name = g_dir_read_name (dir))) test_encodings_1 (name, working_dir); @@ -226,15 +227,18 @@ test_types (void) static gboolean test_suite_init (G_GNUC_UNUSED gpointer data) { + mgw_errno_t err; + test_data.working_dir = g_build_filename (moo_test_get_working_dir (), "editor-work", (char*)0); test_data.encodings_dir = g_build_filename (moo_test_get_data_dir (), "encodings", (char*)0); - if (_moo_mkdir_with_parents (test_data.working_dir) != 0) + if (_moo_mkdir_with_parents (test_data.working_dir, &err) != 0) { - g_critical ("could not create directory '%s'", - test_data.working_dir); + g_critical ("could not create directory '%s': %s", + test_data.working_dir, + mgw_strerror (err)); g_free (test_data.working_dir); test_data.working_dir = NULL; return FALSE; diff --git a/moo/mooedit/mootextprint.c b/moo/mooedit/mootextprint.c index ba87ea0e..5aa2146c 100644 --- a/moo/mooedit/mootextprint.c +++ b/moo/mooedit/mootextprint.c @@ -25,9 +25,8 @@ #include "mooutils/mooutils-debug.h" #include "mooutils/mootype-macros.h" #include "mooedit/mooprint-gxml.h" +#include "mooglib/moo-time.h" #include -#include -#include #include #ifdef __WIN32__ @@ -903,7 +902,8 @@ moo_print_operation_begin_print (GtkPrintOperation *operation, MooPrintSettings *settings; PangoFontDescription *font = NULL; GTimer *timer; - time_t t; + mgw_time_t t; + mgw_errno_t err; g_return_if_fail (op->priv->buffer != NULL); g_return_if_fail (op->priv->first_line >= 0); @@ -984,20 +984,11 @@ moo_print_operation_begin_print (GtkPrintOperation *operation, if (!op->priv->tm) op->priv->tm = g_new (struct tm, 1); - errno = 0; - time (&t); + mgw_time (&t, &err); - if (errno) + if (mgw_errno_is_set (err) || !mgw_localtime_r (&t, op->priv->tm, &err)) { - int err = errno; - g_critical ("time: %s", g_strerror (err)); - g_free (op->priv->tm); - op->priv->tm = NULL; - } - else if (!localtime_r (&t, op->priv->tm)) - { - int err = errno; - g_critical ("time: %s", g_strerror (err)); + g_critical ("time: %s", mgw_strerror (err)); g_free (op->priv->tm); op->priv->tm = NULL; } diff --git a/moo/moofileview/moobookmarkmgr.c b/moo/moofileview/moobookmarkmgr.c index 7a8bb231..27b1da97 100644 --- a/moo/moofileview/moobookmarkmgr.c +++ b/moo/moofileview/moobookmarkmgr.c @@ -27,7 +27,6 @@ #include "mooutils/mootype-macros.h" #include "moofileview/moobookmark-editor-gxml.h" #include -#include #ifdef HAVE_UNISTD_H #include #endif diff --git a/moo/moofileview/moofile.c b/moo/moofileview/moofile.c index 564c1618..e002d456 100644 --- a/moo/moofileview/moofile.c +++ b/moo/moofileview/moofile.c @@ -35,10 +35,10 @@ #include #include +#include #ifdef HAVE_UNISTD_H #include #endif -#include #include #if defined(HAVE_CARBON) #include @@ -320,6 +320,7 @@ _moo_file_stat (MooFile *file, const char *dirname) { char *fullname; + mgw_errno_t err; g_return_if_fail (file != NULL); @@ -331,14 +332,12 @@ _moo_file_stat (MooFile *file, g_free (file->link_target); file->link_target = NULL; - errno = 0; - if (!file->statbuf) file->statbuf = g_slice_new (MgwStatBuf); - if (mgw_lstat (fullname, file->statbuf) != 0) + if (mgw_lstat (fullname, file->statbuf, &err) != 0) { - if (errno == ENOENT) + if (err.value == MGW_ENOENT) { MOO_DEBUG_CODE({ gchar *display_name = g_filename_display_name (fullname); @@ -350,10 +349,9 @@ _moo_file_stat (MooFile *file, else { MOO_DEBUG_CODE({ - int save_errno = errno; gchar *display_name = g_filename_display_name (fullname); _moo_message ("error getting information for '%s': %s", - display_name, g_strerror (save_errno)); + display_name, mgw_strerror (err)); g_free (display_name); }); file->info = MOO_FILE_INFO_IS_LOCKED | MOO_FILE_INFO_EXISTS; @@ -362,17 +360,17 @@ _moo_file_stat (MooFile *file, } else { +#ifndef __WIN32__ if (file->statbuf->islnk) { static char buf[1024]; gssize len; file->info |= MOO_FILE_INFO_IS_LINK; - errno = 0; - if (mgw_stat (fullname, file->statbuf) != 0) + if (mgw_stat (fullname, file->statbuf, &err) != 0) { - if (errno == ENOENT) + if (err.value == MGW_ENOENT) { MOO_DEBUG_CODE({ gchar *display_name = g_filename_display_name (fullname); @@ -384,10 +382,9 @@ _moo_file_stat (MooFile *file, else { MOO_DEBUG_CODE({ - int save_errno = errno; gchar *display_name = g_filename_display_name (fullname); _moo_message ("error getting information for '%s': %s", - display_name, g_strerror (save_errno)); + display_name, mgw_strerror (err)); g_free (display_name); }); file->info = MOO_FILE_INFO_IS_LOCKED | MOO_FILE_INFO_EXISTS; @@ -395,15 +392,16 @@ _moo_file_stat (MooFile *file, } } + errno = 0; len = readlink (fullname, buf, 1024); + err.value = errno; if (len == -1) { MOO_DEBUG_CODE({ - int save_errno = errno; gchar *display_name = g_filename_display_name (fullname); _moo_message ("error getting link target for '%s': %s", - display_name, g_strerror (save_errno)); + display_name, mgw_strerror (err)); g_free (display_name); }); } @@ -412,6 +410,7 @@ _moo_file_stat (MooFile *file, file->link_target = g_strndup (buf, len); } } +#endif // !__WIN32__ } if ((file->info & MOO_FILE_INFO_EXISTS) && diff --git a/moo/moofileview/moofilesystem.c b/moo/moofileview/moofilesystem.c index 93914f13..3821c2eb 100644 --- a/moo/moofileview/moofilesystem.c +++ b/moo/moofileview/moofilesystem.c @@ -22,7 +22,6 @@ #include "mooutils/mooutils.h" #include "marshals.h" #include -#include #include #ifndef __WIN32__ #include @@ -543,6 +542,8 @@ create_folder (G_GNUC_UNUSED MooFileSystem *fs, const char *path, GError **error) { + mgw_errno_t err; + g_return_val_if_fail (path != NULL, FALSE); /* XXX check the caller */ @@ -557,15 +558,14 @@ create_folder (G_GNUC_UNUSED MooFileSystem *fs, /* TODO mkdir must (?) adjust permissions according to umask */ #ifndef __WIN32__ - if (mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO)) + if (mgw_mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO, &err)) #else - if (_moo_mkdir (path)) + if (_moo_mkdir (path, &err)) #endif { - int saved_errno = errno; g_set_error (error, MOO_FILE_ERROR, - _moo_file_error_from_errno (saved_errno), - "%s", g_strerror (saved_errno)); + _moo_file_error_from_errno (err), + "%s", mgw_strerror (err)); return FALSE; } @@ -730,6 +730,7 @@ delete_file (G_GNUC_UNUSED MooFileSystem *fs, GError **error) { gboolean isdir; + mgw_errno_t err; g_return_val_if_fail (path != NULL, FALSE); g_return_val_if_fail (_moo_path_is_absolute (path), FALSE); @@ -755,15 +756,14 @@ delete_file (G_GNUC_UNUSED MooFileSystem *fs, if (isdir) return _moo_remove_dir (path, (flags & MOO_DELETE_RECURSIVE) != 0, error); - if (_moo_remove (path)) + if (mgw_remove (path, &err) != 0) { - int err = errno; char *path_utf8 = g_filename_display_name (path); g_set_error (error, MOO_FILE_ERROR, _moo_file_error_from_errno (err), "Could not delete file '%s': %s", path_utf8 ? path_utf8 : BROKEN_NAME, - g_strerror (err)); + mgw_strerror (err)); g_free (path_utf8); return FALSE; } @@ -781,17 +781,18 @@ move_file_unix (G_GNUC_UNUSED MooFileSystem *fs, const char *new_path, GError **error) { + mgw_errno_t err; + g_return_val_if_fail (old_path && new_path, FALSE); g_return_val_if_fail (_moo_path_is_absolute (old_path), FALSE); g_return_val_if_fail (_moo_path_is_absolute (new_path), FALSE); /* XXX */ - if (_moo_rename (old_path, new_path)) + if (_moo_rename (old_path, new_path, &err)) { - int saved_errno = errno; g_set_error (error, MOO_FILE_ERROR, - _moo_file_error_from_errno (saved_errno), - "%s", g_strerror (saved_errno)); + _moo_file_error_from_errno (err), + "%s", mgw_strerror (err)); return FALSE; } diff --git a/moo/moofileview/moofolder.c b/moo/moofileview/moofolder.c index 5c44173a..6a607a07 100644 --- a/moo/moofileview/moofolder.c +++ b/moo/moofileview/moofolder.c @@ -29,7 +29,6 @@ #ifdef HAVE_UNISTD_H #include #endif -#include #include #include diff --git a/moo/mooglib/moo-glib.c b/moo/mooglib/moo-glib.c deleted file mode 100644 index c258b5b5..00000000 --- a/moo/mooglib/moo-glib.c +++ /dev/null @@ -1,57 +0,0 @@ -#define MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS -#include -#include -#include - -#include -#include - - -static mgw_time_t convert_time_t (time_t t) -{ - mgw_time_t result = { t }; - return result; -} - - -static void convert_g_stat_buf (const GStatBuf* gbuf, MgwStatBuf* mbuf) -{ - mbuf->atime = convert_time_t (gbuf->st_atime); - mbuf->mtime = convert_time_t (gbuf->st_mtime); - mbuf->ctime = convert_time_t (gbuf->st_ctime); - - mbuf->size = gbuf->st_size; - - mbuf->isreg = S_ISREG (gbuf->st_mode); - mbuf->isdir = S_ISDIR (gbuf->st_mode); - mbuf->islnk = S_ISLNK (gbuf->st_mode); - mbuf->issock = S_ISSOCK (gbuf->st_mode); - mbuf->isfifo = S_ISFIFO (gbuf->st_mode); - mbuf->ischr = S_ISCHR (gbuf->st_mode); - mbuf->isblk = S_ISBLK (gbuf->st_mode); -} - - -int mgw_stat (const gchar *filename, MgwStatBuf *buf) -{ - GStatBuf gbuf = { 0 }; - int result = g_stat (filename, &gbuf); - convert_g_stat_buf (&gbuf, buf); - return result; -} - - -int mgw_lstat (const gchar *filename, MgwStatBuf *buf) -{ - GStatBuf gbuf = { 0 }; - int result = g_lstat (filename, &gbuf); - convert_g_stat_buf (&gbuf, buf); - return result; -} - - -const struct tm *mgw_localtime(const mgw_time_t *timep) -{ - time_t t = timep->value; - return localtime(&t); -} diff --git a/moo/mooglib/moo-glib.cpp b/moo/mooglib/moo-glib.cpp new file mode 100644 index 00000000..d1748f5f --- /dev/null +++ b/moo/mooglib/moo-glib.cpp @@ -0,0 +1,191 @@ +#define MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS +#include +#include +#include + +#include +#include +#include +#include + + +const mgw_errno_t MGW_E_NOERROR { MGW_ENOERROR }; +const mgw_errno_t MGW_E_EXIST { MGW_EEXIST }; + + +static mgw_time_t convert_time_t (time_t t) +{ + mgw_time_t result = { t }; + return result; +} + + +static void convert_g_stat_buf (const GStatBuf* gbuf, MgwStatBuf* mbuf) +{ + mbuf->atime = convert_time_t (gbuf->st_atime); + mbuf->mtime = convert_time_t (gbuf->st_mtime); + mbuf->ctime = convert_time_t (gbuf->st_ctime); + + mbuf->size = gbuf->st_size; + + mbuf->isreg = S_ISREG (gbuf->st_mode); + mbuf->isdir = S_ISDIR (gbuf->st_mode); + mbuf->islnk = S_ISLNK (gbuf->st_mode); + mbuf->issock = S_ISSOCK (gbuf->st_mode); + mbuf->isfifo = S_ISFIFO (gbuf->st_mode); + mbuf->ischr = S_ISCHR (gbuf->st_mode); + mbuf->isblk = S_ISBLK (gbuf->st_mode); +} + + +static MGW_FILE *convert_mgw_file (FILE *file) +{ + return reinterpret_cast (file); +} + +static FILE *convert_mgw_file (MGW_FILE *file) +{ + return reinterpret_cast (file); +} + + +template +auto call_with_errno (mgw_errno_t *err, const Func& func, Args... args) -> decltype(func(args...)) +{ + errno = 0; + const auto& result = func (args...); + if (err != nullptr) + err->value = mgw_errno_value_t (errno); + return result; +} + + +const char * +mgw_strerror (mgw_errno_t err) +{ + return g_strerror (err.value); +} + +GFileError +mgw_file_error_from_errno (mgw_errno_t err) +{ + return g_file_error_from_errno (err.value); +} + + +int +mgw_stat (const gchar *filename, MgwStatBuf *buf, mgw_errno_t *err) +{ + GStatBuf gbuf = { 0 }; + int result = call_with_errno (err, g_stat, filename, &gbuf); + convert_g_stat_buf (&gbuf, buf); + return result; +} + +int +mgw_lstat (const gchar *filename, MgwStatBuf *buf, mgw_errno_t *err) +{ + GStatBuf gbuf = { 0 }; + int result = call_with_errno (err, g_lstat, filename, &gbuf); + convert_g_stat_buf (&gbuf, buf); + return result; +} + + +const struct tm * +mgw_localtime (const mgw_time_t *timep) +{ + time_t t = timep->value; + return localtime(&t); +} + +const struct tm * +mgw_localtime_r (const mgw_time_t *timep, struct tm *result, mgw_errno_t *err) +{ + time_t t = timep->value; + return call_with_errno (err, localtime_r, &t, result); +} + +mgw_time_t +mgw_time (mgw_time_t *t, mgw_errno_t *err) +{ + time_t t1; + time_t t2 = call_with_errno (err, time, &t1); + if (t != nullptr) + t->value = t1; + return { t2 }; +} + + +guint64 +mgw_ascii_strtoull (const gchar *nptr, gchar **endptr, guint base, mgw_errno_t *err) +{ + return call_with_errno (err, g_ascii_strtoull, nptr, endptr, base); +} + +gdouble +mgw_ascii_strtod (const gchar *nptr, gchar **endptr, mgw_errno_t *err) +{ + return call_with_errno (err, g_ascii_strtod, nptr, endptr); +} + + +MGW_FILE * +mgw_fopen (const char *filename, const char *mode, mgw_errno_t *err) +{ + return convert_mgw_file (call_with_errno (err, g_fopen, filename, mode)); +} + +int mgw_fclose (MGW_FILE *file) +{ + return fclose (convert_mgw_file (file)); +} + +gsize +mgw_fread(void *ptr, gsize size, gsize nmemb, MGW_FILE *stream, mgw_errno_t *err) +{ + return call_with_errno (err, fread, ptr, size, nmemb, convert_mgw_file (stream)); +} + +gsize +mgw_fwrite(const void *ptr, gsize size, gsize nmemb, MGW_FILE *stream) +{ + return fwrite (ptr, size, nmemb, convert_mgw_file (stream)); +} + +int +mgw_ferror (MGW_FILE *file) +{ + return ferror (convert_mgw_file (file)); +} + +char * +mgw_fgets(char *s, int size, MGW_FILE *stream) +{ + return fgets(s, size, convert_mgw_file (stream)); +} + + +int +mgw_unlink (const char *path, mgw_errno_t *err) +{ + return call_with_errno (err, g_unlink, path); +} + +int +mgw_remove (const char *path, mgw_errno_t *err) +{ + return call_with_errno (err, g_remove, path); +} + +int +mgw_mkdir (const gchar *filename, int mode, mgw_errno_t *err) +{ + return call_with_errno (err, g_mkdir, filename, mode); +} + +int +mgw_mkdir_with_parents (const gchar *pathname, gint mode, mgw_errno_t *err) +{ + return call_with_errno (err, g_mkdir_with_parents, pathname, mode); +} diff --git a/moo/mooglib/moo-glib.h b/moo/mooglib/moo-glib.h index 0d952bfe..5f312172 100644 --- a/moo/mooglib/moo-glib.h +++ b/moo/mooglib/moo-glib.h @@ -3,14 +3,86 @@ #include #include +G_BEGIN_DECLS + +#ifndef __WIN32__ +#define MGW_ERROR_IF_NOT_SHARED_LIBC +#else +#define MGW_ERROR_IF_NOT_SHARED_LIBC \ + #error "C libraries may not be shared between medit and glib" +#endif + +typedef struct mgw_errno_t mgw_errno_t; +typedef struct MGW_FILE MGW_FILE; + +enum mgw_errno_value_t +{ + MGW_ENOERROR = 0, + MGW_EACCES, + MGW_EPERM, + MGW_EEXIST, + MGW_ELOOP, + MGW_ENAMETOOLONG, + MGW_ENOENT, + MGW_ENOTDIR, + MGW_EROFS, + MGW_EXDEV, +}; + +typedef enum mgw_errno_value_t mgw_errno_value_t; + +struct mgw_errno_t +{ + mgw_errno_value_t value; +}; + +extern const mgw_errno_t MGW_E_NOERROR; +extern const mgw_errno_t MGW_E_EXIST; + +inline static gboolean mgw_errno_is_set (mgw_errno_t err) { return err.value != MGW_ENOERROR; } +const char *mgw_strerror (mgw_errno_t err); +GFileError mgw_file_error_from_errno (mgw_errno_t err); + +guint64 mgw_ascii_strtoull (const gchar *nptr, gchar **endptr, guint base, mgw_errno_t *err); +gdouble mgw_ascii_strtod (const gchar *nptr, gchar **endptr, mgw_errno_t *err); + +MGW_FILE *mgw_fopen (const char *filename, const char *mode, mgw_errno_t *err); +int mgw_fclose (MGW_FILE *file); +gsize mgw_fread(void *ptr, gsize size, gsize nmemb, MGW_FILE *stream, mgw_errno_t *err); +gsize mgw_fwrite(const void *ptr, gsize size, gsize nmemb, MGW_FILE *stream); +int mgw_ferror (MGW_FILE *file); +char *mgw_fgets(char *s, int size, MGW_FILE *stream); + +int mgw_unlink (const char *path, mgw_errno_t *err); +int mgw_remove (const char *path, mgw_errno_t *err); +int mgw_mkdir (const gchar *filename, int mode, mgw_errno_t *err); +int mgw_mkdir_with_parents (const gchar *pathname, gint mode, mgw_errno_t *err); + + #ifndef MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS #undef g_stat #undef g_lstat +#undef g_strerror +#undef g_ascii_strtoull +#undef g_file_error_from_errno +#undef g_ascii_strtod +#undef g_fopen +#undef g_unlink +#undef g_mkdir +#undef g_mkdir_with_parents #define g_stat DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD #define g_lstat DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_strerror DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_ascii_strtoull DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_file_error_from_errno DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_ascii_strtod DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_fopen DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_unlink DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_mkdir DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD +#define g_mkdir_with_parents DO_NOT_USE_THIS_DIRECTLY_USE_MGW_WRAPPERS_INSTEAD #endif // MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS -//#include +G_END_DECLS diff --git a/moo/mooglib/moo-stat.h b/moo/mooglib/moo-stat.h index 81f6240d..8ecfd59e 100644 --- a/moo/mooglib/moo-stat.h +++ b/moo/mooglib/moo-stat.h @@ -24,7 +24,7 @@ struct MgwStatBuf isblk : 1; // S_ISBLK }; -int mgw_stat (const gchar *filename, MgwStatBuf *buf); -int mgw_lstat (const gchar *filename, MgwStatBuf *buf); +int mgw_stat (const gchar *filename, MgwStatBuf *buf, mgw_errno_t *err); +int mgw_lstat (const gchar *filename, MgwStatBuf *buf, mgw_errno_t *err); G_END_DECLS diff --git a/moo/mooglib/moo-time.h b/moo/mooglib/moo-time.h index d0b4aac6..d9832144 100644 --- a/moo/mooglib/moo-time.h +++ b/moo/mooglib/moo-time.h @@ -12,5 +12,7 @@ struct mgw_time_t }; const struct tm *mgw_localtime(const mgw_time_t *timep); +const struct tm *mgw_localtime_r(const mgw_time_t *timep, struct tm *result, mgw_errno_t *err); +mgw_time_t mgw_time(mgw_time_t *t, mgw_errno_t *err); G_END_DECLS diff --git a/moo/mooutils/mooapp-ipc.c b/moo/mooutils/mooapp-ipc.c index c1d73b69..cc011f61 100644 --- a/moo/mooutils/mooapp-ipc.c +++ b/moo/mooutils/mooapp-ipc.c @@ -21,7 +21,6 @@ #include #include #include -#include #define VERSION_STRING "0001" #define VERSION_LEN 4 @@ -296,12 +295,12 @@ get_uint (const char *data, char *string, *end; guint64 val; gboolean result = FALSE; + mgw_errno_t err; string = g_strndup (data, len); - errno = 0; - val = g_ascii_strtoull (string, &end, 16); + val = mgw_ascii_strtoull (string, &end, 16, &err); - if (!errno && !end[0] && val <= G_MAXUINT) + if (!mgw_errno_is_set (err) && !end[0] && val <= G_MAXUINT) { *dest = (guint)val; result = TRUE; diff --git a/moo/mooutils/mooappinput.c b/moo/mooutils/mooappinput.c index dc915a40..3c24d385 100644 --- a/moo/mooutils/mooappinput.c +++ b/moo/mooutils/mooappinput.c @@ -25,6 +25,9 @@ # include # include #else +#define MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS +#include +MGW_ERROR_IF_NOT_SHARED_LIBC # include # include #endif @@ -510,6 +513,8 @@ do_write (int fd, #ifdef MOO_APP_INPUT_SOCKET +MGW_ERROR_IF_NOT_SHARED_LIBC + #ifndef UNIX_PATH_MAX #define UNIX_PATH_MAX 108 #endif diff --git a/moo/mooutils/moofilewatch.c b/moo/mooutils/moofilewatch.c index bd4e05e1..1f10a38b 100644 --- a/moo/mooutils/moofilewatch.c +++ b/moo/mooutils/moofilewatch.c @@ -38,7 +38,6 @@ #include #include #include -#include #include "mooutils/mooutils-misc.h" #include "mooutils/mooutils-mem.h" #include "mooutils/moofilewatch.h" @@ -768,7 +767,7 @@ out: #define MOO_STAT_PRIORITY G_PRIORITY_DEFAULT #define MOO_STAT_TIMEOUT 500 -static MooFileWatchError errno_to_file_error (int code); +static MooFileWatchError errno_to_file_error (mgw_errno_t code); static gboolean do_stat (MooFileWatch *watch); @@ -802,18 +801,16 @@ watch_stat_start_monitor (MooFileWatch *watch, GError **error) { MgwStatBuf buf; + mgw_errno_t err; g_return_val_if_fail (watch != NULL, FALSE); g_return_val_if_fail (monitor->filename != NULL, FALSE); - errno = 0; - - if (mgw_stat (monitor->filename, &buf) != 0) + if (mgw_stat (monitor->filename, &buf, &err) != 0) { - int saved_errno = errno; g_set_error (error, MOO_FILE_WATCH_ERROR, - errno_to_file_error (saved_errno), - "stat: %s", g_strerror (saved_errno)); + errno_to_file_error (err), + "stat: %s", mgw_strerror (err)); return FALSE; } @@ -866,6 +863,7 @@ do_stat (MooFileWatch *watch) MooFileEvent event; Monitor *monitor; mgw_time_t old; + mgw_errno_t err; monitor = (Monitor*) g_hash_table_lookup (watch->requests, lid->data); @@ -874,27 +872,24 @@ do_stat (MooFileWatch *watch) old = monitor->statbuf.mtime; - errno = 0; - event.monitor_id = monitor->id; event.filename = monitor->filename; event.error = NULL; - if (mgw_stat (monitor->filename, &monitor->statbuf) != 0) + if (mgw_stat (monitor->filename, &monitor->statbuf, &err) != 0) { - if (errno == ENOENT) + if (err.value == MGW_ENOENT) { event.code = MOO_FILE_EVENT_DELETED; to_remove = g_slist_prepend (to_remove, GUINT_TO_POINTER (monitor->id)); } else { - int err = errno; event.code = MOO_FILE_EVENT_ERROR; g_set_error (&event.error, MOO_FILE_WATCH_ERROR, errno_to_file_error (err), "stat failed: %s", - g_strerror (err)); + mgw_strerror (err)); monitor->alive = FALSE; } @@ -927,24 +922,27 @@ out: static MooFileWatchError -errno_to_file_error (int code) +errno_to_file_error (mgw_errno_t code) { MooFileWatchError fcode = MOO_FILE_WATCH_ERROR_FAILED; - switch (code) + switch (code.value) { - case EACCES: + case MGW_EACCES: fcode = MOO_FILE_WATCH_ERROR_ACCESS_DENIED; break; - case ENAMETOOLONG: + case MGW_ENAMETOOLONG: fcode = MOO_FILE_WATCH_ERROR_BAD_FILENAME; break; - case ENOENT: + case MGW_ENOENT: fcode = MOO_FILE_WATCH_ERROR_NONEXISTENT; break; - case ENOTDIR: + case MGW_ENOTDIR: fcode = MOO_FILE_WATCH_ERROR_NOT_DIR; break; + + default: + break; } return fcode; @@ -1442,18 +1440,16 @@ watch_win32_start_monitor (MooFileWatch *watch, GError **error) { MgwStatBuf buf; + mgw_errno_t err; g_return_val_if_fail (watch != NULL, FALSE); g_return_val_if_fail (monitor->filename != NULL, FALSE); - errno = 0; - - if (mgw_stat (monitor->filename, &buf) != 0) + if (mgw_stat (monitor->filename, &buf, &err) != 0) { - int saved_errno = errno; g_set_error (error, MOO_FILE_WATCH_ERROR, - errno_to_file_error (saved_errno), - "stat: %s", g_strerror (saved_errno)); + errno_to_file_error (err), + "stat: %s", mgw_strerror (err)); return FALSE; } diff --git a/moo/mooutils/moofilewriter.c b/moo/mooutils/moofilewriter.c index 5e4a05e5..f0ead1bb 100644 --- a/moo/mooutils/moofilewriter.c +++ b/moo/mooutils/moofilewriter.c @@ -23,7 +23,6 @@ #include #include #include -#include #include #include #ifdef HAVE_UNISTD_H @@ -36,7 +35,7 @@ struct _MooFileReader { GObject base; - FILE *file; + MGW_FILE *file; }; MOO_DEFINE_TYPE_STATIC (MooFileReader, moo_file_reader, G_TYPE_OBJECT) @@ -52,7 +51,7 @@ moo_file_reader_close_file (MooFileReader *reader) { if (reader->file) { - fclose (reader->file); + mgw_fclose (reader->file); reader->file = NULL; } } @@ -85,20 +84,19 @@ moo_file_reader_new_real (const char *filename, GError **error) { const char *mode = binary ? "rb" : "r"; - FILE *file; + MGW_FILE *file; MooFileReader *reader; + mgw_errno_t err; g_return_val_if_fail (filename != NULL, NULL); g_return_val_if_fail (!error || !*error, NULL); - errno = 0; - if (!(file = g_fopen (filename, mode))) + if (!(file = mgw_fopen (filename, mode, &err))) { - int err = errno; g_set_error (error, MOO_FILE_ERROR, _moo_file_error_from_errno (err), _("Could not open %s: %s"), filename, - g_strerror (err)); + mgw_strerror (err)); return NULL; } @@ -130,6 +128,7 @@ moo_file_reader_read (MooFileReader *reader, GError **error) { gsize size_read; + mgw_errno_t err; g_return_val_if_fail (MOO_IS_FILE_READER (reader), FALSE); g_return_val_if_fail (size_read_p != NULL, FALSE); @@ -140,16 +139,14 @@ moo_file_reader_read (MooFileReader *reader, if (buf_size == 0) return TRUE; - errno = 0; - size_read = fread (buf, 1, buf_size, reader->file); + size_read = mgw_fread (buf, 1, buf_size, reader->file, &err); - if (size_read != buf_size && ferror (reader->file)) + if (size_read != buf_size && mgw_ferror (reader->file)) { - int err = errno; g_set_error (error, MOO_FILE_ERROR, _moo_file_error_from_errno (err), "error reading file: %s", - g_strerror (err)); + mgw_strerror (err)); return FALSE; } @@ -277,18 +274,18 @@ moo_local_file_writer_new (GFile *file, { char *dirname; char *filename; + mgw_errno_t err; filename = g_file_get_path (file); dirname = filename ? g_path_get_dirname (filename) : NULL; - if (dirname && _moo_mkdir_with_parents (dirname) != 0) + if (dirname && _moo_mkdir_with_parents (dirname, &err) != 0) { - int err = errno; char *display_name = g_filename_display_name (dirname); g_set_error (error, G_FILE_ERROR, - g_file_error_from_errno (err), + mgw_file_error_from_errno (err), _("Could not create folder %s: %s"), - display_name, g_strerror (err)); + display_name, mgw_strerror (err)); g_free (display_name); g_free (dirname); goto error; diff --git a/moo/mooutils/mooglade.c b/moo/mooutils/mooglade.c index 12ef3853..de49d018 100644 --- a/moo/mooutils/mooglade.c +++ b/moo/mooutils/mooglade.c @@ -41,8 +41,8 @@ #include "mooutils/moolist.h" #include #include -#include #include +#include typedef enum { MOO_GLADE_XML_ERROR_FAILED @@ -1766,8 +1766,6 @@ parse_property (GParamSpec *param_spec, g_value_init (¶m->value, param_spec->value_type); - errno = 0; - if (param_spec->value_type == G_TYPE_CHAR) { #if GLIB_CHECK_VERSION(2,32,0) @@ -1810,9 +1808,10 @@ parse_property (GParamSpec *param_spec, param_spec->value_type == G_TYPE_ULONG || param_spec->value_type == G_TYPE_UINT64) /* XXX */ { - guint64 val = g_ascii_strtoull (value, NULL, 0); + mgw_errno_t err; + guint64 val = mgw_ascii_strtoull (value, NULL, 0, &err); - if (errno) + if (mgw_errno_is_set (err)) { g_warning ("could not convert string '%s' to a guint", value); @@ -1839,9 +1838,10 @@ parse_property (GParamSpec *param_spec, else if (param_spec->value_type == G_TYPE_FLOAT || param_spec->value_type == G_TYPE_DOUBLE) /* XXX */ { - double val = g_ascii_strtod (value, NULL); + mgw_errno_t err; + double val = mgw_ascii_strtod (value, NULL, &err); - if (errno) + if (mgw_errno_is_set (err)) { g_warning ("could not convert string '%s' to double", value); @@ -1980,13 +1980,13 @@ parse_adjustment (const char *value) if (g_strv_length (pieces) != 6) goto out; - errno = 0; - for (i = 0; i < 6; ++i) { - vals[i] = g_ascii_strtod (pieces[i], NULL); + mgw_errno_t err; + + vals[i] = mgw_ascii_strtod (pieces[i], NULL, &err); - if (errno) + if (mgw_errno_is_set (err)) { g_warning ("could not convert '%s' to double", pieces[i]); goto out; diff --git a/moo/mooutils/moohistorymgr.c b/moo/mooutils/moohistorymgr.c index ee53dec7..65768b50 100644 --- a/moo/mooutils/moohistorymgr.c +++ b/moo/mooutils/moohistorymgr.c @@ -807,7 +807,8 @@ moo_history_mgr_save (MooHistoryMgr *mgr) if (!mgr->priv->files->length) { - _moo_unlink (filename); + mgw_errno_t err; + mgw_unlink (filename, &err); return; } diff --git a/moo/mooutils/mooprefs.c b/moo/mooutils/mooprefs.c index ea540c87..a144ed7a 100644 --- a/moo/mooutils/mooprefs.c +++ b/moo/mooutils/mooprefs.c @@ -21,7 +21,6 @@ #include "mooutils/mooutils-gobject.h" #include "mooutils/mootype-macros.h" #include -#include #include #include @@ -878,8 +877,11 @@ save_file (const char *file, if (empty) { if (g_file_test (file, G_FILE_TEST_EXISTS)) - if (_moo_unlink (file)) - g_critical ("%s", g_strerror (errno)); + { + mgw_errno_t err; + if (mgw_unlink (file, &err) != 0) + g_critical ("%s", mgw_strerror (err)); + } return TRUE; } diff --git a/moo/mooutils/mooutils-fs.c b/moo/mooutils/mooutils-fs.c index d2916b76..eb8c3de4 100644 --- a/moo/mooutils/mooutils-fs.c +++ b/moo/mooutils/mooutils-fs.c @@ -28,8 +28,8 @@ #include #include -#include #include +#include #ifdef __WIN32__ #include @@ -281,16 +281,15 @@ rm_r (const char *path, } else { - errno = 0; + mgw_errno_t err; - if (_moo_remove (file_path) != 0) + if (mgw_remove (file_path, &err) != 0) { - int err = errno; success = FALSE; g_set_error (error, MOO_FILE_ERROR, _moo_file_error_from_errno (err), _("Could not remove %s: %s"), file_path, - g_strerror (err)); + mgw_strerror (err)); } } @@ -301,16 +300,15 @@ rm_r (const char *path, if (success) { - errno = 0; + mgw_errno_t err; - if (_moo_remove (path) != 0) + if (mgw_remove (path, &err) != 0) { - int err = errno; success = FALSE; g_set_error (error, MOO_FILE_ERROR, _moo_file_error_from_errno (err), _("Could not remove %s: %s"), path, - g_strerror (err)); + mgw_strerror (err)); } } @@ -328,16 +326,15 @@ _moo_remove_dir (const char *path, if (!recursive) { - errno = 0; + mgw_errno_t err; - if (_moo_remove (path) != 0) + if (mgw_remove (path, &err) != 0) { - int err = errno; char *path_utf8 = g_filename_display_name (path); g_set_error (error, MOO_FILE_ERROR, _moo_file_error_from_errno (err), _("Could not remove %s: %s"), - path_utf8, g_strerror (err)); + path_utf8, mgw_strerror (err)); g_free (path_utf8); return FALSE; } @@ -356,9 +353,9 @@ _moo_remove_dir (const char *path, int -_moo_mkdir_with_parents (const char *path) +_moo_mkdir_with_parents (const char *path, mgw_errno_t* err) { - return g_mkdir_with_parents (path, S_IRWXU); + return mgw_mkdir_with_parents (path, S_IRWXU, err); } @@ -368,40 +365,35 @@ _moo_create_dir (const char *path, { MgwStatBuf buf; char *utf8_path; + mgw_errno_t err; g_return_val_if_fail (path != NULL, FALSE); - errno = 0; - - if (mgw_stat (path, &buf) != 0 && errno != ENOENT) + if (mgw_stat (path, &buf, &err) != 0 && err.value != MGW_ENOENT) { - int err_code = errno; utf8_path = g_filename_display_name (path); g_set_error (error, MOO_FILE_ERROR, - _moo_file_error_from_errno (err_code), + _moo_file_error_from_errno (err), _("Could not create folder %s: %s"), - utf8_path, g_strerror (err_code)); + utf8_path, mgw_strerror (err)); g_free (utf8_path); return FALSE; } - if (errno != 0) + if (mgw_errno_is_set (err)) { - errno = 0; - - if (_moo_mkdir (path) == -1) + if (_moo_mkdir (path, &err) == -1) { - int err_code = errno; utf8_path = g_filename_display_name (path); g_set_error (error, MOO_FILE_ERROR, - _moo_file_error_from_errno (err_code), + _moo_file_error_from_errno (err), _("Could not create folder %s: %s"), - utf8_path, g_strerror (err_code)); + utf8_path, mgw_strerror (err)); g_free (utf8_path); return FALSE; @@ -417,7 +409,7 @@ _moo_create_dir (const char *path, g_set_error (error, MOO_FILE_ERROR, MOO_FILE_ERROR_ALREADY_EXISTS, _("Could not create folder %s: %s"), - utf8_path, g_strerror (EEXIST)); + utf8_path, mgw_strerror (MGW_E_EXIST)); g_free (utf8_path); return FALSE; @@ -430,23 +422,21 @@ _moo_rename_file (const char *path, GError **error) { // Do not break this for directories! + mgw_errno_t err; g_return_val_if_fail (path != NULL, FALSE); g_return_val_if_fail (new_path != NULL, FALSE); - errno = 0; - - if (_moo_rename (path, new_path) != 0) + if (_moo_rename (path, new_path, &err) != 0) { - int err_code = errno; char *utf8_path = g_filename_display_name (path); char *utf8_new_path = g_filename_display_name (new_path); g_set_error (error, MOO_FILE_ERROR, - _moo_file_error_from_errno (err_code), + _moo_file_error_from_errno (err), _("Could not rename file %s to %s: %s"), - utf8_path, utf8_new_path, g_strerror (err_code)); + utf8_path, utf8_new_path, mgw_strerror (err)); g_free (utf8_path); g_free (utf8_new_path); @@ -458,31 +448,32 @@ _moo_rename_file (const char *path, MooFileError -_moo_file_error_from_errno (int code) +_moo_file_error_from_errno (mgw_errno_t code) { - switch (code) + switch (code.value) { - case EACCES: - case EPERM: + case MGW_EACCES: + case MGW_EPERM: return MOO_FILE_ERROR_ACCESS_DENIED; - case EEXIST: + case MGW_EEXIST: return MOO_FILE_ERROR_ALREADY_EXISTS; #ifndef __WIN32__ - case ELOOP: + case MGW_ELOOP: #endif - case ENAMETOOLONG: + case MGW_ENAMETOOLONG: return MOO_FILE_ERROR_BAD_FILENAME; - case ENOENT: + case MGW_ENOENT: return MOO_FILE_ERROR_NONEXISTENT; - case ENOTDIR: + case MGW_ENOTDIR: return MOO_FILE_ERROR_NOT_FOLDER; - case EROFS: + case MGW_EROFS: return MOO_FILE_ERROR_READONLY; - case EXDEV: + case MGW_EXDEV: return MOO_FILE_ERROR_DIFFERENT_FS; - } - return MOO_FILE_ERROR_FAILED; + default: + return MOO_FILE_ERROR_FAILED; + } } @@ -1163,56 +1154,46 @@ G_STMT_START { \ gboolean use_wide_char_api; \ gpointer converted; \ int retval; \ - int save_errno; \ + mgw_errno_t save_errno; \ \ converted = convert_filename (path, &use_wide_char_api); \ \ if (!converted) \ { \ - errno = EINVAL; \ + mgw_set_errno (MGW_EINVAL); \ return -1; \ } \ \ - errno = 0; \ + mgw_set_errno (0); \ \ if (use_wide_char_api) \ retval = _WFunc (converted); \ else \ retval = _AFunc (converted); \ \ - save_errno = errno; \ + save_errno = mgw_errno (); \ g_free (converted); \ - errno = save_errno; \ + mgw_set_errno (save_errno); \ \ return retval; \ } G_STMT_END int -_moo_unlink (const char *path) -{ - return g_unlink (path); -} - - -int -_moo_mkdir (const char *path) +_moo_mkdir (const char *path, mgw_errno_t *err) { #ifdef __WIN32__ - CCALL_1 (mkdir, _wmkdir, path); + CCALL_1 (mkdir, _wmkdir, path, err); #else - return mkdir (path, S_IRWXU); + int result; + errno = 0; + result = mkdir (path, S_IRWXU); + err->value = errno; + return result; #endif } -int -_moo_remove (const char *path) -{ - return g_remove (path); -} - - gpointer _moo_fopen (const char *path, const char *mode) @@ -1221,7 +1202,7 @@ _moo_fopen (const char *path, gboolean use_wide_char_api; gpointer path_conv, mode_conv; FILE *retval; - int save_errno; + mgw_errno_t save_errno; if (G_WIN32_HAVE_WIDECHAR_API ()) { @@ -1240,21 +1221,21 @@ _moo_fopen (const char *path, { g_free (path_conv); g_free (mode_conv); - errno = EINVAL; + mgw_set_errno (MGW_EINVAL); return NULL; } - errno = 0; + mgw_set_errno (0); if (use_wide_char_api) retval = _wfopen (path_conv, mode_conv); else retval = fopen (path_conv, mode_conv); - save_errno = errno; + save_errno = mgw_errno (); g_free (path_conv); g_free (mode_conv); - errno = save_errno; + mgw_set_errno (save_errno); return retval; #else @@ -1264,13 +1245,14 @@ _moo_fopen (const char *path, int -_moo_rename (const char *old_name, - const char *new_name) +_moo_rename (const char *old_name, + const char *new_name, + mgw_errno_t *err) { #ifdef __WIN32__ gboolean use_wide_char_api; gpointer old_conv, new_conv; - int retval, save_errno; + int retval; if (G_WIN32_HAVE_WIDECHAR_API ()) { @@ -1289,25 +1271,27 @@ _moo_rename (const char *old_name, { g_free (old_conv); g_free (new_conv); - errno = EINVAL; + *err = MGW_E_INVAL; return -1; } - errno = 0; + mgw_set_errno (0); if (use_wide_char_api) - retval = _wrename (old_conv, new_conv); + retval = mw_wrename (old_conv, new_conv, err); else - retval = rename (old_conv, new_conv); + retval = mw_rename (old_conv, new_conv, err); - save_errno = errno; g_free (old_conv); g_free (new_conv); - errno = save_errno; return retval; #else - return rename (old_name, new_name); + int result; + errno = 0; + result = rename (old_name, new_name); + err->value = errno; + return result; #endif } diff --git a/moo/mooutils/mooutils-fs.h b/moo/mooutils/mooutils-fs.h index 4a019dce..2578befa 100644 --- a/moo/mooutils/mooutils-fs.h +++ b/moo/mooutils/mooutils-fs.h @@ -30,7 +30,6 @@ G_BEGIN_DECLS /* XXX */ #define _moo_save_file_utf8 moo_save_file_utf8 -#define _moo_unlink moo_unlink typedef enum { @@ -46,7 +45,7 @@ typedef enum } MooFileError; GQuark _moo_file_error_quark (void) G_GNUC_CONST; -MooFileError _moo_file_error_from_errno (int err_code); +MooFileError _moo_file_error_from_errno (mgw_errno_t err_code); gboolean _moo_save_file_utf8 (const char *name, const char *text, @@ -57,7 +56,8 @@ gboolean _moo_remove_dir (const char *path, GError **error); gboolean _moo_create_dir (const char *path, GError **error); -int _moo_mkdir_with_parents (const char *path); /* S_IRWXU on unix */ +int _moo_mkdir_with_parents (const char *path, + mgw_errno_t* err); /* S_IRWXU on unix */ gboolean _moo_rename_file (const char *path, const char *new_path, GError **error); @@ -81,13 +81,13 @@ gboolean _moo_move_files_ui (GList *filenames, * C library and WinAPI functions wrappers analogous to glib/gstdio.h */ -int _moo_unlink (const char *path); -int _moo_mkdir (const char *path); /* S_IRWXU on unix */ -int _moo_remove (const char *path); -gpointer _moo_fopen (const char *path, - const char *mode); -int _moo_rename (const char *old_name, - const char *new_name); +int _moo_mkdir (const char *path, + mgw_errno_t *err); /* S_IRWXU on unix */ +gpointer _moo_fopen (const char *path, + const char *mode); +int _moo_rename (const char *old_name, + const char *new_name, + mgw_errno_t *err); gboolean _moo_glob_match_simple (const char *pattern, const char *filename); diff --git a/moo/mooutils/mooutils-gobject.c b/moo/mooutils/mooutils-gobject.c index a8c72d52..6c6efa4a 100644 --- a/moo/mooutils/mooutils-gobject.c +++ b/moo/mooutils/mooutils-gobject.c @@ -19,8 +19,8 @@ #include #include #include -#include #include +#include /*****************************************************************************/ @@ -260,11 +260,11 @@ string_to_uint (const char *string, { char *end; guint64 val; + mgw_errno_t err; - errno = 0; - val = g_ascii_strtoull (string, &end, 10); + val = mgw_ascii_strtoull (string, &end, 10, &err); - if (errno || !end || *end) + if (mgw_errno_is_set (err) || !end || *end) return FALSE; if (val > G_MAXUINT) @@ -515,9 +515,9 @@ _moo_value_convert (const GValue *src, if (string && string[0]) { - errno = 0; - val = g_ascii_strtod (string, &end); - if (errno || !end || *end) + mgw_errno_t err; + val = mgw_ascii_strtod (string, &end, &err); + if (mgw_errno_is_set (err) || !end || *end) return FALSE; } diff --git a/moo/mooutils/mooutils-misc.c b/moo/mooutils/mooutils-misc.c index 983bf1e8..85415003 100644 --- a/moo/mooutils/mooutils-misc.c +++ b/moo/mooutils/mooutils-misc.c @@ -37,7 +37,6 @@ #include #include #include -#include #include #ifdef GDK_WINDOWING_X11 @@ -1306,18 +1305,18 @@ moo_make_user_data_dir (const char *path) int result = 0; char *full_path; char *user_dir; + mgw_errno_t err; user_dir = moo_get_user_data_dir (); g_return_val_if_fail (user_dir != NULL, FALSE); full_path = g_build_filename (user_dir, path, NULL); - result = _moo_mkdir_with_parents (full_path); + result = _moo_mkdir_with_parents (full_path, &err); if (result != 0) { - int err = errno; g_critical ("could not create directory '%s': %s", - full_path, g_strerror (err)); + full_path, mgw_strerror (err)); } g_free (user_dir); diff --git a/moo/mooutils/mooutils-script.c b/moo/mooutils/mooutils-script.c index 885b7594..10f973ea 100644 --- a/moo/mooutils/mooutils-script.c +++ b/moo/mooutils/mooutils-script.c @@ -78,12 +78,13 @@ moo_tempdir (void) for (i = 0; i < 1000; ++i) { char *basename; + mgw_errno_t err; basename = g_strdup_printf ("%s-tmpdir-%08x", short_name, g_random_int ()); dirname = g_build_filename (g_get_tmp_dir (), basename, NULL); g_free (basename); - if (_moo_mkdir (dirname) == 0) + if (_moo_mkdir (dirname, &err) == 0) break; g_free (dirname); diff --git a/moo/mooutils/mooutils-thread.c b/moo/mooutils/mooutils-thread.c index 13dee39e..64e77b60 100644 --- a/moo/mooutils/mooutils-thread.c +++ b/moo/mooutils/mooutils-thread.c @@ -21,7 +21,6 @@ #include "mooutils/moolist.h" #include -#include #include #ifdef HAVE_UNISTD_H #include diff --git a/moo/mooutils/mooutils-win32.c b/moo/mooutils/mooutils-win32.c index 33237e5c..df187f54 100644 --- a/moo/mooutils/mooutils-win32.c +++ b/moo/mooutils/mooutils-win32.c @@ -47,7 +47,6 @@ #include #include #include -#include #include #include #include @@ -350,7 +349,7 @@ _moo_win32_gettimeofday (struct timeval *tp, if (tp == NULL || tzp != NULL) { - errno = EINVAL; + mgw_set_errno (MGW_EINVAL); return -1; } @@ -374,7 +373,7 @@ _moo_win32_fnmatch (const char *pattern, { if (flags != 0) { - errno = EINVAL; + mgw_set_errno (MGW_EINVAL); return -1; } @@ -517,13 +516,13 @@ _moo_win32_mmap (gpointer start, g_return_val_if_fail (flags == MAP_SHARED, NULL); g_return_val_if_fail (offset == 0, NULL); - errno = 0; + mgw_set_errno (0); if (mgw_fstat (fd, &st) != 0) return MAP_FAILED; if ((guint64) st.st_size != length) { - errno = EINVAL; + mgw_set_errno (MGW_EINVAL); return MAP_FAILED; } @@ -540,7 +539,7 @@ _moo_win32_mmap (gpointer start, if (!mapping) { - errno = EINVAL; + mgw_set_errno (MGW_EINVAL); return MAP_FAILED; } @@ -549,7 +548,7 @@ _moo_win32_mmap (gpointer start, if (!buffer) { CloseHandle (mapping); - errno = EINVAL; + mgw_set_errno (MGW_EINVAL); return MAP_FAILED; } @@ -564,7 +563,7 @@ _moo_win32_munmap (gpointer start, { if (!remove_mapped_file (start)) { - errno = EINVAL; + mgw_set_errno (MGW_EINVAL); return -1; } else diff --git a/moo/plugins/ctags/ctags-doc.c b/moo/plugins/ctags/ctags-doc.c index 568e8a95..c45b4cf8 100644 --- a/moo/plugins/ctags/ctags-doc.c +++ b/moo/plugins/ctags/ctags-doc.c @@ -15,6 +15,7 @@ */ #include "config.h" +#define MOO_DO_NOT_MANGLE_GLIB_FUNCTIONS #include "ctags-doc.h" #include "ctags-view.h" #include "readtags.h" diff --git a/moo/plugins/moofilelist.c b/moo/plugins/moofilelist.c index 0884925f..2adf18ab 100644 --- a/moo/plugins/moofilelist.c +++ b/moo/plugins/moofilelist.c @@ -1069,7 +1069,8 @@ file_list_save_config (FileList *list, if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (list), &iter)) { - _moo_unlink (filename); + mgw_errno_t err; + mgw_unlink (filename, &err); return; } diff --git a/moo/plugins/moofileselector.c b/moo/plugins/moofileselector.c index 41907497..a393aaa1 100644 --- a/moo/plugins/moofileselector.c +++ b/moo/plugins/moofileselector.c @@ -47,7 +47,6 @@ #include #include #include -#include #define PREFS_LAST_DIR MOO_PLUGIN_PREFS_ROOT "/" MOO_FILE_SELECTOR_PLUGIN_ID "/last_dir" #define PREFS_HIDDEN_FILES MOO_PLUGIN_PREFS_ROOT "/" MOO_FILE_SELECTOR_PLUGIN_ID "/show_hidden_files" @@ -272,15 +271,13 @@ moo_file_selector_activate (MooFileView *fileview, MgwStatBuf statbuf; MooFileSelector *filesel = MOO_FILE_SELECTOR (fileview); gboolean is_text = TRUE, is_exe = FALSE; + mgw_errno_t err; g_return_if_fail (path != NULL); - errno = 0; - - if (mgw_stat (path, &statbuf) != 0) + if (mgw_stat (path, &statbuf, &err) != 0) { - int err = errno; - g_warning ("error in stat(%s): %s", path, g_strerror (err)); + g_warning ("error in stat(%s): %s", path, mgw_strerror (err)); return; } diff --git a/moo/plugins/moofind.c b/moo/plugins/moofind.c index f891e67d..d034cdf6 100644 --- a/moo/plugins/moofind.c +++ b/moo/plugins/moofind.c @@ -41,7 +41,6 @@ #ifndef __WIN32__ #include #endif -#include #include #define FIND_PLUGIN_ID "Find" @@ -697,6 +696,7 @@ process_grep_line (MooLineView *view, int view_line; int line_no; guint64 line_no_64; + mgw_errno_t err; g_return_val_if_fail (line != NULL, FALSE); @@ -750,10 +750,9 @@ process_grep_line (MooLineView *view, g_free (filename); } - errno = 0; - line_no_64 = g_ascii_strtoull (number, NULL, 0); + line_no_64 = mgw_ascii_strtoull (number, NULL, 0, &err); - if (errno) + if (mgw_errno_is_set (err)) { g_warning ("could not parse number '%s'", number); line_no = -1; diff --git a/moo/plugins/usertools/moocommand.c b/moo/plugins/usertools/moocommand.c index 4d304fca..e2d7ecec 100644 --- a/moo/plugins/usertools/moocommand.c +++ b/moo/plugins/usertools/moocommand.c @@ -827,30 +827,31 @@ get_options_from_file (MooCommandFactory *factory, const char *filename, GPtrArray **options) { - FILE *file; + MGW_FILE *file; char buf[2048]; + mgw_errno_t err; - if (!(file = g_fopen (filename, "rb"))) + if (!(file = mgw_fopen (filename, "rb", &err))) { - g_warning ("%s: could not open file %s", G_STRFUNC, filename); + g_warning ("%s: could not open file %s: %s", G_STRFUNC, filename, mgw_strerror (err)); return; } buf[0] = 0; buf[sizeof buf - 1] = '\1'; - if (fgets (buf, sizeof buf, file) && buf[sizeof buf - 1] != 0) + if (mgw_fgets (buf, sizeof buf, file) && buf[sizeof buf - 1] != 0) { int len = strlen (buf); - seriously_ignore_return_value_p (fgets (buf + len, sizeof buf - len, file)); + seriously_ignore_return_value_p (mgw_fgets (buf + len, sizeof buf - len, file)); } - if (ferror (file)) + if (mgw_ferror (file)) g_warning ("%s: error reading file %s", G_STRFUNC, filename); else get_options_from_contents (factory, cmd_data, buf, options, filename); - fclose (file); + mgw_fclose (file); } #endif /* !__WIN32__ */