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