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
Line 
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 <assert.h>
37#include <str.h>
38#include <errno.h>
39#include <stdbool.h>
40#include <stdlib.h>
41#include <async.h>
42#include <io/kio.h>
43#include <vfs/vfs.h>
44#include <vfs/vfs_sess.h>
45#include <vfs/inbox.h>
46#include <ipc/loc.h>
47#include <adt/list.h>
48#include <wchar.h>
49#include "../private/io.h"
50#include "../private/stdio.h"
51
52static void _ffillbuf(FILE *stream);
53static void _fflushbuf(FILE *stream);
54
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
78static FILE stdin_null = {
79 .fd = -1,
80 .pos = 0,
81 .error = true,
82 .eof = true,
83 .ops = &stdio_vfs_ops,
84 .arg = NULL,
85 .sess = NULL,
86 .btype = _IONBF,
87 .buf = NULL,
88 .buf_size = 0,
89 .buf_head = NULL,
90 .buf_tail = NULL,
91 .buf_state = _bs_empty
92};
93
94static FILE stdout_kio = {
95 .fd = -1,
96 .pos = 0,
97 .error = false,
98 .eof = false,
99 .ops = &stdio_kio_ops,
100 .arg = NULL,
101 .sess = NULL,
102 .btype = _IOLBF,
103 .buf = NULL,
104 .buf_size = BUFSIZ,
105 .buf_head = NULL,
106 .buf_tail = NULL,
107 .buf_state = _bs_empty
108};
109
110static FILE stderr_kio = {
111 .fd = -1,
112 .pos = 0,
113 .error = false,
114 .eof = false,
115 .ops = &stdio_kio_ops,
116 .arg = NULL,
117 .sess = NULL,
118 .btype = _IONBF,
119 .buf = NULL,
120 .buf_size = 0,
121 .buf_head = NULL,
122 .buf_tail = NULL,
123 .buf_state = _bs_empty
124};
125
126FILE *stdin = NULL;
127FILE *stdout = NULL;
128FILE *stderr = NULL;
129
130static LIST_INITIALIZE(files);
131
132void __stdio_init(void)
133{
134 /*
135 * The first three standard file descriptors are assigned for compatibility.
136 * This will probably be removed later.
137 */
138 int infd = inbox_get("stdin");
139 if (infd >= 0) {
140 int stdinfd = -1;
141 (void) vfs_clone(infd, -1, false, &stdinfd);
142 assert(stdinfd == 0);
143 vfs_open(stdinfd, MODE_READ);
144 stdin = fdopen(stdinfd, "r");
145 } else {
146 stdin = &stdin_null;
147 list_append(&stdin->link, &files);
148 }
149
150 int outfd = inbox_get("stdout");
151 if (outfd >= 0) {
152 int stdoutfd = -1;
153 (void) vfs_clone(outfd, -1, false, &stdoutfd);
154 assert(stdoutfd <= 1);
155 while (stdoutfd < 1)
156 (void) vfs_clone(outfd, -1, false, &stdoutfd);
157 vfs_open(stdoutfd, MODE_APPEND);
158 stdout = fdopen(stdoutfd, "a");
159 } else {
160 stdout = &stdout_kio;
161 list_append(&stdout->link, &files);
162 }
163
164 int errfd = inbox_get("stderr");
165 if (errfd >= 0) {
166 int stderrfd = -1;
167 (void) vfs_clone(errfd, -1, false, &stderrfd);
168 assert(stderrfd <= 2);
169 while (stderrfd < 2)
170 (void) vfs_clone(errfd, -1, false, &stderrfd);
171 vfs_open(stderrfd, MODE_APPEND);
172 stderr = fdopen(stderrfd, "a");
173 } else {
174 stderr = &stderr_kio;
175 list_append(&stderr->link, &files);
176 }
177}
178
179void __stdio_done(void)
180{
181 while (!list_empty(&files)) {
182 FILE *file = list_get_instance(list_first(&files), FILE, link);
183 fclose(file);
184 }
185}
186
187static bool parse_mode(const char *fmode, int *mode, bool *create, bool *truncate)
188{
189 /* Parse mode except first character. */
190 const char *mp = fmode;
191 if (*mp++ == 0) {
192 errno = EINVAL;
193 return false;
194 }
195
196 if ((*mp == 'b') || (*mp == 't'))
197 mp++;
198
199 bool plus;
200 if (*mp == '+') {
201 mp++;
202 plus = true;
203 } else
204 plus = false;
205
206 if (*mp != 0) {
207 errno = EINVAL;
208 return false;
209 }
210
211 *create = false;
212 *truncate = false;
213
214 /* Parse first character of fmode and determine mode for vfs_open(). */
215 switch (fmode[0]) {
216 case 'r':
217 *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
218 break;
219 case 'w':
220 *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
221 *create = true;
222 if (!plus)
223 *truncate = true;
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 }
231
232 *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
233 *create = true;
234 break;
235 default:
236 errno = EINVAL;
237 return false;
238 }
239
240 return true;
241}
242
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;
250 stream->buf_tail = stream->buf;
251 stream->buf_state = _bs_empty;
252}
253
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
268static void _setvbuf(FILE *stream)
269{
270 /* FIXME: Use more complex rules for setting buffering options. */
271
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
285/** Allocate stream buffer. */
286static int _fallocbuf(FILE *stream)
287{
288 assert(stream->buf == NULL);
289
290 stream->buf = malloc(stream->buf_size);
291 if (stream->buf == NULL) {
292 errno = ENOMEM;
293 return EOF;
294 }
295
296 stream->buf_head = stream->buf;
297 stream->buf_tail = stream->buf;
298 return 0;
299}
300
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 *
306 */
307FILE *fopen(const char *path, const char *fmode)
308{
309 int mode;
310 bool create;
311 bool truncate;
312
313 if (!parse_mode(fmode, &mode, &create, &truncate))
314 return NULL;
315
316 /* Open file. */
317 FILE *stream = malloc(sizeof(FILE));
318 if (stream == NULL) {
319 errno = ENOMEM;
320 return NULL;
321 }
322
323 int flags = WALK_REGULAR;
324 if (create)
325 flags |= WALK_MAY_CREATE;
326 int file;
327 errno_t rc = vfs_lookup(path, flags, &file);
328 if (rc != EOK) {
329 errno = rc;
330 free(stream);
331 return NULL;
332 }
333
334 rc = vfs_open(file, mode);
335 if (rc != EOK) {
336 errno = rc;
337 vfs_put(file);
338 free(stream);
339 return NULL;
340 }
341
342 if (truncate) {
343 rc = vfs_resize(file, 0);
344 if (rc != EOK) {
345 errno = rc;
346 vfs_put(file);
347 free(stream);
348 return NULL;
349 }
350 }
351
352 stream->fd = file;
353 stream->pos = 0;
354 stream->error = false;
355 stream->eof = false;
356 stream->ops = &stdio_vfs_ops;
357 stream->arg = NULL;
358 stream->sess = NULL;
359 stream->need_sync = false;
360 _setvbuf(stream);
361 stream->ungetc_chars = 0;
362
363 list_append(&stream->link, &files);
364
365 return stream;
366}
367
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 }
376
377 stream->fd = fd;
378 stream->pos = 0;
379 stream->error = false;
380 stream->eof = false;
381 stream->ops = &stdio_vfs_ops;
382 stream->arg = NULL;
383 stream->sess = NULL;
384 stream->need_sync = false;
385 _setvbuf(stream);
386 stream->ungetc_chars = 0;
387
388 list_append(&stream->link, &files);
389
390 return stream;
391}
392
393
394static int _fclose_nofree(FILE *stream)
395{
396 errno_t rc = 0;
397
398 fflush(stream);
399
400 if (stream->sess != NULL)
401 async_hangup(stream->sess);
402
403 if (stream->fd >= 0)
404 rc = vfs_put(stream->fd);
405
406 list_remove(&stream->link);
407
408 if (rc != EOK) {
409 errno = rc;
410 return EOF;
411 }
412
413 return 0;
414}
415
416int fclose(FILE *stream)
417{
418 int rc = _fclose_nofree(stream);
419
420 if ((stream != &stdin_null) &&
421 (stream != &stdout_kio) &&
422 (stream != &stderr_kio))
423 free(stream);
424
425 return rc;
426}
427
428FILE *freopen(const char *path, const char *mode, FILE *stream)
429{
430 FILE *nstr;
431
432 if (path == NULL) {
433 /* Changing mode is not supported */
434 return NULL;
435 }
436
437 (void) _fclose_nofree(stream);
438 nstr = fopen(path, mode);
439 if (nstr == NULL) {
440 free(stream);
441 return NULL;
442 }
443
444 list_remove(&nstr->link);
445 *stream = *nstr;
446 list_append(&stream->link, &files);
447
448 free(nstr);
449
450 return stream;
451}
452
453/** Read from a stream (unbuffered).
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.
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.
462 */
463static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
464{
465 return stream->ops->read(buf, size, nmemb, stream);
466}
467
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.
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.
477 */
478static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
479{
480 size_t nwritten;
481
482 if (size == 0 || nmemb == 0)
483 return 0;
484
485 nwritten = stream->ops->write(buf, size, nmemb, stream);
486
487 if (nwritten > 0)
488 stream->need_sync = true;
489
490 return (nwritten / size);
491}
492
493/** Read some data in stream buffer.
494 *
495 * On error, stream error indicator is set and errno is set.
496 */
497static void _ffillbuf(FILE *stream)
498{
499 errno_t rc;
500 size_t nread;
501
502 stream->buf_head = stream->buf_tail = stream->buf;
503
504 rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
505 &nread);
506 if (rc != EOK) {
507 errno = rc;
508 stream->error = true;
509 return;
510 }
511
512 if (nread == 0) {
513 stream->eof = true;
514 return;
515 }
516
517 stream->buf_head += nread;
518 stream->buf_state = _bs_read;
519}
520
521/** Write out stream buffer, do not sync stream. */
522static void _fflushbuf(FILE *stream)
523{
524 size_t bytes_used;
525
526 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
527 return;
528
529 bytes_used = stream->buf_head - stream->buf_tail;
530
531 /* If buffer has prefetched read data, we need to seek back. */
532 if (bytes_used > 0 && stream->buf_state == _bs_read)
533 stream->pos -= bytes_used;
534
535 /* If buffer has unwritten data, we need to write them out. */
536 if (bytes_used > 0 && stream->buf_state == _bs_write) {
537 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
538 /* On error stream error indicator and errno are set by _fwrite */
539 if (stream->error)
540 return;
541 }
542
543 stream->buf_head = stream->buf;
544 stream->buf_tail = stream->buf;
545 stream->buf_state = _bs_empty;
546}
547
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
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;
576 --bytes_left;
577 }
578
579 /* If not buffered stream, read in directly. */
580 if (stream->btype == _IONBF) {
581 total_read += _fread(dest, 1, bytes_left, stream);
582 return total_read / size;
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
599 if (stream->error || stream->eof) {
600 /* On error errno was set by _ffillbuf() */
601 break;
602 }
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
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;
643
644 if (size == 0 || nmemb == 0)
645 return 0;
646
647 /* If not buffered stream, write out directly. */
648 if (stream->btype == _IONBF) {
649 now = _fwrite(buf, size, nmemb, stream);
650 fflush(stream);
651 return now;
652 }
653
654 /* Make sure buffer contains no prefetched data. */
655 if (stream->buf_state == _bs_read)
656 _fflushbuf(stream);
657
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 }
663
664 data = (uint8_t *) buf;
665 bytes_left = size * nmemb;
666 total_written = 0;
667 need_flush = false;
668
669 while ((!stream->error) && (bytes_left > 0)) {
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;
675
676 for (i = 0; i < now; i++) {
677 b = data[i];
678 stream->buf_head[i] = b;
679
680 if ((b == '\n') && (stream->btype == _IOLBF))
681 need_flush = true;
682 }
683
684 data += now;
685 stream->buf_head += now;
686 buf_free -= now;
687 bytes_left -= now;
688 total_written += now;
689 stream->buf_state = _bs_write;
690
691 if (buf_free == 0) {
692 /* Only need to drain buffer. */
693 _fflushbuf(stream);
694 if (!stream->error)
695 need_flush = false;
696 }
697 }
698
699 if (need_flush)
700 fflush(stream);
701
702 return (total_written / size);
703}
704
705wint_t fputwc(wchar_t wc, FILE *stream)
706{
707 char buf[STR_BOUNDS(1)];
708 size_t sz = 0;
709
710 if (chr_encode(wc, buf, &sz, STR_BOUNDS(1)) != EOK) {
711 errno = EILSEQ;
712 return WEOF;
713 }
714
715 size_t wr = fwrite(buf, 1, sz, stream);
716 if (wr < sz)
717 return WEOF;
718
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;
736
737 return b;
738}
739
740int putchar(int c)
741{
742 return fputc(c, stdout);
743}
744
745int fputs(const char *str, FILE *stream)
746{
747 (void) fwrite(str, str_size(str), 1, stream);
748 if (ferror(stream))
749 return EOF;
750 return 0;
751}
752
753int puts(const char *str)
754{
755 if (fputs(str, stdout) < 0)
756 return EOF;
757 return putchar('\n');
758}
759
760int fgetc(FILE *stream)
761{
762 char c;
763
764 /* This could be made faster by only flushing when needed. */
765 if (stdout)
766 fflush(stdout);
767 if (stderr)
768 fflush(stderr);
769
770 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
771 return EOF;
772
773 return (int) c;
774}
775
776char *fgets(char *str, int size, FILE *stream)
777{
778 int c;
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
803int getchar(void)
804{
805 return fgetc(stdin);
806}
807
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
823int fseek64(FILE *stream, off64_t offset, int whence)
824{
825 errno_t rc;
826
827 if (stream->error)
828 return -1;
829
830 _fflushbuf(stream);
831 if (stream->error) {
832 /* errno was set by _fflushbuf() */
833 return -1;
834 }
835
836 stream->ungetc_chars = 0;
837
838 vfs_stat_t st;
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:
847 rc = vfs_stat(stream->fd, &st);
848 if (rc != EOK) {
849 errno = rc;
850 stream->error = true;
851 return -1;
852 }
853 stream->pos = st.size + offset;
854 break;
855 }
856
857 stream->eof = false;
858 return 0;
859}
860
861off64_t ftell64(FILE *stream)
862{
863 if (stream->error)
864 return EOF;
865
866 _fflushbuf(stream);
867 if (stream->error) {
868 /* errno was set by _fflushbuf() */
869 return EOF;
870 }
871
872 return stream->pos - stream->ungetc_chars;
873}
874
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
891void rewind(FILE *stream)
892{
893 (void) fseek(stream, 0, SEEK_SET);
894}
895
896int fflush(FILE *stream)
897{
898 if (stream->error)
899 return EOF;
900
901 _fflushbuf(stream);
902 if (stream->error) {
903 /* errno was set by _fflushbuf() */
904 return EOF;
905 }
906
907 if (stream->need_sync) {
908 /**
909 * Better than syncing always, but probably still not the
910 * right thing to do.
911 */
912 if (stream->ops->flush(stream) == EOF)
913 return EOF;
914
915 stream->need_sync = false;
916 }
917
918 return 0;
919}
920
921int feof(FILE *stream)
922{
923 return stream->eof;
924}
925
926int ferror(FILE *stream)
927{
928 return stream->error;
929}
930
931void clearerr(FILE *stream)
932{
933 stream->eof = false;
934 stream->error = false;
935}
936
937int fileno(FILE *stream)
938{
939 if (stream->ops != &stdio_vfs_ops) {
940 errno = EBADF;
941 return EOF;
942 }
943
944 return stream->fd;
945}
946
947async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
948{
949 if (stream->fd >= 0) {
950 if (stream->sess == NULL)
951 stream->sess = vfs_fd_session(stream->fd, iface);
952
953 return stream->sess;
954 }
955
956 return NULL;
957}
958
959errno_t vfs_fhandle(FILE *stream, int *handle)
960{
961 if (stream->fd >= 0) {
962 *handle = stream->fd;
963 return EOK;
964 }
965
966 return ENOENT;
967}
968
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
1049/** @}
1050 */
Note: See TracBrowser for help on using the repository browser.