+ GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
+ GSource *source = g_source_new (&g_io_watch_funcs, sizeof (GIOWin32Watch));
+ GIOWin32Watch *watch = (GIOWin32Watch *)source;
+
+ watch->channel = channel;
+ g_io_channel_ref (channel);
+
+ watch->condition = condition;
+
+ if (win32_channel->event == 0)
+ win32_channel->event = WSACreateEvent ();
+
+ watch->pollfd.fd = (gintptr) win32_channel->event;
+ watch->pollfd.events = condition;
+
+ if (win32_channel->debug)
+ g_print ("g_io_win32_sock_create_watch: channel=%p sock=%d event=%p condition={%s}\n",
+ channel, win32_channel->fd, (HANDLE) watch->pollfd.fd,
+ condition_to_string (watch->condition));
+
+ g_source_add_poll (source, &watch->pollfd);
+
+ return source;
+}
+
+GIOChannel *
+g_io_channel_new_file (const gchar *filename,
+ const gchar *mode,
+ GError **error)
+{
+ int fid, flags, pmode;
+ GIOChannel *channel;
+
+ enum { /* Cheesy hack */
+ MODE_R = 1 << 0,
+ MODE_W = 1 << 1,
+ MODE_A = 1 << 2,
+ MODE_PLUS = 1 << 3,
+ };
+ int mode_num;
+
+ g_return_val_if_fail (filename != NULL, NULL);
+ g_return_val_if_fail (mode != NULL, NULL);
+ g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
+
+ switch (mode[0])
+ {
+ case 'r':
+ mode_num = MODE_R;
+ break;
+ case 'w':
+ mode_num = MODE_W;
+ break;
+ case 'a':
+ mode_num = MODE_A;
+ break;
+ default:
+ g_warning ("Invalid GIOFileMode %s.", mode);
+ return NULL;
+ }
+
+ switch (mode[1])
+ {
+ case '\0':
+ break;
+ case '+':
+ if (mode[2] == '\0')
+ {
+ mode_num |= MODE_PLUS;
+ break;
+ }
+ /* Fall through */
+ default:
+ g_warning ("Invalid GIOFileMode %s.", mode);
+ return NULL;
+ }
+
+ switch (mode_num)
+ {
+ case MODE_R:
+ flags = O_RDONLY;
+ pmode = _S_IREAD;
+ break;
+ case MODE_W:
+ flags = O_WRONLY | O_TRUNC | O_CREAT;
+ pmode = _S_IWRITE;
+ break;
+ case MODE_A:
+ flags = O_WRONLY | O_APPEND | O_CREAT;
+ pmode = _S_IWRITE;
+ break;
+ case MODE_R | MODE_PLUS:
+ flags = O_RDWR;
+ pmode = _S_IREAD | _S_IWRITE;
+ break;
+ case MODE_W | MODE_PLUS:
+ flags = O_RDWR | O_TRUNC | O_CREAT;
+ pmode = _S_IREAD | _S_IWRITE;
+ break;
+ case MODE_A | MODE_PLUS:
+ flags = O_RDWR | O_APPEND | O_CREAT;
+ pmode = _S_IREAD | _S_IWRITE;
+ break;
+ default:
+ g_assert_not_reached ();
+ abort ();
+ }
+
+ /* always open 'untranslated' */
+ fid = g_open (filename, flags | _O_BINARY, pmode);
+
+ if (g_io_win32_get_debug_flag ())
+ {
+ g_print ("g_io_channel_win32_new_file: open(\"%s\",", filename);
+ g_win32_print_access_mode (flags|_O_BINARY);
+ g_print (",%#o)=%d\n", pmode, fid);
+ }
+
+ if (fid < 0)
+ {
+ g_set_error_literal (error, G_FILE_ERROR,
+ g_file_error_from_errno (errno),
+ g_strerror (errno));
+ return (GIOChannel *)NULL;
+ }
+
+ channel = g_io_channel_win32_new_fd (fid);
+
+ /* XXX: move this to g_io_channel_win32_new_fd () */
+ channel->close_on_unref = TRUE;
+ channel->is_seekable = TRUE;
+
+ /* g_io_channel_win32_new_fd sets is_readable and is_writeable to
+ * correspond to actual readability/writeability. Set to FALSE those
+ * that mode doesn't allow
+ */
+ switch (mode_num)
+ {
+ case MODE_R:
+ channel->is_writeable = FALSE;
+ break;
+ case MODE_W:
+ case MODE_A:
+ channel->is_readable = FALSE;
+ break;
+ case MODE_R | MODE_PLUS:
+ case MODE_W | MODE_PLUS:
+ case MODE_A | MODE_PLUS:
+ break;
+ default:
+ g_assert_not_reached ();
+ abort ();
+ }
+
+ return channel;
+}
+
+#if !defined (_WIN64)
+
+#undef g_io_channel_new_file
+
+/* Binary compatibility version. Not for newly compiled code. */
+
+GIOChannel *
+g_io_channel_new_file (const gchar *filename,
+ const gchar *mode,
+ GError **error)
+{
+ gchar *utf8_filename = g_locale_to_utf8 (filename, -1, NULL, NULL, error);
+ GIOChannel *retval;
+
+ if (utf8_filename == NULL)
+ return NULL;
+
+ retval = g_io_channel_new_file_utf8 (utf8_filename, mode, error);
+
+ g_free (utf8_filename);
+
+ return retval;
+}
+
+#endif
+
+static GIOStatus
+g_io_win32_unimpl_set_flags (GIOChannel *channel,
+ GIOFlags flags,
+ GError **err)
+{
+ GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
+
+ if (win32_channel->debug)
+ {
+ g_print ("g_io_win32_unimpl_set_flags: ");
+ g_win32_print_gioflags (flags);
+ g_print ("\n");
+ }
+
+ g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+ G_IO_CHANNEL_ERROR_FAILED,
+ "Not implemented on Win32");
+
+ return G_IO_STATUS_ERROR;
+}
+
+static GIOFlags
+g_io_win32_fd_get_flags_internal (GIOChannel *channel,
+ struct _stati64 *st)
+{
+ GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
+ gchar c;
+ DWORD count;
+
+ if (st->st_mode & _S_IFIFO)
+ {
+ channel->is_readable =
+ (PeekNamedPipe ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL, NULL) != 0) || GetLastError () == ERROR_BROKEN_PIPE;
+ channel->is_writeable =
+ (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
+ channel->is_seekable = FALSE;
+ }
+ else
+ {
+ channel->is_readable =
+ (ReadFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
+ channel->is_writeable =
+ (WriteFile ((HANDLE) _get_osfhandle (win32_channel->fd), &c, 0, &count, NULL) != 0);
+ channel->is_seekable = TRUE;
+ }
+
+ /* XXX: G_IO_FLAG_APPEND */
+ /* XXX: G_IO_FLAG_NONBLOCK */
+
+ return 0;
+}
+
+static GIOFlags
+g_io_win32_fd_get_flags (GIOChannel *channel)
+{
+ struct _stati64 st;
+ GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
+
+ g_return_val_if_fail (win32_channel != NULL, 0);
+ g_return_val_if_fail (win32_channel->type == G_IO_WIN32_FILE_DESC, 0);
+
+ if (0 == _fstati64 (win32_channel->fd, &st))
+ return g_io_win32_fd_get_flags_internal (channel, &st);
+ else
+ return 0;
+}
+
+static GIOFlags
+g_io_win32_console_get_flags_internal (GIOChannel *channel)
+{
+ GIOWin32Channel *win32_channel = (GIOWin32Channel *) channel;
+ HANDLE handle = (HANDLE) _get_osfhandle (win32_channel->fd);
+ gchar c;
+ DWORD count;
+ INPUT_RECORD record;
+
+ channel->is_readable = PeekConsoleInput (handle, &record, 1, &count);
+ channel->is_writeable = WriteFile (handle, &c, 0, &count, NULL);
+ channel->is_seekable = FALSE;
+
+ return 0;
+}
+
+static GIOFlags
+g_io_win32_console_get_flags (GIOChannel *channel)
+{
+ GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
+
+ g_return_val_if_fail (win32_channel != NULL, 0);
+ g_return_val_if_fail (win32_channel->type == G_IO_WIN32_CONSOLE, 0);
+
+ return g_io_win32_console_get_flags_internal (channel);
+}
+
+static GIOFlags
+g_io_win32_msg_get_flags (GIOChannel *channel)
+{
+ return 0;
+}
+
+static GIOStatus
+g_io_win32_sock_set_flags (GIOChannel *channel,
+ GIOFlags flags,
+ GError **err)
+{
+ GIOWin32Channel *win32_channel = (GIOWin32Channel *)channel;
+ u_long arg;
+
+ if (win32_channel->debug)
+ {
+ g_print ("g_io_win32_sock_set_flags: ");
+ g_win32_print_gioflags (flags);
+ g_print ("\n");
+ }
+
+ if (flags & G_IO_FLAG_NONBLOCK)
+ {
+ arg = 1;
+ if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
+ {
+ gchar *emsg = g_win32_error_message (WSAGetLastError ());
+
+ g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+ G_IO_CHANNEL_ERROR_FAILED,
+ emsg);
+ g_free (emsg);
+
+ return G_IO_STATUS_ERROR;
+ }
+ }
+ else
+ {
+ arg = 0;
+ if (ioctlsocket (win32_channel->fd, FIONBIO, &arg) == SOCKET_ERROR)
+ {
+ gchar *emsg = g_win32_error_message (WSAGetLastError ());
+
+ g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+ G_IO_CHANNEL_ERROR_FAILED,
+ emsg);
+ g_free (emsg);
+
+ return G_IO_STATUS_ERROR;
+ }
+ }
+
+ return G_IO_STATUS_NORMAL;
+}
+
+static GIOFlags
+g_io_win32_sock_get_flags (GIOChannel *channel)
+{
+ /* Could we do something here? */
+ return 0;