1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giounix.c: IO Channels using unix file descriptors
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
35 #include <sys/types.h>
43 typedef struct _GIOUnixChannel GIOUnixChannel;
44 typedef struct _GIOUnixWatch GIOUnixWatch;
46 struct _GIOUnixChannel {
51 struct _GIOUnixWatch {
54 GIOCondition condition;
59 static GIOError g_io_unix_read (GIOChannel *channel,
62 guint *bytes_written);
64 static GIOError g_io_unix_write(GIOChannel *channel,
67 guint *bytes_written);
68 static GIOError g_io_unix_seek (GIOChannel *channel,
71 static void g_io_unix_close (GIOChannel *channel);
72 static void g_io_unix_free (GIOChannel *channel);
73 static guint g_io_unix_add_watch (GIOChannel *channel,
75 GIOCondition condition,
78 GDestroyNotify notify);
79 static gboolean g_io_unix_prepare (gpointer source_data,
80 GTimeVal *current_time,
82 static gboolean g_io_unix_check (gpointer source_data,
83 GTimeVal *current_time);
84 static gboolean g_io_unix_dispatch (gpointer source_data,
85 GTimeVal *current_time,
87 static void g_io_unix_destroy (gpointer source_data);
89 GSourceFuncs unix_watch_funcs = {
96 GIOFuncs unix_channel_funcs = {
106 g_io_unix_prepare (gpointer source_data,
107 GTimeVal *current_time,
115 g_io_unix_check (gpointer source_data,
116 GTimeVal *current_time)
118 GIOUnixWatch *data = source_data;
120 return (data->pollfd.revents & data->condition);
124 g_io_unix_dispatch (gpointer source_data,
125 GTimeVal *current_time,
129 GIOUnixWatch *data = source_data;
131 return (*data->callback)(data->channel,
132 data->pollfd.revents & data->condition,
137 g_io_unix_destroy (gpointer source_data)
139 GIOUnixWatch *data = source_data;
141 g_main_remove_poll (&data->pollfd);
142 g_io_channel_unref (data->channel);
147 g_io_unix_read (GIOChannel *channel,
152 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
155 result = read (unix_channel->fd, buf, count);
163 return G_IO_ERROR_INVAL;
165 return G_IO_ERROR_AGAIN;
167 return G_IO_ERROR_UNKNOWN;
172 *bytes_read = result;
173 return G_IO_ERROR_NONE;
178 g_io_unix_write(GIOChannel *channel,
181 guint *bytes_written)
183 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
186 result = write (unix_channel->fd, buf, count);
194 return G_IO_ERROR_INVAL;
196 return G_IO_ERROR_AGAIN;
198 return G_IO_ERROR_UNKNOWN;
203 *bytes_written = result;
204 return G_IO_ERROR_NONE;
209 g_io_unix_seek (GIOChannel *channel,
213 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
229 g_warning ("g_io_unix_seek: unknown seek type");
230 return G_IO_ERROR_UNKNOWN;
233 result = lseek (unix_channel->fd, offset, whence);
240 return G_IO_ERROR_INVAL;
242 return G_IO_ERROR_UNKNOWN;
246 return G_IO_ERROR_NONE;
251 g_io_unix_close (GIOChannel *channel)
253 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
255 close (unix_channel->fd);
259 g_io_unix_free (GIOChannel *channel)
261 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
263 g_free (unix_channel);
267 g_io_unix_add_watch (GIOChannel *channel,
269 GIOCondition condition,
272 GDestroyNotify notify)
274 GIOUnixWatch *watch = g_new (GIOUnixWatch, 1);
275 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
277 watch->channel = channel;
278 g_io_channel_ref (channel);
280 watch->callback = func;
281 watch->condition = condition;
283 watch->pollfd.fd = unix_channel->fd;
284 watch->pollfd.events = condition;
286 g_main_add_poll (&watch->pollfd, priority);
288 return g_source_add (priority, TRUE, &unix_watch_funcs, watch, user_data, notify);
292 g_io_channel_unix_new (gint fd)
294 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
295 GIOChannel *channel = (GIOChannel *)unix_channel;
297 g_io_channel_init (channel);
298 channel->funcs = &unix_channel_funcs;
300 unix_channel->fd = fd;
305 g_io_channel_unix_get_fd (GIOChannel *channel)
307 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
308 return unix_channel->fd;