[b861b58] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2005 Martin Decky
|
---|
[b861b58] | 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.
|
---|
[b2951e2] | 27 | */
|
---|
| 28 |
|
---|
[fadd381] | 29 | /** @addtogroup libc
|
---|
[b2951e2] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[2595dab] | 33 | */
|
---|
[b861b58] | 34 |
|
---|
[cc6f688] | 35 | #include <stdio.h>
|
---|
[ef8bcc6] | 36 | #include <assert.h>
|
---|
[19f857a] | 37 | #include <str.h>
|
---|
[56fa418] | 38 | #include <errno.h>
|
---|
[3e6a98c5] | 39 | #include <stdbool.h>
|
---|
[38d150e] | 40 | #include <stdlib.h>
|
---|
[64d2b10] | 41 | #include <async.h>
|
---|
[6fa9a99d] | 42 | #include <io/kio.h>
|
---|
[2595dab] | 43 | #include <vfs/vfs.h>
|
---|
[79ae36dd] | 44 | #include <vfs/vfs_sess.h>
|
---|
[bb9ec2d] | 45 | #include <vfs/inbox.h>
|
---|
[15f3c3f] | 46 | #include <ipc/loc.h>
|
---|
[d9c8c81] | 47 | #include <adt/list.h>
|
---|
[47b7006] | 48 | #include "../private/io.h"
|
---|
[79ae36dd] | 49 | #include "../private/stdio.h"
|
---|
[b861b58] | 50 |
|
---|
[facebd56] | 51 | static void _ffillbuf(FILE *stream);
|
---|
[ef8bcc6] | 52 | static void _fflushbuf(FILE *stream);
|
---|
| 53 |
|
---|
[a68f737] | 54 | static FILE stdin_null = {
|
---|
[2595dab] | 55 | .fd = -1,
|
---|
[58898d1d] | 56 | .pos = 0,
|
---|
[2595dab] | 57 | .error = true,
|
---|
| 58 | .eof = true,
|
---|
[6fa9a99d] | 59 | .kio = false,
|
---|
[79ae36dd] | 60 | .sess = NULL,
|
---|
[ef8bcc6] | 61 | .btype = _IONBF,
|
---|
| 62 | .buf = NULL,
|
---|
| 63 | .buf_size = 0,
|
---|
[facebd56] | 64 | .buf_head = NULL,
|
---|
| 65 | .buf_tail = NULL,
|
---|
[c70703a] | 66 | .buf_state = _bs_empty
|
---|
[2595dab] | 67 | };
|
---|
[350514c] | 68 |
|
---|
[6fa9a99d] | 69 | static FILE stdout_kio = {
|
---|
[2595dab] | 70 | .fd = -1,
|
---|
[58898d1d] | 71 | .pos = 0,
|
---|
[2595dab] | 72 | .error = false,
|
---|
| 73 | .eof = false,
|
---|
[6fa9a99d] | 74 | .kio = true,
|
---|
[79ae36dd] | 75 | .sess = NULL,
|
---|
[ef8bcc6] | 76 | .btype = _IOLBF,
|
---|
| 77 | .buf = NULL,
|
---|
| 78 | .buf_size = BUFSIZ,
|
---|
[facebd56] | 79 | .buf_head = NULL,
|
---|
| 80 | .buf_tail = NULL,
|
---|
| 81 | .buf_state = _bs_empty
|
---|
[2595dab] | 82 | };
|
---|
| 83 |
|
---|
[6fa9a99d] | 84 | static FILE stderr_kio = {
|
---|
[a68f737] | 85 | .fd = -1,
|
---|
[58898d1d] | 86 | .pos = 0,
|
---|
[a68f737] | 87 | .error = false,
|
---|
| 88 | .eof = false,
|
---|
[6fa9a99d] | 89 | .kio = true,
|
---|
[79ae36dd] | 90 | .sess = NULL,
|
---|
[ef8bcc6] | 91 | .btype = _IONBF,
|
---|
| 92 | .buf = NULL,
|
---|
| 93 | .buf_size = 0,
|
---|
[facebd56] | 94 | .buf_head = NULL,
|
---|
| 95 | .buf_tail = NULL,
|
---|
| 96 | .buf_state = _bs_empty
|
---|
[a68f737] | 97 | };
|
---|
| 98 |
|
---|
| 99 | FILE *stdin = NULL;
|
---|
| 100 | FILE *stdout = NULL;
|
---|
| 101 | FILE *stderr = NULL;
|
---|
| 102 |
|
---|
| 103 | static LIST_INITIALIZE(files);
|
---|
| 104 |
|
---|
[bb9ec2d] | 105 | void __stdio_init(void)
|
---|
[a68f737] | 106 | {
|
---|
[bb9ec2d] | 107 | /* The first three standard file descriptors are assigned for compatibility.
|
---|
| 108 | * This will probably be removed later.
|
---|
| 109 | */
|
---|
| 110 |
|
---|
| 111 | int infd = inbox_get("stdin");
|
---|
| 112 | if (infd >= 0) {
|
---|
[fcab7ef] | 113 | int stdinfd = vfs_clone(infd, -1, false);
|
---|
[bb9ec2d] | 114 | assert(stdinfd == 0);
|
---|
[b19e892] | 115 | vfs_open(stdinfd, MODE_READ);
|
---|
[bb9ec2d] | 116 | stdin = fdopen(stdinfd, "r");
|
---|
[a68f737] | 117 | } else {
|
---|
| 118 | stdin = &stdin_null;
|
---|
| 119 | list_append(&stdin->link, &files);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[bb9ec2d] | 122 | int outfd = inbox_get("stdout");
|
---|
| 123 | if (outfd >= 0) {
|
---|
[fcab7ef] | 124 | int stdoutfd = vfs_clone(outfd, -1, false);
|
---|
[bb9ec2d] | 125 | assert(stdoutfd <= 1);
|
---|
[fcab7ef] | 126 | while (stdoutfd < 1)
|
---|
| 127 | stdoutfd = vfs_clone(outfd, -1, false);
|
---|
[b19e892] | 128 | vfs_open(stdoutfd, MODE_APPEND);
|
---|
[bb9ec2d] | 129 | stdout = fdopen(stdoutfd, "a");
|
---|
[a68f737] | 130 | } else {
|
---|
[6fa9a99d] | 131 | stdout = &stdout_kio;
|
---|
[a68f737] | 132 | list_append(&stdout->link, &files);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[bb9ec2d] | 135 | int errfd = inbox_get("stderr");
|
---|
| 136 | if (errfd >= 0) {
|
---|
[fcab7ef] | 137 | int stderrfd = vfs_clone(errfd, -1, false);
|
---|
[bb9ec2d] | 138 | assert(stderrfd <= 2);
|
---|
[fcab7ef] | 139 | while (stderrfd < 2)
|
---|
| 140 | stderrfd = vfs_clone(errfd, -1, false);
|
---|
[b19e892] | 141 | vfs_open(stderrfd, MODE_APPEND);
|
---|
[bb9ec2d] | 142 | stderr = fdopen(stderrfd, "a");
|
---|
[a68f737] | 143 | } else {
|
---|
[6fa9a99d] | 144 | stderr = &stderr_kio;
|
---|
[a68f737] | 145 | list_append(&stderr->link, &files);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[db24058] | 149 | void __stdio_done(void)
|
---|
[a68f737] | 150 | {
|
---|
[b72efe8] | 151 | while (!list_empty(&files)) {
|
---|
| 152 | FILE *file = list_get_instance(list_first(&files), FILE, link);
|
---|
[a68f737] | 153 | fclose(file);
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
[2595dab] | 156 |
|
---|
[b19e892] | 157 | static bool parse_mode(const char *fmode, int *mode, bool *create, bool *truncate)
|
---|
[b861b58] | 158 | {
|
---|
[2595dab] | 159 | /* Parse mode except first character. */
|
---|
[b19e892] | 160 | const char *mp = fmode;
|
---|
[2595dab] | 161 | if (*mp++ == 0) {
|
---|
| 162 | errno = EINVAL;
|
---|
| 163 | return false;
|
---|
| 164 | }
|
---|
[cc6f688] | 165 |
|
---|
[2595dab] | 166 | if ((*mp == 'b') || (*mp == 't'))
|
---|
| 167 | mp++;
|
---|
[c9857c6] | 168 |
|
---|
[2595dab] | 169 | bool plus;
|
---|
| 170 | if (*mp == '+') {
|
---|
| 171 | mp++;
|
---|
| 172 | plus = true;
|
---|
| 173 | } else
|
---|
| 174 | plus = false;
|
---|
[566f4cfb] | 175 |
|
---|
[2595dab] | 176 | if (*mp != 0) {
|
---|
| 177 | errno = EINVAL;
|
---|
| 178 | return false;
|
---|
[350514c] | 179 | }
|
---|
[b19e892] | 180 |
|
---|
| 181 | *create = false;
|
---|
| 182 | *truncate = false;
|
---|
[350514c] | 183 |
|
---|
[b19e892] | 184 | /* Parse first character of fmode and determine mode for vfs_open(). */
|
---|
| 185 | switch (fmode[0]) {
|
---|
[2595dab] | 186 | case 'r':
|
---|
[b19e892] | 187 | *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
|
---|
[2595dab] | 188 | break;
|
---|
| 189 | case 'w':
|
---|
[b19e892] | 190 | *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
|
---|
| 191 | *create = true;
|
---|
| 192 | if (!plus)
|
---|
| 193 | *truncate = true;
|
---|
[2595dab] | 194 | break;
|
---|
| 195 | case 'a':
|
---|
| 196 | /* TODO: a+ must read from beginning, append to the end. */
|
---|
| 197 | if (plus) {
|
---|
| 198 | errno = ENOTSUP;
|
---|
| 199 | return false;
|
---|
| 200 | }
|
---|
[b19e892] | 201 |
|
---|
| 202 | *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
|
---|
| 203 | *create = true;
|
---|
[82582e4] | 204 | break;
|
---|
[2595dab] | 205 | default:
|
---|
| 206 | errno = EINVAL;
|
---|
| 207 | return false;
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | return true;
|
---|
[cc6f688] | 211 | }
|
---|
[b861b58] | 212 |
|
---|
[ef8bcc6] | 213 | /** Set stream buffer. */
|
---|
| 214 | void setvbuf(FILE *stream, void *buf, int mode, size_t size)
|
---|
| 215 | {
|
---|
| 216 | stream->btype = mode;
|
---|
| 217 | stream->buf = buf;
|
---|
| 218 | stream->buf_size = size;
|
---|
| 219 | stream->buf_head = stream->buf;
|
---|
[facebd56] | 220 | stream->buf_tail = stream->buf;
|
---|
| 221 | stream->buf_state = _bs_empty;
|
---|
[ef8bcc6] | 222 | }
|
---|
| 223 |
|
---|
[7699c21] | 224 | /** Set stream buffer.
|
---|
| 225 | *
|
---|
| 226 | * When @p buf is NULL, the stream is set as unbuffered, otherwise
|
---|
| 227 | * full buffering is enabled.
|
---|
| 228 | */
|
---|
| 229 | void setbuf(FILE *stream, void *buf)
|
---|
| 230 | {
|
---|
| 231 | if (buf == NULL) {
|
---|
| 232 | setvbuf(stream, NULL, _IONBF, BUFSIZ);
|
---|
| 233 | } else {
|
---|
| 234 | setvbuf(stream, buf, _IOFBF, BUFSIZ);
|
---|
| 235 | }
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[bfd247f] | 238 | static void _setvbuf(FILE *stream)
|
---|
| 239 | {
|
---|
| 240 | /* FIXME: Use more complex rules for setting buffering options. */
|
---|
| 241 |
|
---|
| 242 | switch (stream->fd) {
|
---|
| 243 | case 1:
|
---|
| 244 | setvbuf(stream, NULL, _IOLBF, BUFSIZ);
|
---|
| 245 | break;
|
---|
| 246 | case 0:
|
---|
| 247 | case 2:
|
---|
| 248 | setvbuf(stream, NULL, _IONBF, 0);
|
---|
| 249 | break;
|
---|
| 250 | default:
|
---|
| 251 | setvbuf(stream, NULL, _IOFBF, BUFSIZ);
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[ef8bcc6] | 255 | /** Allocate stream buffer. */
|
---|
| 256 | static int _fallocbuf(FILE *stream)
|
---|
| 257 | {
|
---|
| 258 | assert(stream->buf == NULL);
|
---|
[bfd247f] | 259 |
|
---|
[ef8bcc6] | 260 | stream->buf = malloc(stream->buf_size);
|
---|
| 261 | if (stream->buf == NULL) {
|
---|
| 262 | errno = ENOMEM;
|
---|
[6afc9d7] | 263 | return EOF;
|
---|
[ef8bcc6] | 264 | }
|
---|
[bfd247f] | 265 |
|
---|
[ef8bcc6] | 266 | stream->buf_head = stream->buf;
|
---|
[facebd56] | 267 | stream->buf_tail = stream->buf;
|
---|
[ef8bcc6] | 268 | return 0;
|
---|
| 269 | }
|
---|
| 270 |
|
---|
[2595dab] | 271 | /** Open a stream.
|
---|
| 272 | *
|
---|
| 273 | * @param path Path of the file to open.
|
---|
| 274 | * @param mode Mode string, (r|w|a)[b|t][+].
|
---|
| 275 | *
|
---|
[4e2cf8b] | 276 | */
|
---|
[b19e892] | 277 | FILE *fopen(const char *path, const char *fmode)
|
---|
[4e2cf8b] | 278 | {
|
---|
[b19e892] | 279 | int mode;
|
---|
| 280 | bool create;
|
---|
| 281 | bool truncate;
|
---|
| 282 |
|
---|
| 283 | if (!parse_mode(fmode, &mode, &create, &truncate))
|
---|
[2595dab] | 284 | return NULL;
|
---|
[4e2cf8b] | 285 |
|
---|
[2595dab] | 286 | /* Open file. */
|
---|
| 287 | FILE *stream = malloc(sizeof(FILE));
|
---|
| 288 | if (stream == NULL) {
|
---|
| 289 | errno = ENOMEM;
|
---|
| 290 | return NULL;
|
---|
| 291 | }
|
---|
[b19e892] | 292 |
|
---|
| 293 | int flags = WALK_REGULAR;
|
---|
| 294 | if (create)
|
---|
| 295 | flags |= WALK_MAY_CREATE;
|
---|
| 296 | int file = vfs_lookup(path, flags);
|
---|
| 297 | if (file < 0) {
|
---|
| 298 | errno = file;
|
---|
| 299 | free(stream);
|
---|
| 300 | return NULL;
|
---|
| 301 | }
|
---|
| 302 |
|
---|
| 303 | int rc = vfs_open(file, mode);
|
---|
| 304 | if (rc != EOK) {
|
---|
| 305 | errno = rc;
|
---|
[9c4cf0d] | 306 | vfs_put(file);
|
---|
[2595dab] | 307 | free(stream);
|
---|
| 308 | return NULL;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[b19e892] | 311 | if (truncate) {
|
---|
| 312 | rc = vfs_resize(file, 0);
|
---|
| 313 | if (rc != EOK) {
|
---|
| 314 | errno = rc;
|
---|
[9c4cf0d] | 315 | vfs_put(file);
|
---|
[b19e892] | 316 | free(stream);
|
---|
| 317 | return NULL;
|
---|
| 318 | }
|
---|
| 319 | }
|
---|
| 320 |
|
---|
| 321 | stream->fd = file;
|
---|
[58898d1d] | 322 | stream->pos = 0;
|
---|
[2595dab] | 323 | stream->error = false;
|
---|
| 324 | stream->eof = false;
|
---|
[6fa9a99d] | 325 | stream->kio = false;
|
---|
[79ae36dd] | 326 | stream->sess = NULL;
|
---|
[facebd56] | 327 | stream->need_sync = false;
|
---|
[bfd247f] | 328 | _setvbuf(stream);
|
---|
[e86a617a] | 329 | stream->ungetc_chars = 0;
|
---|
[2595dab] | 330 |
|
---|
[a68f737] | 331 | list_append(&stream->link, &files);
|
---|
| 332 |
|
---|
[2595dab] | 333 | return stream;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[080ad7f] | 336 | FILE *fdopen(int fd, const char *mode)
|
---|
| 337 | {
|
---|
| 338 | /* Open file. */
|
---|
| 339 | FILE *stream = malloc(sizeof(FILE));
|
---|
| 340 | if (stream == NULL) {
|
---|
| 341 | errno = ENOMEM;
|
---|
| 342 | return NULL;
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | stream->fd = fd;
|
---|
[58898d1d] | 346 | stream->pos = 0;
|
---|
[080ad7f] | 347 | stream->error = false;
|
---|
| 348 | stream->eof = false;
|
---|
[6fa9a99d] | 349 | stream->kio = false;
|
---|
[79ae36dd] | 350 | stream->sess = NULL;
|
---|
[facebd56] | 351 | stream->need_sync = false;
|
---|
[bfd247f] | 352 | _setvbuf(stream);
|
---|
[e86a617a] | 353 | stream->ungetc_chars = 0;
|
---|
[080ad7f] | 354 |
|
---|
| 355 | list_append(&stream->link, &files);
|
---|
| 356 |
|
---|
| 357 | return stream;
|
---|
| 358 | }
|
---|
| 359 |
|
---|
[80bee81] | 360 |
|
---|
| 361 | static int _fclose_nofree(FILE *stream)
|
---|
[2595dab] | 362 | {
|
---|
| 363 | int rc = 0;
|
---|
| 364 |
|
---|
| 365 | fflush(stream);
|
---|
| 366 |
|
---|
[79ae36dd] | 367 | if (stream->sess != NULL)
|
---|
| 368 | async_hangup(stream->sess);
|
---|
[2595dab] | 369 |
|
---|
| 370 | if (stream->fd >= 0)
|
---|
[9c4cf0d] | 371 | rc = vfs_put(stream->fd);
|
---|
[2595dab] | 372 |
|
---|
[a68f737] | 373 | list_remove(&stream->link);
|
---|
| 374 |
|
---|
[80bee81] | 375 | if (rc != 0) {
|
---|
| 376 | /* errno was set by close() */
|
---|
| 377 | return EOF;
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 | return 0;
|
---|
| 381 | }
|
---|
| 382 |
|
---|
| 383 | int fclose(FILE *stream)
|
---|
| 384 | {
|
---|
| 385 | int rc = _fclose_nofree(stream);
|
---|
| 386 |
|
---|
[a68f737] | 387 | if ((stream != &stdin_null)
|
---|
[6fa9a99d] | 388 | && (stream != &stdout_kio)
|
---|
| 389 | && (stream != &stderr_kio))
|
---|
[2595dab] | 390 | free(stream);
|
---|
| 391 |
|
---|
[80bee81] | 392 | return rc;
|
---|
| 393 | }
|
---|
| 394 |
|
---|
| 395 | FILE *freopen(const char *path, const char *mode, FILE *stream)
|
---|
| 396 | {
|
---|
| 397 | FILE *nstr;
|
---|
[2595dab] | 398 |
|
---|
[80bee81] | 399 | if (path == NULL) {
|
---|
| 400 | /* Changing mode is not supported */
|
---|
| 401 | return NULL;
|
---|
[2595dab] | 402 | }
|
---|
| 403 |
|
---|
[80bee81] | 404 | (void) _fclose_nofree(stream);
|
---|
| 405 | nstr = fopen(path, mode);
|
---|
| 406 | if (nstr == NULL) {
|
---|
| 407 | free(stream);
|
---|
| 408 | return NULL;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | list_remove(&nstr->link);
|
---|
| 412 | *stream = *nstr;
|
---|
| 413 | list_append(&stream->link, &files);
|
---|
| 414 |
|
---|
| 415 | free(nstr);
|
---|
| 416 |
|
---|
| 417 | return stream;
|
---|
[4e2cf8b] | 418 | }
|
---|
| 419 |
|
---|
[facebd56] | 420 | /** Read from a stream (unbuffered).
|
---|
[2595dab] | 421 | *
|
---|
| 422 | * @param buf Destination buffer.
|
---|
| 423 | * @param size Size of each record.
|
---|
| 424 | * @param nmemb Number of records to read.
|
---|
| 425 | * @param stream Pointer to the stream.
|
---|
[6afc9d7] | 426 | *
|
---|
| 427 | * @return Number of elements successfully read. On error this is less than
|
---|
| 428 | * nmemb, stream error indicator is set and errno is set.
|
---|
[4e2cf8b] | 429 | */
|
---|
[facebd56] | 430 | static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
[4e2cf8b] | 431 | {
|
---|
[8e3498b] | 432 | int rc;
|
---|
| 433 | size_t nread;
|
---|
| 434 |
|
---|
[002252a] | 435 | if (size == 0 || nmemb == 0)
|
---|
| 436 | return 0;
|
---|
| 437 |
|
---|
[8e3498b] | 438 | rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
|
---|
| 439 | if (rc != EOK) {
|
---|
| 440 | errno = rc;
|
---|
[58898d1d] | 441 | stream->error = true;
|
---|
[8e3498b] | 442 | } else if (nread == 0) {
|
---|
[58898d1d] | 443 | stream->eof = true;
|
---|
[2595dab] | 444 | }
|
---|
[8e3498b] | 445 |
|
---|
| 446 | return (nread / size);
|
---|
[2595dab] | 447 | }
|
---|
[55cff86] | 448 |
|
---|
[facebd56] | 449 | /** Write to a stream (unbuffered).
|
---|
| 450 | *
|
---|
| 451 | * @param buf Source buffer.
|
---|
| 452 | * @param size Size of each record.
|
---|
| 453 | * @param nmemb Number of records to write.
|
---|
| 454 | * @param stream Pointer to the stream.
|
---|
[6afc9d7] | 455 | *
|
---|
| 456 | * @return Number of elements successfully written. On error this is less than
|
---|
| 457 | * nmemb, stream error indicator is set and errno is set.
|
---|
[facebd56] | 458 | */
|
---|
[ef8bcc6] | 459 | static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
[2595dab] | 460 | {
|
---|
[8e3498b] | 461 | int rc;
|
---|
| 462 | size_t nwritten;
|
---|
| 463 |
|
---|
[002252a] | 464 | if (size == 0 || nmemb == 0)
|
---|
| 465 | return 0;
|
---|
| 466 |
|
---|
[58898d1d] | 467 | if (stream->kio) {
|
---|
[8e3498b] | 468 | rc = kio_write(buf, size * nmemb, &nwritten);
|
---|
| 469 | if (rc != EOK) {
|
---|
[58898d1d] | 470 | stream->error = true;
|
---|
[8e3498b] | 471 | nwritten = 0;
|
---|
[6afc9d7] | 472 | }
|
---|
[58898d1d] | 473 | } else {
|
---|
[8e3498b] | 474 | rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb,
|
---|
| 475 | &nwritten);
|
---|
| 476 | if (rc != EOK) {
|
---|
| 477 | errno = rc;
|
---|
[2595dab] | 478 | stream->error = true;
|
---|
| 479 | }
|
---|
| 480 | }
|
---|
[facebd56] | 481 |
|
---|
[8e3498b] | 482 | if (nwritten > 0)
|
---|
[facebd56] | 483 | stream->need_sync = true;
|
---|
[8e3498b] | 484 |
|
---|
| 485 | return (nwritten / size);
|
---|
[4e2cf8b] | 486 | }
|
---|
| 487 |
|
---|
[6afc9d7] | 488 | /** Read some data in stream buffer.
|
---|
| 489 | *
|
---|
| 490 | * On error, stream error indicator is set and errno is set.
|
---|
| 491 | */
|
---|
[facebd56] | 492 | static void _ffillbuf(FILE *stream)
|
---|
| 493 | {
|
---|
[8e3498b] | 494 | int rc;
|
---|
| 495 | size_t nread;
|
---|
[facebd56] | 496 |
|
---|
| 497 | stream->buf_head = stream->buf_tail = stream->buf;
|
---|
| 498 |
|
---|
[8e3498b] | 499 | rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
|
---|
| 500 | &nread);
|
---|
| 501 | if (rc != EOK) {
|
---|
[ce04ea44] | 502 | errno = rc;
|
---|
[facebd56] | 503 | stream->error = true;
|
---|
| 504 | return;
|
---|
| 505 | }
|
---|
| 506 |
|
---|
[8e3498b] | 507 | if (nread == 0) {
|
---|
[facebd56] | 508 | stream->eof = true;
|
---|
| 509 | return;
|
---|
| 510 | }
|
---|
| 511 |
|
---|
[8e3498b] | 512 | stream->buf_head += nread;
|
---|
[facebd56] | 513 | stream->buf_state = _bs_read;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | /** Write out stream buffer, do not sync stream. */
|
---|
[ef8bcc6] | 517 | static void _fflushbuf(FILE *stream)
|
---|
| 518 | {
|
---|
| 519 | size_t bytes_used;
|
---|
[facebd56] | 520 |
|
---|
[bfd247f] | 521 | if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
|
---|
[ef8bcc6] | 522 | return;
|
---|
[facebd56] | 523 |
|
---|
| 524 | bytes_used = stream->buf_head - stream->buf_tail;
|
---|
| 525 |
|
---|
| 526 | /* If buffer has prefetched read data, we need to seek back. */
|
---|
[58898d1d] | 527 | if (bytes_used > 0 && stream->buf_state == _bs_read)
|
---|
| 528 | stream->pos -= bytes_used;
|
---|
[facebd56] | 529 |
|
---|
| 530 | /* If buffer has unwritten data, we need to write them out. */
|
---|
[6afc9d7] | 531 | if (bytes_used > 0 && stream->buf_state == _bs_write) {
|
---|
[facebd56] | 532 | (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
|
---|
[6afc9d7] | 533 | /* On error stream error indicator and errno are set by _fwrite */
|
---|
| 534 | if (stream->error)
|
---|
| 535 | return;
|
---|
| 536 | }
|
---|
[facebd56] | 537 |
|
---|
[ef8bcc6] | 538 | stream->buf_head = stream->buf;
|
---|
[facebd56] | 539 | stream->buf_tail = stream->buf;
|
---|
| 540 | stream->buf_state = _bs_empty;
|
---|
[ef8bcc6] | 541 | }
|
---|
| 542 |
|
---|
[facebd56] | 543 | /** Read from a stream.
|
---|
| 544 | *
|
---|
| 545 | * @param dest Destination buffer.
|
---|
| 546 | * @param size Size of each record.
|
---|
| 547 | * @param nmemb Number of records to read.
|
---|
| 548 | * @param stream Pointer to the stream.
|
---|
| 549 | *
|
---|
| 550 | */
|
---|
| 551 | size_t fread(void *dest, size_t size, size_t nmemb, FILE *stream)
|
---|
| 552 | {
|
---|
| 553 | uint8_t *dp;
|
---|
| 554 | size_t bytes_left;
|
---|
| 555 | size_t now;
|
---|
| 556 | size_t data_avail;
|
---|
| 557 | size_t total_read;
|
---|
| 558 | size_t i;
|
---|
| 559 |
|
---|
| 560 | if (size == 0 || nmemb == 0)
|
---|
| 561 | return 0;
|
---|
| 562 |
|
---|
[bd5414e] | 563 | bytes_left = size * nmemb;
|
---|
| 564 | total_read = 0;
|
---|
| 565 | dp = (uint8_t *) dest;
|
---|
| 566 |
|
---|
| 567 | /* Bytes from ungetc() buffer */
|
---|
| 568 | while (stream->ungetc_chars > 0 && bytes_left > 0) {
|
---|
| 569 | *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
|
---|
| 570 | ++total_read;
|
---|
[a955fcc] | 571 | --bytes_left;
|
---|
[bd5414e] | 572 | }
|
---|
| 573 |
|
---|
[facebd56] | 574 | /* If not buffered stream, read in directly. */
|
---|
| 575 | if (stream->btype == _IONBF) {
|
---|
[bd5414e] | 576 | total_read += _fread(dest, 1, bytes_left, stream);
|
---|
| 577 | return total_read / size;
|
---|
[facebd56] | 578 | }
|
---|
| 579 |
|
---|
| 580 | /* Make sure no data is pending write. */
|
---|
| 581 | if (stream->buf_state == _bs_write)
|
---|
| 582 | _fflushbuf(stream);
|
---|
| 583 |
|
---|
| 584 | /* Perform lazy allocation of stream buffer. */
|
---|
| 585 | if (stream->buf == NULL) {
|
---|
| 586 | if (_fallocbuf(stream) != 0)
|
---|
| 587 | return 0; /* Errno set by _fallocbuf(). */
|
---|
| 588 | }
|
---|
| 589 |
|
---|
| 590 | while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
|
---|
| 591 | if (stream->buf_head == stream->buf_tail)
|
---|
| 592 | _ffillbuf(stream);
|
---|
| 593 |
|
---|
[6afc9d7] | 594 | if (stream->error || stream->eof) {
|
---|
| 595 | /* On error errno was set by _ffillbuf() */
|
---|
[facebd56] | 596 | break;
|
---|
[6afc9d7] | 597 | }
|
---|
[facebd56] | 598 |
|
---|
| 599 | data_avail = stream->buf_head - stream->buf_tail;
|
---|
| 600 |
|
---|
| 601 | if (bytes_left > data_avail)
|
---|
| 602 | now = data_avail;
|
---|
| 603 | else
|
---|
| 604 | now = bytes_left;
|
---|
| 605 |
|
---|
| 606 | for (i = 0; i < now; i++) {
|
---|
| 607 | dp[i] = stream->buf_tail[i];
|
---|
| 608 | }
|
---|
| 609 |
|
---|
| 610 | dp += now;
|
---|
| 611 | stream->buf_tail += now;
|
---|
| 612 | bytes_left -= now;
|
---|
| 613 | total_read += now;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | return (total_read / size);
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 |
|
---|
[ef8bcc6] | 620 | /** Write to a stream.
|
---|
| 621 | *
|
---|
| 622 | * @param buf Source buffer.
|
---|
| 623 | * @param size Size of each record.
|
---|
| 624 | * @param nmemb Number of records to write.
|
---|
| 625 | * @param stream Pointer to the stream.
|
---|
| 626 | *
|
---|
| 627 | */
|
---|
| 628 | size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
| 629 | {
|
---|
| 630 | uint8_t *data;
|
---|
| 631 | size_t bytes_left;
|
---|
| 632 | size_t now;
|
---|
| 633 | size_t buf_free;
|
---|
| 634 | size_t total_written;
|
---|
| 635 | size_t i;
|
---|
| 636 | uint8_t b;
|
---|
| 637 | bool need_flush;
|
---|
[002252a] | 638 |
|
---|
| 639 | if (size == 0 || nmemb == 0)
|
---|
| 640 | return 0;
|
---|
| 641 |
|
---|
[ef8bcc6] | 642 | /* If not buffered stream, write out directly. */
|
---|
[bfd247f] | 643 | if (stream->btype == _IONBF) {
|
---|
| 644 | now = _fwrite(buf, size, nmemb, stream);
|
---|
| 645 | fflush(stream);
|
---|
| 646 | return now;
|
---|
| 647 | }
|
---|
[facebd56] | 648 |
|
---|
| 649 | /* Make sure buffer contains no prefetched data. */
|
---|
| 650 | if (stream->buf_state == _bs_read)
|
---|
| 651 | _fflushbuf(stream);
|
---|
| 652 |
|
---|
[ef8bcc6] | 653 | /* Perform lazy allocation of stream buffer. */
|
---|
| 654 | if (stream->buf == NULL) {
|
---|
| 655 | if (_fallocbuf(stream) != 0)
|
---|
| 656 | return 0; /* Errno set by _fallocbuf(). */
|
---|
| 657 | }
|
---|
[bfd247f] | 658 |
|
---|
[ef8bcc6] | 659 | data = (uint8_t *) buf;
|
---|
| 660 | bytes_left = size * nmemb;
|
---|
| 661 | total_written = 0;
|
---|
| 662 | need_flush = false;
|
---|
[bfd247f] | 663 |
|
---|
| 664 | while ((!stream->error) && (bytes_left > 0)) {
|
---|
[ef8bcc6] | 665 | buf_free = stream->buf_size - (stream->buf_head - stream->buf);
|
---|
| 666 | if (bytes_left > buf_free)
|
---|
| 667 | now = buf_free;
|
---|
| 668 | else
|
---|
| 669 | now = bytes_left;
|
---|
[bfd247f] | 670 |
|
---|
[ef8bcc6] | 671 | for (i = 0; i < now; i++) {
|
---|
| 672 | b = data[i];
|
---|
| 673 | stream->buf_head[i] = b;
|
---|
[bfd247f] | 674 |
|
---|
| 675 | if ((b == '\n') && (stream->btype == _IOLBF))
|
---|
[ef8bcc6] | 676 | need_flush = true;
|
---|
| 677 | }
|
---|
[bfd247f] | 678 |
|
---|
[0803134] | 679 | data += now;
|
---|
[ef8bcc6] | 680 | stream->buf_head += now;
|
---|
| 681 | buf_free -= now;
|
---|
| 682 | bytes_left -= now;
|
---|
| 683 | total_written += now;
|
---|
[0803134] | 684 | stream->buf_state = _bs_write;
|
---|
[bfd247f] | 685 |
|
---|
[ef8bcc6] | 686 | if (buf_free == 0) {
|
---|
| 687 | /* Only need to drain buffer. */
|
---|
| 688 | _fflushbuf(stream);
|
---|
[6afc9d7] | 689 | if (!stream->error)
|
---|
| 690 | need_flush = false;
|
---|
[ef8bcc6] | 691 | }
|
---|
| 692 | }
|
---|
[facebd56] | 693 |
|
---|
[ef8bcc6] | 694 | if (need_flush)
|
---|
| 695 | fflush(stream);
|
---|
[bfd247f] | 696 |
|
---|
[ef8bcc6] | 697 | return (total_written / size);
|
---|
| 698 | }
|
---|
| 699 |
|
---|
[2595dab] | 700 | int fputc(wchar_t c, FILE *stream)
|
---|
[c9857c6] | 701 | {
|
---|
[56fa418] | 702 | char buf[STR_BOUNDS(1)];
|
---|
[2595dab] | 703 | size_t sz = 0;
|
---|
| 704 |
|
---|
| 705 | if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
|
---|
[2a5af223] | 706 | size_t wr = fwrite(buf, 1, sz, stream);
|
---|
[2595dab] | 707 |
|
---|
| 708 | if (wr < sz)
|
---|
| 709 | return EOF;
|
---|
| 710 |
|
---|
| 711 | return (int) c;
|
---|
| 712 | }
|
---|
| 713 |
|
---|
| 714 | return EOF;
|
---|
| 715 | }
|
---|
[56fa418] | 716 |
|
---|
[2595dab] | 717 | int putchar(wchar_t c)
|
---|
| 718 | {
|
---|
| 719 | return fputc(c, stdout);
|
---|
| 720 | }
|
---|
[56fa418] | 721 |
|
---|
[2595dab] | 722 | int fputs(const char *str, FILE *stream)
|
---|
| 723 | {
|
---|
[7db5cfd] | 724 | (void) fwrite(str, str_size(str), 1, stream);
|
---|
| 725 | if (ferror(stream))
|
---|
| 726 | return EOF;
|
---|
| 727 | return 0;
|
---|
[2595dab] | 728 | }
|
---|
[56fa418] | 729 |
|
---|
[2595dab] | 730 | int puts(const char *str)
|
---|
| 731 | {
|
---|
| 732 | return fputs(str, stdout);
|
---|
[c9857c6] | 733 | }
|
---|
[b27a97bb] | 734 |
|
---|
[2595dab] | 735 | int fgetc(FILE *stream)
|
---|
[b27a97bb] | 736 | {
|
---|
[2595dab] | 737 | char c;
|
---|
[bfd247f] | 738 |
|
---|
[bac82eeb] | 739 | /* This could be made faster by only flushing when needed. */
|
---|
[b8e57e8c] | 740 | if (stdout)
|
---|
| 741 | fflush(stdout);
|
---|
| 742 | if (stderr)
|
---|
| 743 | fflush(stderr);
|
---|
[bfd247f] | 744 |
|
---|
[2595dab] | 745 | if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
|
---|
| 746 | return EOF;
|
---|
[b27a97bb] | 747 |
|
---|
[2595dab] | 748 | return (int) c;
|
---|
| 749 | }
|
---|
| 750 |
|
---|
[c62d2e1] | 751 | char *fgets(char *str, int size, FILE *stream)
|
---|
| 752 | {
|
---|
[a634485] | 753 | int c;
|
---|
[c62d2e1] | 754 | int idx;
|
---|
| 755 |
|
---|
| 756 | idx = 0;
|
---|
| 757 | while (idx < size - 1) {
|
---|
| 758 | c = fgetc(stream);
|
---|
| 759 | if (c == EOF)
|
---|
| 760 | break;
|
---|
| 761 |
|
---|
| 762 | str[idx++] = c;
|
---|
| 763 |
|
---|
| 764 | if (c == '\n')
|
---|
| 765 | break;
|
---|
| 766 | }
|
---|
| 767 |
|
---|
| 768 | if (ferror(stream))
|
---|
| 769 | return NULL;
|
---|
| 770 |
|
---|
| 771 | if (idx == 0)
|
---|
| 772 | return NULL;
|
---|
| 773 |
|
---|
| 774 | str[idx] = '\0';
|
---|
| 775 | return str;
|
---|
| 776 | }
|
---|
| 777 |
|
---|
[2595dab] | 778 | int getchar(void)
|
---|
| 779 | {
|
---|
| 780 | return fgetc(stdin);
|
---|
[b27a97bb] | 781 | }
|
---|
| 782 |
|
---|
[bd5414e] | 783 | int ungetc(int c, FILE *stream)
|
---|
| 784 | {
|
---|
| 785 | if (c == EOF)
|
---|
| 786 | return EOF;
|
---|
| 787 |
|
---|
| 788 | if (stream->ungetc_chars >= UNGETC_MAX)
|
---|
| 789 | return EOF;
|
---|
| 790 |
|
---|
| 791 | stream->ungetc_buf[stream->ungetc_chars++] =
|
---|
| 792 | (uint8_t)c;
|
---|
| 793 |
|
---|
| 794 | stream->eof = false;
|
---|
| 795 | return (uint8_t)c;
|
---|
| 796 | }
|
---|
| 797 |
|
---|
[456c086] | 798 | int fseek(FILE *stream, long offset, int whence)
|
---|
[d2cc7e1] | 799 | {
|
---|
[23a0368] | 800 | int rc;
|
---|
| 801 |
|
---|
[6afc9d7] | 802 | if (stream->error)
|
---|
[58898d1d] | 803 | return -1;
|
---|
[6afc9d7] | 804 |
|
---|
[facebd56] | 805 | _fflushbuf(stream);
|
---|
[6afc9d7] | 806 | if (stream->error) {
|
---|
| 807 | /* errno was set by _fflushbuf() */
|
---|
[58898d1d] | 808 | return -1;
|
---|
[6afc9d7] | 809 | }
|
---|
| 810 |
|
---|
[bd5414e] | 811 | stream->ungetc_chars = 0;
|
---|
[facebd56] | 812 |
|
---|
[58898d1d] | 813 | struct stat st;
|
---|
| 814 | switch (whence) {
|
---|
| 815 | case SEEK_SET:
|
---|
| 816 | stream->pos = offset;
|
---|
| 817 | break;
|
---|
| 818 | case SEEK_CUR:
|
---|
| 819 | stream->pos += offset;
|
---|
| 820 | break;
|
---|
| 821 | case SEEK_END:
|
---|
[23a0368] | 822 | rc = vfs_stat(stream->fd, &st);
|
---|
| 823 | if (rc != EOK) {
|
---|
| 824 | errno = rc;
|
---|
[58898d1d] | 825 | stream->error = true;
|
---|
| 826 | return -1;
|
---|
| 827 | }
|
---|
| 828 | stream->pos = st.size + offset;
|
---|
| 829 | break;
|
---|
[2595dab] | 830 | }
|
---|
[facebd56] | 831 |
|
---|
[2595dab] | 832 | stream->eof = false;
|
---|
[566f4cfb] | 833 | return 0;
|
---|
[d2cc7e1] | 834 | }
|
---|
| 835 |
|
---|
[456c086] | 836 | long ftell(FILE *stream)
|
---|
[08232ee] | 837 | {
|
---|
[59f388a] | 838 | /* The native position is too large for the C99-ish interface. */
|
---|
[456c086] | 839 | if (stream->pos - stream->ungetc_chars > LONG_MAX)
|
---|
[59f388a] | 840 | return EOF;
|
---|
| 841 |
|
---|
[6afc9d7] | 842 | if (stream->error)
|
---|
| 843 | return EOF;
|
---|
| 844 |
|
---|
[8ad496d] | 845 | _fflushbuf(stream);
|
---|
[6afc9d7] | 846 | if (stream->error) {
|
---|
| 847 | /* errno was set by _fflushbuf() */
|
---|
| 848 | return EOF;
|
---|
| 849 | }
|
---|
| 850 |
|
---|
[58898d1d] | 851 | return stream->pos - stream->ungetc_chars;
|
---|
[08232ee] | 852 | }
|
---|
| 853 |
|
---|
[080ad7f] | 854 | void rewind(FILE *stream)
|
---|
| 855 | {
|
---|
| 856 | (void) fseek(stream, 0, SEEK_SET);
|
---|
| 857 | }
|
---|
| 858 |
|
---|
[2595dab] | 859 | int fflush(FILE *stream)
|
---|
| 860 | {
|
---|
[6afc9d7] | 861 | if (stream->error)
|
---|
| 862 | return EOF;
|
---|
| 863 |
|
---|
[ef8bcc6] | 864 | _fflushbuf(stream);
|
---|
[6afc9d7] | 865 | if (stream->error) {
|
---|
| 866 | /* errno was set by _fflushbuf() */
|
---|
| 867 | return EOF;
|
---|
| 868 | }
|
---|
[bfd247f] | 869 |
|
---|
[6fa9a99d] | 870 | if (stream->kio) {
|
---|
| 871 | kio_update();
|
---|
[6afc9d7] | 872 | return 0;
|
---|
[2595dab] | 873 | }
|
---|
| 874 |
|
---|
[79ae36dd] | 875 | if ((stream->fd >= 0) && (stream->need_sync)) {
|
---|
[a56cef9] | 876 | int rc;
|
---|
| 877 |
|
---|
[facebd56] | 878 | /**
|
---|
| 879 | * Better than syncing always, but probably still not the
|
---|
| 880 | * right thing to do.
|
---|
| 881 | */
|
---|
| 882 | stream->need_sync = false;
|
---|
[a56cef9] | 883 | rc = vfs_sync(stream->fd);
|
---|
| 884 | if (rc != EOK) {
|
---|
| 885 | errno = rc;
|
---|
[6afc9d7] | 886 | return EOF;
|
---|
| 887 | }
|
---|
| 888 |
|
---|
| 889 | return 0;
|
---|
[facebd56] | 890 | }
|
---|
[2595dab] | 891 |
|
---|
[6afc9d7] | 892 | return 0;
|
---|
[2595dab] | 893 | }
|
---|
| 894 |
|
---|
| 895 | int feof(FILE *stream)
|
---|
| 896 | {
|
---|
| 897 | return stream->eof;
|
---|
| 898 | }
|
---|
| 899 |
|
---|
| 900 | int ferror(FILE *stream)
|
---|
| 901 | {
|
---|
| 902 | return stream->error;
|
---|
| 903 | }
|
---|
| 904 |
|
---|
[c77a64f] | 905 | void clearerr(FILE *stream)
|
---|
| 906 | {
|
---|
| 907 | stream->eof = false;
|
---|
| 908 | stream->error = false;
|
---|
| 909 | }
|
---|
| 910 |
|
---|
[3629481] | 911 | int fileno(FILE *stream)
|
---|
| 912 | {
|
---|
[6fa9a99d] | 913 | if (stream->kio) {
|
---|
[3629481] | 914 | errno = EBADF;
|
---|
[6afc9d7] | 915 | return EOF;
|
---|
[3629481] | 916 | }
|
---|
| 917 |
|
---|
| 918 | return stream->fd;
|
---|
| 919 | }
|
---|
| 920 |
|
---|
[6afc9d7] | 921 | async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
|
---|
[2595dab] | 922 | {
|
---|
| 923 | if (stream->fd >= 0) {
|
---|
[79ae36dd] | 924 | if (stream->sess == NULL)
|
---|
[6afc9d7] | 925 | stream->sess = vfs_fd_session(stream->fd, iface);
|
---|
[2595dab] | 926 |
|
---|
[79ae36dd] | 927 | return stream->sess;
|
---|
[2595dab] | 928 | }
|
---|
| 929 |
|
---|
[79ae36dd] | 930 | return NULL;
|
---|
[2595dab] | 931 | }
|
---|
| 932 |
|
---|
[6afc9d7] | 933 | int vfs_fhandle(FILE *stream, int *handle)
|
---|
[2595dab] | 934 | {
|
---|
[7171760] | 935 | if (stream->fd >= 0) {
|
---|
| 936 | *handle = stream->fd;
|
---|
| 937 | return EOK;
|
---|
| 938 | }
|
---|
[a68f737] | 939 |
|
---|
| 940 | return ENOENT;
|
---|
[2595dab] | 941 | }
|
---|
| 942 |
|
---|
[fadd381] | 943 | /** @}
|
---|
[b2951e2] | 944 | */
|
---|