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 {
44 struct _GIOUnixWatch {
47 GIOCondition condition;
52 static GIOError g_io_unix_read (GIOChannel *channel,
55 guint *bytes_written);
57 static GIOError g_io_unix_write(GIOChannel *channel,
60 guint *bytes_written);
61 static GIOError g_io_unix_seek (GIOChannel *channel,
64 static void g_io_unix_close (GIOChannel *channel);
65 static void g_io_unix_free (GIOChannel *channel);
66 static guint g_io_unix_add_watch (GIOChannel *channel,
68 GIOCondition condition,
71 GDestroyNotify notify);
72 static gboolean g_io_unix_prepare (gpointer source_data,
73 GTimeVal *current_time,
75 static gboolean g_io_unix_check (gpointer source_data,
76 GTimeVal *current_time);
77 static gboolean g_io_unix_dispatch (gpointer source_data,
78 GTimeVal *current_time,
80 static void g_io_unix_destroy (gpointer source_data);
82 GSourceFuncs unix_watch_funcs = {
89 GIOFuncs unix_channel_funcs = {
99 g_io_unix_prepare (gpointer source_data,
100 GTimeVal *current_time,
108 g_io_unix_check (gpointer source_data,
109 GTimeVal *current_time)
111 GIOUnixWatch *data = source_data;
113 return (data->pollfd.revents & data->condition);
117 g_io_unix_dispatch (gpointer source_data,
118 GTimeVal *current_time,
122 GIOUnixWatch *data = source_data;
124 return (*data->callback)(data->channel,
125 data->pollfd.revents & data->condition,
130 g_io_unix_destroy (gpointer source_data)
132 GIOUnixWatch *data = source_data;
134 g_main_remove_poll (&data->pollfd);
135 g_io_channel_unref (data->channel);
140 g_io_unix_read (GIOChannel *channel,
145 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
148 result = read (unix_channel->fd, buf, count);
156 return G_IO_ERROR_INVAL;
158 return G_IO_ERROR_AGAIN;
160 return G_IO_ERROR_UNKNOWN;
165 *bytes_read = result;
166 return G_IO_ERROR_NONE;
171 g_io_unix_write(GIOChannel *channel,
174 guint *bytes_written)
176 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
179 result = write (unix_channel->fd, buf, count);
187 return G_IO_ERROR_INVAL;
189 return G_IO_ERROR_AGAIN;
191 return G_IO_ERROR_UNKNOWN;
196 *bytes_written = result;
197 return G_IO_ERROR_NONE;
202 g_io_unix_seek (GIOChannel *channel,
206 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
222 g_warning ("g_io_unix_seek: unknown seek type");
223 return G_IO_ERROR_UNKNOWN;
226 result = lseek (unix_channel->fd, offset, whence);
233 return G_IO_ERROR_INVAL;
235 return G_IO_ERROR_UNKNOWN;
239 return G_IO_ERROR_NONE;
244 g_io_unix_close (GIOChannel *channel)
246 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
248 close (unix_channel->fd);
252 g_io_unix_free (GIOChannel *channel)
254 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
256 g_free (unix_channel);
260 g_io_unix_add_watch (GIOChannel *channel,
262 GIOCondition condition,
265 GDestroyNotify notify)
267 GIOUnixWatch *watch = g_new (GIOUnixWatch, 1);
268 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
270 watch->channel = channel;
271 g_io_channel_ref (channel);
273 watch->callback = func;
274 watch->condition = condition;
276 watch->pollfd.fd = unix_channel->fd;
277 watch->pollfd.events = condition;
279 g_main_add_poll (&watch->pollfd, priority);
281 return g_source_add (priority, TRUE, &unix_watch_funcs, watch, user_data, notify);
285 g_io_channel_unix_new (gint fd)
287 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
288 GIOChannel *channel = (GIOChannel *)unix_channel;
290 g_io_channel_init (channel);
291 channel->funcs = &unix_channel_funcs;
293 unix_channel->fd = fd;
298 g_io_channel_unix_get_fd (GIOChannel *channel)
300 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
301 return unix_channel->fd;