source: mainline/uspace/lib/c/generic/io/io.c@ 58daded

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

setvbuf should return an integer.

  • Property mode set to 100644
File size: 19.8 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. */
[58daded]244int setvbuf(FILE *stream, void *buf, int mode, size_t size)
[ef8bcc6]245{
[58daded]246 if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF)
247 return -1;
248
[ef8bcc6]249 stream->btype = mode;
250 stream->buf = buf;
251 stream->buf_size = size;
252 stream->buf_head = stream->buf;
[facebd56]253 stream->buf_tail = stream->buf;
254 stream->buf_state = _bs_empty;
[58daded]255
256 return 0;
[ef8bcc6]257}
258
[7699c21]259/** Set stream buffer.
260 *
261 * When @p buf is NULL, the stream is set as unbuffered, otherwise
262 * full buffering is enabled.
263 */
264void setbuf(FILE *stream, void *buf)
265{
266 if (buf == NULL) {
267 setvbuf(stream, NULL, _IONBF, BUFSIZ);
268 } else {
269 setvbuf(stream, buf, _IOFBF, BUFSIZ);
270 }
271}
272
[bfd247f]273static void _setvbuf(FILE *stream)
274{
275 /* FIXME: Use more complex rules for setting buffering options. */
[a35b458]276
[bfd247f]277 switch (stream->fd) {
278 case 1:
279 setvbuf(stream, NULL, _IOLBF, BUFSIZ);
280 break;
281 case 0:
282 case 2:
283 setvbuf(stream, NULL, _IONBF, 0);
284 break;
285 default:
286 setvbuf(stream, NULL, _IOFBF, BUFSIZ);
287 }
288}
289
[ef8bcc6]290/** Allocate stream buffer. */
291static int _fallocbuf(FILE *stream)
292{
293 assert(stream->buf == NULL);
[a35b458]294
[ef8bcc6]295 stream->buf = malloc(stream->buf_size);
296 if (stream->buf == NULL) {
297 errno = ENOMEM;
[6afc9d7]298 return EOF;
[ef8bcc6]299 }
[a35b458]300
[ef8bcc6]301 stream->buf_head = stream->buf;
[facebd56]302 stream->buf_tail = stream->buf;
[ef8bcc6]303 return 0;
304}
305
[2595dab]306/** Open a stream.
307 *
308 * @param path Path of the file to open.
309 * @param mode Mode string, (r|w|a)[b|t][+].
310 *
[4e2cf8b]311 */
[b19e892]312FILE *fopen(const char *path, const char *fmode)
[4e2cf8b]313{
[b19e892]314 int mode;
315 bool create;
316 bool truncate;
317
318 if (!parse_mode(fmode, &mode, &create, &truncate))
[2595dab]319 return NULL;
[a35b458]320
[2595dab]321 /* Open file. */
322 FILE *stream = malloc(sizeof(FILE));
323 if (stream == NULL) {
324 errno = ENOMEM;
325 return NULL;
326 }
[b19e892]327
328 int flags = WALK_REGULAR;
329 if (create)
330 flags |= WALK_MAY_CREATE;
[f77c1c9]331 int file;
[b7fd2a0]332 errno_t rc = vfs_lookup(path, flags, &file);
[f77c1c9]333 if (rc != EOK) {
334 errno = rc;
[b19e892]335 free(stream);
336 return NULL;
337 }
338
[f77c1c9]339 rc = vfs_open(file, mode);
[b19e892]340 if (rc != EOK) {
341 errno = rc;
[9c4cf0d]342 vfs_put(file);
[2595dab]343 free(stream);
344 return NULL;
345 }
[a35b458]346
[b19e892]347 if (truncate) {
348 rc = vfs_resize(file, 0);
349 if (rc != EOK) {
350 errno = rc;
[9c4cf0d]351 vfs_put(file);
[b19e892]352 free(stream);
353 return NULL;
354 }
355 }
356
357 stream->fd = file;
[58898d1d]358 stream->pos = 0;
[2595dab]359 stream->error = false;
360 stream->eof = false;
[01cc7b4]361 stream->ops = &stdio_vfs_ops;
362 stream->arg = NULL;
[79ae36dd]363 stream->sess = NULL;
[facebd56]364 stream->need_sync = false;
[bfd247f]365 _setvbuf(stream);
[e86a617a]366 stream->ungetc_chars = 0;
[a35b458]367
[a68f737]368 list_append(&stream->link, &files);
[a35b458]369
[2595dab]370 return stream;
371}
372
[080ad7f]373FILE *fdopen(int fd, const char *mode)
374{
375 /* Open file. */
376 FILE *stream = malloc(sizeof(FILE));
377 if (stream == NULL) {
378 errno = ENOMEM;
379 return NULL;
380 }
[a35b458]381
[080ad7f]382 stream->fd = fd;
[58898d1d]383 stream->pos = 0;
[080ad7f]384 stream->error = false;
385 stream->eof = false;
[01cc7b4]386 stream->ops = &stdio_vfs_ops;
387 stream->arg = NULL;
[79ae36dd]388 stream->sess = NULL;
[facebd56]389 stream->need_sync = false;
[bfd247f]390 _setvbuf(stream);
[e86a617a]391 stream->ungetc_chars = 0;
[a35b458]392
[080ad7f]393 list_append(&stream->link, &files);
[a35b458]394
[080ad7f]395 return stream;
396}
397
[80bee81]398
399static int _fclose_nofree(FILE *stream)
[2595dab]400{
[b7fd2a0]401 errno_t rc = 0;
[a35b458]402
[2595dab]403 fflush(stream);
[a35b458]404
[79ae36dd]405 if (stream->sess != NULL)
406 async_hangup(stream->sess);
[a35b458]407
[2595dab]408 if (stream->fd >= 0)
[9c4cf0d]409 rc = vfs_put(stream->fd);
[a35b458]410
[a68f737]411 list_remove(&stream->link);
[a35b458]412
[f77c1c9]413 if (rc != EOK) {
414 errno = rc;
[80bee81]415 return EOF;
416 }
[a35b458]417
[80bee81]418 return 0;
419}
420
421int fclose(FILE *stream)
422{
423 int rc = _fclose_nofree(stream);
[a35b458]424
[3bacee1]425 if ((stream != &stdin_null) &&
426 (stream != &stdout_kio) &&
427 (stream != &stderr_kio))
[2595dab]428 free(stream);
[a35b458]429
[80bee81]430 return rc;
431}
432
433FILE *freopen(const char *path, const char *mode, FILE *stream)
434{
435 FILE *nstr;
[a35b458]436
[80bee81]437 if (path == NULL) {
438 /* Changing mode is not supported */
439 return NULL;
[2595dab]440 }
[a35b458]441
[80bee81]442 (void) _fclose_nofree(stream);
443 nstr = fopen(path, mode);
444 if (nstr == NULL) {
445 free(stream);
446 return NULL;
447 }
[a35b458]448
[80bee81]449 list_remove(&nstr->link);
450 *stream = *nstr;
451 list_append(&stream->link, &files);
[a35b458]452
[80bee81]453 free(nstr);
[a35b458]454
[80bee81]455 return stream;
[4e2cf8b]456}
457
[facebd56]458/** Read from a stream (unbuffered).
[2595dab]459 *
460 * @param buf Destination buffer.
461 * @param size Size of each record.
462 * @param nmemb Number of records to read.
463 * @param stream Pointer to the stream.
[6afc9d7]464 *
465 * @return Number of elements successfully read. On error this is less than
466 * nmemb, stream error indicator is set and errno is set.
[4e2cf8b]467 */
[facebd56]468static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
[4e2cf8b]469{
[01cc7b4]470 return stream->ops->read(buf, size, nmemb, stream);
[2595dab]471}
[55cff86]472
[facebd56]473/** Write to a stream (unbuffered).
474 *
475 * @param buf Source buffer.
476 * @param size Size of each record.
477 * @param nmemb Number of records to write.
478 * @param stream Pointer to the stream.
[6afc9d7]479 *
480 * @return Number of elements successfully written. On error this is less than
481 * nmemb, stream error indicator is set and errno is set.
[facebd56]482 */
[ef8bcc6]483static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
[2595dab]484{
[8e3498b]485 size_t nwritten;
486
[002252a]487 if (size == 0 || nmemb == 0)
488 return 0;
489
[01cc7b4]490 nwritten = stream->ops->write(buf, size, nmemb, stream);
[facebd56]491
[8e3498b]492 if (nwritten > 0)
[facebd56]493 stream->need_sync = true;
[8e3498b]494
495 return (nwritten / size);
[4e2cf8b]496}
497
[6afc9d7]498/** Read some data in stream buffer.
499 *
500 * On error, stream error indicator is set and errno is set.
501 */
[facebd56]502static void _ffillbuf(FILE *stream)
503{
[b7fd2a0]504 errno_t rc;
[8e3498b]505 size_t nread;
[facebd56]506
507 stream->buf_head = stream->buf_tail = stream->buf;
508
[8e3498b]509 rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
510 &nread);
511 if (rc != EOK) {
[ce04ea44]512 errno = rc;
[facebd56]513 stream->error = true;
514 return;
515 }
516
[8e3498b]517 if (nread == 0) {
[facebd56]518 stream->eof = true;
519 return;
520 }
521
[8e3498b]522 stream->buf_head += nread;
[facebd56]523 stream->buf_state = _bs_read;
524}
525
526/** Write out stream buffer, do not sync stream. */
[ef8bcc6]527static void _fflushbuf(FILE *stream)
528{
529 size_t bytes_used;
[facebd56]530
[bfd247f]531 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
[ef8bcc6]532 return;
[facebd56]533
534 bytes_used = stream->buf_head - stream->buf_tail;
535
536 /* If buffer has prefetched read data, we need to seek back. */
[58898d1d]537 if (bytes_used > 0 && stream->buf_state == _bs_read)
538 stream->pos -= bytes_used;
[facebd56]539
540 /* If buffer has unwritten data, we need to write them out. */
[6afc9d7]541 if (bytes_used > 0 && stream->buf_state == _bs_write) {
[facebd56]542 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
[6afc9d7]543 /* On error stream error indicator and errno are set by _fwrite */
544 if (stream->error)
545 return;
546 }
[facebd56]547
[ef8bcc6]548 stream->buf_head = stream->buf;
[facebd56]549 stream->buf_tail = stream->buf;
550 stream->buf_state = _bs_empty;
[ef8bcc6]551}
552
[facebd56]553/** Read from a stream.
554 *
555 * @param dest Destination buffer.
556 * @param size Size of each record.
557 * @param nmemb Number of records to read.
558 * @param stream Pointer to the stream.
559 *
560 */
561size_t fread(void *dest, size_t size, size_t nmemb, FILE *stream)
562{
563 uint8_t *dp;
564 size_t bytes_left;
565 size_t now;
566 size_t data_avail;
567 size_t total_read;
568 size_t i;
569
570 if (size == 0 || nmemb == 0)
571 return 0;
572
[bd5414e]573 bytes_left = size * nmemb;
574 total_read = 0;
575 dp = (uint8_t *) dest;
576
577 /* Bytes from ungetc() buffer */
578 while (stream->ungetc_chars > 0 && bytes_left > 0) {
579 *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
580 ++total_read;
[a955fcc]581 --bytes_left;
[bd5414e]582 }
583
[facebd56]584 /* If not buffered stream, read in directly. */
585 if (stream->btype == _IONBF) {
[bd5414e]586 total_read += _fread(dest, 1, bytes_left, stream);
587 return total_read / size;
[facebd56]588 }
589
590 /* Make sure no data is pending write. */
591 if (stream->buf_state == _bs_write)
592 _fflushbuf(stream);
593
594 /* Perform lazy allocation of stream buffer. */
595 if (stream->buf == NULL) {
596 if (_fallocbuf(stream) != 0)
597 return 0; /* Errno set by _fallocbuf(). */
598 }
599
600 while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
601 if (stream->buf_head == stream->buf_tail)
602 _ffillbuf(stream);
603
[6afc9d7]604 if (stream->error || stream->eof) {
605 /* On error errno was set by _ffillbuf() */
[facebd56]606 break;
[6afc9d7]607 }
[facebd56]608
609 data_avail = stream->buf_head - stream->buf_tail;
610
611 if (bytes_left > data_avail)
612 now = data_avail;
613 else
614 now = bytes_left;
615
616 for (i = 0; i < now; i++) {
617 dp[i] = stream->buf_tail[i];
618 }
619
620 dp += now;
621 stream->buf_tail += now;
622 bytes_left -= now;
623 total_read += now;
624 }
625
626 return (total_read / size);
627}
628
629
[ef8bcc6]630/** Write to a stream.
631 *
632 * @param buf Source buffer.
633 * @param size Size of each record.
634 * @param nmemb Number of records to write.
635 * @param stream Pointer to the stream.
636 *
637 */
638size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
639{
640 uint8_t *data;
641 size_t bytes_left;
642 size_t now;
643 size_t buf_free;
644 size_t total_written;
645 size_t i;
646 uint8_t b;
647 bool need_flush;
[002252a]648
649 if (size == 0 || nmemb == 0)
650 return 0;
651
[ef8bcc6]652 /* If not buffered stream, write out directly. */
[bfd247f]653 if (stream->btype == _IONBF) {
654 now = _fwrite(buf, size, nmemb, stream);
655 fflush(stream);
656 return now;
657 }
[facebd56]658
659 /* Make sure buffer contains no prefetched data. */
660 if (stream->buf_state == _bs_read)
661 _fflushbuf(stream);
662
[ef8bcc6]663 /* Perform lazy allocation of stream buffer. */
664 if (stream->buf == NULL) {
665 if (_fallocbuf(stream) != 0)
666 return 0; /* Errno set by _fallocbuf(). */
667 }
[a35b458]668
[ef8bcc6]669 data = (uint8_t *) buf;
670 bytes_left = size * nmemb;
671 total_written = 0;
672 need_flush = false;
[a35b458]673
[bfd247f]674 while ((!stream->error) && (bytes_left > 0)) {
[ef8bcc6]675 buf_free = stream->buf_size - (stream->buf_head - stream->buf);
676 if (bytes_left > buf_free)
677 now = buf_free;
678 else
679 now = bytes_left;
[a35b458]680
[ef8bcc6]681 for (i = 0; i < now; i++) {
682 b = data[i];
683 stream->buf_head[i] = b;
[a35b458]684
[bfd247f]685 if ((b == '\n') && (stream->btype == _IOLBF))
[ef8bcc6]686 need_flush = true;
687 }
[a35b458]688
[0803134]689 data += now;
[ef8bcc6]690 stream->buf_head += now;
691 buf_free -= now;
692 bytes_left -= now;
693 total_written += now;
[0803134]694 stream->buf_state = _bs_write;
[a35b458]695
[ef8bcc6]696 if (buf_free == 0) {
697 /* Only need to drain buffer. */
698 _fflushbuf(stream);
[6afc9d7]699 if (!stream->error)
700 need_flush = false;
[ef8bcc6]701 }
702 }
[facebd56]703
[ef8bcc6]704 if (need_flush)
705 fflush(stream);
[a35b458]706
[ef8bcc6]707 return (total_written / size);
708}
709
[ed88c8e]710wint_t fputwc(wchar_t wc, FILE *stream)
[c9857c6]711{
[56fa418]712 char buf[STR_BOUNDS(1)];
[2595dab]713 size_t sz = 0;
[a35b458]714
[ed88c8e]715 if (chr_encode(wc, buf, &sz, STR_BOUNDS(1)) != EOK) {
716 errno = EILSEQ;
717 return WEOF;
718 }
[a35b458]719
[ed88c8e]720 size_t wr = fwrite(buf, 1, sz, stream);
721 if (wr < sz)
722 return WEOF;
[a35b458]723
[ed88c8e]724 return wc;
725}
726
727wint_t putwchar(wchar_t wc)
728{
729 return fputwc(wc, stdout);
730}
731
732int fputc(int c, FILE *stream)
733{
734 unsigned char b;
735 size_t wr;
736
737 b = (unsigned char) c;
738 wr = fwrite(&b, sizeof(b), 1, stream);
739 if (wr < 1)
740 return EOF;
[a35b458]741
[ed88c8e]742 return b;
[2595dab]743}
[56fa418]744
[ed88c8e]745int putchar(int c)
[2595dab]746{
747 return fputc(c, stdout);
748}
[56fa418]749
[2595dab]750int fputs(const char *str, FILE *stream)
751{
[7db5cfd]752 (void) fwrite(str, str_size(str), 1, stream);
753 if (ferror(stream))
754 return EOF;
755 return 0;
[2595dab]756}
[56fa418]757
[2595dab]758int puts(const char *str)
759{
[1abcf1d]760 if (fputs(str, stdout) < 0)
761 return EOF;
762 return putchar('\n');
[c9857c6]763}
[b27a97bb]764
[2595dab]765int fgetc(FILE *stream)
[b27a97bb]766{
[2595dab]767 char c;
[a35b458]768
[bac82eeb]769 /* This could be made faster by only flushing when needed. */
[b8e57e8c]770 if (stdout)
771 fflush(stdout);
772 if (stderr)
773 fflush(stderr);
[a35b458]774
[2595dab]775 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
776 return EOF;
[a35b458]777
[2595dab]778 return (int) c;
779}
780
[c62d2e1]781char *fgets(char *str, int size, FILE *stream)
782{
[a634485]783 int c;
[c62d2e1]784 int idx;
785
786 idx = 0;
787 while (idx < size - 1) {
788 c = fgetc(stream);
789 if (c == EOF)
790 break;
791
792 str[idx++] = c;
793
794 if (c == '\n')
795 break;
796 }
797
798 if (ferror(stream))
799 return NULL;
800
801 if (idx == 0)
802 return NULL;
803
804 str[idx] = '\0';
805 return str;
806}
807
[2595dab]808int getchar(void)
809{
810 return fgetc(stdin);
[b27a97bb]811}
812
[bd5414e]813int ungetc(int c, FILE *stream)
814{
815 if (c == EOF)
816 return EOF;
817
818 if (stream->ungetc_chars >= UNGETC_MAX)
819 return EOF;
820
821 stream->ungetc_buf[stream->ungetc_chars++] =
822 (uint8_t)c;
823
824 stream->eof = false;
825 return (uint8_t)c;
826}
827
[1c7f381]828int fseek64(FILE *stream, off64_t offset, int whence)
[d2cc7e1]829{
[b7fd2a0]830 errno_t rc;
[23a0368]831
[6afc9d7]832 if (stream->error)
[58898d1d]833 return -1;
[6afc9d7]834
[facebd56]835 _fflushbuf(stream);
[6afc9d7]836 if (stream->error) {
837 /* errno was set by _fflushbuf() */
[58898d1d]838 return -1;
[6afc9d7]839 }
840
[bd5414e]841 stream->ungetc_chars = 0;
[facebd56]842
[39330200]843 vfs_stat_t st;
[58898d1d]844 switch (whence) {
845 case SEEK_SET:
846 stream->pos = offset;
847 break;
848 case SEEK_CUR:
849 stream->pos += offset;
850 break;
851 case SEEK_END:
[23a0368]852 rc = vfs_stat(stream->fd, &st);
853 if (rc != EOK) {
854 errno = rc;
[58898d1d]855 stream->error = true;
[1b20da0]856 return -1;
[58898d1d]857 }
858 stream->pos = st.size + offset;
859 break;
[2595dab]860 }
[facebd56]861
[2595dab]862 stream->eof = false;
[566f4cfb]863 return 0;
[d2cc7e1]864}
865
[1c7f381]866off64_t ftell64(FILE *stream)
[08232ee]867{
[6afc9d7]868 if (stream->error)
869 return EOF;
[a35b458]870
[8ad496d2]871 _fflushbuf(stream);
[6afc9d7]872 if (stream->error) {
873 /* errno was set by _fflushbuf() */
874 return EOF;
875 }
876
[58898d1d]877 return stream->pos - stream->ungetc_chars;
[08232ee]878}
879
[1c7f381]880int fseek(FILE *stream, long offset, int whence)
881{
882 return fseek64(stream, offset, whence);
883}
884
885long ftell(FILE *stream)
886{
887 off64_t off = ftell64(stream);
888
889 /* The native position is too large for the C99-ish interface. */
890 if (off > LONG_MAX)
891 return EOF;
892
893 return off;
894}
895
[080ad7f]896void rewind(FILE *stream)
897{
898 (void) fseek(stream, 0, SEEK_SET);
899}
900
[2595dab]901int fflush(FILE *stream)
902{
[6afc9d7]903 if (stream->error)
904 return EOF;
[a35b458]905
[ef8bcc6]906 _fflushbuf(stream);
[6afc9d7]907 if (stream->error) {
908 /* errno was set by _fflushbuf() */
909 return EOF;
910 }
[a35b458]911
[01cc7b4]912 if (stream->need_sync) {
[facebd56]913 /**
914 * Better than syncing always, but probably still not the
915 * right thing to do.
916 */
[01cc7b4]917 if (stream->ops->flush(stream) == EOF)
[6afc9d7]918 return EOF;
919
[01cc7b4]920 stream->need_sync = false;
[facebd56]921 }
[a35b458]922
[6afc9d7]923 return 0;
[2595dab]924}
925
926int feof(FILE *stream)
927{
928 return stream->eof;
929}
930
931int ferror(FILE *stream)
932{
933 return stream->error;
934}
935
[c77a64f]936void clearerr(FILE *stream)
937{
938 stream->eof = false;
939 stream->error = false;
940}
941
[3629481]942int fileno(FILE *stream)
943{
[01cc7b4]944 if (stream->ops != &stdio_vfs_ops) {
[3629481]945 errno = EBADF;
[6afc9d7]946 return EOF;
[3629481]947 }
[a35b458]948
[3629481]949 return stream->fd;
950}
951
[6afc9d7]952async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
[2595dab]953{
954 if (stream->fd >= 0) {
[79ae36dd]955 if (stream->sess == NULL)
[6afc9d7]956 stream->sess = vfs_fd_session(stream->fd, iface);
[a35b458]957
[79ae36dd]958 return stream->sess;
[2595dab]959 }
[a35b458]960
[79ae36dd]961 return NULL;
[2595dab]962}
963
[b7fd2a0]964errno_t vfs_fhandle(FILE *stream, int *handle)
[2595dab]965{
[7171760]966 if (stream->fd >= 0) {
967 *handle = stream->fd;
968 return EOK;
969 }
[a35b458]970
[a68f737]971 return ENOENT;
[2595dab]972}
973
[01cc7b4]974/** Read from KIO stream. */
975static size_t stdio_kio_read(void *buf, size_t size, size_t nmemb, FILE *stream)
976{
977 stream->eof = true;
978 return 0;
979}
980
981/** Write to KIO stream. */
982static size_t stdio_kio_write(const void *buf, size_t size, size_t nmemb,
983 FILE *stream)
984{
985 errno_t rc;
986 size_t nwritten;
987
988 rc = kio_write(buf, size * nmemb, &nwritten);
989 if (rc != EOK) {
990 stream->error = true;
991 nwritten = 0;
992 }
993
994 return nwritten / size;
995}
996
997/** Flush KIO stream. */
998static int stdio_kio_flush(FILE *stream)
999{
1000 kio_update();
1001 return 0;
1002}
1003
1004/** Read from VFS stream. */
1005static size_t stdio_vfs_read(void *buf, size_t size, size_t nmemb, FILE *stream)
1006{
1007 errno_t rc;
1008 size_t nread;
1009
1010 if (size == 0 || nmemb == 0)
1011 return 0;
1012
1013 rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
1014 if (rc != EOK) {
1015 errno = rc;
1016 stream->error = true;
1017 } else if (nread == 0) {
1018 stream->eof = true;
1019 }
1020
1021 return (nread / size);
1022}
1023
1024/** Write to VFS stream. */
1025static size_t stdio_vfs_write(const void *buf, size_t size, size_t nmemb,
1026 FILE *stream)
1027{
1028 errno_t rc;
1029 size_t nwritten;
1030
1031 rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb, &nwritten);
1032 if (rc != EOK) {
1033 errno = rc;
1034 stream->error = true;
1035 }
1036
1037 return nwritten / size;
1038}
1039
1040/** Flush VFS stream. */
1041static int stdio_vfs_flush(FILE *stream)
1042{
1043 errno_t rc;
1044
1045 rc = vfs_sync(stream->fd);
1046 if (rc != EOK) {
1047 errno = rc;
1048 return EOF;
1049 }
1050
1051 return 0;
1052}
1053
[fadd381]1054/** @}
[b2951e2]1055 */
Note: See TracBrowser for help on using the repository browser.