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