ia64/xen-unstable

view tools/vnet/libxutil/file_stream.c @ 19848:5839491bbf20

[IA64] replace MAX_VCPUS with d->max_vcpus where necessary.

don't use MAX_VCPUS, and use vcpu::max_vcpus.
The changeset of 2f9e1348aa98 introduced max_vcpus to allow more vcpus
per guest. This patch is ia64 counter part.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Mon Jun 29 11:26:05 2009 +0900 (2009-06-29)
parents 9f22db685802
children
line source
1 /*
2 * Copyright (C) 2001 - 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 FILE*.
21 */
22 #ifndef __KERNEL__
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include "allocate.h"
26 #include "file_stream.h"
28 static int file_read(IOStream *s, void *buf, size_t n);
29 static int file_write(IOStream *s, const void *buf, size_t n);
30 static int file_error(IOStream *s);
31 static int file_close(IOStream *s);
32 static void file_free(IOStream *s);
33 static int file_flush(IOStream *s);
35 /** Methods used by a FILE* IOStream. */
36 static const IOMethods file_methods = {
37 read: file_read,
38 write: file_write,
39 error: file_error,
40 close: file_close,
41 free: file_free,
42 flush: file_flush,
43 };
45 /** IOStream for stdin. */
46 static IOStream _iostdin = {
47 methods: &file_methods,
48 data: (void*)1,
49 nofree: 1,
50 };
52 /** IOStream for stdout. */
53 static IOStream _iostdout = {
54 methods: &file_methods,
55 data: (void*)2,
56 nofree: 1,
57 };
59 /** IOStream for stderr. */
60 static IOStream _iostderr = {
61 methods: &file_methods,
62 data: (void*)3,
63 nofree: 1,
64 };
66 /** IOStream for stdin. */
67 IOStream *iostdin = &_iostdin;
69 /** IOStream for stdout. */
70 IOStream *iostdout = &_iostdout;
72 /** IOStream for stderr. */
73 IOStream *iostderr = &_iostderr;
75 /* Get the underlying FILE*.
76 *
77 * @param s file stream
78 * @return the stream s wraps
79 */
80 static inline FILE *get_file(IOStream *s){
81 FILE *data = NULL;
82 switch((long)s->data){
83 case 1:
84 data = stdin;
85 break;
86 case 2:
87 data = stdout;
88 break;
89 case 3:
90 data = stderr;
91 break;
92 default:
93 data = (FILE*)s->data;
94 break;
95 }
96 return data;
97 }
99 /** Control buffering on the underlying stream, like setvbuf().
100 *
101 * @param io file stream
102 * @param buf buffer
103 * @param mode buffering mode (see man setvbuf())
104 * @param size buffer size
105 * @return 0 on success, non-zero otherwise
106 */
107 int file_stream_setvbuf(IOStream *io, char *buf, int mode, size_t size){
108 return setvbuf(get_file(io), buf, mode, size);
109 }
111 /** Write to the underlying stream using fwrite();
112 *
113 * @param stream input
114 * @param buf where to put input
115 * @param n number of bytes to write
116 * @return number of bytes written
117 */
118 static int file_write(IOStream *s, const void *buf, size_t n){
119 return fwrite(buf, 1, n, get_file(s));
120 }
122 /** Read from the underlying stream using fread();
123 *
124 * @param stream input
125 * @param buf where to put input
126 * @param n number of bytes to read
127 * @return number of bytes read
128 */
129 static int file_read(IOStream *s, void *buf, size_t n){
130 return fread(buf, 1, n, get_file(s));
131 }
133 /** Fush the underlying stream using fflush().
134 *
135 * @param s file stream
136 * @return 0 on success, error code otherwise
137 */
138 static int file_flush(IOStream *s){
139 return fflush(get_file(s));
140 }
142 /** Check if a stream has an error.
143 *
144 * @param s file stream
145 * @return 1 if has an error, 0 otherwise
146 */
147 static int file_error(IOStream *s){
148 return ferror(get_file(s));
149 }
151 /** Close a file stream.
152 *
153 * @param s file stream to close
154 * @return result of the close
155 */
156 static int file_close(IOStream *s){
157 int result = 0;
158 result = fclose(get_file(s));
159 return result;
160 }
162 /** Free a file stream.
163 *
164 * @param s file stream
165 */
166 static void file_free(IOStream *s){
167 // Nothing extra to do - close did it all.
168 }
170 /** Create an IOStream for a stream.
171 *
172 * @param f stream to wrap
173 * @return new IOStream using f for i/o
174 */
175 IOStream *file_stream_new(FILE *f){
176 IOStream *io = ALLOCATE(IOStream);
177 if(io){
178 io->methods = &file_methods;
179 io->data = (void*)f;
180 }
181 return io;
182 }
184 /** IOStream version of fopen().
185 *
186 * @param file name of the file to open
187 * @param flags giving the mode to open in (as for fopen())
188 * @return new stream for the open file, or 0 if failed
189 */
190 IOStream *file_stream_fopen(const char *file, const char *flags){
191 IOStream *io = 0;
192 FILE *fin = fopen(file, flags);
193 if(fin){
194 io = file_stream_new(fin);
195 if(!io){
196 fclose(fin);
197 }
198 }
199 return io;
200 }
202 /** IOStream version of fdopen().
203 *
204 * @param fd file descriptor
205 * @param flags giving the mode to open in (as for fdopen())
206 * @return new stream for the open file, or 0 if failed. Always takes
207 * ownership of fd.
208 */
209 IOStream *file_stream_fdopen(int fd, const char *flags){
210 IOStream *io = 0;
211 FILE *fin = fdopen(fd, flags);
212 if(fin){
213 io = file_stream_new(fin);
214 if(!io){
215 fclose(fin);
216 }
217 }
218 return io;
219 }
220 #endif