| 1 | /*
|
|---|
| 2 | * Copyright (c) 2005 Martin Decky
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup libc
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #include <stdio.h>
|
|---|
| 36 | #include <assert.h>
|
|---|
| 37 | #include <str.h>
|
|---|
| 38 | #include <errno.h>
|
|---|
| 39 | #include <stdbool.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 | #include <async.h>
|
|---|
| 42 | #include <io/kio.h>
|
|---|
| 43 | #include <vfs/vfs.h>
|
|---|
| 44 | #include <vfs/vfs_sess.h>
|
|---|
| 45 | #include <vfs/inbox.h>
|
|---|
| 46 | #include <ipc/loc.h>
|
|---|
| 47 | #include <adt/list.h>
|
|---|
| 48 | #include <wchar.h>
|
|---|
| 49 | #include "../private/io.h"
|
|---|
| 50 | #include "../private/stdio.h"
|
|---|
| 51 |
|
|---|
| 52 | static void _ffillbuf(FILE *stream);
|
|---|
| 53 | static void _fflushbuf(FILE *stream);
|
|---|
| 54 |
|
|---|
| 55 | static size_t stdio_kio_read(void *, size_t, size_t, FILE *);
|
|---|
| 56 | static size_t stdio_kio_write(const void *, size_t, size_t, FILE *);
|
|---|
| 57 | static int stdio_kio_flush(FILE *);
|
|---|
| 58 |
|
|---|
| 59 | static size_t stdio_vfs_read(void *, size_t, size_t, FILE *);
|
|---|
| 60 | static size_t stdio_vfs_write(const void *, size_t, size_t, FILE *);
|
|---|
| 61 |
|
|---|
| 62 | static int stdio_vfs_flush(FILE *);
|
|---|
| 63 |
|
|---|
| 64 | /** KIO stream ops */
|
|---|
| 65 | static __stream_ops_t stdio_kio_ops = {
|
|---|
| 66 | .read = stdio_kio_read,
|
|---|
| 67 | .write = stdio_kio_write,
|
|---|
| 68 | .flush = stdio_kio_flush
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | /** VFS stream ops */
|
|---|
| 72 | static __stream_ops_t stdio_vfs_ops = {
|
|---|
| 73 | .read = stdio_vfs_read,
|
|---|
| 74 | .write = stdio_vfs_write,
|
|---|
| 75 | .flush = stdio_vfs_flush
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | static FILE stdin_null = {
|
|---|
| 79 | .fd = -1,
|
|---|
| 80 | .pos = 0,
|
|---|
| 81 | .error = true,
|
|---|
| 82 | .eof = true,
|
|---|
| 83 | .ops = &stdio_vfs_ops,
|
|---|
| 84 | .arg = NULL,
|
|---|
| 85 | .sess = NULL,
|
|---|
| 86 | .btype = _IONBF,
|
|---|
| 87 | .buf = NULL,
|
|---|
| 88 | .buf_size = 0,
|
|---|
| 89 | .buf_head = NULL,
|
|---|
| 90 | .buf_tail = NULL,
|
|---|
| 91 | .buf_state = _bs_empty
|
|---|
| 92 | };
|
|---|
| 93 |
|
|---|
| 94 | static FILE stdout_kio = {
|
|---|
| 95 | .fd = -1,
|
|---|
| 96 | .pos = 0,
|
|---|
| 97 | .error = false,
|
|---|
| 98 | .eof = false,
|
|---|
| 99 | .ops = &stdio_kio_ops,
|
|---|
| 100 | .arg = NULL,
|
|---|
| 101 | .sess = NULL,
|
|---|
| 102 | .btype = _IOLBF,
|
|---|
| 103 | .buf = NULL,
|
|---|
| 104 | .buf_size = BUFSIZ,
|
|---|
| 105 | .buf_head = NULL,
|
|---|
| 106 | .buf_tail = NULL,
|
|---|
| 107 | .buf_state = _bs_empty
|
|---|
| 108 | };
|
|---|
| 109 |
|
|---|
| 110 | static FILE stderr_kio = {
|
|---|
| 111 | .fd = -1,
|
|---|
| 112 | .pos = 0,
|
|---|
| 113 | .error = false,
|
|---|
| 114 | .eof = false,
|
|---|
| 115 | .ops = &stdio_kio_ops,
|
|---|
| 116 | .arg = NULL,
|
|---|
| 117 | .sess = NULL,
|
|---|
| 118 | .btype = _IONBF,
|
|---|
| 119 | .buf = NULL,
|
|---|
| 120 | .buf_size = 0,
|
|---|
| 121 | .buf_head = NULL,
|
|---|
| 122 | .buf_tail = NULL,
|
|---|
| 123 | .buf_state = _bs_empty
|
|---|
| 124 | };
|
|---|
| 125 |
|
|---|
| 126 | FILE *stdin = NULL;
|
|---|
| 127 | FILE *stdout = NULL;
|
|---|
| 128 | FILE *stderr = NULL;
|
|---|
| 129 |
|
|---|
| 130 | static LIST_INITIALIZE(files);
|
|---|
| 131 |
|
|---|
| 132 | void __stdio_init(void)
|
|---|
| 133 | {
|
|---|
| 134 | /*
|
|---|
| 135 | * The first three standard file descriptors are assigned for compatibility.
|
|---|
| 136 | * This will probably be removed later.
|
|---|
| 137 | */
|
|---|
| 138 | int infd = inbox_get("stdin");
|
|---|
| 139 | if (infd >= 0) {
|
|---|
| 140 | int stdinfd = -1;
|
|---|
| 141 | (void) vfs_clone(infd, -1, false, &stdinfd);
|
|---|
| 142 | assert(stdinfd == 0);
|
|---|
| 143 | vfs_open(stdinfd, MODE_READ);
|
|---|
| 144 | stdin = fdopen(stdinfd, "r");
|
|---|
| 145 | } else {
|
|---|
| 146 | stdin = &stdin_null;
|
|---|
| 147 | list_append(&stdin->link, &files);
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | int outfd = inbox_get("stdout");
|
|---|
| 151 | if (outfd >= 0) {
|
|---|
| 152 | int stdoutfd = -1;
|
|---|
| 153 | (void) vfs_clone(outfd, -1, false, &stdoutfd);
|
|---|
| 154 | assert(stdoutfd <= 1);
|
|---|
| 155 | while (stdoutfd < 1)
|
|---|
| 156 | (void) vfs_clone(outfd, -1, false, &stdoutfd);
|
|---|
| 157 | vfs_open(stdoutfd, MODE_APPEND);
|
|---|
| 158 | stdout = fdopen(stdoutfd, "a");
|
|---|
| 159 | } else {
|
|---|
| 160 | stdout = &stdout_kio;
|
|---|
| 161 | list_append(&stdout->link, &files);
|
|---|
| 162 | }
|
|---|
| 163 |
|
|---|
| 164 | int errfd = inbox_get("stderr");
|
|---|
| 165 | if (errfd >= 0) {
|
|---|
| 166 | int stderrfd = -1;
|
|---|
| 167 | (void) vfs_clone(errfd, -1, false, &stderrfd);
|
|---|
| 168 | assert(stderrfd <= 2);
|
|---|
| 169 | while (stderrfd < 2)
|
|---|
| 170 | (void) vfs_clone(errfd, -1, false, &stderrfd);
|
|---|
| 171 | vfs_open(stderrfd, MODE_APPEND);
|
|---|
| 172 | stderr = fdopen(stderrfd, "a");
|
|---|
| 173 | } else {
|
|---|
| 174 | stderr = &stderr_kio;
|
|---|
| 175 | list_append(&stderr->link, &files);
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | void __stdio_done(void)
|
|---|
| 180 | {
|
|---|
| 181 | while (!list_empty(&files)) {
|
|---|
| 182 | FILE *file = list_get_instance(list_first(&files), FILE, link);
|
|---|
| 183 | fclose(file);
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | static bool parse_mode(const char *fmode, int *mode, bool *create, bool *excl,
|
|---|
| 188 | bool *truncate)
|
|---|
| 189 | {
|
|---|
| 190 | /* Parse mode except first character. */
|
|---|
| 191 | const char *mp = fmode;
|
|---|
| 192 |
|
|---|
| 193 | if (*mp++ == '\0') {
|
|---|
| 194 | errno = EINVAL;
|
|---|
| 195 | return false;
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | if ((*mp == 'b') || (*mp == 't'))
|
|---|
| 199 | mp++;
|
|---|
| 200 |
|
|---|
| 201 | bool plus;
|
|---|
| 202 | if (*mp == '+') {
|
|---|
| 203 | mp++;
|
|---|
| 204 | plus = true;
|
|---|
| 205 | } else {
|
|---|
| 206 | plus = false;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | bool ex;
|
|---|
| 210 | if (*mp == 'x') {
|
|---|
| 211 | mp++;
|
|---|
| 212 | ex = true;
|
|---|
| 213 | } else {
|
|---|
| 214 | ex = false;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | if (*mp != '\0') {
|
|---|
| 218 | errno = EINVAL;
|
|---|
| 219 | return false;
|
|---|
| 220 | }
|
|---|
| 221 |
|
|---|
| 222 | *create = false;
|
|---|
| 223 | *truncate = false;
|
|---|
| 224 | *excl = false;
|
|---|
| 225 |
|
|---|
| 226 | /* Parse first character of fmode and determine mode for vfs_open(). */
|
|---|
| 227 | switch (fmode[0]) {
|
|---|
| 228 | case 'r':
|
|---|
| 229 | *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
|
|---|
| 230 | if (ex) {
|
|---|
| 231 | errno = EINVAL;
|
|---|
| 232 | return false;
|
|---|
| 233 | }
|
|---|
| 234 | break;
|
|---|
| 235 | case 'w':
|
|---|
| 236 | *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
|
|---|
| 237 | *create = true;
|
|---|
| 238 | *excl = ex;
|
|---|
| 239 | if (!plus)
|
|---|
| 240 | *truncate = true;
|
|---|
| 241 | break;
|
|---|
| 242 | case 'a':
|
|---|
| 243 | /* TODO: a+ must read from beginning, append to the end. */
|
|---|
| 244 | if (plus) {
|
|---|
| 245 | errno = ENOTSUP;
|
|---|
| 246 | return false;
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | if (ex) {
|
|---|
| 250 | errno = EINVAL;
|
|---|
| 251 | return false;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
|
|---|
| 255 | *create = true;
|
|---|
| 256 | break;
|
|---|
| 257 | default:
|
|---|
| 258 | errno = EINVAL;
|
|---|
| 259 | return false;
|
|---|
| 260 | }
|
|---|
| 261 |
|
|---|
| 262 | return true;
|
|---|
| 263 | }
|
|---|
| 264 |
|
|---|
| 265 | /** Set stream buffer. */
|
|---|
| 266 | int setvbuf(FILE *stream, void *buf, int mode, size_t size)
|
|---|
| 267 | {
|
|---|
| 268 | if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF)
|
|---|
| 269 | return -1;
|
|---|
| 270 |
|
|---|
| 271 | stream->btype = mode;
|
|---|
| 272 | stream->buf = buf;
|
|---|
| 273 | stream->buf_size = size;
|
|---|
| 274 | stream->buf_head = stream->buf;
|
|---|
| 275 | stream->buf_tail = stream->buf;
|
|---|
| 276 | stream->buf_state = _bs_empty;
|
|---|
| 277 |
|
|---|
| 278 | return 0;
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | /** Set stream buffer.
|
|---|
| 282 | *
|
|---|
| 283 | * When @p buf is NULL, the stream is set as unbuffered, otherwise
|
|---|
| 284 | * full buffering is enabled.
|
|---|
| 285 | */
|
|---|
| 286 | void setbuf(FILE *stream, void *buf)
|
|---|
| 287 | {
|
|---|
| 288 | if (buf == NULL) {
|
|---|
| 289 | setvbuf(stream, NULL, _IONBF, BUFSIZ);
|
|---|
| 290 | } else {
|
|---|
| 291 | setvbuf(stream, buf, _IOFBF, BUFSIZ);
|
|---|
| 292 | }
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | static void _setvbuf(FILE *stream)
|
|---|
| 296 | {
|
|---|
| 297 | /* FIXME: Use more complex rules for setting buffering options. */
|
|---|
| 298 |
|
|---|
| 299 | switch (stream->fd) {
|
|---|
| 300 | case 1:
|
|---|
| 301 | setvbuf(stream, NULL, _IOLBF, BUFSIZ);
|
|---|
| 302 | break;
|
|---|
| 303 | case 0:
|
|---|
| 304 | case 2:
|
|---|
| 305 | setvbuf(stream, NULL, _IONBF, 0);
|
|---|
| 306 | break;
|
|---|
| 307 | default:
|
|---|
| 308 | setvbuf(stream, NULL, _IOFBF, BUFSIZ);
|
|---|
| 309 | }
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | /** Allocate stream buffer. */
|
|---|
| 313 | static int _fallocbuf(FILE *stream)
|
|---|
| 314 | {
|
|---|
| 315 | assert(stream->buf == NULL);
|
|---|
| 316 |
|
|---|
| 317 | stream->buf = malloc(stream->buf_size);
|
|---|
| 318 | if (stream->buf == NULL) {
|
|---|
| 319 | errno = ENOMEM;
|
|---|
| 320 | return EOF;
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | stream->buf_head = stream->buf;
|
|---|
| 324 | stream->buf_tail = stream->buf;
|
|---|
| 325 | return 0;
|
|---|
| 326 | }
|
|---|
| 327 |
|
|---|
| 328 | /** Open a stream.
|
|---|
| 329 | *
|
|---|
| 330 | * @param path Path of the file to open.
|
|---|
| 331 | * @param mode Mode string, (r|w|a)[b|t][+][x].
|
|---|
| 332 | *
|
|---|
| 333 | */
|
|---|
| 334 | FILE *fopen(const char *path, const char *fmode)
|
|---|
| 335 | {
|
|---|
| 336 | int mode;
|
|---|
| 337 | bool create;
|
|---|
| 338 | bool excl;
|
|---|
| 339 | bool truncate;
|
|---|
| 340 |
|
|---|
| 341 | if (!parse_mode(fmode, &mode, &create, &excl, &truncate))
|
|---|
| 342 | return NULL;
|
|---|
| 343 |
|
|---|
| 344 | /* Open file. */
|
|---|
| 345 | FILE *stream = malloc(sizeof(FILE));
|
|---|
| 346 | if (stream == NULL) {
|
|---|
| 347 | errno = ENOMEM;
|
|---|
| 348 | return NULL;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | int flags = WALK_REGULAR;
|
|---|
| 352 | if (create && excl)
|
|---|
| 353 | flags |= WALK_MUST_CREATE;
|
|---|
| 354 | else if (create)
|
|---|
| 355 | flags |= WALK_MAY_CREATE;
|
|---|
| 356 | int file;
|
|---|
| 357 | errno_t rc = vfs_lookup(path, flags, &file);
|
|---|
| 358 | if (rc != EOK) {
|
|---|
| 359 | errno = rc;
|
|---|
| 360 | free(stream);
|
|---|
| 361 | return NULL;
|
|---|
| 362 | }
|
|---|
| 363 |
|
|---|
| 364 | rc = vfs_open(file, mode);
|
|---|
| 365 | if (rc != EOK) {
|
|---|
| 366 | errno = rc;
|
|---|
| 367 | vfs_put(file);
|
|---|
| 368 | free(stream);
|
|---|
| 369 | return NULL;
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | if (truncate) {
|
|---|
| 373 | rc = vfs_resize(file, 0);
|
|---|
| 374 | if (rc != EOK) {
|
|---|
| 375 | errno = rc;
|
|---|
| 376 | vfs_put(file);
|
|---|
| 377 | free(stream);
|
|---|
| 378 | return NULL;
|
|---|
| 379 | }
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | stream->fd = file;
|
|---|
| 383 | stream->pos = 0;
|
|---|
| 384 | stream->error = false;
|
|---|
| 385 | stream->eof = false;
|
|---|
| 386 | stream->ops = &stdio_vfs_ops;
|
|---|
| 387 | stream->arg = NULL;
|
|---|
| 388 | stream->sess = NULL;
|
|---|
| 389 | stream->need_sync = false;
|
|---|
| 390 | _setvbuf(stream);
|
|---|
| 391 | stream->ungetc_chars = 0;
|
|---|
| 392 |
|
|---|
| 393 | list_append(&stream->link, &files);
|
|---|
| 394 |
|
|---|
| 395 | return stream;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | FILE *fdopen(int fd, const char *mode)
|
|---|
| 399 | {
|
|---|
| 400 | /* Open file. */
|
|---|
| 401 | FILE *stream = malloc(sizeof(FILE));
|
|---|
| 402 | if (stream == NULL) {
|
|---|
| 403 | errno = ENOMEM;
|
|---|
| 404 | return NULL;
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | stream->fd = fd;
|
|---|
| 408 | stream->pos = 0;
|
|---|
| 409 | stream->error = false;
|
|---|
| 410 | stream->eof = false;
|
|---|
| 411 | stream->ops = &stdio_vfs_ops;
|
|---|
| 412 | stream->arg = NULL;
|
|---|
| 413 | stream->sess = NULL;
|
|---|
| 414 | stream->need_sync = false;
|
|---|
| 415 | _setvbuf(stream);
|
|---|
| 416 | stream->ungetc_chars = 0;
|
|---|
| 417 |
|
|---|
| 418 | list_append(&stream->link, &files);
|
|---|
| 419 |
|
|---|
| 420 | return stream;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 |
|
|---|
| 424 | static int _fclose_nofree(FILE *stream)
|
|---|
| 425 | {
|
|---|
| 426 | errno_t rc = 0;
|
|---|
| 427 |
|
|---|
| 428 | fflush(stream);
|
|---|
| 429 |
|
|---|
| 430 | if (stream->sess != NULL)
|
|---|
| 431 | async_hangup(stream->sess);
|
|---|
| 432 |
|
|---|
| 433 | if (stream->fd >= 0)
|
|---|
| 434 | rc = vfs_put(stream->fd);
|
|---|
| 435 |
|
|---|
| 436 | list_remove(&stream->link);
|
|---|
| 437 |
|
|---|
| 438 | if (rc != EOK) {
|
|---|
| 439 | errno = rc;
|
|---|
| 440 | return EOF;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | return 0;
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | int fclose(FILE *stream)
|
|---|
| 447 | {
|
|---|
| 448 | int rc = _fclose_nofree(stream);
|
|---|
| 449 |
|
|---|
| 450 | if ((stream != &stdin_null) &&
|
|---|
| 451 | (stream != &stdout_kio) &&
|
|---|
| 452 | (stream != &stderr_kio))
|
|---|
| 453 | free(stream);
|
|---|
| 454 |
|
|---|
| 455 | return rc;
|
|---|
| 456 | }
|
|---|
| 457 |
|
|---|
| 458 | FILE *freopen(const char *path, const char *mode, FILE *stream)
|
|---|
| 459 | {
|
|---|
| 460 | FILE *nstr;
|
|---|
| 461 |
|
|---|
| 462 | if (path == NULL) {
|
|---|
| 463 | /* Changing mode is not supported */
|
|---|
| 464 | return NULL;
|
|---|
| 465 | }
|
|---|
| 466 |
|
|---|
| 467 | (void) _fclose_nofree(stream);
|
|---|
| 468 | nstr = fopen(path, mode);
|
|---|
| 469 | if (nstr == NULL) {
|
|---|
| 470 | free(stream);
|
|---|
| 471 | return NULL;
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | list_remove(&nstr->link);
|
|---|
| 475 | *stream = *nstr;
|
|---|
| 476 | list_append(&stream->link, &files);
|
|---|
| 477 |
|
|---|
| 478 | free(nstr);
|
|---|
| 479 |
|
|---|
| 480 | return stream;
|
|---|
| 481 | }
|
|---|
| 482 |
|
|---|
| 483 | /** Read from a stream (unbuffered).
|
|---|
| 484 | *
|
|---|
| 485 | * @param buf Destination buffer.
|
|---|
| 486 | * @param size Size of each record.
|
|---|
| 487 | * @param nmemb Number of records to read.
|
|---|
| 488 | * @param stream Pointer to the stream.
|
|---|
| 489 | *
|
|---|
| 490 | * @return Number of elements successfully read. On error this is less than
|
|---|
| 491 | * nmemb, stream error indicator is set and errno is set.
|
|---|
| 492 | */
|
|---|
| 493 | static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
|
|---|
| 494 | {
|
|---|
| 495 | return stream->ops->read(buf, size, nmemb, stream);
|
|---|
| 496 | }
|
|---|
| 497 |
|
|---|
| 498 | /** Write to a stream (unbuffered).
|
|---|
| 499 | *
|
|---|
| 500 | * @param buf Source buffer.
|
|---|
| 501 | * @param size Size of each record.
|
|---|
| 502 | * @param nmemb Number of records to write.
|
|---|
| 503 | * @param stream Pointer to the stream.
|
|---|
| 504 | *
|
|---|
| 505 | * @return Number of elements successfully written. On error this is less than
|
|---|
| 506 | * nmemb, stream error indicator is set and errno is set.
|
|---|
| 507 | */
|
|---|
| 508 | static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
|---|
| 509 | {
|
|---|
| 510 | size_t nwritten;
|
|---|
| 511 |
|
|---|
| 512 | if (size == 0 || nmemb == 0)
|
|---|
| 513 | return 0;
|
|---|
| 514 |
|
|---|
| 515 | nwritten = stream->ops->write(buf, size, nmemb, stream);
|
|---|
| 516 |
|
|---|
| 517 | if (nwritten > 0)
|
|---|
| 518 | stream->need_sync = true;
|
|---|
| 519 |
|
|---|
| 520 | return (nwritten / size);
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | /** Read some data in stream buffer.
|
|---|
| 524 | *
|
|---|
| 525 | * On error, stream error indicator is set and errno is set.
|
|---|
| 526 | */
|
|---|
| 527 | static void _ffillbuf(FILE *stream)
|
|---|
| 528 | {
|
|---|
| 529 | errno_t rc;
|
|---|
| 530 | size_t nread;
|
|---|
| 531 |
|
|---|
| 532 | stream->buf_head = stream->buf_tail = stream->buf;
|
|---|
| 533 |
|
|---|
| 534 | rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
|
|---|
| 535 | &nread);
|
|---|
| 536 | if (rc != EOK) {
|
|---|
| 537 | errno = rc;
|
|---|
| 538 | stream->error = true;
|
|---|
| 539 | return;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | if (nread == 0) {
|
|---|
| 543 | stream->eof = true;
|
|---|
| 544 | return;
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | stream->buf_head += nread;
|
|---|
| 548 | stream->buf_state = _bs_read;
|
|---|
| 549 | }
|
|---|
| 550 |
|
|---|
| 551 | /** Write out stream buffer, do not sync stream. */
|
|---|
| 552 | static void _fflushbuf(FILE *stream)
|
|---|
| 553 | {
|
|---|
| 554 | size_t bytes_used;
|
|---|
| 555 |
|
|---|
| 556 | if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
|
|---|
| 557 | return;
|
|---|
| 558 |
|
|---|
| 559 | bytes_used = stream->buf_head - stream->buf_tail;
|
|---|
| 560 |
|
|---|
| 561 | /* If buffer has prefetched read data, we need to seek back. */
|
|---|
| 562 | if (bytes_used > 0 && stream->buf_state == _bs_read)
|
|---|
| 563 | stream->pos -= bytes_used;
|
|---|
| 564 |
|
|---|
| 565 | /* If buffer has unwritten data, we need to write them out. */
|
|---|
| 566 | if (bytes_used > 0 && stream->buf_state == _bs_write) {
|
|---|
| 567 | (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
|
|---|
| 568 | /* On error stream error indicator and errno are set by _fwrite */
|
|---|
| 569 | if (stream->error)
|
|---|
| 570 | return;
|
|---|
| 571 | }
|
|---|
| 572 |
|
|---|
| 573 | stream->buf_head = stream->buf;
|
|---|
| 574 | stream->buf_tail = stream->buf;
|
|---|
| 575 | stream->buf_state = _bs_empty;
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | /** Read from a stream.
|
|---|
| 579 | *
|
|---|
| 580 | * @param dest Destination buffer.
|
|---|
| 581 | * @param size Size of each record.
|
|---|
| 582 | * @param nmemb Number of records to read.
|
|---|
| 583 | * @param stream Pointer to the stream.
|
|---|
| 584 | *
|
|---|
| 585 | */
|
|---|
| 586 | size_t fread(void *dest, size_t size, size_t nmemb, FILE *stream)
|
|---|
| 587 | {
|
|---|
| 588 | uint8_t *dp;
|
|---|
| 589 | size_t bytes_left;
|
|---|
| 590 | size_t now;
|
|---|
| 591 | size_t data_avail;
|
|---|
| 592 | size_t total_read;
|
|---|
| 593 | size_t i;
|
|---|
| 594 |
|
|---|
| 595 | if (size == 0 || nmemb == 0)
|
|---|
| 596 | return 0;
|
|---|
| 597 |
|
|---|
| 598 | bytes_left = size * nmemb;
|
|---|
| 599 | total_read = 0;
|
|---|
| 600 | dp = (uint8_t *) dest;
|
|---|
| 601 |
|
|---|
| 602 | /* Bytes from ungetc() buffer */
|
|---|
| 603 | while (stream->ungetc_chars > 0 && bytes_left > 0) {
|
|---|
| 604 | *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
|
|---|
| 605 | ++total_read;
|
|---|
| 606 | --bytes_left;
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | /* If not buffered stream, read in directly. */
|
|---|
| 610 | if (stream->btype == _IONBF) {
|
|---|
| 611 | total_read += _fread(dest, 1, bytes_left, stream);
|
|---|
| 612 | return total_read / size;
|
|---|
| 613 | }
|
|---|
| 614 |
|
|---|
| 615 | /* Make sure no data is pending write. */
|
|---|
| 616 | if (stream->buf_state == _bs_write)
|
|---|
| 617 | _fflushbuf(stream);
|
|---|
| 618 |
|
|---|
| 619 | /* Perform lazy allocation of stream buffer. */
|
|---|
| 620 | if (stream->buf == NULL) {
|
|---|
| 621 | if (_fallocbuf(stream) != 0)
|
|---|
| 622 | return 0; /* Errno set by _fallocbuf(). */
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
|
|---|
| 626 | if (stream->buf_head == stream->buf_tail)
|
|---|
| 627 | _ffillbuf(stream);
|
|---|
| 628 |
|
|---|
| 629 | if (stream->error || stream->eof) {
|
|---|
| 630 | /* On error errno was set by _ffillbuf() */
|
|---|
| 631 | break;
|
|---|
| 632 | }
|
|---|
| 633 |
|
|---|
| 634 | data_avail = stream->buf_head - stream->buf_tail;
|
|---|
| 635 |
|
|---|
| 636 | if (bytes_left > data_avail)
|
|---|
| 637 | now = data_avail;
|
|---|
| 638 | else
|
|---|
| 639 | now = bytes_left;
|
|---|
| 640 |
|
|---|
| 641 | for (i = 0; i < now; i++) {
|
|---|
| 642 | dp[i] = stream->buf_tail[i];
|
|---|
| 643 | }
|
|---|
| 644 |
|
|---|
| 645 | dp += now;
|
|---|
| 646 | stream->buf_tail += now;
|
|---|
| 647 | bytes_left -= now;
|
|---|
| 648 | total_read += now;
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | return (total_read / size);
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 |
|
|---|
| 655 | /** Write to a stream.
|
|---|
| 656 | *
|
|---|
| 657 | * @param buf Source buffer.
|
|---|
| 658 | * @param size Size of each record.
|
|---|
| 659 | * @param nmemb Number of records to write.
|
|---|
| 660 | * @param stream Pointer to the stream.
|
|---|
| 661 | *
|
|---|
| 662 | */
|
|---|
| 663 | size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
|---|
| 664 | {
|
|---|
| 665 | uint8_t *data;
|
|---|
| 666 | size_t bytes_left;
|
|---|
| 667 | size_t now;
|
|---|
| 668 | size_t buf_free;
|
|---|
| 669 | size_t total_written;
|
|---|
| 670 | size_t i;
|
|---|
| 671 | uint8_t b;
|
|---|
| 672 | bool need_flush;
|
|---|
| 673 |
|
|---|
| 674 | if (size == 0 || nmemb == 0)
|
|---|
| 675 | return 0;
|
|---|
| 676 |
|
|---|
| 677 | /* If not buffered stream, write out directly. */
|
|---|
| 678 | if (stream->btype == _IONBF) {
|
|---|
| 679 | now = _fwrite(buf, size, nmemb, stream);
|
|---|
| 680 | fflush(stream);
|
|---|
| 681 | return now;
|
|---|
| 682 | }
|
|---|
| 683 |
|
|---|
| 684 | /* Make sure buffer contains no prefetched data. */
|
|---|
| 685 | if (stream->buf_state == _bs_read)
|
|---|
| 686 | _fflushbuf(stream);
|
|---|
| 687 |
|
|---|
| 688 | /* Perform lazy allocation of stream buffer. */
|
|---|
| 689 | if (stream->buf == NULL) {
|
|---|
| 690 | if (_fallocbuf(stream) != 0)
|
|---|
| 691 | return 0; /* Errno set by _fallocbuf(). */
|
|---|
| 692 | }
|
|---|
| 693 |
|
|---|
| 694 | data = (uint8_t *) buf;
|
|---|
| 695 | bytes_left = size * nmemb;
|
|---|
| 696 | total_written = 0;
|
|---|
| 697 | need_flush = false;
|
|---|
| 698 |
|
|---|
| 699 | while ((!stream->error) && (bytes_left > 0)) {
|
|---|
| 700 | buf_free = stream->buf_size - (stream->buf_head - stream->buf);
|
|---|
| 701 | if (bytes_left > buf_free)
|
|---|
| 702 | now = buf_free;
|
|---|
| 703 | else
|
|---|
| 704 | now = bytes_left;
|
|---|
| 705 |
|
|---|
| 706 | for (i = 0; i < now; i++) {
|
|---|
| 707 | b = data[i];
|
|---|
| 708 | stream->buf_head[i] = b;
|
|---|
| 709 |
|
|---|
| 710 | if ((b == '\n') && (stream->btype == _IOLBF))
|
|---|
| 711 | need_flush = true;
|
|---|
| 712 | }
|
|---|
| 713 |
|
|---|
| 714 | data += now;
|
|---|
| 715 | stream->buf_head += now;
|
|---|
| 716 | buf_free -= now;
|
|---|
| 717 | bytes_left -= now;
|
|---|
| 718 | total_written += now;
|
|---|
| 719 | stream->buf_state = _bs_write;
|
|---|
| 720 |
|
|---|
| 721 | if (buf_free == 0) {
|
|---|
| 722 | /* Only need to drain buffer. */
|
|---|
| 723 | _fflushbuf(stream);
|
|---|
| 724 | if (!stream->error)
|
|---|
| 725 | need_flush = false;
|
|---|
| 726 | }
|
|---|
| 727 | }
|
|---|
| 728 |
|
|---|
| 729 | if (need_flush)
|
|---|
| 730 | fflush(stream);
|
|---|
| 731 |
|
|---|
| 732 | return (total_written / size);
|
|---|
| 733 | }
|
|---|
| 734 |
|
|---|
| 735 | wint_t fputwc(wchar_t wc, FILE *stream)
|
|---|
| 736 | {
|
|---|
| 737 | char buf[STR_BOUNDS(1)];
|
|---|
| 738 | size_t sz = 0;
|
|---|
| 739 |
|
|---|
| 740 | if (chr_encode(wc, buf, &sz, STR_BOUNDS(1)) != EOK) {
|
|---|
| 741 | errno = EILSEQ;
|
|---|
| 742 | return WEOF;
|
|---|
| 743 | }
|
|---|
| 744 |
|
|---|
| 745 | size_t wr = fwrite(buf, 1, sz, stream);
|
|---|
| 746 | if (wr < sz)
|
|---|
| 747 | return WEOF;
|
|---|
| 748 |
|
|---|
| 749 | return wc;
|
|---|
| 750 | }
|
|---|
| 751 |
|
|---|
| 752 | wint_t putwchar(wchar_t wc)
|
|---|
| 753 | {
|
|---|
| 754 | return fputwc(wc, stdout);
|
|---|
| 755 | }
|
|---|
| 756 |
|
|---|
| 757 | int fputc(int c, FILE *stream)
|
|---|
| 758 | {
|
|---|
| 759 | unsigned char b;
|
|---|
| 760 | size_t wr;
|
|---|
| 761 |
|
|---|
| 762 | b = (unsigned char) c;
|
|---|
| 763 | wr = fwrite(&b, sizeof(b), 1, stream);
|
|---|
| 764 | if (wr < 1)
|
|---|
| 765 | return EOF;
|
|---|
| 766 |
|
|---|
| 767 | return b;
|
|---|
| 768 | }
|
|---|
| 769 |
|
|---|
| 770 | int putchar(int c)
|
|---|
| 771 | {
|
|---|
| 772 | return fputc(c, stdout);
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | int fputs(const char *str, FILE *stream)
|
|---|
| 776 | {
|
|---|
| 777 | (void) fwrite(str, str_size(str), 1, stream);
|
|---|
| 778 | if (ferror(stream))
|
|---|
| 779 | return EOF;
|
|---|
| 780 | return 0;
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | int puts(const char *str)
|
|---|
| 784 | {
|
|---|
| 785 | if (fputs(str, stdout) < 0)
|
|---|
| 786 | return EOF;
|
|---|
| 787 | return putchar('\n');
|
|---|
| 788 | }
|
|---|
| 789 |
|
|---|
| 790 | int fgetc(FILE *stream)
|
|---|
| 791 | {
|
|---|
| 792 | char c;
|
|---|
| 793 |
|
|---|
| 794 | /* This could be made faster by only flushing when needed. */
|
|---|
| 795 | if (stdout)
|
|---|
| 796 | fflush(stdout);
|
|---|
| 797 | if (stderr)
|
|---|
| 798 | fflush(stderr);
|
|---|
| 799 |
|
|---|
| 800 | if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
|
|---|
| 801 | return EOF;
|
|---|
| 802 |
|
|---|
| 803 | return (int) c;
|
|---|
| 804 | }
|
|---|
| 805 |
|
|---|
| 806 | char *fgets(char *str, int size, FILE *stream)
|
|---|
| 807 | {
|
|---|
| 808 | int c;
|
|---|
| 809 | int idx;
|
|---|
| 810 |
|
|---|
| 811 | idx = 0;
|
|---|
| 812 | while (idx < size - 1) {
|
|---|
| 813 | c = fgetc(stream);
|
|---|
| 814 | if (c == EOF)
|
|---|
| 815 | break;
|
|---|
| 816 |
|
|---|
| 817 | str[idx++] = c;
|
|---|
| 818 |
|
|---|
| 819 | if (c == '\n')
|
|---|
| 820 | break;
|
|---|
| 821 | }
|
|---|
| 822 |
|
|---|
| 823 | if (ferror(stream))
|
|---|
| 824 | return NULL;
|
|---|
| 825 |
|
|---|
| 826 | if (idx == 0)
|
|---|
| 827 | return NULL;
|
|---|
| 828 |
|
|---|
| 829 | str[idx] = '\0';
|
|---|
| 830 | return str;
|
|---|
| 831 | }
|
|---|
| 832 |
|
|---|
| 833 | int getchar(void)
|
|---|
| 834 | {
|
|---|
| 835 | return fgetc(stdin);
|
|---|
| 836 | }
|
|---|
| 837 |
|
|---|
| 838 | int ungetc(int c, FILE *stream)
|
|---|
| 839 | {
|
|---|
| 840 | if (c == EOF)
|
|---|
| 841 | return EOF;
|
|---|
| 842 |
|
|---|
| 843 | if (stream->ungetc_chars >= UNGETC_MAX)
|
|---|
| 844 | return EOF;
|
|---|
| 845 |
|
|---|
| 846 | stream->ungetc_buf[stream->ungetc_chars++] =
|
|---|
| 847 | (uint8_t)c;
|
|---|
| 848 |
|
|---|
| 849 | stream->eof = false;
|
|---|
| 850 | return (uint8_t)c;
|
|---|
| 851 | }
|
|---|
| 852 |
|
|---|
| 853 | int fseek64(FILE *stream, off64_t offset, int whence)
|
|---|
| 854 | {
|
|---|
| 855 | errno_t rc;
|
|---|
| 856 |
|
|---|
| 857 | if (stream->error)
|
|---|
| 858 | return -1;
|
|---|
| 859 |
|
|---|
| 860 | _fflushbuf(stream);
|
|---|
| 861 | if (stream->error) {
|
|---|
| 862 | /* errno was set by _fflushbuf() */
|
|---|
| 863 | return -1;
|
|---|
| 864 | }
|
|---|
| 865 |
|
|---|
| 866 | stream->ungetc_chars = 0;
|
|---|
| 867 |
|
|---|
| 868 | vfs_stat_t st;
|
|---|
| 869 | switch (whence) {
|
|---|
| 870 | case SEEK_SET:
|
|---|
| 871 | stream->pos = offset;
|
|---|
| 872 | break;
|
|---|
| 873 | case SEEK_CUR:
|
|---|
| 874 | stream->pos += offset;
|
|---|
| 875 | break;
|
|---|
| 876 | case SEEK_END:
|
|---|
| 877 | rc = vfs_stat(stream->fd, &st);
|
|---|
| 878 | if (rc != EOK) {
|
|---|
| 879 | errno = rc;
|
|---|
| 880 | stream->error = true;
|
|---|
| 881 | return -1;
|
|---|
| 882 | }
|
|---|
| 883 | stream->pos = st.size + offset;
|
|---|
| 884 | break;
|
|---|
| 885 | }
|
|---|
| 886 |
|
|---|
| 887 | stream->eof = false;
|
|---|
| 888 | return 0;
|
|---|
| 889 | }
|
|---|
| 890 |
|
|---|
| 891 | off64_t ftell64(FILE *stream)
|
|---|
| 892 | {
|
|---|
| 893 | if (stream->error)
|
|---|
| 894 | return EOF;
|
|---|
| 895 |
|
|---|
| 896 | _fflushbuf(stream);
|
|---|
| 897 | if (stream->error) {
|
|---|
| 898 | /* errno was set by _fflushbuf() */
|
|---|
| 899 | return EOF;
|
|---|
| 900 | }
|
|---|
| 901 |
|
|---|
| 902 | return stream->pos - stream->ungetc_chars;
|
|---|
| 903 | }
|
|---|
| 904 |
|
|---|
| 905 | int fseek(FILE *stream, long offset, int whence)
|
|---|
| 906 | {
|
|---|
| 907 | return fseek64(stream, offset, whence);
|
|---|
| 908 | }
|
|---|
| 909 |
|
|---|
| 910 | long ftell(FILE *stream)
|
|---|
| 911 | {
|
|---|
| 912 | off64_t off = ftell64(stream);
|
|---|
| 913 |
|
|---|
| 914 | /* The native position is too large for the C99-ish interface. */
|
|---|
| 915 | if (off > LONG_MAX)
|
|---|
| 916 | return EOF;
|
|---|
| 917 |
|
|---|
| 918 | return off;
|
|---|
| 919 | }
|
|---|
| 920 |
|
|---|
| 921 | void rewind(FILE *stream)
|
|---|
| 922 | {
|
|---|
| 923 | (void) fseek(stream, 0, SEEK_SET);
|
|---|
| 924 | }
|
|---|
| 925 |
|
|---|
| 926 | int fflush(FILE *stream)
|
|---|
| 927 | {
|
|---|
| 928 | if (stream->error)
|
|---|
| 929 | return EOF;
|
|---|
| 930 |
|
|---|
| 931 | _fflushbuf(stream);
|
|---|
| 932 | if (stream->error) {
|
|---|
| 933 | /* errno was set by _fflushbuf() */
|
|---|
| 934 | return EOF;
|
|---|
| 935 | }
|
|---|
| 936 |
|
|---|
| 937 | if (stream->need_sync) {
|
|---|
| 938 | /**
|
|---|
| 939 | * Better than syncing always, but probably still not the
|
|---|
| 940 | * right thing to do.
|
|---|
| 941 | */
|
|---|
| 942 | if (stream->ops->flush(stream) == EOF)
|
|---|
| 943 | return EOF;
|
|---|
| 944 |
|
|---|
| 945 | stream->need_sync = false;
|
|---|
| 946 | }
|
|---|
| 947 |
|
|---|
| 948 | return 0;
|
|---|
| 949 | }
|
|---|
| 950 |
|
|---|
| 951 | int feof(FILE *stream)
|
|---|
| 952 | {
|
|---|
| 953 | return stream->eof;
|
|---|
| 954 | }
|
|---|
| 955 |
|
|---|
| 956 | int ferror(FILE *stream)
|
|---|
| 957 | {
|
|---|
| 958 | return stream->error;
|
|---|
| 959 | }
|
|---|
| 960 |
|
|---|
| 961 | void clearerr(FILE *stream)
|
|---|
| 962 | {
|
|---|
| 963 | stream->eof = false;
|
|---|
| 964 | stream->error = false;
|
|---|
| 965 | }
|
|---|
| 966 |
|
|---|
| 967 | int fileno(FILE *stream)
|
|---|
| 968 | {
|
|---|
| 969 | if (stream->ops != &stdio_vfs_ops) {
|
|---|
| 970 | errno = EBADF;
|
|---|
| 971 | return EOF;
|
|---|
| 972 | }
|
|---|
| 973 |
|
|---|
| 974 | return stream->fd;
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
|
|---|
| 978 | {
|
|---|
| 979 | if (stream->fd >= 0) {
|
|---|
| 980 | if (stream->sess == NULL)
|
|---|
| 981 | stream->sess = vfs_fd_session(stream->fd, iface);
|
|---|
| 982 |
|
|---|
| 983 | return stream->sess;
|
|---|
| 984 | }
|
|---|
| 985 |
|
|---|
| 986 | return NULL;
|
|---|
| 987 | }
|
|---|
| 988 |
|
|---|
| 989 | errno_t vfs_fhandle(FILE *stream, int *handle)
|
|---|
| 990 | {
|
|---|
| 991 | if (stream->fd >= 0) {
|
|---|
| 992 | *handle = stream->fd;
|
|---|
| 993 | return EOK;
|
|---|
| 994 | }
|
|---|
| 995 |
|
|---|
| 996 | return ENOENT;
|
|---|
| 997 | }
|
|---|
| 998 |
|
|---|
| 999 | /** Read from KIO stream. */
|
|---|
| 1000 | static size_t stdio_kio_read(void *buf, size_t size, size_t nmemb, FILE *stream)
|
|---|
| 1001 | {
|
|---|
| 1002 | stream->eof = true;
|
|---|
| 1003 | return 0;
|
|---|
| 1004 | }
|
|---|
| 1005 |
|
|---|
| 1006 | /** Write to KIO stream. */
|
|---|
| 1007 | static size_t stdio_kio_write(const void *buf, size_t size, size_t nmemb,
|
|---|
| 1008 | FILE *stream)
|
|---|
| 1009 | {
|
|---|
| 1010 | errno_t rc;
|
|---|
| 1011 | size_t nwritten;
|
|---|
| 1012 |
|
|---|
| 1013 | rc = kio_write(buf, size * nmemb, &nwritten);
|
|---|
| 1014 | if (rc != EOK) {
|
|---|
| 1015 | stream->error = true;
|
|---|
| 1016 | nwritten = 0;
|
|---|
| 1017 | }
|
|---|
| 1018 |
|
|---|
| 1019 | return nwritten / size;
|
|---|
| 1020 | }
|
|---|
| 1021 |
|
|---|
| 1022 | /** Flush KIO stream. */
|
|---|
| 1023 | static int stdio_kio_flush(FILE *stream)
|
|---|
| 1024 | {
|
|---|
| 1025 | kio_update();
|
|---|
| 1026 | return 0;
|
|---|
| 1027 | }
|
|---|
| 1028 |
|
|---|
| 1029 | /** Read from VFS stream. */
|
|---|
| 1030 | static size_t stdio_vfs_read(void *buf, size_t size, size_t nmemb, FILE *stream)
|
|---|
| 1031 | {
|
|---|
| 1032 | errno_t rc;
|
|---|
| 1033 | size_t nread;
|
|---|
| 1034 |
|
|---|
| 1035 | if (size == 0 || nmemb == 0)
|
|---|
| 1036 | return 0;
|
|---|
| 1037 |
|
|---|
| 1038 | rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
|
|---|
| 1039 | if (rc != EOK) {
|
|---|
| 1040 | errno = rc;
|
|---|
| 1041 | stream->error = true;
|
|---|
| 1042 | } else if (nread == 0) {
|
|---|
| 1043 | stream->eof = true;
|
|---|
| 1044 | }
|
|---|
| 1045 |
|
|---|
| 1046 | return (nread / size);
|
|---|
| 1047 | }
|
|---|
| 1048 |
|
|---|
| 1049 | /** Write to VFS stream. */
|
|---|
| 1050 | static size_t stdio_vfs_write(const void *buf, size_t size, size_t nmemb,
|
|---|
| 1051 | FILE *stream)
|
|---|
| 1052 | {
|
|---|
| 1053 | errno_t rc;
|
|---|
| 1054 | size_t nwritten;
|
|---|
| 1055 |
|
|---|
| 1056 | rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb, &nwritten);
|
|---|
| 1057 | if (rc != EOK) {
|
|---|
| 1058 | errno = rc;
|
|---|
| 1059 | stream->error = true;
|
|---|
| 1060 | }
|
|---|
| 1061 |
|
|---|
| 1062 | return nwritten / size;
|
|---|
| 1063 | }
|
|---|
| 1064 |
|
|---|
| 1065 | /** Flush VFS stream. */
|
|---|
| 1066 | static int stdio_vfs_flush(FILE *stream)
|
|---|
| 1067 | {
|
|---|
| 1068 | errno_t rc;
|
|---|
| 1069 |
|
|---|
| 1070 | rc = vfs_sync(stream->fd);
|
|---|
| 1071 | if (rc != EOK) {
|
|---|
| 1072 | errno = rc;
|
|---|
| 1073 | return EOF;
|
|---|
| 1074 | }
|
|---|
| 1075 |
|
|---|
| 1076 | return 0;
|
|---|
| 1077 | }
|
|---|
| 1078 |
|
|---|
| 1079 | /** @}
|
|---|
| 1080 | */
|
|---|