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.
28 #include <sys/types.h>
36 typedef struct _GIOUnixChannel GIOUnixChannel;
37 typedef struct _GIOUnixWatch GIOUnixWatch;
39 struct _GIOUnixChannel {
43 struct _GIOUnixWatch {
46 GIOCondition condition;
51 static GIOError g_io_unix_read (GIOChannel *channel,
54 guint *bytes_written);
56 static GIOError g_io_unix_write(GIOChannel *channel,
59 guint *bytes_written);
60 static GIOError g_io_unix_seek (GIOChannel *channel,
63 static void g_io_unix_close (GIOChannel *channel);
64 static void g_io_unix_free (GIOChannel *channel);
65 static guint g_io_unix_add_watch (GIOChannel *channel,
67 GIOCondition condition,
70 GDestroyNotify notify);
71 static gboolean g_io_unix_prepare (gpointer source_data,
72 GTimeVal *current_time,
74 static gboolean g_io_unix_check (gpointer source_data,
75 GTimeVal *current_time);
76 static gboolean g_io_unix_dispatch (gpointer source_data,
77 GTimeVal *current_time,
79 static void g_io_unix_destroy (gpointer source_data);
81 GSourceFuncs unix_watch_funcs = {
88 GIOFuncs unix_channel_funcs = {
98 g_io_unix_prepare (gpointer source_data,
99 GTimeVal *current_time,
107 g_io_unix_check (gpointer source_data,
108 GTimeVal *current_time)
110 GIOUnixWatch *data = source_data;
112 return (data->pollfd.revents & data->condition);
116 g_io_unix_dispatch (gpointer source_data,
117 GTimeVal *current_time,
121 GIOUnixWatch *data = source_data;
123 return (*data->callback)(data->channel,
124 data->pollfd.revents & data->condition,
129 g_io_unix_destroy (gpointer source_data)
131 GIOUnixWatch *data = source_data;
133 g_main_poll_remove (&data->pollfd);
134 g_io_channel_unref (data->channel);
139 g_io_unix_read (GIOChannel *channel,
144 GIOUnixChannel *unix_channel = channel->channel_data;
147 result = read (unix_channel->fd, buf, count);
155 return G_IO_ERROR_INVAL;
157 return G_IO_ERROR_AGAIN;
159 return G_IO_ERROR_UNKNOWN;
164 *bytes_read = result;
165 return G_IO_ERROR_NONE;
170 g_io_unix_write(GIOChannel *channel,
173 guint *bytes_written)
175 GIOUnixChannel *unix_channel = channel->channel_data;
178 result = write (unix_channel->fd, buf, count);
186 return G_IO_ERROR_INVAL;
188 return G_IO_ERROR_AGAIN;
190 return G_IO_ERROR_UNKNOWN;
195 *bytes_written = result;
196 return G_IO_ERROR_NONE;
201 g_io_unix_seek (GIOChannel *channel,
205 GIOUnixChannel *unix_channel = channel->channel_data;
221 g_warning ("g_io_unix_seek: unknown seek type");
222 return G_IO_ERROR_UNKNOWN;
225 result = lseek (unix_channel->fd, offset, whence);
232 return G_IO_ERROR_INVAL;
234 return G_IO_ERROR_UNKNOWN;
238 return G_IO_ERROR_NONE;
243 g_io_unix_close (GIOChannel *channel)
245 GIOUnixChannel *unix_channel = channel->channel_data;
247 close (unix_channel->fd);
251 g_io_unix_free (GIOChannel *channel)
253 GIOUnixChannel *unix_channel = channel->channel_data;
255 g_free (unix_channel);
259 g_io_unix_add_watch (GIOChannel *channel,
261 GIOCondition condition,
264 GDestroyNotify notify)
266 GIOUnixWatch *watch = g_new (GIOUnixWatch, 1);
267 GIOUnixChannel *unix_channel = channel->channel_data;
269 watch->channel = channel;
270 g_io_channel_ref (channel);
272 watch->callback = func;
273 watch->condition = condition;
275 watch->pollfd.fd = unix_channel->fd;
276 watch->pollfd.events = condition;
278 g_main_poll_add (priority, &watch->pollfd);
280 return g_source_add (priority, TRUE, &unix_watch_funcs, watch, user_data, notify);
284 g_io_channel_unix_new (gint fd)
286 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
288 unix_channel->fd = fd;
289 return g_io_channel_new (&unix_channel_funcs, unix_channel);
293 g_io_channel_unix_get_fd (GIOChannel *channel)
295 GIOUnixChannel *unix_channel = channel->channel_data;
296 return unix_channel->fd;