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