source: mainline/uspace/lib/c/generic/io/io.c@ 0245896

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export 0.7.2
Last change on this file since 0245896 was 01cc7b4, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Implement sscanf via virtualizing FILE and implementing string backend for FILE.

  • Property mode set to 100644
File size: 19.7 KB
RevLine 
[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]52static void _ffillbuf(FILE *stream);
[ef8bcc6]53static void _fflushbuf(FILE *stream);
54
[01cc7b4]55static size_t stdio_kio_read(void *, size_t, size_t, FILE *);
56static size_t stdio_kio_write(const void *, size_t, size_t, FILE *);
57static int stdio_kio_flush(FILE *);
58
59static size_t stdio_vfs_read(void *, size_t, size_t, FILE *);
60static size_t stdio_vfs_write(const void *, size_t, size_t, FILE *);
61
62static int stdio_vfs_flush(FILE *);
63
64/** KIO stream ops */
65static __stream_ops_t stdio_kio_ops = {
66 .read = stdio_kio_read,
67 .write = stdio_kio_write,
68 .flush = stdio_kio_flush
69};
70
71/** VFS stream ops */
72static __stream_ops_t stdio_vfs_ops = {
73 .read = stdio_vfs_read,
74 .write = stdio_vfs_write,
75 .flush = stdio_vfs_flush
76};
77
[a68f737]78static FILE stdin_null = {
[2595dab]79 .fd = -1,
[1b20da0]80 .pos = 0,
[2595dab]81 .error = true,
82 .eof = true,
[01cc7b4]83 .ops = &stdio_vfs_ops,
84 .arg = NULL,
[79ae36dd]85 .sess = NULL,
[ef8bcc6]86 .btype = _IONBF,
87 .buf = NULL,
88 .buf_size = 0,
[facebd56]89 .buf_head = NULL,
90 .buf_tail = NULL,
[c70703a]91 .buf_state = _bs_empty
[2595dab]92};
[350514c]93
[6fa9a99d]94static FILE stdout_kio = {
[2595dab]95 .fd = -1,
[1b20da0]96 .pos = 0,
[2595dab]97 .error = false,
98 .eof = false,
[01cc7b4]99 .ops = &stdio_kio_ops,
100 .arg = NULL,
[79ae36dd]101 .sess = NULL,
[ef8bcc6]102 .btype = _IOLBF,
103 .buf = NULL,
104 .buf_size = BUFSIZ,
[facebd56]105 .buf_head = NULL,
106 .buf_tail = NULL,
107 .buf_state = _bs_empty
[2595dab]108};
109
[6fa9a99d]110static FILE stderr_kio = {
[a68f737]111 .fd = -1,
[1b20da0]112 .pos = 0,
[a68f737]113 .error = false,
114 .eof = false,
[01cc7b4]115 .ops = &stdio_kio_ops,
116 .arg = NULL,
[79ae36dd]117 .sess = NULL,
[ef8bcc6]118 .btype = _IONBF,
119 .buf = NULL,
120 .buf_size = 0,
[facebd56]121 .buf_head = NULL,
122 .buf_tail = NULL,
123 .buf_state = _bs_empty
[a68f737]124};
125
126FILE *stdin = NULL;
127FILE *stdout = NULL;
128FILE *stderr = NULL;
129
130static LIST_INITIALIZE(files);
131
[bb9ec2d]132void __stdio_init(void)
[a68f737]133{
[7c3fb9b]134 /*
135 * The first three standard file descriptors are assigned for compatibility.
[bb9ec2d]136 * This will probably be removed later.
137 */
138 int infd = inbox_get("stdin");
139 if (infd >= 0) {
[f77c1c9]140 int stdinfd = -1;
141 (void) vfs_clone(infd, -1, false, &stdinfd);
[bb9ec2d]142 assert(stdinfd == 0);
[b19e892]143 vfs_open(stdinfd, MODE_READ);
[bb9ec2d]144 stdin = fdopen(stdinfd, "r");
[a68f737]145 } else {
146 stdin = &stdin_null;
147 list_append(&stdin->link, &files);
148 }
[a35b458]149
[bb9ec2d]150 int outfd = inbox_get("stdout");
151 if (outfd >= 0) {
[f77c1c9]152 int stdoutfd = -1;
153 (void) vfs_clone(outfd, -1, false, &stdoutfd);
[bb9ec2d]154 assert(stdoutfd <= 1);
[fcab7ef]155 while (stdoutfd < 1)
[f77c1c9]156 (void) vfs_clone(outfd, -1, false, &stdoutfd);
[b19e892]157 vfs_open(stdoutfd, MODE_APPEND);
[bb9ec2d]158 stdout = fdopen(stdoutfd, "a");
[a68f737]159 } else {
[6fa9a99d]160 stdout = &stdout_kio;
[a68f737]161 list_append(&stdout->link, &files);
162 }
[a35b458]163
[bb9ec2d]164 int errfd = inbox_get("stderr");
165 if (errfd >= 0) {
[f77c1c9]166 int stderrfd = -1;
167 (void) vfs_clone(errfd, -1, false, &stderrfd);
[bb9ec2d]168 assert(stderrfd <= 2);
[fcab7ef]169 while (stderrfd < 2)
[f77c1c9]170 (void) vfs_clone(errfd, -1, false, &stderrfd);
[b19e892]171 vfs_open(stderrfd, MODE_APPEND);
[bb9ec2d]172 stderr = fdopen(stderrfd, "a");
[a68f737]173 } else {
[6fa9a99d]174 stderr = &stderr_kio;
[a68f737]175 list_append(&stderr->link, &files);
176 }
177}
178
[db24058]179void __stdio_done(void)
[a68f737]180{
[b72efe8]181 while (!list_empty(&files)) {
182 FILE *file = list_get_instance(list_first(&files), FILE, link);
[a68f737]183 fclose(file);
184 }
185}
[2595dab]186
[b19e892]187static bool parse_mode(const char *fmode, int *mode, bool *create, bool *truncate)
[b861b58]188{
[2595dab]189 /* Parse mode except first character. */
[b19e892]190 const char *mp = fmode;
[2595dab]191 if (*mp++ == 0) {
192 errno = EINVAL;
193 return false;
194 }
[a35b458]195
[2595dab]196 if ((*mp == 'b') || (*mp == 't'))
197 mp++;
[a35b458]198
[2595dab]199 bool plus;
200 if (*mp == '+') {
201 mp++;
202 plus = true;
203 } else
204 plus = false;
[a35b458]205
[2595dab]206 if (*mp != 0) {
207 errno = EINVAL;
208 return false;
[350514c]209 }
[b19e892]210
211 *create = false;
212 *truncate = false;
[a35b458]213
[b19e892]214 /* Parse first character of fmode and determine mode for vfs_open(). */
215 switch (fmode[0]) {
[2595dab]216 case 'r':
[b19e892]217 *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
[2595dab]218 break;
219 case 'w':
[b19e892]220 *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
221 *create = true;
222 if (!plus)
223 *truncate = true;
[2595dab]224 break;
225 case 'a':
226 /* TODO: a+ must read from beginning, append to the end. */
227 if (plus) {
228 errno = ENOTSUP;
229 return false;
230 }
[b19e892]231
232 *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
233 *create = true;
[82582e4]234 break;
[2595dab]235 default:
236 errno = EINVAL;
237 return false;
238 }
[a35b458]239
[2595dab]240 return true;
[cc6f688]241}
[b861b58]242
[ef8bcc6]243/** Set stream buffer. */
244void setvbuf(FILE *stream, void *buf, int mode, size_t size)
245{
246 stream->btype = mode;
247 stream->buf = buf;
248 stream->buf_size = size;
249 stream->buf_head = stream->buf;
[facebd56]250 stream->buf_tail = stream->buf;
251 stream->buf_state = _bs_empty;
[ef8bcc6]252}
253
[7699c21]254/** Set stream buffer.
255 *
256 * When @p buf is NULL, the stream is set as unbuffered, otherwise
257 * full buffering is enabled.
258 */
259void setbuf(FILE *stream, void *buf)
260{
261 if (buf == NULL) {
262 setvbuf(stream, NULL, _IONBF, BUFSIZ);
263 } else {
264 setvbuf(stream, buf, _IOFBF, BUFSIZ);
265 }
266}
267
[bfd247f]268static void _setvbuf(FILE *stream)
269{
270 /* FIXME: Use more complex rules for setting buffering options. */
[a35b458]271
[bfd247f]272 switch (stream->fd) {
273 case 1:
274 setvbuf(stream, NULL, _IOLBF, BUFSIZ);
275 break;
276 case 0:
277 case 2:
278 setvbuf(stream, NULL, _IONBF, 0);
279 break;
280 default:
281 setvbuf(stream, NULL, _IOFBF, BUFSIZ);
282 }
283}
284
[ef8bcc6]285/** Allocate stream buffer. */
286static int _fallocbuf(FILE *stream)
287{
288 assert(stream->buf == NULL);
[a35b458]289
[ef8bcc6]290 stream->buf = malloc(stream->buf_size);
291 if (stream->buf == NULL) {
292 errno = ENOMEM;
[6afc9d7]293 return EOF;
[ef8bcc6]294 }
[a35b458]295
[ef8bcc6]296 stream->buf_head = stream->buf;
[facebd56]297 stream->buf_tail = stream->buf;
[ef8bcc6]298 return 0;
299}
300
[2595dab]301/** Open a stream.
302 *
303 * @param path Path of the file to open.
304 * @param mode Mode string, (r|w|a)[b|t][+].
305 *
[4e2cf8b]306 */
[b19e892]307FILE *fopen(const char *path, const char *fmode)
[4e2cf8b]308{
[b19e892]309 int mode;
310 bool create;
311 bool truncate;
312
313 if (!parse_mode(fmode, &mode, &create, &truncate))
[2595dab]314 return NULL;
[a35b458]315
[2595dab]316 /* Open file. */
317 FILE *stream = malloc(sizeof(FILE));
318 if (stream == NULL) {
319 errno = ENOMEM;
320 return NULL;
321 }
[b19e892]322
323 int flags = WALK_REGULAR;
324 if (create)
325 flags |= WALK_MAY_CREATE;
[f77c1c9]326 int file;
[b7fd2a0]327 errno_t rc = vfs_lookup(path, flags, &file);
[f77c1c9]328 if (rc != EOK) {
329 errno = rc;
[b19e892]330 free(stream);
331 return NULL;
332 }
333
[f77c1c9]334 rc = vfs_open(file, mode);
[b19e892]335 if (rc != EOK) {
336 errno = rc;
[9c4cf0d]337 vfs_put(file);
[2595dab]338 free(stream);
339 return NULL;
340 }
[a35b458]341
[b19e892]342 if (truncate) {
343 rc = vfs_resize(file, 0);
344 if (rc != EOK) {
345 errno = rc;
[9c4cf0d]346 vfs_put(file);
[b19e892]347 free(stream);
348 return NULL;
349 }
350 }
351
352 stream->fd = file;
[58898d1d]353 stream->pos = 0;
[2595dab]354 stream->error = false;
355 stream->eof = false;
[01cc7b4]356 stream->ops = &stdio_vfs_ops;
357 stream->arg = NULL;
[79ae36dd]358 stream->sess = NULL;
[facebd56]359 stream->need_sync = false;
[bfd247f]360 _setvbuf(stream);
[e86a617a]361 stream->ungetc_chars = 0;
[a35b458]362
[a68f737]363 list_append(&stream->link, &files);
[a35b458]364
[2595dab]365 return stream;
366}
367
[080ad7f]368FILE *fdopen(int fd, const char *mode)
369{
370 /* Open file. */
371 FILE *stream = malloc(sizeof(FILE));
372 if (stream == NULL) {
373 errno = ENOMEM;
374 return NULL;
375 }
[a35b458]376
[080ad7f]377 stream->fd = fd;
[58898d1d]378 stream->pos = 0;
[080ad7f]379 stream->error = false;
380 stream->eof = false;
[01cc7b4]381 stream->ops = &stdio_vfs_ops;
382 stream->arg = NULL;
[79ae36dd]383 stream->sess = NULL;
[facebd56]384 stream->need_sync = false;
[bfd247f]385 _setvbuf(stream);
[e86a617a]386 stream->ungetc_chars = 0;
[a35b458]387
[080ad7f]388 list_append(&stream->link, &files);
[a35b458]389
[080ad7f]390 return stream;
391}
392
[80bee81]393
394static int _fclose_nofree(FILE *stream)
[2595dab]395{
[b7fd2a0]396 errno_t rc = 0;
[a35b458]397
[2595dab]398 fflush(stream);
[a35b458]399
[79ae36dd]400 if (stream->sess != NULL)
401 async_hangup(stream->sess);
[a35b458]402
[2595dab]403 if (stream->fd >= 0)
[9c4cf0d]404 rc = vfs_put(stream->fd);
[a35b458]405
[a68f737]406 list_remove(&stream->link);
[a35b458]407
[f77c1c9]408 if (rc != EOK) {
409 errno = rc;
[80bee81]410 return EOF;
411 }
[a35b458]412
[80bee81]413 return 0;
414}
415
416int fclose(FILE *stream)
417{
418 int rc = _fclose_nofree(stream);
[a35b458]419
[3bacee1]420 if ((stream != &stdin_null) &&
421 (stream != &stdout_kio) &&
422 (stream != &stderr_kio))
[2595dab]423 free(stream);
[a35b458]424
[80bee81]425 return rc;
426}
427
428FILE *freopen(const char *path, const char *mode, FILE *stream)
429{
430 FILE *nstr;
[a35b458]431
[80bee81]432 if (path == NULL) {
433 /* Changing mode is not supported */
434 return NULL;
[2595dab]435 }
[a35b458]436
[80bee81]437 (void) _fclose_nofree(stream);
438 nstr = fopen(path, mode);
439 if (nstr == NULL) {
440 free(stream);
441 return NULL;
442 }
[a35b458]443
[80bee81]444 list_remove(&nstr->link);
445 *stream = *nstr;
446 list_append(&stream->link, &files);
[a35b458]447
[80bee81]448 free(nstr);
[a35b458]449
[80bee81]450 return stream;
[4e2cf8b]451}
452
[facebd56]453/** Read from a stream (unbuffered).
[2595dab]454 *
455 * @param buf Destination buffer.
456 * @param size Size of each record.
457 * @param nmemb Number of records to read.
458 * @param stream Pointer to the stream.
[6afc9d7]459 *
460 * @return Number of elements successfully read. On error this is less than
461 * nmemb, stream error indicator is set and errno is set.
[4e2cf8b]462 */
[facebd56]463static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
[4e2cf8b]464{
[01cc7b4]465 return stream->ops->read(buf, size, nmemb, stream);
[2595dab]466}
[55cff86]467
[facebd56]468/** Write to a stream (unbuffered).
469 *
470 * @param buf Source buffer.
471 * @param size Size of each record.
472 * @param nmemb Number of records to write.
473 * @param stream Pointer to the stream.
[6afc9d7]474 *
475 * @return Number of elements successfully written. On error this is less than
476 * nmemb, stream error indicator is set and errno is set.
[facebd56]477 */
[ef8bcc6]478static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
[2595dab]479{
[8e3498b]480 size_t nwritten;
481
[002252a]482 if (size == 0 || nmemb == 0)
483 return 0;
484
[01cc7b4]485 nwritten = stream->ops->write(buf, size, nmemb, stream);
[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]497static 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]522static 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 */
556size_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 */
633size_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]705wint_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
722wint_t putwchar(wchar_t wc)
723{
724 return fputwc(wc, stdout);
725}
726
727int 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]740int putchar(int c)
[2595dab]741{
742 return fputc(c, stdout);
743}
[56fa418]744
[2595dab]745int 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]753int puts(const char *str)
754{
[1abcf1d]755 if (fputs(str, stdout) < 0)
756 return EOF;
757 return putchar('\n');
[c9857c6]758}
[b27a97bb]759
[2595dab]760int 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]776char *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]803int getchar(void)
804{
805 return fgetc(stdin);
[b27a97bb]806}
807
[bd5414e]808int 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]823int 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]861off64_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]875int fseek(FILE *stream, long offset, int whence)
876{
877 return fseek64(stream, offset, whence);
878}
879
880long 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]891void rewind(FILE *stream)
892{
893 (void) fseek(stream, 0, SEEK_SET);
894}
895
[2595dab]896int 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
[01cc7b4]907 if (stream->need_sync) {
[facebd56]908 /**
909 * Better than syncing always, but probably still not the
910 * right thing to do.
911 */
[01cc7b4]912 if (stream->ops->flush(stream) == EOF)
[6afc9d7]913 return EOF;
914
[01cc7b4]915 stream->need_sync = false;
[facebd56]916 }
[a35b458]917
[6afc9d7]918 return 0;
[2595dab]919}
920
921int feof(FILE *stream)
922{
923 return stream->eof;
924}
925
926int ferror(FILE *stream)
927{
928 return stream->error;
929}
930
[c77a64f]931void clearerr(FILE *stream)
932{
933 stream->eof = false;
934 stream->error = false;
935}
936
[3629481]937int fileno(FILE *stream)
938{
[01cc7b4]939 if (stream->ops != &stdio_vfs_ops) {
[3629481]940 errno = EBADF;
[6afc9d7]941 return EOF;
[3629481]942 }
[a35b458]943
[3629481]944 return stream->fd;
945}
946
[6afc9d7]947async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
[2595dab]948{
949 if (stream->fd >= 0) {
[79ae36dd]950 if (stream->sess == NULL)
[6afc9d7]951 stream->sess = vfs_fd_session(stream->fd, iface);
[a35b458]952
[79ae36dd]953 return stream->sess;
[2595dab]954 }
[a35b458]955
[79ae36dd]956 return NULL;
[2595dab]957}
958
[b7fd2a0]959errno_t vfs_fhandle(FILE *stream, int *handle)
[2595dab]960{
[7171760]961 if (stream->fd >= 0) {
962 *handle = stream->fd;
963 return EOK;
964 }
[a35b458]965
[a68f737]966 return ENOENT;
[2595dab]967}
968
[01cc7b4]969/** Read from KIO stream. */
970static size_t stdio_kio_read(void *buf, size_t size, size_t nmemb, FILE *stream)
971{
972 stream->eof = true;
973 return 0;
974}
975
976/** Write to KIO stream. */
977static size_t stdio_kio_write(const void *buf, size_t size, size_t nmemb,
978 FILE *stream)
979{
980 errno_t rc;
981 size_t nwritten;
982
983 rc = kio_write(buf, size * nmemb, &nwritten);
984 if (rc != EOK) {
985 stream->error = true;
986 nwritten = 0;
987 }
988
989 return nwritten / size;
990}
991
992/** Flush KIO stream. */
993static int stdio_kio_flush(FILE *stream)
994{
995 kio_update();
996 return 0;
997}
998
999/** Read from VFS stream. */
1000static size_t stdio_vfs_read(void *buf, size_t size, size_t nmemb, FILE *stream)
1001{
1002 errno_t rc;
1003 size_t nread;
1004
1005 if (size == 0 || nmemb == 0)
1006 return 0;
1007
1008 rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
1009 if (rc != EOK) {
1010 errno = rc;
1011 stream->error = true;
1012 } else if (nread == 0) {
1013 stream->eof = true;
1014 }
1015
1016 return (nread / size);
1017}
1018
1019/** Write to VFS stream. */
1020static size_t stdio_vfs_write(const void *buf, size_t size, size_t nmemb,
1021 FILE *stream)
1022{
1023 errno_t rc;
1024 size_t nwritten;
1025
1026 rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb, &nwritten);
1027 if (rc != EOK) {
1028 errno = rc;
1029 stream->error = true;
1030 }
1031
1032 return nwritten / size;
1033}
1034
1035/** Flush VFS stream. */
1036static int stdio_vfs_flush(FILE *stream)
1037{
1038 errno_t rc;
1039
1040 rc = vfs_sync(stream->fd);
1041 if (rc != EOK) {
1042 errno = rc;
1043 return EOF;
1044 }
1045
1046 return 0;
1047}
1048
[fadd381]1049/** @}
[b2951e2]1050 */
Note: See TracBrowser for help on using the repository browser.