Build fixes for the fall-out of the inclusion changes
[platform/upstream/glib.git] / glib / giochannel.c
index fd952ed..fb7fd0a 100644 (file)
@@ -32,7 +32,6 @@
  */
 
 #include "config.h"
-#include "giochannel.h"
 
 #include <string.h>
 #include <errno.h>
 
 #undef G_DISABLE_DEPRECATED
 
-#include "glib.h"
+#include "giochannel.h"
 
+#include "gstrfuncs.h"
+#include "gtestutils.h"
 #include "glibintl.h"
 
+
+/**
+ * SECTION: iochannels
+ * @title: IO Channels
+ * @short_description: portable support for using files, pipes and
+ *                     sockets
+ * @see_also: <para> <variablelist> <varlistentry>
+ *            <term>g_io_add_watch(), g_io_add_watch_full(),
+ *            g_source_remove()</term> <listitem><para> Convenience
+ *            functions for creating #GIOChannel instances and adding
+ *            them to the <link linkend="glib-The-Main-Event-Loop">main
+ *            event loop</link>. </para></listitem> </varlistentry>
+ *            </variablelist> </para>
+ *
+ * The #GIOChannel data type aims to provide a portable method for
+ * using file descriptors, pipes, and sockets, and integrating them
+ * into the <link linkend="glib-The-Main-Event-Loop">main event
+ * loop</link>. Currently full support is available on UNIX platforms,
+ * support for Windows is only partially complete.
+ *
+ * To create a new #GIOChannel on UNIX systems use
+ * g_io_channel_unix_new(). This works for plain file descriptors,
+ * pipes and sockets. Alternatively, a channel can be created for a
+ * file in a system independent manner using g_io_channel_new_file().
+ *
+ * Once a #GIOChannel has been created, it can be used in a generic
+ * manner with the functions g_io_channel_read_chars(),
+ * g_io_channel_write_chars(), g_io_channel_seek_position(), and
+ * g_io_channel_shutdown().
+ *
+ * To add a #GIOChannel to the <link
+ * linkend="glib-The-Main-Event-Loop">main event loop</link> use
+ * g_io_add_watch() or g_io_add_watch_full(). Here you specify which
+ * events you are interested in on the #GIOChannel, and provide a
+ * function to be called whenever these events occur.
+ *
+ * #GIOChannel instances are created with an initial reference count of
+ * 1. g_io_channel_ref() and g_io_channel_unref() can be used to
+ * increment or decrement the reference count respectively. When the
+ * reference count falls to 0, the #GIOChannel is freed. (Though it
+ * isn't closed automatically, unless it was created using
+ * g_io_channel_new_from_file().) Using g_io_add_watch() or
+ * g_io_add_watch_full() increments a channel's reference count.
+ *
+ * The new functions g_io_channel_read_chars(),
+ * g_io_channel_read_line(), g_io_channel_read_line_string(),
+ * g_io_channel_read_to_end(), g_io_channel_write_chars(),
+ * g_io_channel_seek_position(), and g_io_channel_flush() should not be
+ * mixed with the deprecated functions g_io_channel_read(),
+ * g_io_channel_write(), and g_io_channel_seek() on the same channel.
+ **/
+
+/**
+ * GIOChannel:
+ *
+ * A data structure representing an IO Channel. The fields should be
+ * considered private and should only be accessed with the following
+ * functions.
+ **/
+
+/**
+ * GIOFuncs:
+ * @io_read: reads raw bytes from the channel.  This is called from
+ *           various functions such as g_io_channel_read_chars() to
+ *           read raw bytes from the channel.  Encoding and buffering
+ *           issues are dealt with at a higher level.
+ * @io_write: writes raw bytes to the channel.  This is called from
+ *            various functions such as g_io_channel_write_chars() to
+ *            write raw bytes to the channel.  Encoding and buffering
+ *            issues are dealt with at a higher level.
+ * @io_seek: (optional) seeks the channel.  This is called from
+ *           g_io_channel_seek() on channels that support it.
+ * @io_close: closes the channel.  This is called from
+ *            g_io_channel_close() after flushing the buffers.
+ * @io_create_watch: creates a watch on the channel.  This call
+ *                   corresponds directly to g_io_create_watch().
+ * @io_free: called from g_io_channel_unref() when the channel needs to
+ *           be freed.  This function must free the memory associated
+ *           with the channel, including freeing the #GIOChannel
+ *           structure itself.  The channel buffers have been flushed
+ *           and possibly @io_close has been called by the time this
+ *           function is called.
+ * @io_set_flags: sets the #GIOFlags on the channel.  This is called
+ *                from g_io_channel_set_flags() with all flags except
+ *                for %G_IO_FLAG_APPEND and %G_IO_FLAG_NONBLOCK masked
+ *                out.
+ * @io_get_flags: gets the #GIOFlags for the channel.  This function
+ *                need only return the %G_IO_FLAG_APPEND and
+ *                %G_IO_FLAG_NONBLOCK flags; g_io_channel_get_flags()
+ *                automatically adds the others as appropriate.
+ *
+ * A table of functions used to handle different types of #GIOChannel
+ * in a generic way.
+ **/
+
+/**
+ * GIOStatus:
+ * @G_IO_STATUS_ERROR: An error occurred.
+ * @G_IO_STATUS_NORMAL: Success.
+ * @G_IO_STATUS_EOF: End of file.
+ * @G_IO_STATUS_AGAIN: Resource temporarily unavailable.
+ *
+ * Stati returned by most of the #GIOFuncs functions.
+ **/
+
+/**
+ * GIOError:
+ * @G_IO_ERROR_NONE: no error
+ * @G_IO_ERROR_AGAIN: an EAGAIN error occurred
+ * @G_IO_ERROR_INVAL: an EINVAL error occurred
+ * @G_IO_ERROR_UNKNOWN: another error occurred
+ *
+ * #GIOError is only used by the deprecated functions
+ * g_io_channel_read(), g_io_channel_write(), and g_io_channel_seek().
+ **/
+
 #define G_IO_NICE_BUF_SIZE     1024
 
 /* This needs to be as wide as the largest character in any possible encoding */
@@ -70,6 +187,16 @@ static GIOStatus    g_io_channel_read_line_backend  (GIOChannel  *channel,
                                                         gsize       *terminator_pos,
                                                         GError     **error);
 
+/**
+ * g_io_channel_init:
+ * @channel: a #GIOChannel
+ *
+ * Initializes a #GIOChannel struct. 
+ *
+ * This is called by each of the above functions when creating a 
+ * #GIOChannel, and so is not often needed by the application 
+ * programmer (unless you are creating a new type of #GIOChannel).
+ */
 void
 g_io_channel_init (GIOChannel *channel)
 {
@@ -89,21 +216,40 @@ g_io_channel_init (GIOChannel *channel)
   channel->close_on_unref = FALSE;
 }
 
-void 
+/**
+ * g_io_channel_ref:
+ * @channel: a #GIOChannel
+ *
+ * Increments the reference count of a #GIOChannel.
+ *
+ * Returns: the @channel that was passed in (since 2.6)
+ */
+GIOChannel *
 g_io_channel_ref (GIOChannel *channel)
 {
-  g_return_if_fail (channel != NULL);
+  g_return_val_if_fail (channel != NULL, NULL);
+
+  g_atomic_int_inc (&channel->ref_count);
 
-  channel->ref_count++;
+  return channel;
 }
 
