[590] | 1 | //vio.c: |
---|
| 2 | |
---|
[690] | 3 | /* |
---|
[1284] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009 |
---|
[690] | 5 | * |
---|
| 6 | * This file is part of libroar a part of RoarAudio, |
---|
| 7 | * a cross-platform sound system for both, home and professional use. |
---|
| 8 | * See README for details. |
---|
| 9 | * |
---|
| 10 | * This file is free software; you can redistribute it and/or modify |
---|
| 11 | * it under the terms of the GNU General Public License version 3 |
---|
| 12 | * as published by the Free Software Foundation. |
---|
| 13 | * |
---|
| 14 | * libroar is distributed in the hope that it will be useful, |
---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 17 | * GNU General Public License for more details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License |
---|
| 20 | * along with this software; see the file COPYING. If not, write to |
---|
| 21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 22 | * |
---|
| 23 | * NOTE for everyone want's to change something and send patches: |
---|
| 24 | * read README and HACKING! There a addition information on |
---|
| 25 | * the license of this document you need to read before you send |
---|
| 26 | * any patches. |
---|
| 27 | * |
---|
| 28 | * NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc |
---|
| 29 | * or libpulse*: |
---|
| 30 | * The libs libroaresd, libroararts and libroarpulse link this lib |
---|
| 31 | * and are therefore GPL. Because of this it may be illigal to use |
---|
| 32 | * them with any software that uses libesd, libartsc or libpulse*. |
---|
| 33 | */ |
---|
| 34 | |
---|
[590] | 35 | #include "libroar.h" |
---|
| 36 | |
---|
[591] | 37 | int roar_vio_init_calls (struct roar_vio_calls * calls) { |
---|
[1118] | 38 | if ( calls == NULL ) |
---|
[591] | 39 | return -1; |
---|
| 40 | |
---|
| 41 | memset((void*)calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 42 | |
---|
[881] | 43 | /* |
---|
[597] | 44 | calls->read = (ssize_t (*)(int fd, void *buf, size_t count, void * inst))read; |
---|
| 45 | calls->write = (ssize_t (*)(int fd, void *buf, size_t count, void * inst))write; |
---|
| 46 | calls->lseek = (off_t (*)(int fildes, off_t offset, int whence, void * inst))lseek; |
---|
[881] | 47 | */ |
---|
| 48 | |
---|
[1118] | 49 | calls->read = roar_vio_basic_read; |
---|
| 50 | calls->write = roar_vio_basic_write; |
---|
| 51 | calls->lseek = roar_vio_basic_lseek; |
---|
| 52 | calls->nonblock = roar_vio_basic_nonblock; |
---|
| 53 | calls->sync = roar_vio_basic_sync; |
---|
[1241] | 54 | calls->close = roar_vio_basic_close; |
---|
[881] | 55 | |
---|
| 56 | return 0; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) { |
---|
| 60 | if ( vio == NULL ) |
---|
| 61 | return -1; |
---|
| 62 | |
---|
| 63 | vio->inst = inst; |
---|
[591] | 64 | |
---|
| 65 | return 0; |
---|
| 66 | } |
---|
| 67 | |
---|
[881] | 68 | int roar_vio_set_fh (struct roar_vio_calls * vio, int fh) { |
---|
[989] | 69 | return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1)); |
---|
[881] | 70 | } |
---|
| 71 | |
---|
| 72 | int roar_vio_get_fh (struct roar_vio_calls * vio) { |
---|
| 73 | if ( vio == NULL ) |
---|
| 74 | return -1; |
---|
| 75 | |
---|
[989] | 76 | return ((int)(ROAR_INSTINT)vio->inst) - 1; |
---|
[881] | 77 | } |
---|
| 78 | |
---|
| 79 | |
---|
| 80 | ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
[1319] | 81 | ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count); |
---|
| 82 | |
---|
[881] | 83 | if ( vio == NULL ) |
---|
| 84 | return -1; |
---|
| 85 | |
---|
| 86 | if ( vio->read == NULL ) |
---|
| 87 | return -1; |
---|
| 88 | |
---|
| 89 | return vio->read(vio, buf, count); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 93 | if ( vio == NULL ) |
---|
| 94 | return -1; |
---|
| 95 | |
---|
| 96 | if ( vio->write == NULL ) |
---|
| 97 | return -1; |
---|
| 98 | |
---|
| 99 | return vio->write(vio, buf, count); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | off_t roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 103 | if ( vio == NULL ) |
---|
| 104 | return -1; |
---|
| 105 | |
---|
| 106 | if ( vio->lseek == NULL ) |
---|
| 107 | return -1; |
---|
| 108 | |
---|
| 109 | return vio->lseek(vio, offset, whence); |
---|
| 110 | } |
---|
| 111 | |
---|
[1118] | 112 | int roar_vio_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 113 | if ( vio == NULL ) |
---|
| 114 | return -1; |
---|
| 115 | |
---|
| 116 | if ( vio->nonblock == NULL ) |
---|
| 117 | return -1; |
---|
| 118 | |
---|
| 119 | return vio->nonblock(vio, state); |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | int roar_vio_sync (struct roar_vio_calls * vio) { |
---|
| 123 | if ( vio == NULL ) |
---|
| 124 | return -1; |
---|
| 125 | |
---|
| 126 | if ( vio->sync == NULL ) |
---|
| 127 | return -1; |
---|
| 128 | |
---|
| 129 | return vio->sync(vio); |
---|
| 130 | } |
---|
| 131 | |
---|
[1140] | 132 | int roar_vio_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 133 | if ( vio == NULL ) |
---|
| 134 | return -1; |
---|
| 135 | |
---|
| 136 | if ( vio->ctl == NULL ) |
---|
| 137 | return -1; |
---|
| 138 | |
---|
| 139 | return vio->ctl(vio, cmd, data); |
---|
| 140 | } |
---|
| 141 | |
---|
[1241] | 142 | int roar_vio_close (struct roar_vio_calls * vio) { |
---|
| 143 | if ( vio == NULL ) |
---|
| 144 | return -1; |
---|
| 145 | |
---|
[1258] | 146 | if ( vio->close == NULL ) |
---|
[1241] | 147 | return -1; |
---|
| 148 | |
---|
| 149 | return vio->close(vio); |
---|
| 150 | } |
---|
| 151 | |
---|
[1314] | 152 | int roar_vio_putc (struct roar_vio_calls * vio, char c) { |
---|
| 153 | return roar_vio_write(vio, &c, 1); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | int roar_vio_getc (struct roar_vio_calls * vio) { |
---|
| 157 | unsigned char c; |
---|
| 158 | |
---|
| 159 | if ( roar_vio_read(vio, &c, 1) != 1 ) |
---|
| 160 | return EOF; |
---|
| 161 | |
---|
| 162 | return c; |
---|
| 163 | } |
---|
| 164 | |
---|
[1351] | 165 | int roar_vio_printf(struct roar_vio_calls * vio, const char *format, ...) { |
---|
| 166 | va_list ap; |
---|
| 167 | int ret; |
---|
| 168 | char buf[8192]; |
---|
| 169 | |
---|
| 170 | va_start(ap, format); |
---|
| 171 | ret = vsnprintf(buf, 8192, format, ap); |
---|
| 172 | va_end(ap); |
---|
| 173 | |
---|
| 174 | return roar_vio_write(vio, buf, ret); |
---|
| 175 | } |
---|
| 176 | |
---|
[1252] | 177 | // converters: |
---|
| 178 | int roar_vio_open_file (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) { |
---|
| 179 | int fh; |
---|
| 180 | |
---|
| 181 | if ( calls == NULL || filename == NULL ) |
---|
| 182 | return -1; |
---|
| 183 | |
---|
| 184 | if ( (fh = open(filename, flags, mode)) == -1 ) |
---|
| 185 | return -1; |
---|
| 186 | |
---|
| 187 | if ( roar_vio_open_fh(calls, fh) == -1 ) { |
---|
| 188 | close(fh); |
---|
| 189 | return -1; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | return 0; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | int roar_vio_open_fh (struct roar_vio_calls * calls, int fh) { |
---|
| 196 | if ( calls == NULL ) |
---|
| 197 | return -1; |
---|
| 198 | |
---|
| 199 | if ( roar_vio_init_calls(calls) == -1 ) |
---|
| 200 | return -1; |
---|
| 201 | |
---|
| 202 | return roar_vio_set_fh(calls, fh); |
---|
| 203 | } |
---|
| 204 | |
---|
[1290] | 205 | int roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) { |
---|
| 206 | if ( calls == NULL ) |
---|
| 207 | return -1; |
---|
| 208 | |
---|
| 209 | return roar_vio_open_fh(calls, fh); |
---|
| 210 | } |
---|
| 211 | |
---|
[1291] | 212 | int roar_vio_open_socket (struct roar_vio_calls * calls, char * host, int port) { |
---|
| 213 | int fh; |
---|
| 214 | |
---|
| 215 | if ( calls == NULL ) |
---|
| 216 | return -1; |
---|
| 217 | |
---|
| 218 | if ( (fh = roar_socket_connect(host, port)) == -1 ) |
---|
| 219 | return -1; |
---|
| 220 | |
---|
| 221 | return roar_vio_open_fh_socket(calls, fh); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | int roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) { |
---|
| 225 | int fh; |
---|
| 226 | |
---|
| 227 | if ( calls == NULL ) |
---|
| 228 | return -1; |
---|
| 229 | |
---|
| 230 | if ( (fh = roar_socket_listen(type, host, port)) == -1 ) |
---|
| 231 | return -1; |
---|
| 232 | |
---|
| 233 | return roar_vio_open_fh_socket(calls, fh); |
---|
| 234 | } |
---|
| 235 | |
---|
[1275] | 236 | int roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec, |
---|
| 237 | char * server, int dir, char * name) { |
---|
| 238 | int fh; |
---|
| 239 | |
---|
| 240 | if ( calls == NULL ) |
---|
| 241 | return -1; |
---|
| 242 | |
---|
| 243 | if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 ) |
---|
| 244 | return -1; |
---|
| 245 | |
---|
[1290] | 246 | return roar_vio_open_fh_socket(calls, fh); |
---|
[1275] | 247 | } |
---|
| 248 | |
---|
| 249 | |
---|
[1252] | 250 | int roar_vio_open_stdio (struct roar_vio_calls * calls, FILE * dst) { |
---|
| 251 | if ( calls == NULL || dst == NULL ) |
---|
| 252 | return -1; |
---|
| 253 | |
---|
[1253] | 254 | memset(calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 255 | |
---|
| 256 | calls->read = roar_vio_stdio_read; |
---|
| 257 | calls->write = roar_vio_stdio_write; |
---|
| 258 | calls->lseek = roar_vio_stdio_lseek; |
---|
| 259 | calls->sync = roar_vio_stdio_sync; |
---|
| 260 | calls->close = roar_vio_stdio_close; |
---|
| 261 | |
---|
| 262 | calls->inst = dst; |
---|
| 263 | |
---|
| 264 | return 0; |
---|
[1252] | 265 | } |
---|
| 266 | |
---|
| 267 | FILE * roar_vio_to_stdio (struct roar_vio_calls * calls, int flags) { |
---|
| 268 | #ifdef ROAR_HAVE_FOPENCOOKIE |
---|
| 269 | cookie_io_functions_t foc_funcs; |
---|
| 270 | #endif |
---|
| 271 | |
---|
| 272 | if ( calls == NULL ) |
---|
| 273 | return NULL; |
---|
| 274 | |
---|
| 275 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 276 | memset(&foc_funcs, 0, sizeof(cookie_io_functions_t)); |
---|
| 277 | |
---|
[1293] | 278 | foc_funcs.close = roar_vio_to_stdio_close; |
---|
| 279 | foc_funcs.read = roar_vio_to_stdio_read; |
---|
| 280 | foc_funcs.write = roar_vio_to_stdio_write; |
---|
| 281 | |
---|
[1252] | 282 | return fopencookie((void*) calls, "rw", foc_funcs); |
---|
| 283 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
[1293] | 284 | return funopen((void*) calls, roar_vio_to_stdio_read, roar_vio_to_stdio_write, |
---|
| 285 | roar_vio_to_stdio_lseek, roar_vio_to_stdio_close); |
---|
[1252] | 286 | #else |
---|
| 287 | return NULL; |
---|
| 288 | #endif |
---|
| 289 | } |
---|
| 290 | |
---|
[1293] | 291 | #if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN) |
---|
| 292 | int roar_vio_to_stdio_close (void *__cookie) { |
---|
| 293 | return roar_vio_close((struct roar_vio_calls *) __cookie); |
---|
| 294 | } |
---|
| 295 | #endif |
---|
| 296 | |
---|
| 297 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 298 | __ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) { |
---|
| 299 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 300 | int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) { |
---|
| 301 | #endif |
---|
| 302 | return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 306 | __ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) { |
---|
| 307 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 308 | int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) { |
---|
| 309 | #endif |
---|
| 310 | return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n); |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | #if defined(ROAR_HAVE_FOPENCOOKIE) |
---|
| 314 | int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w); |
---|
| 315 | #elif defined(ROAR_HAVE_FUNOPEN) |
---|
| 316 | fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) { |
---|
| 317 | return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w); |
---|
| 318 | } |
---|
| 319 | #endif |
---|
| 320 | |
---|
[886] | 321 | // VIOs: |
---|
[881] | 322 | |
---|
[886] | 323 | // basic |
---|
[881] | 324 | ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 325 | return read(roar_vio_get_fh(vio), buf, count); |
---|
| 326 | } |
---|
| 327 | |
---|
| 328 | ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 329 | return write(roar_vio_get_fh(vio), buf, count); |
---|
| 330 | } |
---|
| 331 | |
---|
| 332 | off_t roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 333 | return lseek(roar_vio_get_fh(vio), offset, whence); |
---|
| 334 | } |
---|
| 335 | |
---|
[1118] | 336 | int roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 337 | if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 ) |
---|
| 338 | return -1; |
---|
| 339 | |
---|
| 340 | if ( state == ROAR_SOCKET_NONBLOCK ) |
---|
| 341 | return 0; |
---|
| 342 | |
---|
[1125] | 343 | roar_vio_sync(vio); |
---|
| 344 | |
---|
| 345 | return 0; |
---|
[1118] | 346 | } |
---|
| 347 | |
---|
| 348 | int roar_vio_basic_sync (struct roar_vio_calls * vio) { |
---|
[1171] | 349 | return ROAR_FDATASYNC(roar_vio_get_fh(vio)); |
---|
[1118] | 350 | } |
---|
| 351 | |
---|
[1241] | 352 | int roar_vio_basic_close (struct roar_vio_calls * vio) { |
---|
[1336] | 353 | if ( roar_vio_get_fh(vio) != -1 ) |
---|
| 354 | return close(roar_vio_get_fh(vio)); |
---|
| 355 | |
---|
| 356 | return 0; |
---|
[1241] | 357 | } |
---|
| 358 | |
---|
[943] | 359 | // null |
---|
| 360 | ssize_t roar_vio_null_rw (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 361 | if ( vio == NULL || buf == NULL ) |
---|
| 362 | return -1; |
---|
| 363 | |
---|
| 364 | return 0; |
---|
| 365 | } |
---|
| 366 | |
---|
[886] | 367 | // pass |
---|
[1247] | 368 | int roar_vio_open_pass (struct roar_vio_calls * calls, struct roar_vio_calls * dst) { |
---|
| 369 | if ( calls == NULL ) |
---|
| 370 | return -1; |
---|
| 371 | |
---|
| 372 | memset((void*)calls, 0, sizeof(struct roar_vio_calls)); |
---|
| 373 | |
---|
| 374 | calls->read = roar_vio_pass_read; |
---|
| 375 | calls->write = roar_vio_pass_write; |
---|
| 376 | calls->lseek = roar_vio_pass_lseek; |
---|
| 377 | calls->nonblock = roar_vio_pass_nonblock; |
---|
| 378 | calls->sync = roar_vio_pass_sync; |
---|
| 379 | calls->close = roar_vio_pass_close; |
---|
| 380 | |
---|
| 381 | calls->inst = dst; |
---|
| 382 | |
---|
| 383 | return 0; |
---|
| 384 | } |
---|
| 385 | |
---|
[886] | 386 | ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 387 | return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count); |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 391 | return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count); |
---|
| 392 | } |
---|
| 393 | |
---|
| 394 | off_t roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 395 | return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence); |
---|
| 396 | } |
---|
| 397 | |
---|
[1241] | 398 | int roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) { |
---|
| 399 | return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state); |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | int roar_vio_pass_sync (struct roar_vio_calls * vio) { |
---|
| 403 | return roar_vio_sync((struct roar_vio_calls *) vio->inst); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | int roar_vio_pass_ctl (struct roar_vio_calls * vio, int cmd, void * data) { |
---|
| 407 | return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data); |
---|
| 408 | } |
---|
| 409 | |
---|
| 410 | int roar_vio_pass_close (struct roar_vio_calls * vio) { |
---|
| 411 | return roar_vio_close((struct roar_vio_calls *) vio->inst); |
---|
| 412 | } |
---|
| 413 | |
---|
[886] | 414 | |
---|
| 415 | // re |
---|
[1247] | 416 | int roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) { |
---|
| 417 | if ( roar_vio_open_pass(calls, dst) == -1 ) |
---|
| 418 | return -1; |
---|
| 419 | |
---|
| 420 | calls->read = roar_vio_re_read; |
---|
| 421 | calls->write = roar_vio_re_write; |
---|
| 422 | calls->lseek = roar_vio_re_lseek; |
---|
| 423 | |
---|
| 424 | return 0; |
---|
| 425 | } |
---|
[886] | 426 | ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 427 | size_t len = 0; |
---|
| 428 | ssize_t r = -1; |
---|
| 429 | |
---|
| 430 | if ( vio == NULL ) |
---|
| 431 | return -1; |
---|
| 432 | |
---|
| 433 | if ( vio->inst == NULL ) |
---|
| 434 | return -1; |
---|
| 435 | |
---|
| 436 | errno = 0; |
---|
| 437 | |
---|
| 438 | while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) { |
---|
| 439 | len += r; |
---|
| 440 | buf += r; |
---|
| 441 | count -= r; |
---|
| 442 | if ( count == 0 ) |
---|
| 443 | break; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | if ( len == 0 && r == -1 ) |
---|
| 447 | return -1; |
---|
| 448 | |
---|
| 449 | return len; |
---|
| 450 | } |
---|
| 451 | |
---|
| 452 | ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 453 | size_t len = 0; |
---|
| 454 | ssize_t r = -1; |
---|
| 455 | |
---|
| 456 | if ( vio == NULL ) |
---|
| 457 | return -1; |
---|
| 458 | |
---|
| 459 | if ( vio->inst == NULL ) |
---|
| 460 | return -1; |
---|
| 461 | |
---|
| 462 | errno = 0; |
---|
| 463 | |
---|
| 464 | while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) { |
---|
| 465 | len += r; |
---|
| 466 | buf += r; |
---|
| 467 | count -= r; |
---|
| 468 | if ( count == 0 ) |
---|
| 469 | break; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | if ( len == 0 && r == -1 ) |
---|
| 473 | return -1; |
---|
| 474 | |
---|
| 475 | return len; |
---|
| 476 | } |
---|
| 477 | |
---|
[1247] | 478 | // TODO: we should do a some more intelgent thing here. |
---|
[886] | 479 | off_t roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 480 | return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence); |
---|
| 481 | } |
---|
| 482 | |
---|
[1253] | 483 | // stdio: |
---|
| 484 | ssize_t roar_vio_stdio_read (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 485 | return fread(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 486 | } |
---|
| 487 | |
---|
| 488 | ssize_t roar_vio_stdio_write (struct roar_vio_calls * vio, void *buf, size_t count) { |
---|
| 489 | return fwrite(buf, 1, count, (FILE*)(vio->inst)); |
---|
| 490 | } |
---|
| 491 | |
---|
| 492 | off_t roar_vio_stdio_lseek (struct roar_vio_calls * vio, off_t offset, int whence) { |
---|
| 493 | if ( fseek((FILE*)(vio->inst), offset, whence) == -1 ) |
---|
| 494 | return -1; |
---|
| 495 | |
---|
| 496 | return ftell((FILE*)(vio->inst)); |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | int roar_vio_stdio_sync (struct roar_vio_calls * vio) { |
---|
| 500 | return fflush((FILE*)(vio->inst)); |
---|
| 501 | } |
---|
| 502 | |
---|
| 503 | int roar_vio_stdio_close (struct roar_vio_calls * vio) { |
---|
| 504 | return fclose((FILE*)(vio->inst)); |
---|
| 505 | } |
---|
[886] | 506 | |
---|
[590] | 507 | //ll |
---|