ia64/xen-unstable

view tools/vnet/libxutil/fd_stream.c @ 6766:219d96d545fc

merge?
author cl349@firebug.cl.cam.ac.uk
date Mon Sep 12 20:00:41 2005 +0000 (2005-09-12)
parents 9f22db685802
children
line source
1 /*
2 * Copyright (C) 2004 Mike Wray <mike.wray@hp.com>
3 *
4 * This library is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
19 /** @file
20 * An IOStream implementation using fds.
21 */
22 #ifndef __KERNEL__
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 #include "allocate.h"
30 #include "fd_stream.h"
32 #define MODULE_NAME "fd_stream"
33 #define DEBUG 1
34 //#undef DEBUG
35 #include "debug.h"
37 static int fd_read(IOStream *s, void *buf, size_t n);
38 static int fd_write(IOStream *s, const void *buf, size_t n);
39 static int fd_error(IOStream *s);
40 static int fd_close(IOStream *s);
41 static void fd_free(IOStream *s);
42 static int fd_flush(IOStream *s);
44 /** Methods used by a fd IOStream. */
45 static const IOMethods fd_methods = {
46 read: fd_read,
47 write: fd_write,
48 error: fd_error,
49 close: fd_close,
50 free: fd_free,
51 flush: fd_flush,
52 };
54 /** Get the fd data.
55 *
56 * @param io fd stream
57 * @return data
58 */
59 static inline FDData * fd_data(IOStream *io){
60 return (FDData *)io->data;
61 }
63 /** Test if a stream is a fd stream.
64 *
65 * @param io stream
66 * @return 0 if a fd stream, -EINVAL if not
67 */
68 int fd_stream_check(IOStream *io){
69 return (io && io->methods == &fd_methods ? 0 : -EINVAL);
70 }
72 /** Get the data for a fd stream.
73 *
74 * @param io stream
75 * @param data return value for the data
76 * @return 0 if a fd stream, -EINVAL if not
77 */
78 int fd_stream_data(IOStream *io, FDData **data){
79 int err = fd_stream_check(io);
80 if(err){
81 *data = NULL;
82 } else {
83 *data = fd_data(io);
84 }
85 return err;
86 }
89 /** Write to the underlying fd.
90 *
91 * @param stream input
92 * @param buf where to put input
93 * @param n number of bytes to write
94 * @return number of bytes written
95 */
96 static int fd_write(IOStream *s, const void *buf, size_t n){
97 FDData *data = fd_data(s);
98 int k;
99 k = write(data->fd, buf, n);
100 return k;
101 }
103 /** Read from the underlying stream;
104 *
105 * @param stream input
106 * @param buf where to put input
107 * @param n number of bytes to read
108 * @return number of bytes read
109 */
110 static int fd_read(IOStream *s, void *buf, size_t n){
111 FDData *data = fd_data(s);
112 int k;
113 k = read(data->fd, buf, n);
114 //printf("> fd_read> buf=%p n=%d --> k=%d\n", buf, n, k);
115 return k;
116 }
118 /** Flush the fd (no-op).
119 *
120 * @param s fd stream
121 * @return 0 on success, error code otherwise
122 */
123 static int fd_flush(IOStream *s){
124 return 0;
125 }
127 /** Check if a fd stream has an error (no-op).
128 *
129 * @param s fd stream
130 * @return 1 if has an error, 0 otherwise
131 */
132 static int fd_error(IOStream *s){
133 return 0;
134 }
136 /** Close a fd stream.
137 *
138 * @param s fd stream to close
139 * @return result of the close
140 */
141 static int fd_close(IOStream *s){
142 FDData *data = fd_data(s);
143 return close(data->fd);
144 }
146 /** Free a fd stream.
147 *
148 * @param s fd stream
149 */
150 static void fd_free(IOStream *s){
151 FDData *data = fd_data(s);
152 deallocate(data);
153 }
155 /** Create an IOStream for a fd.
156 *
157 * @param fd fd to wtap
158 * @return new IOStream using fd for i/o
159 */
160 IOStream *fd_stream_new(int fd){
161 int err = -ENOMEM;
162 IOStream *io = NULL;
163 FDData *data = NULL;
165 io = ALLOCATE(IOStream);
166 if(!io) goto exit;
167 io->methods = &fd_methods;
168 data = ALLOCATE(FDData);
169 if(!data) goto exit;
170 io->data = data;
171 data->fd = fd;
172 err = 0;
173 exit:
174 if(err){
175 if(io){
176 if(data) deallocate(data);
177 deallocate(io);
178 io = NULL;
179 }
180 }
181 return io;
182 }
184 #endif