+/**
+ * g_io_channel_unref:
+ * @channel: a #GIOChannel
+ *
+ * Decrements the reference count of a #GIOChannel.
+ */
 void 
 g_io_channel_unref (GIOChannel *channel)
 {
+  gboolean is_zero;
+
   g_return_if_fail (channel != NULL);
 
-  channel->ref_count--;
-  if (channel->ref_count == 0)
+  is_zero = g_atomic_int_dec_and_test (&channel->ref_count);
+
+  if (G_UNLIKELY (is_zero))
     {
       if (channel->close_on_unref)
         g_io_channel_shutdown (channel, TRUE, NULL);
@@ -114,8 +260,7 @@ g_io_channel_unref (GIOChannel *channel)
         g_iconv_close (channel->read_cd);
       if (channel->write_cd != (GIConv) -1)
         g_iconv_close (channel->write_cd);
-      if (channel->line_term)
-        g_free (channel->line_term);
+      g_free (channel->line_term);
       if (channel->read_buf)
         g_string_free (channel->read_buf, TRUE);
       if (channel->write_buf)
@@ -127,8 +272,8 @@ g_io_channel_unref (GIOChannel *channel)
 }
 
 static GIOError
-g_io_error_get_from_g_error (GIOStatus status,
-                            GError *err)
+g_io_error_get_from_g_error (GIOStatus  status,
+                            GError    *err)
 {
   switch (status)
     {
@@ -138,6 +283,8 @@ g_io_error_get_from_g_error (GIOStatus status,
       case G_IO_STATUS_AGAIN:
         return G_IO_ERROR_AGAIN;
       case G_IO_STATUS_ERROR:
+       g_return_val_if_fail (err != NULL, G_IO_ERROR_UNKNOWN);
+       
         if (err->domain != G_IO_CHANNEL_ERROR)
           return G_IO_ERROR_UNKNOWN;
         switch (err->code)
@@ -149,21 +296,22 @@ g_io_error_get_from_g_error (GIOStatus status,
           }
       default:
         g_assert_not_reached ();
-        return G_IO_ERROR_UNKNOWN; /* Keep the compiler happy */
     }
 }
 
 /**
  * g_io_channel_read:
- * @channel: a #GIOChannel. 
- * @buf: a buffer to read the data into (which should be at least count bytes long).
- * @count: the number of bytes to read from the #GIOChannel.
- * @bytes_read: returns the number of bytes actually read. 
+ * @channel: a #GIOChannel
+ * @buf: a buffer to read the data into (which should be at least 
+ *       count bytes long)
+ * @count: the number of bytes to read from the #GIOChannel
+ * @bytes_read: returns the number of bytes actually read
  * 
- * Reads data from a #GIOChannel. This function is depricated. New code should
- * use g_io_channel_read_chars() instead.
+ * Reads data from a #GIOChannel. 
  * 
  * Return value: %G_IO_ERROR_NONE if the operation was successful. 
+ *
+ * Deprecated:2.2: Use g_io_channel_read_chars() instead.
  **/
 GIOError 
 g_io_channel_read (GIOChannel *channel, 
@@ -178,6 +326,15 @@ g_io_channel_read (GIOChannel *channel,
   g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
   g_return_val_if_fail (bytes_read != NULL, G_IO_ERROR_UNKNOWN);
 
+  if (count == 0)
+    {
+      if (bytes_read)
+        *bytes_read = 0;
+      return G_IO_ERROR_NONE;
+    }
+
+  g_return_val_if_fail (buf != NULL, G_IO_ERROR_UNKNOWN);
+
   status = channel->funcs->io_read (channel, buf, count, bytes_read, &err);
 
   error = g_io_error_get_from_g_error (status, err);
@@ -190,15 +347,16 @@ g_io_channel_read (GIOChannel *channel,
 
 /**
  * g_io_channel_write:
- * @channel:  a #GIOChannel.
- * @buf: the buffer containing the data to write
- * @count: the number of bytes to write.
- * @bytes_written:  the number of bytes actually written.
+ * @channel:  a #GIOChannel
+ * @buf: the buffer containing the data to write
+ * @count: the number of bytes to write
+ * @bytes_written: the number of bytes actually written
  * 
- * Writes data to a #GIOChannel. This function is depricated. New code should
- * use g_io_channel_write_chars() instead.
+ * Writes data to a #GIOChannel. 
  * 
  * Return value:  %G_IO_ERROR_NONE if the operation was successful.
+ *
+ * Deprecated:2.2: Use g_io_channel_write_chars() instead.
  **/
 GIOError 
 g_io_channel_write (GIOChannel  *channel, 
@@ -225,22 +383,24 @@ g_io_channel_write (GIOChannel  *channel,
 
 /**
  * g_io_channel_seek:
- * @channel: a #GIOChannel. 
- * @offset: an offset, in bytes, which is added to the position specified by @type
+ * @channel: a #GIOChannel
+ * @offset: an offset, in bytes, which is added to the position specified 
+ *          by @type
  * @type: the position in the file, which can be %G_SEEK_CUR (the current
- *        position), %G_SEEK_SET (the start of the file), or %G_SEEK_END (the end of the
- *        file).
+ *        position), %G_SEEK_SET (the start of the file), or %G_SEEK_END 
+ *        (the end of the file)
  * 
- * Sets the current position in the #GIOChannel, similar to the standard library
- * function fseek(). This function is depricated. New code should
- * use g_io_channel_seek_position() instead.
+ * Sets the current position in the #GIOChannel, similar to the standard 
+ * library function fseek(). 
  * 
  * Return value: %G_IO_ERROR_NONE if the operation was successful.
+ *
+ * Deprecated:2.2: Use g_io_channel_seek_position() instead.
  **/
 GIOError 
-g_io_channel_seek  (GIOChannel   *channel,
-                   glong         offset, 
-                   GSeekType     type)
+g_io_channel_seek (GIOChannel *channel,
+                  gint64      offset, 
+                  GSeekType   type)
 {
   GError *err = NULL;
   GIOError error;
@@ -276,10 +436,10 @@ g_io_channel_seek  (GIOChannel   *channel,
 
 /**
  * g_io_channel_new_file:
- * @filename: A string containing the name of a file.
+ * @filename: A string containing the name of a file
  * @mode: One of "r", "w", "a", "r+", "w+", "a+". These have
- *        the same meaning as in fopen().
- * @error: A location to return an error of type %G_IO_FILE_ERROR.
+ *        the same meaning as in fopen()
+ * @error: A location to return an error of type %G_FILE_ERROR
  *
  * Open a file @filename as a #GIOChannel using mode @mode. This
  * channel will be closed when the last reference to it is dropped,
@@ -296,9 +456,9 @@ g_io_channel_seek  (GIOChannel   *channel,
  * 
  * Close an IO channel. Any pending data to be written will be
  * flushed, ignoring errors. The channel will not be freed until the
- * last reference is dropped using g_io_channel_unref(). This
- * function is deprecated: you should use g_io_channel_shutdown()
- * instead.
+ * last reference is dropped using g_io_channel_unref(). 
+ *
+ * Deprecated:2.2: Use g_io_channel_shutdown() instead.
  **/
 void
 g_io_channel_close (GIOChannel *channel)
@@ -336,9 +496,9 @@ g_io_channel_close (GIOChannel *channel)
  * Return value: the status of the operation.
  **/
 GIOStatus
-g_io_channel_shutdown (GIOChannel *channel,
-                      gboolean    flush,
-                      GError    **err)
+g_io_channel_shutdown (GIOChannel  *channel,
+                      gboolean     flush,
+                      GError     **err)
 {
   GIOStatus status, result;
   GError *tmperr = NULL;
@@ -346,27 +506,35 @@ g_io_channel_shutdown (GIOChannel *channel,
   g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
   g_return_val_if_fail (err == NULL || *err == NULL, G_IO_STATUS_ERROR);
 
-  if (flush && channel->write_buf && channel->write_buf->len > 0)
+  if (channel->write_buf && channel->write_buf->len > 0)
     {
-      GIOFlags flags;
+      if (flush)
+        {
+          GIOFlags flags;
       
-      /* Set the channel to blocking, to avoid a busy loop
-       */
-      flags = g_io_channel_get_flags (channel);
-      /* Ignore any errors here, they're irrelevant */
-      g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
+          /* Set the channel to blocking, to avoid a busy loop
+           */
+          flags = g_io_channel_get_flags (channel);
+          /* Ignore any errors here, they're irrelevant */
+          g_io_channel_set_flags (channel, flags & ~G_IO_FLAG_NONBLOCK, NULL);
 
-      result = g_io_channel_flush (channel, &tmperr);
-
-      if (channel->partial_write_buf[0] != '\0')
-        {
-          g_warning ("Partial character at end of write buffer not flushed.\n");
-          channel->partial_write_buf[0] = '\0';
+          result = g_io_channel_flush (channel, &tmperr);
         }
+      else
+        result = G_IO_STATUS_NORMAL;
+
+      g_string_truncate(channel->write_buf, 0);
     }
   else
     result = G_IO_STATUS_NORMAL;
 
+  if (channel->partial_write_buf[0] != '\0')
+    {
+      if (flush)
+        g_warning ("Partial character at end of write buffer not flushed.\n");
+      channel->partial_write_buf[0] = '\0';
+    }
+
   status = channel->funcs->io_close (channel, err);
 
   channel->close_on_unref = FALSE; /* Because we already did */
@@ -434,15 +602,52 @@ g_io_channel_purge (GIOChannel *channel)
     }
 }
 
+/**
+ * g_io_create_watch:
+ * @channel: a #GIOChannel to watch
+ * @condition: conditions to watch for
+ *
+ * Creates a #GSource that's dispatched when @condition is met for the 
+ * given @channel. For example, if condition is #G_IO_IN, the source will 
+ * be dispatched when there's data available for reading.
+ *
+ * g_io_add_watch() is a simpler interface to this same functionality, for 
+ * the case where you want to add the source to the default main loop context 
+ * at the default priority.
+ *
+ * On Windows, polling a #GSource created to watch a channel for a socket
+ * puts the socket in non-blocking mode. This is a side-effect of the
+ * implementation and unavoidable.
+ *
+ * Returns: a new #GSource
+ */
 GSource *
-g_io_create_watch (GIOChannel  *channel,
-                  GIOCondition condition)
+g_io_create_watch (GIOChannel   *channel,
+                  GIOCondition  condition)
 {
   g_return_val_if_fail (channel != NULL, NULL);
 
   return channel->funcs->io_create_watch (channel, condition);
 }
 
+/**
+ * g_io_add_watch_full:
+ * @channel: a #GIOChannel
+ * @priority: the priority of the #GIOChannel source
+ * @condition: the condition to watch for
+ * @func: the function to call when the condition is satisfied
+ * @user_data: user data to pass to @func
+ * @notify: the function to call when the source is removed
+ *
+ * Adds the #GIOChannel into the default main loop context
+ * with the given priority.
+ *
+ * This internally creates a main loop source using g_io_create_watch()
+ * and attaches it to the main loop context with g_source_attach().
+ * You can do these steps manuallt if you need greater control.
+ *
+ * Returns: the event source id
+ */
 guint 
 g_io_add_watch_full (GIOChannel    *channel,
                     gint           priority,
@@ -468,11 +673,48 @@ g_io_add_watch_full (GIOChannel    *channel,
   return id;
 }
 
+/**
+ * g_io_add_watch:
+ * @channel: a #GIOChannel
+ * @condition: the condition to watch for
+ * @func: the function to call when the condition is satisfied
+ * @user_data: user data to pass to @func
+ *
+ * Adds the #GIOChannel into the default main loop context
+ * with the default priority.
+ *
+ * Returns: the event source id
+ */
+/**
+ * GIOFunc:
+ * @source: the #GIOChannel event source
+ * @condition: the condition which has been satisfied
+ * @data: user data set in g_io_add_watch() or g_io_add_watch_full()
+ * @Returns: the function should return %FALSE if the event source
+ *           should be removed
+ *
+ * Specifies the type of function passed to g_io_add_watch() or
+ * g_io_add_watch_full(), which is called when the requested condition
+ * on a #GIOChannel is satisfied.
+ **/
+/**
+ * GIOCondition:
+ * @G_IO_IN: There is data to read.
+ * @G_IO_OUT: Data can be written (without blocking).
+ * @G_IO_PRI: There is urgent data to read.
+ * @G_IO_ERR: Error condition.
+ * @G_IO_HUP: Hung up (the connection has been broken, usually for
+ *            pipes and sockets).
+ * @G_IO_NVAL: Invalid request. The file descriptor is not open.
+ *
+ * A bitwise combination representing a condition to watch for on an
+ * event source.
+ **/
 guint 
-g_io_add_watch (GIOChannel    *channel,
-               GIOCondition   condition,
-               GIOFunc        func,
-               gpointer       user_data)
+g_io_add_watch (GIOChannel   *channel,
+               GIOCondition  condition,
+               GIOFunc       func,
+               gpointer      user_data)
 {
   return g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, condition, func, user_data, NULL);
 }
@@ -482,9 +724,8 @@ g_io_add_watch (GIOChannel    *channel,
  * @channel: A #GIOChannel
  *
  * This function returns a #GIOCondition depending on whether there
- * is data to be read/space to write data in the
- * internal buffers in the #GIOChannel. Only the flags %G_IO_IN and
- * %G_IO_OUT may be set.
+ * is data to be read/space to write data in the internal buffers in 
+ * the #GIOChannel. Only the flags %G_IO_IN and %G_IO_OUT may be set.
  *
  * Return value: A #GIOCondition
  **/
@@ -512,11 +753,12 @@ g_io_channel_get_buffer_condition (GIOChannel *channel)
 
 /**
  * g_io_channel_error_from_errno:
- * @en: an <literal>errno</literal> error number, e.g. %EINVAL.
+ * @en: an <literal>errno</literal> error number, e.g. %EINVAL
  *
  * Converts an <literal>errno</literal> error number to a #GIOChannelError.
  *
- * Return value: a #GIOChannelError error number, e.g. %G_IO_CHANNEL_ERROR_INVAL.
+ * Return value: a #GIOChannelError error number, e.g. 
+ *      %G_IO_CHANNEL_ERROR_INVAL.
  **/
 GIOChannelError
 g_io_channel_error_from_errno (gint en)
@@ -524,9 +766,6 @@ g_io_channel_error_from_errno (gint en)
 #ifdef EAGAIN
   g_return_val_if_fail (en != EAGAIN, G_IO_CHANNEL_ERROR_FAILED);
 #endif
-#ifdef EINTR
-  g_return_val_if_fail (en != EINTR, G_IO_CHANNEL_ERROR_FAILED);
-#endif
 
   switch (en)
     {
@@ -538,7 +777,7 @@ g_io_channel_error_from_errno (gint en)
 
 #ifdef EFAULT
     case EFAULT:
-      g_warning("File descriptor outside valid address space.\n");
+      g_warning("Buffer outside valid address space.\n");
       return G_IO_CHANNEL_ERROR_FAILED;
 #endif
 
@@ -547,6 +786,18 @@ g_io_channel_error_from_errno (gint en)
       return G_IO_CHANNEL_ERROR_FBIG;
 #endif
 
+#ifdef EINTR
+    /* In general, we should catch EINTR before we get here,
+     * but close() is allowed to return EINTR by POSIX, so
+     * we need to catch it here; EINTR from close() is
+     * unrecoverable, because it's undefined whether
+     * the fd was actually closed or not, so we just return
+     * a generic error code.
+     */
+    case EINTR:
+      return G_IO_CHANNEL_ERROR_FAILED;
+#endif
+
 #ifdef EINVAL
     case EINVAL:
       return G_IO_CHANNEL_ERROR_INVAL;
@@ -590,13 +841,13 @@ g_io_channel_error_from_errno (gint en)
 /**
  * g_io_channel_set_buffer_size:
  * @channel: a #GIOChannel
- * @size: the size of the buffer. 0 == pick a good size
+ * @size: the size of the buffer, or 0 to let GLib pick a good size
  *
- * Set the buffer size.
+ * Sets the buffer size.
  **/  
 void
-g_io_channel_set_buffer_size (GIOChannel       *channel,
-                              gsize             size)
+g_io_channel_set_buffer_size (GIOChannel *channel,
+                              gsize       size)
 {
   g_return_if_fail (channel != NULL);
 
@@ -613,12 +864,12 @@ g_io_channel_set_buffer_size (GIOChannel  *channel,
  * g_io_channel_get_buffer_size:
  * @channel: a #GIOChannel
  *
- * Get the buffer size.
+ * Gets the buffer size.
  *
  * Return value: the size of the buffer.
  **/  
 gsize
-g_io_channel_get_buffer_size (GIOChannel       *channel)
+g_io_channel_get_buffer_size (GIOChannel *channel)
 {
   g_return_val_if_fail (channel != NULL, 0);
 
@@ -628,13 +879,13 @@ g_io_channel_get_buffer_size (GIOChannel  *channel)
 /**
  * g_io_channel_set_line_term:
  * @channel: a #GIOChannel
- * @line_term: The line termination string. Use %NULL for auto detect.
- *             Auto detection breaks on "\n", "\r\n", "\r", "\0", and
- *             the unicode paragraph separator. Auto detection should
+ * @line_term: The line termination string. Use %NULL for autodetect.
+ *             Autodetection breaks on "\n", "\r\n", "\r", "\0", and
+ *             the Unicode paragraph separator. Autodetection should
  *             not be used for anything other than file-based channels.
  * @length: The length of the termination string. If -1 is passed, the
- *          string is assumed to be nulterminated. This option allows
- *          termination strings with embeded nulls.
+ *          string is assumed to be nul-terminated. This option allows
+ *          termination strings with embedded nuls.
  *
  * This sets the string that #GIOChannel uses to determine
  * where in the file a line break occurs.
@@ -652,8 +903,7 @@ g_io_channel_set_line_term (GIOChannel      *channel,
   else if (length < 0)
     length = strlen (line_term);
 
-  if (channel->line_term)
-    g_free (channel->line_term);
+  g_free (channel->line_term);
   channel->line_term = line_term ? g_memdup (line_term, length) : NULL;
   channel->line_term_len = length;
 }
@@ -665,16 +915,16 @@ g_io_channel_set_line_term (GIOChannel    *channel,
  *
  * This returns the string that #GIOChannel uses to determine
  * where in the file a line break occurs. A value of %NULL
- * indicates auto detection.
+ * indicates autodetection.
  *
  * Return value: The line termination string. This value
  *   is owned by GLib and must not be freed.
  **/
 G_CONST_RETURN gchar*
-g_io_channel_get_line_term (GIOChannel *channel,
-                           gint        *length)
+g_io_channel_get_line_term (GIOChannel *channel,
+                           gint       *length)
 {
-  g_return_val_if_fail (channel != NULL, 0);
+  g_return_val_if_fail (channel != NULL, NULL);
 
   if (length)
     *length = channel->line_term_len;
@@ -684,24 +934,49 @@ g_io_channel_get_line_term (GIOChannel    *channel,
 
 /**
  * g_io_channel_set_flags:
- * @channel: a #GIOChannel.
- * @flags: the flags to set on the channel.
- * @error: A location to return an error of type #GIOChannelError.
+ * @channel: a #GIOChannel
+ * @flags: the flags to set on the IO channel
+ * @error: A location to return an error of type #GIOChannelError
  *
- * Sets flags on the channel.
+ * Sets the (writeable) flags in @channel to (@flags & %G_IO_CHANNEL_SET_MASK).
  *
  * Return value: the status of the operation. 
  **/
+/**
+ * GIOFlags:
+ * @G_IO_FLAG_APPEND: turns on append mode, corresponds to %O_APPEND
+ *                    (see the documentation of the UNIX open()
+ *                    syscall).
+ * @G_IO_FLAG_NONBLOCK: turns on nonblocking mode, corresponds to
+ *                      %O_NONBLOCK/%O_NDELAY (see the documentation of
+ *                      the UNIX open() syscall).
+ * @G_IO_FLAG_IS_READABLE: indicates that the io channel is readable.
+ *                         This flag can not be changed.
+ * @G_IO_FLAG_IS_WRITEABLE: indicates that the io channel is writable.
+ *                          This flag can not be changed.
+ * @G_IO_FLAG_IS_SEEKABLE: indicates that the io channel is seekable,
+ *                         i.e. that g_io_channel_seek_position() can
+ *                         be used on it.  This flag can not be changed.
+ * @G_IO_FLAG_MASK: the mask that specifies all the valid flags.
+ * @G_IO_FLAG_GET_MASK: the mask of the flags that are returned from
+ *                      g_io_channel_get_flags().
+ * @G_IO_FLAG_SET_MASK: the mask of the flags that the user can modify
+ *                      with g_io_channel_set_flags().
+ *
+ * Specifies properties of a #GIOChannel. Some of the flags can only be
+ * read with g_io_channel_get_flags(), but not changed with
+ * g_io_channel_set_flags().
+ **/
 GIOStatus
-g_io_channel_set_flags (GIOChannel *channel,
-                        GIOFlags    flags,
-                        GError    **error)
+g_io_channel_set_flags (GIOChannel  *channel,
+                        GIOFlags     flags,
+                        GError     **error)
 {
   g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
   g_return_val_if_fail ((error == NULL) || (*error == NULL),
                        G_IO_STATUS_ERROR);
 
-  return (* channel->funcs->io_set_flags)(channel,
+  return (*channel->funcs->io_set_flags) (channel,
                                          flags & G_IO_FLAG_SET_MASK,
                                          error);
 }
@@ -716,8 +991,8 @@ g_io_channel_set_flags (GIOChannel *channel,
  * The values of the flags %G_IO_FLAG_IS_READABLE and %G_IO_FLAG_IS_WRITEABLE
  * are cached for internal use by the channel when it is created.
  * If they should change at some later point (e.g. partial shutdown
- * of a socket with the unix shutdown () function), the user
- * should immediately call g_io_channel_get_flags () to update
+ * of a socket with the UNIX shutdown() function), the user
+ * should immediately call g_io_channel_get_flags() to update
  * the internal values of these flags.
  *
  * Return value: the flags which are set on the channel
@@ -727,7 +1002,7 @@ g_io_channel_get_flags (GIOChannel *channel)
 {
   GIOFlags flags;
 
-  g_return_val_if_fail (channel != NULL, G_IO_STATUS_ERROR);
+  g_return_val_if_fail (channel != NULL, 0);
 
   flags = (* channel->funcs->io_get_flags) (channel);
 
@@ -767,13 +1042,13 @@ g_io_channel_set_close_on_unref  (GIOChannel *channel,
  * g_io_channel_get_close_on_unref:
  * @channel: a #GIOChannel.
  *
- * Returns whether the channel will be closed on the final unref of the 
- * #GIOChannel data structure. The default value of this is %TRUE for 
- * channels created by g_io_channel_new_file(), and %FALSE for all other 
- * channels.
- * 
- * Return value: %TRUE if the channel will be closed on the final unref of
- *               the #GIOChannel data structure. 
+ * Returns whether the file/socket/whatever associated with @channel
+ * will be closed when @channel receives its final unref and is
+ * destroyed. The default value of this is %TRUE for channels created
+ * by g_io_channel_new_file (), and %FALSE for all other channels.
+ *
+ * Return value: Whether the channel will be closed on the final unref of
+ *               the GIOChannel data structure.
  **/
 gboolean
 g_io_channel_get_close_on_unref        (GIOChannel *channel)
@@ -797,11 +1072,20 @@ g_io_channel_get_close_on_unref  (GIOChannel *channel)
  *
  * Return value: the status of the operation.
  **/
+/**
+ * GSeekType:
+ * @G_SEEK_CUR: the current position in the file.
+ * @G_SEEK_SET: the start of the file.
+ * @G_SEEK_END: the end of the file.
+ *
+ * An enumeration specifying the base position for a
+ * g_io_channel_seek_position() operation.
+ **/
 GIOStatus
-g_io_channel_seek_position     (GIOChannel* channel,
-                                glong       offset,
-                                GSeekType   type,
-                                GError    **error)
+g_io_channel_seek_position (GIOChannel  *channel,
+                            gint64       offset,
+                            GSeekType    type,
+                            GError     **error)
 {
   GIOStatus status;
 
@@ -890,11 +1174,11 @@ g_io_channel_seek_position       (GIOChannel* channel,
  * @channel: a #GIOChannel
  * @error: location to store an error of type #GIOChannelError
  *
- * Flush the write buffer for the GIOChannel.
+ * Flushes the write buffer for the GIOChannel.
  *
  * Return value: the status of the operation: One of
- *   G_IO_CHANNEL_NORMAL, G_IO_CHANNEL_AGAIN, or
- *   G_IO_CHANNEL_ERROR.
+ *   #G_IO_STATUS_NORMAL, #G_IO_STATUS_AGAIN, or
+ *   #G_IO_STATUS_ERROR.
  **/
 GIOStatus
 g_io_channel_flush (GIOChannel *channel,
@@ -937,7 +1221,7 @@ g_io_channel_flush (GIOChannel     *channel,
  *
  * A buffered channel can only be set unbuffered if the channel's
  * internal buffers have been flushed. Newly created channels or
- * channels which have returned G_IO_STATUS_EOF
+ * channels which have returned %G_IO_STATUS_EOF
  * not require such a flush. For write-only channels, a call to
  * g_io_channel_flush () is sufficient. For all other channels,
  * the buffers may be flushed by a call to g_io_channel_seek_position ().
@@ -953,8 +1237,8 @@ g_io_channel_flush (GIOChannel     *channel,
  * The default state of the channel is buffered.
  **/
 void
-g_io_channel_set_buffered      (GIOChannel *channel,
-                                gboolean    buffered)
+g_io_channel_set_buffered (GIOChannel *channel,
+                           gboolean    buffered)
 {
   g_return_if_fail (channel != NULL);
 
@@ -973,14 +1257,14 @@ g_io_channel_set_buffered        (GIOChannel *channel,
 
 /**
  * g_io_channel_get_buffered:
- * @channel: a #GIOChannel.
+ * @channel: a #GIOChannel
  *
- * Returns the buffering state of the channel.
+ * Returns whether @channel is buffered.
  *
  * Return Value: %TRUE if the @channel is buffered. 
  **/
 gboolean
-g_io_channel_get_buffered      (GIOChannel *channel)
+g_io_channel_get_buffered (GIOChannel *channel)
 {
   g_return_val_if_fail (channel != NULL, FALSE);
 
@@ -991,44 +1275,53 @@ g_io_channel_get_buffered        (GIOChannel *channel)
  * g_io_channel_set_encoding:
  * @channel: a #GIOChannel
  * @encoding: the encoding type
- * @error: location to store an error of type #GConvertError.
+ * @error: location to store an error of type #GConvertError
  *
- * Set the encoding for the input/output of the channel. The internal
- * encoding is always UTF-8. The default encoding for the
- * external file is UTF-8.
+ * Sets the encoding for the input/output of the channel. 
+ * The internal encoding is always UTF-8. The default encoding 
+ * for the external file is UTF-8.
  *
  * The encoding %NULL is safe to use with binary data.
  *
- * The encoding can only be set under the following conditions:
- *
- * 1. The channel was just created, and has not been written to
- *    or read from yet.
- *
- * 2. The channel is write-only.
- *
- * 3. The channel is a file, and the file pointer was just
+ * The encoding can only be set if one of the following conditions
+ * is true:
+ * <itemizedlist>
+ * <listitem><para>
+ *    The channel was just created, and has not been written to or read 
+ *    from yet.
+ * </para></listitem>
+ * <listitem><para>
+ *    The channel is write-only.
+ * </para></listitem>
+ * <listitem><para>
+ *    The channel is a file, and the file pointer was just
  *    repositioned by a call to g_io_channel_seek_position().
  *    (This flushes all the internal buffers.)
+ * </para></listitem>
+ * <listitem><para>
+ *    The current encoding is %NULL or UTF-8.
+ * </para></listitem>
+ * <listitem><para>
+ *    One of the (new API) read functions has just returned %G_IO_STATUS_EOF
+ *    (or, in the case of g_io_channel_read_to_end(), %G_IO_STATUS_NORMAL).
+ * </para></listitem>
+ * <listitem><para>
+ *    One of the functions g_io_channel_read_chars() or 
+ *    g_io_channel_read_unichar() has returned %G_IO_STATUS_AGAIN or 
+ *    %G_IO_STATUS_ERROR. This may be useful in the case of 
+ *    %G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
+ *    Returning one of these statuses from g_io_channel_read_line(),
+ *    g_io_channel_read_line_string(), or g_io_channel_read_to_end()
+ *    does <emphasis>not</emphasis> guarantee that the encoding can 
+ *    be changed.
+ * </para></listitem>
+ * </itemizedlist>
+ * Channels which do not meet one of the above conditions cannot call
+ * g_io_channel_seek_position() with an offset of %G_SEEK_CUR, and, if 
+ * they are "seekable", cannot call g_io_channel_write_chars() after 
+ * calling one of the API "read" functions.
  *
- * 4. The current encoding is %NULL or UTF-8.
- *
- * 5. One of the (new API) read functions has just returned G_IO_STATUS_EOF
- *    (or, in the case of g_io_channel_read_to_end (), G_IO_STATUS_NORMAL).
- *
- * 6. One of the functions g_io_channel_read_chars () or g_io_channel_read_unichar ()
- *    has returned G_IO_STATUS_AGAIN or G_IO_STATUS_ERROR. This may be
- *    useful in the case of G_CONVERT_ERROR_ILLEGAL_SEQUENCE.
- *    Returning one of these statuses from g_io_channel_read_line (),
- *    g_io_channel_read_line_string (), or g_io_channel_read_to_end ()
- *    does _not_ guarantee that the encoding can be changed.
- *
- * Channels which do not meet the above conditions cannot call
- * g_io_channel_seek_position () with an offset of %G_SEEK_CUR,
- * and if they are "seekable" cannot
- * call g_io_channel_write_chars () after calling one
- * of the API "read" functions.
- *
- * Return Value: %G_IO_STATUS_NORMAL if the encoding was succesfully set.
+ * Return Value: %G_IO_STATUS_NORMAL if the encoding was successfully set.
  **/
 GIOStatus
 g_io_channel_set_encoding (GIOChannel  *channel,
@@ -1079,8 +1372,8 @@ g_io_channel_set_encoding (GIOChannel     *channel,
           if (read_cd == (GIConv) -1)
             {
               err = errno;
-              from_enc = "UTF-8";
-              to_enc = encoding;
+              from_enc = encoding;
+              to_enc = "UTF-8";
             }
         }
       else
@@ -1093,8 +1386,8 @@ g_io_channel_set_encoding (GIOChannel     *channel,
           if (write_cd == (GIConv) -1)
             {
               err = errno;
-              from_enc = encoding;
-              to_enc = "UTF-8";
+              from_enc = "UTF-8";
+              to_enc = encoding;
             }
         }
       else
@@ -1107,12 +1400,12 @@ g_io_channel_set_encoding (GIOChannel   *channel,
 
           if (err == EINVAL)
             g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_NO_CONVERSION,
-                         _("Conversion from character set `%s' to `%s' is not supported"),
+                         _("Conversion from character set '%s' to '%s' is not supported"),
                          from_enc, to_enc);
           else
             g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-                         _("Could not open converter from `%s' to `%s': %s"),
-                         from_enc, to_enc, strerror (err));
+                         _("Could not open converter from '%s' to '%s': %s"),
+                         from_enc, to_enc, g_strerror (err));
 
           if (read_cd != (GIConv) -1)
             g_iconv_close (read_cd);
@@ -1158,15 +1451,15 @@ g_io_channel_set_encoding (GIOChannel   *channel,
  * g_io_channel_get_encoding:
  * @channel: a #GIOChannel
  *
- * Get the encoding for the input/output of the channel. The internal
- * encoding is always UTF-8. The encoding %NULL makes the
- * channel safe for binary data.
+ * Gets the encoding for the input/output of the channel. 
+ * The internal encoding is always UTF-8. The encoding %NULL 
+ * makes the channel safe for binary data.
  *
  * Return value: A string containing the encoding, this string is
  *   owned by GLib and must not be freed.
  **/
 G_CONST_RETURN gchar*
-g_io_channel_get_encoding (GIOChannel      *channel)
+g_io_channel_get_encoding (GIOChannel *channel)
 {
   g_return_val_if_fail (channel != NULL, NULL);
 
@@ -1174,8 +1467,8 @@ g_io_channel_get_encoding (GIOChannel      *channel)
 }
 
 static GIOStatus
-g_io_channel_fill_buffer (GIOChannel *channel,
-                          GError    **err)
+g_io_channel_fill_buffer (GIOChannel  *channel,
+                          GError     **err)
 {
   gsize read_size, cur_len, oldlen;
   GIOStatus status;
@@ -1206,8 +1499,8 @@ g_io_channel_fill_buffer (GIOChannel *channel,
 
   g_string_truncate (channel->read_buf, read_size + cur_len);
 
-  if ((status != G_IO_STATUS_NORMAL)
-    && ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
+  if ((status != G_IO_STATUS_NORMAL) &&
+      ((status != G_IO_STATUS_EOF) || (channel->read_buf->len == 0)))
     return status;
 
   g_assert (channel->read_buf->len > 0);
@@ -1223,7 +1516,7 @@ g_io_channel_fill_buffer (GIOChannel *channel,
 
   if (channel->do_encode)
     {
-      size_t errnum, inbytes_left, outbytes_left;
+      gsize errnum, inbytes_left, outbytes_left;
       gchar *inbuf, *outbuf;
       int errval;
 
@@ -1257,7 +1550,7 @@ reencode:
       g_string_truncate (channel->encoded_read_buf,
                         channel->encoded_read_buf->len - outbytes_left);
 
-      if (errnum == (size_t) -1)
+      if (errnum == (gsize) -1)
         {
           switch (errval)
             {
@@ -1277,7 +1570,7 @@ reencode:
                   status = G_IO_STATUS_NORMAL;
                 else
                   {
-                    g_set_error (err, G_CONVERT_ERROR,
+                    g_set_error_literal (err, G_CONVERT_ERROR,
                       G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
                       _("Invalid byte sequence in conversion input"));
                     return G_IO_STATUS_ERROR;
@@ -1286,7 +1579,7 @@ reencode:
               default:
                 g_assert (errval != EBADF); /* The converter should be open */
                 g_set_error (err, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-                  _("Error during conversion: %s"), strerror (errval));
+                  _("Error during conversion: %s"), g_strerror (errval));
                 return G_IO_STATUS_ERROR;
             }
         }
@@ -1319,7 +1612,7 @@ reencode:
                   status = G_IO_STATUS_NORMAL;
                 else
                   {
-                    g_set_error (err, G_CONVERT_ERROR,
+                    g_set_error_literal (err, G_CONVERT_ERROR,
                       G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
                       _("Invalid byte sequence in conversion input"));
                     status = G_IO_STATUS_ERROR;
@@ -1348,30 +1641,28 @@ reencode:
 /**
  * g_io_channel_read_line:
  * @channel: a #GIOChannel
- * @str_return: The line read from the #GIOChannel, not including the
+ * @str_return: The line read from the #GIOChannel, including the
  *              line terminator. This data should be freed with g_free()
- *              when no longer needed. This
- *              is a null terminated string. If a @length of zero is
- *              returned, this will be %NULL instead.
+ *              when no longer needed. This is a nul-terminated string. 
+ *              If a @length of zero is returned, this will be %NULL instead.
  * @length: location to store length of the read data, or %NULL
  * @terminator_pos: location to store position of line terminator, or %NULL
  * @error: A location to return an error of type #GConvertError
  *         or #GIOChannelError
  *
- * Read a line, including the terminating character(s),
- * from a #GIOChannel into a newly allocated string.
- * @length will contain allocated memory if the return
+ * Reads a line, including the terminating character(s),
+ * from a #GIOChannel into a newly-allocated string.
+ * @str_return will contain allocated memory if the return
  * is %G_IO_STATUS_NORMAL.
  *
- * Return value: a newly allocated string. Free this string
- *   with g_free() when you are done with it.
+ * Return value: the status of the operation.
  **/
 GIOStatus
-g_io_channel_read_line (GIOChannel *channel,
-                        gchar     **str_return,
-                        gsize      *length,
-                       gsize      *terminator_pos,
-                       GError    **error)
+g_io_channel_read_line (GIOChannel  *channel,
+                        gchar      **str_return,
+                        gsize       *length,
+                       gsize       *terminator_pos,
+                       GError     **error)
 {
   GIOStatus status;
   gsize got_length;
@@ -1409,15 +1700,15 @@ g_io_channel_read_line (GIOChannel *channel,
  * @error: a location to store an error of type #GConvertError
  *         or #GIOChannelError
  *
- * Read a line from a #GIOChannel, using a #GString as a buffer.
+ * Reads a line from a #GIOChannel, using a #GString as a buffer.
  *
  * Return value: the status of the operation.
  **/
 GIOStatus
-g_io_channel_read_line_string (GIOChannel *channel,
-                               GString   *buffer,
-                              gsize      *terminator_pos,
-                               GError   **error)
+g_io_channel_read_line_string (GIOChannel  *channel,
+                               GString    *buffer,
+                              gsize       *terminator_pos,
+                               GError    **error)
 {
   gsize length;
   GIOStatus status;
@@ -1445,10 +1736,10 @@ g_io_channel_read_line_string (GIOChannel *channel,
 
 
 static GIOStatus
-g_io_channel_read_line_backend (GIOChannel *channel,
-                                gsize      *length,
-                                gsize      *terminator_pos,
-                                GError    **error)
+g_io_channel_read_line_backend (GIOChannel  *channel,
+                                gsize       *length,
+                                gsize       *terminator_pos,
+                                GError     **error)
 {
   GIOStatus status;
   gsize checked_to, line_term_len, line_length, got_term_len;
@@ -1457,8 +1748,8 @@ g_io_channel_read_line_backend    (GIOChannel *channel,
   if (!channel->use_buffer)
     {
       /* Can't do a raw read in read_line */
-      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-                   _("Can't do a raw read in g_io_channel_read_line_string"));
+      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
+                           _("Can't do a raw read in g_io_channel_read_line_string"));
       return G_IO_STATUS_ERROR;
     }
 
@@ -1503,9 +1794,10 @@ read_again:
 
                     if (channel->encoding && channel->read_buf->len != 0)
                       {
-                        g_set_error (error, G_CONVERT_ERROR,
-                                     G_CONVERT_ERROR_PARTIAL_INPUT,
-                                     _("Leftover unconverted data in read buffer"));
+                        g_set_error_literal (error, G_CONVERT_ERROR,
+                                             G_CONVERT_ERROR_PARTIAL_INPUT,
+                                             _("Leftover unconverted data in "
+                                               "read buffer"));
                         return G_IO_STATUS_ERROR;
                       }
                     else
@@ -1584,8 +1876,8 @@ read_again:
         {
           if (channel->encoding && channel->read_buf->len > 0)
             {
-              g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
-                           _("Channel terminates in a partial character"));
+              g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
+                                   _("Channel terminates in a partial character"));
               return G_IO_STATUS_ERROR;
             }
           line_length = use_buf->len;
@@ -1593,7 +1885,10 @@ read_again:
           break;
         }
 
-      checked_to = MAX (use_buf->len - (line_term_len - 1), 0);
+      if (use_buf->len > line_term_len - 1)
+       checked_to = use_buf->len - (line_term_len - 1);
+      else
+       checked_to = 0;
     }
 
 done:
@@ -1613,22 +1908,22 @@ done:
  * @str_return: Location to store a pointer to a string holding
  *              the remaining data in the #GIOChannel. This data should
  *              be freed with g_free() when no longer needed. This
- *              data is terminated by an extra null, but there may be other
- *              nulls in the intervening data.
- * @length: Location to store length of the data
- * @error: location to return an error of type #GConvertError
+ *              data is terminated by an extra nul character, but there 
+ *              may be other nuls in the intervening data.
+ * @length: location to store length of the data
+ * @error: location to return an error of type #GConvertError
  *         or #GIOChannelError
  *
- * Read all the remaining data from the file.
+ * Reads all the remaining data from the file.
  *
- * Return value: %G_IO_STATUS_NORMAL on success. This function never
- *               returns %G_IO_STATUS_EOF.
+ * Return value: %G_IO_STATUS_NORMAL on success. 
+ *     This function never returns %G_IO_STATUS_EOF.
  **/
 GIOStatus
-g_io_channel_read_to_end (GIOChannel   *channel,
-                          gchar        **str_return,
-                          gsize                *length,
-                          GError       **error)
+g_io_channel_read_to_end (GIOChannel  *channel,
+                          gchar      **str_return,
+                          gsize              *length,
+                          GError     **error)
 {
   GIOStatus status;
     
@@ -1644,8 +1939,8 @@ g_io_channel_read_to_end (GIOChannel      *channel,
 
   if (!channel->use_buffer)
     {
-      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-                   _("Can't do a raw read in g_io_channel_read_to_end"));
+      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
+                           _("Can't do a raw read in g_io_channel_read_to_end"));
       return G_IO_STATUS_ERROR;
     }
 
@@ -1658,8 +1953,8 @@ g_io_channel_read_to_end (GIOChannel      *channel,
 
   if (channel->encoding && channel->read_buf->len > 0)
     {
-      g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
-                   _("Channel terminates in a partial character"));
+      g_set_error_literal (error, G_CONVERT_ERROR, G_CONVERT_ERROR_PARTIAL_INPUT,
+                           _("Channel terminates in a partial character"));
       return G_IO_STATUS_ERROR;
     }
 
@@ -1700,7 +1995,7 @@ g_io_channel_read_to_end (GIOChannel      *channel,
  *              success if count < 6 and the channel's encoding is non-%NULL.
  *              This indicates that the next UTF-8 character is too wide for
  *              the buffer.
- * @error: A location to return an error of type #GConvertError
+ * @error: a location to return an error of type #GConvertError
  *         or #GIOChannelError.
  *
  * Replacement for g_io_channel_read() with the new API.
@@ -1708,11 +2003,11 @@ g_io_channel_read_to_end (GIOChannel    *channel,
  * Return value: the status of the operation.
  **/
 GIOStatus
-g_io_channel_read_chars (GIOChannel    *channel,
-                         gchar         *buf,
-                         gsize          count,
-                        gsize          *bytes_read,
-                         GError        **error)
+g_io_channel_read_chars (GIOChannel  *channel,
+                         gchar      *buf,
+                         gsize       count,
+                        gsize       *bytes_read,
+                         GError     **error)
 {
   GIOStatus status;
   gsize got_bytes;
@@ -1731,7 +2026,7 @@ g_io_channel_read_chars (GIOChannel       *channel,
 
   if (!channel->use_buffer)
     {
-      gint tmp_bytes;
+      gsize tmp_bytes;
       
       g_assert (!channel->read_buf || channel->read_buf->len == 0);
 
@@ -1757,9 +2052,9 @@ g_io_channel_read_chars (GIOChannel       *channel,
       if (status == G_IO_STATUS_EOF && channel->encoding
           && BUF_LEN (channel->read_buf) > 0)
         {
-          g_set_error (error, G_CONVERT_ERROR,
-                       G_CONVERT_ERROR_PARTIAL_INPUT,
-                       _("Leftover unconverted data in read buffer"));
+          g_set_error_literal (error, G_CONVERT_ERROR,
+                               G_CONVERT_ERROR_PARTIAL_INPUT,
+                               _("Leftover unconverted data in read buffer"));
           status = G_IO_STATUS_ERROR;
         }
 
@@ -1812,17 +2107,18 @@ g_io_channel_read_chars (GIOChannel     *channel,
  * g_io_channel_read_unichar:
  * @channel: a #GIOChannel
  * @thechar: a location to return a character
- * @error: A location to return an error of type #GConvertError
+ * @error: a location to return an error of type #GConvertError
  *         or #GIOChannelError
  *
+ * Reads a Unicode character from @channel.
  * This function cannot be called on a channel with %NULL encoding.
  *
  * Return value: a #GIOStatus
  **/
 GIOStatus
-g_io_channel_read_unichar     (GIOChannel   *channel,
-                              gunichar     *thechar,
-                              GError      **error)
+g_io_channel_read_unichar (GIOChannel  *channel,
+                          gunichar    *thechar,
+                          GError     **error)
 {
   GIOStatus status = G_IO_STATUS_NORMAL;
 
@@ -1843,9 +2139,9 @@ g_io_channel_read_unichar     (GIOChannel   *channel,
 
       if (status == G_IO_STATUS_EOF && BUF_LEN (channel->read_buf) > 0)
         {
-          g_set_error (error, G_CONVERT_ERROR,
-                       G_CONVERT_ERROR_PARTIAL_INPUT,
-                       _("Leftover unconverted data in read buffer"));
+          g_set_error_literal (error, G_CONVERT_ERROR,
+                               G_CONVERT_ERROR_PARTIAL_INPUT,
+                               _("Leftover unconverted data in read buffer"));
           status = G_IO_STATUS_ERROR;
         }
 
@@ -1873,13 +2169,13 @@ g_io_channel_read_unichar     (GIOChannel   *channel,
  * @channel: a #GIOChannel
  * @buf: a buffer to write data from
  * @count: the size of the buffer. If -1, the buffer
- *         is taken to be a nul terminated string.
+ *         is taken to be a nul-terminated string.
  * @bytes_written: The number of bytes written. This can be nonzero
  *                 even if the return value is not %G_IO_STATUS_NORMAL.
  *                 If the return value is %G_IO_STATUS_NORMAL and the
  *                 channel is blocking, this will always be equal
  *                 to @count if @count >= 0.
- * @error: A location to return an error of type #GConvertError
+ * @error: a location to return an error of type #GConvertError
  *         or #GIOChannelError
  *
  * Replacement for g_io_channel_write() with the new API.
@@ -1892,11 +2188,11 @@ g_io_channel_read_unichar     (GIOChannel   *channel,
  * Return value: the status of the operation.
  **/
 GIOStatus
-g_io_channel_write_chars (GIOChannel   *channel,
-                          const gchar  *buf,
-                          gssize        count,
-                         gsize         *bytes_written,
-                          GError       **error)
+g_io_channel_write_chars (GIOChannel   *channel,
+                          const gchar  *buf,
+                          gssize        count,
+                         gsize        *bytes_written,
+                          GError      **error)
 {
   GIOStatus status;
   gssize wrote_bytes = 0;
@@ -1923,7 +2219,7 @@ g_io_channel_write_chars (GIOChannel      *channel,
 
   if (!channel->use_buffer)
     {
-      gint tmp_bytes;
+      gsize tmp_bytes;
       
       g_assert (!channel->write_buf || channel->write_buf->len == 0);
       g_assert (channel->partial_write_buf[0] == '\0');
@@ -1968,7 +2264,7 @@ g_io_channel_write_chars (GIOChannel      *channel,
        * and never receiving an EAGAIN.
        */
 
-      if (channel->write_buf->len >= channel->buf_size)
+      if (channel->write_buf->len >= channel->buf_size - MAX_CHAR_SIZE)
         {
           gsize did_write = 0, this_time;
 
@@ -2014,7 +2310,7 @@ g_io_channel_write_chars (GIOChannel      *channel,
         {
           const gchar *from_buf;
           gsize from_buf_len, from_buf_old_len, left_len;
-          size_t err;
+          gsize err;
           gint errnum;
 
           if (channel->partial_write_buf[0] != '\0')
@@ -2048,48 +2344,49 @@ reconvert:
               if (!g_utf8_validate (from_buf, try_len, &badchar))
                 {
                   gunichar try_char;
+                  gsize incomplete_len = from_buf + try_len - badchar;
 
-                  left_len = from_buf + try_len - badchar;
+                  left_len = from_buf + from_buf_len - badchar;
 
-                  try_char = g_utf8_get_char_validated (badchar, left_len);
+                  try_char = g_utf8_get_char_validated (badchar, incomplete_len);
 
                   switch (try_char)
                     {
                       case -2:
-                        g_assert (left_len < 6);
+                        g_assert (incomplete_len < 6);
                         if (try_len == from_buf_len)
                           {
                             errnum = EINVAL;
-                            err = (size_t) -1;
+                            err = (gsize) -1;
                           }
                         else
                           {
                             errnum = 0;
-                            err = (size_t) -1;
+                            err = (gsize) 0;
                           }
                         break;
                       case -1:
                         g_warning ("Invalid UTF-8 passed to g_io_channel_write_chars().");
                         /* FIXME bail here? */
                         errnum = EILSEQ;
-                        err = (size_t) -1;
+                        err = (gsize) -1;
                         break;
                       default:
                         g_assert_not_reached ();
-                        err = (size_t) -1;
+                        err = (gsize) -1;
                         errnum = 0; /* Don't confunse the compiler */
                     }
                 }
               else
                 {
-                  err = (size_t) 0;
+                  err = (gsize) 0;
                   errnum = 0;
-                  left_len = 0;
+                  left_len = from_buf_len - try_len;
                 }
 
               g_string_append_len (channel->write_buf, from_buf,
-                                   try_len - left_len);
-              from_buf += try_len - left_len;
+                                   from_buf_len - left_len);
+              from_buf += from_buf_len - left_len;
             }
           else
             {
@@ -2107,7 +2404,7 @@ reconvert:
                                   - space_in_buf);
             }
 
-          if (err == (size_t) -1)
+          if (err == (gsize) -1)
             {
               switch (errnum)
                {
@@ -2159,7 +2456,7 @@ reconvert:
                       }
                     break;
                   case EILSEQ:
-                    g_set_error (error, G_CONVERT_ERROR,
+                    g_set_error_literal (error, G_CONVERT_ERROR,
                       G_CONVERT_ERROR_ILLEGAL_SEQUENCE,
                       _("Invalid byte sequence in conversion input"));
                     if (from_buf_old_len > 0 && from_buf_len == left_len)
@@ -2173,7 +2470,7 @@ reconvert:
                     return G_IO_STATUS_ERROR;
                   default:
                     g_set_error (error, G_CONVERT_ERROR, G_CONVERT_ERROR_FAILED,
-                      _("Error during conversion: %s"), strerror (errnum));
+                      _("Error during conversion: %s"), g_strerror (errnum));
                     if (from_buf_len >= left_len + from_buf_old_len)
                       wrote_bytes += from_buf_len - left_len - from_buf_old_len;
                     if (bytes_written)
@@ -2209,17 +2506,18 @@ reconvert:
  * g_io_channel_write_unichar:
  * @channel: a #GIOChannel
  * @thechar: a character
- * @error: location to return an error of type #GConvertError
+ * @error: location to return an error of type #GConvertError
  *         or #GIOChannelError
  *
+ * Writes a Unicode character to @channel.
  * This function cannot be called on a channel with %NULL encoding.
  *
  * Return value: a #GIOStatus
  **/
 GIOStatus
-g_io_channel_write_unichar    (GIOChannel   *channel,
-                              gunichar      thechar,
-                              GError      **error)
+g_io_channel_write_unichar (GIOChannel  *channel,
+                           gunichar     thechar,
+                           GError     **error)
 {
   GIOStatus status;
   gchar static_buf[6];
@@ -2252,14 +2550,31 @@ g_io_channel_write_unichar    (GIOChannel   *channel,
 /**
  * g_io_channel_error_quark:
  *
- * Return value: The quark used as %G_IO_CHANNEL_ERROR
+ * Return value: the quark used as %G_IO_CHANNEL_ERROR
+ **/
+/**
+ * G_IO_CHANNEL_ERROR:
+ *
+ * Error domain for #GIOChannel operations. Errors in this domain will
+ * be from the #GIOChannelError enumeration. See #GError for
+ * information on error domains.
+ **/
+/**
+ * GIOChannelError:
+ * @G_IO_CHANNEL_ERROR_FBIG: File too large.
+ * @G_IO_CHANNEL_ERROR_INVAL: Invalid argument.
+ * @G_IO_CHANNEL_ERROR_IO: IO error.
+ * @G_IO_CHANNEL_ERROR_ISDIR: File is a directory.
+ * @G_IO_CHANNEL_ERROR_NOSPC: No space left on device.
+ * @G_IO_CHANNEL_ERROR_NXIO: No such device or address.
+ * @G_IO_CHANNEL_ERROR_OVERFLOW: Value too large for defined datatype.
+ * @G_IO_CHANNEL_ERROR_PIPE: Broken pipe.
+ * @G_IO_CHANNEL_ERROR_FAILED: Some other error.
+ *
+ * Error codes returned by #GIOChannel operations.
  **/
 GQuark
 g_io_channel_error_quark (void)
 {
-  static GQuark q = 0;
-  if (q == 0)
-    q = g_quark_from_static_string ("g-io-channel-error-quark");
-
-  return q;
+  return g_quark_from_static_string ("g-io-channel-error-quark");
 }