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 Lesser 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 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser 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-2000. 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
,
83 static gboolean
g_io_unix_check (gpointer source_data
,
84 GTimeVal
*current_time
,
86 static gboolean
g_io_unix_dispatch (gpointer source_data
,
87 GTimeVal
*current_time
,
89 static void g_io_unix_destroy (gpointer source_data
);
91 GSourceFuncs unix_watch_funcs
= {
98 GIOFuncs unix_channel_funcs
= {
108 g_io_unix_prepare (gpointer source_data
,
109 GTimeVal
*current_time
,
118 g_io_unix_check (gpointer source_data
,
119 GTimeVal
*current_time
,
122 GIOUnixWatch
*data
= source_data
;
124 return (data
->pollfd
.revents
& data
->condition
);
128 g_io_unix_dispatch (gpointer source_data
,
129 GTimeVal
*current_time
,
133 GIOUnixWatch
*data
= source_data
;
135 return (*data
->callback
)(data
->channel
,
136 data
->pollfd
.revents
& data
->condition
,
141 g_io_unix_destroy (gpointer source_data
)
143 GIOUnixWatch
*data
= source_data
;
145 g_main_remove_poll (&data
->pollfd
);
146 g_io_channel_unref (data
->channel
);
151 g_io_unix_read (GIOChannel
*channel
,
156 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
159 result
= read (unix_channel
->fd
, buf
, count
);
167 return G_IO_ERROR_INVAL
;
170 return G_IO_ERROR_AGAIN
;
172 return G_IO_ERROR_UNKNOWN
;
177 *bytes_read
= result
;
178 return G_IO_ERROR_NONE
;
183 g_io_unix_write(GIOChannel
*channel
,
186 guint
*bytes_written
)
188 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
191 result
= write (unix_channel
->fd
, buf
, count
);
199 return G_IO_ERROR_INVAL
;
202 return G_IO_ERROR_AGAIN
;
204 return G_IO_ERROR_UNKNOWN
;
209 *bytes_written
= result
;
210 return G_IO_ERROR_NONE
;
215 g_io_unix_seek (GIOChannel
*channel
,
219 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
235 g_warning ("g_io_unix_seek: unknown seek type");
236 return G_IO_ERROR_UNKNOWN
;
239 result
= lseek (unix_channel
->fd
, offset
, whence
);
246 return G_IO_ERROR_INVAL
;
248 return G_IO_ERROR_UNKNOWN
;
252 return G_IO_ERROR_NONE
;
257 g_io_unix_close (GIOChannel
*channel
)
259 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
261 close (unix_channel
->fd
);
265 g_io_unix_free (GIOChannel
*channel
)
267 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
269 g_free (unix_channel
);
273 g_io_unix_add_watch (GIOChannel
*channel
,
275 GIOCondition condition
,
278 GDestroyNotify notify
)
280 GIOUnixWatch
*watch
= g_new (GIOUnixWatch
, 1);
281 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
283 watch
->channel
= channel
;
284 g_io_channel_ref (channel
);
286 watch
->callback
= func
;
287 watch
->condition
= condition
;
289 watch
->pollfd
.fd
= unix_channel
->fd
;
290 watch
->pollfd
.events
= condition
;
292 g_main_add_poll (&watch
->pollfd
, priority
);
294 return g_source_add (priority
, TRUE
, &unix_watch_funcs
, watch
, user_data
, notify
);
298 g_io_channel_unix_new (gint fd
)
300 GIOUnixChannel
*unix_channel
= g_new (GIOUnixChannel
, 1);
301 GIOChannel
*channel
= (GIOChannel
*)unix_channel
;
303 g_io_channel_init (channel
);
304 channel
->funcs
= &unix_channel_funcs
;
306 unix_channel
->fd
= fd
;
311 g_io_channel_unix_get_fd (GIOChannel
*channel
)
313 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
314 return unix_channel
->fd
;