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
57 GIOCondition condition
;
62 static GIOError
g_io_unix_read (GIOChannel
*channel
,
65 guint
*bytes_written
);
66 static GIOError
g_io_unix_write (GIOChannel
*channel
,
69 guint
*bytes_written
);
70 static GIOError
g_io_unix_seek (GIOChannel
*channel
,
73 static void g_io_unix_close (GIOChannel
*channel
);
74 static void g_io_unix_free (GIOChannel
*channel
);
75 static GSource
*g_io_unix_create_watch (GIOChannel
*channel
,
76 GIOCondition condition
);
78 static gboolean
g_io_unix_prepare (GSource
*source
,
80 static gboolean
g_io_unix_check (GSource
*source
);
81 static gboolean
g_io_unix_dispatch (GSource
*source
,
84 static void g_io_unix_destroy (GSource
*source
);
86 GSourceFuncs unix_watch_funcs
= {
93 GIOFuncs unix_channel_funcs
= {
98 g_io_unix_create_watch
,
103 g_io_unix_prepare (GSource
*source
,
111 g_io_unix_check (GSource
*source
)
113 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
115 return (watch
->pollfd
.revents
& watch
->condition
);
119 g_io_unix_dispatch (GSource
*source
,
120 GSourceFunc callback
,
124 GIOFunc func
= (GIOFunc
)callback
;
125 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
129 g_warning ("IO watch dispatched without callback\n"
130 "You must call g_source_connect().");
134 return (*func
) (watch
->channel
,
135 watch
->pollfd
.revents
& watch
->condition
,
140 g_io_unix_destroy (GSource
*source
)
142 GIOUnixWatch
*watch
= (GIOUnixWatch
*)source
;
144 g_io_channel_unref (watch
->channel
);
148 g_io_unix_read (GIOChannel
*channel
,
153 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
156 result
= read (unix_channel
->fd
, buf
, count
);
164 return G_IO_ERROR_INVAL
;
167 return G_IO_ERROR_AGAIN
;
169 return G_IO_ERROR_UNKNOWN
;
174 *bytes_read
= result
;
175 return G_IO_ERROR_NONE
;
180 g_io_unix_write(GIOChannel
*channel
,
183 guint
*bytes_written
)
185 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
188 result
= write (unix_channel
->fd
, buf
, count
);
196 return G_IO_ERROR_INVAL
;
199 return G_IO_ERROR_AGAIN
;
201 return G_IO_ERROR_UNKNOWN
;
206 *bytes_written
= result
;
207 return G_IO_ERROR_NONE
;
212 g_io_unix_seek (GIOChannel
*channel
,
216 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
232 g_warning ("g_io_unix_seek: unknown seek type");
233 return G_IO_ERROR_UNKNOWN
;
236 result
= lseek (unix_channel
->fd
, offset
, whence
);
243 return G_IO_ERROR_INVAL
;
245 return G_IO_ERROR_UNKNOWN
;
249 return G_IO_ERROR_NONE
;
254 g_io_unix_close (GIOChannel
*channel
)
256 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
258 close (unix_channel
->fd
);
262 g_io_unix_free (GIOChannel
*channel
)
264 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
266 g_free (unix_channel
);
270 g_io_unix_create_watch (GIOChannel
*channel
,
271 GIOCondition condition
)
273 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
278 source
= g_source_new (&unix_watch_funcs
, sizeof (GIOUnixWatch
));
279 watch
= (GIOUnixWatch
*)source
;
281 watch
->channel
= channel
;
282 g_io_channel_ref (channel
);
284 watch
->condition
= condition
;
286 watch
->pollfd
.fd
= unix_channel
->fd
;
287 watch
->pollfd
.events
= condition
;
289 g_source_add_poll (source
, &watch
->pollfd
);
295 g_io_channel_unix_new (gint fd
)
297 GIOUnixChannel
*unix_channel
= g_new (GIOUnixChannel
, 1);
298 GIOChannel
*channel
= (GIOChannel
*)unix_channel
;
300 g_io_channel_init (channel
);
301 channel
->funcs
= &unix_channel_funcs
;
303 unix_channel
->fd
= fd
;
308 g_io_channel_unix_get_fd (GIOChannel
*channel
)
310 GIOUnixChannel
*unix_channel
= (GIOUnixChannel
*)channel
;
311 return unix_channel
->fd
;