diff options
-rw-r--r-- | eglib/gmain.c | 46 | ||||
-rw-r--r-- | eglib/gmain.h | 43 |
2 files changed, 74 insertions, 15 deletions
diff --git a/eglib/gmain.c b/eglib/gmain.c index b51da600..8071c62b 100644 --- a/eglib/gmain.c +++ b/eglib/gmain.c @@ -201,6 +201,52 @@ gint g_io_channel_unix_get_fd(GIOChannel *channel) return channel->fd; } +static int set_flags(int fd, long flags) +{ + long arg; + + arg = fcntl(fd, F_GETFL); + if (arg < 0) + return -errno; + + /* Return if already set */ + if ((arg & flags) == flags) + return 0; + + arg |= flags; + if (fcntl(fd, F_SETFL, arg) < 0) + return -errno; + + return 0; +} + +GIOStatus g_io_channel_set_flags(GIOChannel *channel, GIOFlags flags, + GError **error) +{ + int err, fd; + long fd_flags = 0; + + if (!channel || channel->closed) + return G_IO_STATUS_ERROR; + + fd = g_io_channel_unix_get_fd(channel); + + if (flags & G_IO_FLAG_APPEND) + fd_flags |= O_APPEND; + if (flags & G_IO_FLAG_NONBLOCK) + fd_flags |= O_NONBLOCK; + + err = set_flags(fd, fd_flags); + if (err < 0) { + if (error) + g_set_error(error, 0, 0, "Unable to set flags: %s", + strerror(-err)); + return G_IO_STATUS_ERROR; + } + + return G_IO_STATUS_NORMAL; +} + struct io_watch { guint id; GIOChannel *channel; diff --git a/eglib/gmain.h b/eglib/gmain.h index 15b6c072..b7b93072 100644 --- a/eglib/gmain.h +++ b/eglib/gmain.h @@ -58,6 +58,17 @@ typedef enum { G_IO_STATUS_AGAIN = 2 } GIOStatus; +typedef enum { + G_IO_FLAG_APPEND = 1 << 0, + G_IO_FLAG_NONBLOCK = 1 << 1, + G_IO_FLAG_IS_READABLE = 1 << 2, + G_IO_FLAG_IS_WRITEABLE = 1 << 3, + G_IO_FLAG_IS_SEEKABLE = 1 << 4, + G_IO_FLAG_MASK = (1 << 5) - 1, + G_IO_FLAG_GET_MASK = G_IO_FLAG_MASK, + G_IO_FLAG_SET_MASK = G_IO_FLAG_APPEND | G_IO_FLAG_NONBLOCK +} GIOFlags; + #ifndef FALSE #define FALSE (0) #endif @@ -78,6 +89,21 @@ typedef enum { #undef CLAMP #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x))) +/* GError */ + +typedef guint32 GQuark; + +typedef struct { + GQuark domain; + gint code; + gchar *message; +} GError; + +void g_set_error(GError **err, GQuark domain, gint code, + const gchar *format, ...); +GError* g_error_new_literal(GQuark domain, gint code, const gchar *message); +void g_error_free(GError *err); + typedef enum { G_IO_IN = POLLIN, G_IO_OUT = POLLOUT, @@ -109,6 +135,8 @@ GIOChannel *g_io_channel_ref(GIOChannel *channel); void g_io_channel_unref(GIOChannel *channel); void g_io_channel_set_close_on_unref(GIOChannel *channel, gboolean do_close); gint g_io_channel_unix_get_fd(GIOChannel *channel); +GIOStatus g_io_channel_set_flags(GIOChannel *channel, GIOFlags flags, + GError **error); guint g_io_add_watch(GIOChannel *channel, GIOCondition condition, GIOFunc func, gpointer user_data); guint g_io_add_watch_full(GIOChannel *channel, gint priority, @@ -123,21 +151,6 @@ guint g_timeout_add(guint interval, GSourceFunc function, gpointer data); guint g_idle_add(GSourceFunc function, gpointer data); gboolean g_source_remove(guint tag); -/* GError */ - -typedef guint32 GQuark; - -typedef struct { - GQuark domain; - gint code; - gchar *message; -} GError; - -void g_set_error(GError **err, GQuark domain, gint code, - const gchar *format, ...); -GError* g_error_new_literal(GQuark domain, gint code, const gchar *message); -void g_error_free(GError *err); - /* Spawning related functions */ typedef enum { |