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