Blame glib/giounix.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
Packit ae235b
 *
Packit ae235b
 * giounix.c: IO Channels using unix file descriptors
Packit ae235b
 * Copyright 1998 Owen Taylor
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
Packit ae235b
 * file for a list of people on the GLib Team.  See the ChangeLog
Packit ae235b
 * files for a list of changes.  These files are distributed with
Packit ae235b
 * GLib at ftp://ftp.gtk.org/pub/gtk/.
Packit ae235b
 */
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * MT safe
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#define _POSIX_SOURCE		/* for SSIZE_MAX */
Packit ae235b
Packit ae235b
#include <sys/types.h>
Packit ae235b
#include <sys/stat.h>
Packit ae235b
#include <stdio.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
#include <errno.h>
Packit ae235b
#include <string.h>
Packit ae235b
#include <fcntl.h>
Packit ae235b
#include <glib/gstdio.h>
Packit ae235b
Packit ae235b
#include "giochannel.h"
Packit ae235b
Packit ae235b
#include "gerror.h"
Packit ae235b
#include "gfileutils.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gtestutils.h"
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * Unix IO Channels
Packit ae235b
 */
Packit ae235b
Packit ae235b
typedef struct _GIOUnixChannel GIOUnixChannel;
Packit ae235b
typedef struct _GIOUnixWatch GIOUnixWatch;
Packit ae235b
Packit ae235b
struct _GIOUnixChannel
Packit ae235b
{
Packit ae235b
  GIOChannel channel;
Packit ae235b
  gint fd;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GIOUnixWatch
Packit ae235b
{
Packit ae235b
  GSource       source;
Packit ae235b
  GPollFD       pollfd;
Packit ae235b
  GIOChannel   *channel;
Packit ae235b
  GIOCondition  condition;
Packit ae235b
};
Packit ae235b
Packit ae235b
Packit ae235b
static GIOStatus	g_io_unix_read		(GIOChannel   *channel,
Packit ae235b
						 gchar        *buf,
Packit ae235b
						 gsize         count,
Packit ae235b
						 gsize        *bytes_read,
Packit ae235b
						 GError      **err);
Packit ae235b
static GIOStatus	g_io_unix_write		(GIOChannel   *channel,
Packit ae235b
						 const gchar  *buf,
Packit ae235b
						 gsize         count,
Packit ae235b
						 gsize        *bytes_written,
Packit ae235b
						 GError      **err);
Packit ae235b
static GIOStatus	g_io_unix_seek		(GIOChannel   *channel,
Packit ae235b
						 gint64        offset,
Packit ae235b
						 GSeekType     type,
Packit ae235b
						 GError      **err);
Packit ae235b
static GIOStatus	g_io_unix_close		(GIOChannel   *channel,
Packit ae235b
						 GError      **err);
Packit ae235b
static void		g_io_unix_free		(GIOChannel   *channel);
Packit ae235b
static GSource*		g_io_unix_create_watch	(GIOChannel   *channel,
Packit ae235b
						 GIOCondition  condition);
Packit ae235b
static GIOStatus	g_io_unix_set_flags	(GIOChannel   *channel,
Packit ae235b
                       				 GIOFlags      flags,
Packit ae235b
						 GError      **err);
Packit ae235b
static GIOFlags 	g_io_unix_get_flags	(GIOChannel   *channel);
Packit ae235b
Packit ae235b
static gboolean g_io_unix_prepare  (GSource     *source,
Packit ae235b
				    gint        *timeout);
Packit ae235b
static gboolean g_io_unix_check    (GSource     *source);
Packit ae235b
static gboolean g_io_unix_dispatch (GSource     *source,
Packit ae235b
				    GSourceFunc  callback,
Packit ae235b
				    gpointer     user_data);
Packit ae235b
static void     g_io_unix_finalize (GSource     *source);
Packit ae235b
Packit ae235b
GSourceFuncs g_io_watch_funcs = {
Packit ae235b
  g_io_unix_prepare,
Packit ae235b
  g_io_unix_check,
Packit ae235b
  g_io_unix_dispatch,
Packit ae235b
  g_io_unix_finalize
Packit ae235b
};
Packit ae235b
Packit ae235b
static GIOFuncs unix_channel_funcs = {
Packit ae235b
  g_io_unix_read,
Packit ae235b
  g_io_unix_write,
Packit ae235b
  g_io_unix_seek,
Packit ae235b
  g_io_unix_close,
Packit ae235b
  g_io_unix_create_watch,
Packit ae235b
  g_io_unix_free,
Packit ae235b
  g_io_unix_set_flags,
Packit ae235b
  g_io_unix_get_flags,
Packit ae235b
};
Packit ae235b
Packit ae235b
static gboolean 
Packit ae235b
g_io_unix_prepare (GSource  *source,
Packit ae235b
		   gint     *timeout)
Packit ae235b
{
Packit ae235b
  GIOUnixWatch *watch = (GIOUnixWatch *)source;
Packit ae235b
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
Packit ae235b
Packit ae235b
  *timeout = -1;
Packit ae235b
Packit ae235b
  /* Only return TRUE here if _all_ bits in watch->condition will be set
Packit ae235b
   */
Packit ae235b
  return ((watch->condition & buffer_condition) == watch->condition);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean 
Packit ae235b
g_io_unix_check (GSource  *source)
Packit ae235b
{
Packit ae235b
  GIOUnixWatch *watch = (GIOUnixWatch *)source;
Packit ae235b
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
Packit ae235b
  GIOCondition poll_condition = watch->pollfd.revents;
Packit ae235b
Packit ae235b
  return ((poll_condition | buffer_condition) & watch->condition);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
g_io_unix_dispatch (GSource     *source,
Packit ae235b
		    GSourceFunc  callback,
Packit ae235b
		    gpointer     user_data)
Packit ae235b
Packit ae235b
{
Packit ae235b
  GIOFunc func = (GIOFunc)callback;
Packit ae235b
  GIOUnixWatch *watch = (GIOUnixWatch *)source;
Packit ae235b
  GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
Packit ae235b
Packit ae235b
  if (!func)
Packit ae235b
    {
Packit ae235b
      g_warning ("IO watch dispatched without callback\n"
Packit ae235b
		 "You must call g_source_connect().");
Packit ae235b
      return FALSE;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  return (*func) (watch->channel,
Packit ae235b
		  (watch->pollfd.revents | buffer_condition) & watch->condition,
Packit ae235b
		  user_data);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void 
Packit ae235b
g_io_unix_finalize (GSource *source)
Packit ae235b
{
Packit ae235b
  GIOUnixWatch *watch = (GIOUnixWatch *)source;
Packit ae235b
Packit ae235b
  g_io_channel_unref (watch->channel);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_unix_read (GIOChannel *channel, 
Packit ae235b
		gchar      *buf, 
Packit ae235b
		gsize       count,
Packit ae235b
		gsize      *bytes_read,
Packit ae235b
		GError    **err)
Packit ae235b
{
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
Packit ae235b
  gssize result;
Packit ae235b
Packit ae235b
  if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
Packit ae235b
    count = SSIZE_MAX;
Packit ae235b
Packit ae235b
 retry:
Packit ae235b
  result = read (unix_channel->fd, buf, count);
Packit ae235b
Packit ae235b
  if (result < 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      *bytes_read = 0;
Packit ae235b
Packit ae235b
      switch (errsv)
Packit ae235b
        {
Packit ae235b
#ifdef EINTR
Packit ae235b
          case EINTR:
Packit ae235b
            goto retry;
Packit ae235b
#endif
Packit ae235b
#ifdef EAGAIN
Packit ae235b
          case EAGAIN:
Packit ae235b
            return G_IO_STATUS_AGAIN;
Packit ae235b
#endif
Packit ae235b
          default:
Packit ae235b
            g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                                 g_io_channel_error_from_errno (errsv),
Packit ae235b
                                 g_strerror (errsv));
Packit ae235b
            return G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *bytes_read = result;
Packit ae235b
Packit ae235b
  return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_unix_write (GIOChannel  *channel, 
Packit ae235b
		 const gchar *buf, 
Packit ae235b
		 gsize       count,
Packit ae235b
		 gsize      *bytes_written,
Packit ae235b
		 GError    **err)
Packit ae235b
{
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
Packit ae235b
  gssize result;
Packit ae235b
Packit ae235b
 retry:
Packit ae235b
  result = write (unix_channel->fd, buf, count);
Packit ae235b
Packit ae235b
  if (result < 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      *bytes_written = 0;
Packit ae235b
Packit ae235b
      switch (errsv)
Packit ae235b
        {
Packit ae235b
#ifdef EINTR
Packit ae235b
          case EINTR:
Packit ae235b
            goto retry;
Packit ae235b
#endif
Packit ae235b
#ifdef EAGAIN
Packit ae235b
          case EAGAIN:
Packit ae235b
            return G_IO_STATUS_AGAIN;
Packit ae235b
#endif
Packit ae235b
          default:
Packit ae235b
            g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                                 g_io_channel_error_from_errno (errsv),
Packit ae235b
                                 g_strerror (errsv));
Packit ae235b
            return G_IO_STATUS_ERROR;
Packit ae235b
        }
Packit ae235b
    }
Packit ae235b
Packit ae235b
  *bytes_written = result;
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_unix_seek (GIOChannel *channel,
Packit ae235b
		gint64      offset, 
Packit ae235b
		GSeekType   type,
Packit ae235b
                GError    **err)
Packit ae235b
{
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
Packit ae235b
  int whence;
Packit ae235b
  off_t tmp_offset;
Packit ae235b
  off_t result;
Packit ae235b
Packit ae235b
  switch (type)
Packit ae235b
    {
Packit ae235b
    case G_SEEK_SET:
Packit ae235b
      whence = SEEK_SET;
Packit ae235b
      break;
Packit ae235b
    case G_SEEK_CUR:
Packit ae235b
      whence = SEEK_CUR;
Packit ae235b
      break;
Packit ae235b
    case G_SEEK_END:
Packit ae235b
      whence = SEEK_END;
Packit ae235b
      break;
Packit ae235b
    default:
Packit ae235b
      whence = -1; /* Shut the compiler up */
Packit ae235b
      g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  tmp_offset = offset;
Packit ae235b
  if (tmp_offset != offset)
Packit ae235b
    {
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                           g_io_channel_error_from_errno (EINVAL),
Packit ae235b
                           g_strerror (EINVAL));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
  
Packit ae235b
  result = lseek (unix_channel->fd, tmp_offset, whence);
Packit ae235b
Packit ae235b
  if (result < 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                           g_io_channel_error_from_errno (errsv),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_unix_close (GIOChannel *channel,
Packit ae235b
		 GError    **err)
Packit ae235b
{
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
Packit ae235b
Packit ae235b
  if (close (unix_channel->fd) < 0)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                           g_io_channel_error_from_errno (errsv),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void 
Packit ae235b
g_io_unix_free (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
Packit ae235b
Packit ae235b
  g_free (unix_channel);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GSource *
Packit ae235b
g_io_unix_create_watch (GIOChannel   *channel,
Packit ae235b
			GIOCondition  condition)
Packit ae235b
{
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
Packit ae235b
  GSource *source;
Packit ae235b
  GIOUnixWatch *watch;
Packit ae235b
Packit ae235b
Packit ae235b
  source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
Packit ae235b
  g_source_set_name (source, "GIOChannel (Unix)");
Packit ae235b
  watch = (GIOUnixWatch *)source;
Packit ae235b
  
Packit ae235b
  watch->channel = channel;
Packit ae235b
  g_io_channel_ref (channel);
Packit ae235b
  
Packit ae235b
  watch->condition = condition;
Packit ae235b
Packit ae235b
  watch->pollfd.fd = unix_channel->fd;
Packit ae235b
  watch->pollfd.events = condition;
Packit ae235b
Packit ae235b
  g_source_add_poll (source, &watch->pollfd);
Packit ae235b
Packit ae235b
  return source;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOStatus
Packit ae235b
g_io_unix_set_flags (GIOChannel *channel,
Packit ae235b
                     GIOFlags    flags,
Packit ae235b
                     GError    **err)
Packit ae235b
{
Packit ae235b
  glong fcntl_flags;
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
Packit ae235b
Packit ae235b
  fcntl_flags = 0;
Packit ae235b
Packit ae235b
  if (flags & G_IO_FLAG_APPEND)
Packit ae235b
    fcntl_flags |= O_APPEND;
Packit ae235b
  if (flags & G_IO_FLAG_NONBLOCK)
Packit ae235b
#ifdef O_NONBLOCK
Packit ae235b
    fcntl_flags |= O_NONBLOCK;
Packit ae235b
#else
Packit ae235b
    fcntl_flags |= O_NDELAY;
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
Packit ae235b
    {
Packit ae235b
      int errsv = errno;
Packit ae235b
      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
Packit ae235b
                           g_io_channel_error_from_errno (errsv),
Packit ae235b
                           g_strerror (errsv));
Packit ae235b
      return G_IO_STATUS_ERROR;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return G_IO_STATUS_NORMAL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static GIOFlags
Packit ae235b
g_io_unix_get_flags (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOFlags flags = 0;
Packit ae235b
  glong fcntl_flags;
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
Packit ae235b
Packit ae235b
  fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
Packit ae235b
Packit ae235b
  if (fcntl_flags == -1)
Packit ae235b
    {
Packit ae235b
      int err = errno;
Packit ae235b
      g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
Packit ae235b
		 g_strerror (err), err);
Packit ae235b
      return 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (fcntl_flags & O_APPEND)
Packit ae235b
    flags |= G_IO_FLAG_APPEND;
Packit ae235b
#ifdef O_NONBLOCK
Packit ae235b
  if (fcntl_flags & O_NONBLOCK)
Packit ae235b
#else
Packit ae235b
  if (fcntl_flags & O_NDELAY)
Packit ae235b
#endif
Packit ae235b
    flags |= G_IO_FLAG_NONBLOCK;
Packit ae235b
Packit ae235b
  switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
Packit ae235b
    {
Packit ae235b
      case O_RDONLY:
Packit ae235b
        channel->is_readable = TRUE;
Packit ae235b
        channel->is_writeable = FALSE;
Packit ae235b
        break;
Packit ae235b
      case O_WRONLY:
Packit ae235b
        channel->is_readable = FALSE;
Packit ae235b
        channel->is_writeable = TRUE;
Packit ae235b
        break;
Packit ae235b
      case O_RDWR:
Packit ae235b
        channel->is_readable = TRUE;
Packit ae235b
        channel->is_writeable = TRUE;
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return flags;
Packit ae235b
}
Packit ae235b
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_new_file (const gchar *filename,
Packit ae235b
                       const gchar *mode,
Packit ae235b
                       GError     **error)
Packit ae235b
{
Packit ae235b
  int fid, flags;
Packit ae235b
  mode_t create_mode;
Packit ae235b
  GIOChannel *channel;
Packit ae235b
  enum { /* Cheesy hack */
Packit ae235b
    MODE_R = 1 << 0,
Packit ae235b
    MODE_W = 1 << 1,
Packit ae235b
    MODE_A = 1 << 2,
Packit ae235b
    MODE_PLUS = 1 << 3,
Packit ae235b
    MODE_R_PLUS = MODE_R | MODE_PLUS,
Packit ae235b
    MODE_W_PLUS = MODE_W | MODE_PLUS,
Packit ae235b
    MODE_A_PLUS = MODE_A | MODE_PLUS
Packit ae235b
  } mode_num;
Packit ae235b
  struct stat buffer;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (filename != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (mode != NULL, NULL);
Packit ae235b
  g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
Packit ae235b
Packit ae235b
  switch (mode[0])
Packit ae235b
    {
Packit ae235b
      case 'r':
Packit ae235b
        mode_num = MODE_R;
Packit ae235b
        break;
Packit ae235b
      case 'w':
Packit ae235b
        mode_num = MODE_W;
Packit ae235b
        break;
Packit ae235b
      case 'a':
Packit ae235b
        mode_num = MODE_A;
Packit ae235b
        break;
Packit ae235b
      default:
Packit ae235b
        g_warning ("Invalid GIOFileMode %s.\n", mode);
Packit ae235b
        return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  switch (mode[1])
Packit ae235b
    {
Packit ae235b
      case '\0':
Packit ae235b
        break;
Packit ae235b
      case '+':
Packit ae235b
        if (mode[2] == '\0')
Packit ae235b
          {
Packit ae235b
            mode_num |= MODE_PLUS;
Packit ae235b
            break;
Packit ae235b
          }
Packit ae235b
        /* Fall through */
Packit ae235b
      default:
Packit ae235b
        g_warning ("Invalid GIOFileMode %s.\n", mode);
Packit ae235b
        return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  switch (mode_num)
Packit ae235b
    {
Packit ae235b
      case MODE_R:
Packit ae235b
        flags = O_RDONLY;
Packit ae235b
        break;
Packit ae235b
      case MODE_W:
Packit ae235b
        flags = O_WRONLY | O_TRUNC | O_CREAT;
Packit ae235b
        break;
Packit ae235b
      case MODE_A:
Packit ae235b
        flags = O_WRONLY | O_APPEND | O_CREAT;
Packit ae235b
        break;
Packit ae235b
      case MODE_R_PLUS:
Packit ae235b
        flags = O_RDWR;
Packit ae235b
        break;
Packit ae235b
      case MODE_W_PLUS:
Packit ae235b
        flags = O_RDWR | O_TRUNC | O_CREAT;
Packit ae235b
        break;
Packit ae235b
      case MODE_A_PLUS:
Packit ae235b
        flags = O_RDWR | O_APPEND | O_CREAT;
Packit ae235b
        break;
Packit ae235b
      case MODE_PLUS:
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
        flags = 0;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
Packit ae235b
Packit ae235b
  fid = g_open (filename, flags, create_mode);
Packit ae235b
  if (fid == -1)
Packit ae235b
    {
Packit ae235b
      int err = errno;
Packit ae235b
      g_set_error_literal (error, G_FILE_ERROR,
Packit ae235b
                           g_file_error_from_errno (err),
Packit ae235b
                           g_strerror (err));
Packit ae235b
      return (GIOChannel *)NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
Packit ae235b
    {
Packit ae235b
      int err = errno;
Packit ae235b
      close (fid);
Packit ae235b
      g_set_error_literal (error, G_FILE_ERROR,
Packit ae235b
                           g_file_error_from_errno (err),
Packit ae235b
                           g_strerror (err));
Packit ae235b
      return (GIOChannel *)NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
Packit ae235b
Packit ae235b
  channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
Packit ae235b
                         || S_ISBLK (buffer.st_mode);
Packit ae235b
Packit ae235b
  switch (mode_num)
Packit ae235b
    {
Packit ae235b
      case MODE_R:
Packit ae235b
        channel->is_readable = TRUE;
Packit ae235b
        channel->is_writeable = FALSE;
Packit ae235b
        break;
Packit ae235b
      case MODE_W:
Packit ae235b
      case MODE_A:
Packit ae235b
        channel->is_readable = FALSE;
Packit ae235b
        channel->is_writeable = TRUE;
Packit ae235b
        break;
Packit ae235b
      case MODE_R_PLUS:
Packit ae235b
      case MODE_W_PLUS:
Packit ae235b
      case MODE_A_PLUS:
Packit ae235b
        channel->is_readable = TRUE;
Packit ae235b
        channel->is_writeable = TRUE;
Packit ae235b
        break;
Packit ae235b
      case MODE_PLUS:
Packit ae235b
      default:
Packit ae235b
        g_assert_not_reached ();
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_io_channel_init (channel);
Packit ae235b
  channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
Packit ae235b
  channel->funcs = &unix_channel_funcs;
Packit ae235b
Packit ae235b
  ((GIOUnixChannel *) channel)->fd = fid;
Packit ae235b
  return channel;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_unix_new:
Packit ae235b
 * @fd: a file descriptor.
Packit ae235b
 *
Packit ae235b
 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
Packit ae235b
 * this works for plain files, pipes, and sockets.
Packit ae235b
 *
Packit ae235b
 * The returned #GIOChannel has a reference count of 1.
Packit ae235b
 *
Packit ae235b
 * The default encoding for #GIOChannel is UTF-8. If your application
Packit ae235b
 * is reading output from a command using via pipe, you may need to set
Packit ae235b
 * the encoding to the encoding of the current locale (see
Packit ae235b
 * g_get_charset()) with the g_io_channel_set_encoding() function.
Packit ae235b
 * By default, the fd passed will not be closed when the final reference
Packit ae235b
 * to the #GIOChannel data structure is dropped.
Packit ae235b
 *
Packit ae235b
 * If you want to read raw binary data without interpretation, then
Packit ae235b
 * call the g_io_channel_set_encoding() function with %NULL for the
Packit ae235b
 * encoding argument.
Packit ae235b
 *
Packit ae235b
 * This function is available in GLib on Windows, too, but you should
Packit ae235b
 * avoid using it on Windows. The domain of file descriptors and
Packit ae235b
 * sockets overlap. There is no way for GLib to know which one you mean
Packit ae235b
 * in case the argument you pass to this function happens to be both a
Packit ae235b
 * valid file descriptor and socket. If that happens a warning is
Packit ae235b
 * issued, and GLib assumes that it is the file descriptor you mean.
Packit ae235b
 *
Packit ae235b
 * Returns: a new #GIOChannel.
Packit ae235b
 **/
Packit ae235b
GIOChannel *
Packit ae235b
g_io_channel_unix_new (gint fd)
Packit ae235b
{
Packit ae235b
  struct stat buffer;
Packit ae235b
  GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
Packit ae235b
  GIOChannel *channel = (GIOChannel *)unix_channel;
Packit ae235b
Packit ae235b
  g_io_channel_init (channel);
Packit ae235b
  channel->funcs = &unix_channel_funcs;
Packit ae235b
Packit ae235b
  unix_channel->fd = fd;
Packit ae235b
Packit ae235b
  /* I'm not sure if fstat on a non-file (e.g., socket) works
Packit ae235b
   * it should be safe to say if it fails, the fd isn't seekable.
Packit ae235b
   */
Packit ae235b
  /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
Packit ae235b
   * succeed in most cases.
Packit ae235b
   */
Packit ae235b
  if (fstat (unix_channel->fd, &buffer) == 0)
Packit ae235b
    channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
Packit ae235b
                           || S_ISBLK (buffer.st_mode);
Packit ae235b
  else /* Assume not seekable */
Packit ae235b
    channel->is_seekable = FALSE;
Packit ae235b
Packit ae235b
  g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
Packit ae235b
Packit ae235b
  return channel;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_io_channel_unix_get_fd:
Packit ae235b
 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
Packit ae235b
 *
Packit ae235b
 * Returns the file descriptor of the #GIOChannel.
Packit ae235b
 *
Packit ae235b
 * On Windows this function returns the file descriptor or socket of
Packit ae235b
 * the #GIOChannel.
Packit ae235b
 *
Packit ae235b
 * Returns: the file descriptor of the #GIOChannel.
Packit ae235b
 **/
Packit ae235b
gint
Packit ae235b
g_io_channel_unix_get_fd (GIOChannel *channel)
Packit ae235b
{
Packit ae235b
  GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
Packit ae235b
  return unix_channel->fd;
Packit ae235b
}