+static GIOStatus
+g_io_unix_set_flags (GIOChannel *channel,
+ GIOFlags flags,
+ GError **err)
+{
+ glong fcntl_flags;
+ GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
+
+ fcntl_flags = 0;
+
+ if (flags & G_IO_FLAG_APPEND)
+ fcntl_flags |= O_APPEND;
+ if (flags & G_IO_FLAG_NONBLOCK)
+#ifdef O_NONBLOCK
+ fcntl_flags |= O_NONBLOCK;
+#else
+ fcntl_flags |= O_NDELAY;
+#endif
+
+ if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
+ {
+ int errsv = errno;
+ g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+ g_io_channel_error_from_errno (errsv),
+ g_strerror (errsv));
+ return G_IO_STATUS_ERROR;
+ }
+
+ return G_IO_STATUS_NORMAL;
+}
+
+static GIOFlags
+g_io_unix_get_flags (GIOChannel *channel)
+{
+ GIOFlags flags = 0;
+ glong fcntl_flags;
+ GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
+
+ fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
+
+ if (fcntl_flags == -1)
+ {
+ int err = errno;
+ g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
+ g_strerror (err), err);
+ return 0;
+ }
+
+ if (fcntl_flags & O_APPEND)
+ flags |= G_IO_FLAG_APPEND;
+#ifdef O_NONBLOCK
+ if (fcntl_flags & O_NONBLOCK)
+#else
+ if (fcntl_flags & O_NDELAY)
+#endif
+ flags |= G_IO_FLAG_NONBLOCK;
+
+ switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
+ {
+ case O_RDONLY:
+ channel->is_readable = TRUE;
+ channel->is_writeable = FALSE;
+ break;
+ case O_WRONLY:
+ channel->is_readable = FALSE;
+ channel->is_writeable = TRUE;
+ break;
+ case O_RDWR:
+ channel->is_readable = TRUE;
+ channel->is_writeable = TRUE;
+ break;
+ default:
+ g_assert_not_reached ();
+ }
+
+ return flags;
+}
+
+GIOChannel *
+g_io_channel_new_file (const gchar *filename,
+ const gchar *mode,
+ GError **error)
+{
+ int fid, flags;
+ mode_t create_mode;
+ GIOChannel *channel;
+ enum { /* Cheesy hack */
+ MODE_R = 1 << 0,
+ MODE_W = 1 << 1,
+ MODE_A = 1 << 2,
+ MODE_PLUS = 1 << 3,
+ MODE_R_PLUS = MODE_R | MODE_PLUS,
+ MODE_W_PLUS = MODE_W | MODE_PLUS,
+ MODE_A_PLUS = MODE_A | MODE_PLUS
+ } mode_num;
+ struct stat buffer;
+
+ 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.\n", 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.\n", mode);
+ return NULL;
+ }
+
+ switch (mode_num)
+ {
+ case MODE_R:
+ flags = O_RDONLY;
+ break;
+ case MODE_W:
+ flags = O_WRONLY | O_TRUNC | O_CREAT;
+ break;
+ case MODE_A:
+ flags = O_WRONLY | O_APPEND | O_CREAT;
+ break;
+ case MODE_R_PLUS:
+ flags = O_RDWR;
+ break;
+ case MODE_W_PLUS:
+ flags = O_RDWR | O_TRUNC | O_CREAT;
+ break;
+ case MODE_A_PLUS:
+ flags = O_RDWR | O_APPEND | O_CREAT;
+ break;
+ case MODE_PLUS:
+ default:
+ g_assert_not_reached ();
+ flags = 0;
+ }
+
+ create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+
+ fid = g_open (filename, flags, create_mode);
+ if (fid == -1)
+ {
+ int err = errno;
+ g_set_error_literal (error, G_FILE_ERROR,
+ g_file_error_from_errno (err),
+ g_strerror (err));
+ return (GIOChannel *)NULL;
+ }
+
+ if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
+ {
+ int err = errno;
+ close (fid);
+ g_set_error_literal (error, G_FILE_ERROR,
+ g_file_error_from_errno (err),
+ g_strerror (err));
+ return (GIOChannel *)NULL;
+ }
+
+ channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
+
+ channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
+ || S_ISBLK (buffer.st_mode);
+
+ switch (mode_num)
+ {
+ case MODE_R:
+ channel->is_readable = TRUE;
+ channel->is_writeable = FALSE;
+ break;
+ case MODE_W:
+ case MODE_A:
+ channel->is_readable = FALSE;
+ channel->is_writeable = TRUE;
+ break;
+ case MODE_R_PLUS:
+ case MODE_W_PLUS:
+ case MODE_A_PLUS:
+ channel->is_readable = TRUE;
+ channel->is_writeable = TRUE;
+ break;
+ case MODE_PLUS:
+ default:
+ g_assert_not_reached ();
+ }
+
+ g_io_channel_init (channel);
+ channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
+ channel->funcs = &unix_channel_funcs;
+
+ ((GIOUnixChannel *) channel)->fd = fid;
+ return channel;
+}
+
+/**
+ * g_io_channel_unix_new:
+ * @fd: a file descriptor.
+ *
+ * Creates a new #GIOChannel given a file descriptor. On UNIX systems
+ * this works for plain files, pipes, and sockets.
+ *
+ * The returned #GIOChannel has a reference count of 1.
+ *
+ * The default encoding for #GIOChannel is UTF-8. If your application
+ * is reading output from a command using via pipe, you may need to set
+ * the encoding to the encoding of the current locale (see
+ * g_get_charset()) with the g_io_channel_set_encoding() function.
+ *
+ * If you want to read raw binary data without interpretation, then
+ * call the g_io_channel_set_encoding() function with %NULL for the
+ * encoding argument.
+ *
+ * This function is available in GLib on Windows, too, but you should
+ * avoid using it on Windows. The domain of file descriptors and
+ * sockets overlap. There is no way for GLib to know which one you mean
+ * in case the argument you pass to this function happens to be both a
+ * valid file descriptor and socket. If that happens a warning is
+ * issued, and GLib assumes that it is the file descriptor you mean.
+ *
+ * Returns: a new #GIOChannel.
+ **/