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
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. */
244int setvbuf(FILE *stream, void *buf, int mode, size_t size)
245{
246 if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF)
247 return -1;
248
249 stream->btype = mode;
250 stream->buf = buf;
251 stream->buf_size = size;
252 stream->buf_head = stream->buf;
253 stream->buf_tail = stream->buf;
254 stream->buf_state = _bs_empty;
255
256 return 0;
257}
258
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
273static void _setvbuf(FILE *stream)
274{
275 /* FIXME: Use more complex rules for setting buffering options. */
276
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
290/** Allocate stream buffer. */
291static int _fallocbuf(FILE *stream)
292{
293 assert(stream->buf == NULL);
294
295 stream->buf = malloc(stream->buf_size);
296 if (stream->buf == NULL) {
297 errno = ENOMEM;
298 return EOF;
299 }
300
301 stream->buf_head = stream->buf;
302 stream->buf_tail = stream->buf;
303 return 0;
304}
305
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 *
311 */
312FILE *fopen(const char *path, const char *fmode)
313{
314 int mode;
315 bool create;
316 bool truncate;
317
318 if (!parse_mode(fmode, &mode, &create, &truncate))
319 return NULL;
320
321 /* Open file. */
322 FILE *stream = malloc(sizeof(FILE));
323 if (stream == NULL) {
324 errno = ENOMEM;
325 return NULL;
326 }
327
328 int flags = WALK_REGULAR;
329 if (create)
330 flags |= WALK_MAY_CREATE;
331 int file;
332 errno_t rc = vfs_lookup(path, flags, &file);
333 if (rc != EOK) {
334 errno = rc;
335 free(stream);
336 return NULL;
337 }
338
339 rc = vfs_open(file, mode);
340 if (rc != EOK) {
341 errno = rc;
342 vfs_put(file);
343 free(stream);
344 return NULL;
345 }
346
347 if (truncate) {
348 rc = vfs_resize(file, 0);
349 if (rc != EOK) {
350 errno = rc;
351 vfs_put(file);
352 free(stream);
353 return NULL;
354 }
355 }
356
357 stream->fd = file;
358 stream->pos = 0;
359 stream->error = false;
360 stream->eof = false;
361 stream->ops = &stdio_vfs_ops;
362 stream->arg = NULL;
363 stream->sess = NULL;
364 stream->need_sync = false;
365 _setvbuf(stream);
366 stream->ungetc_chars = 0;
367
368 list_append(&stream->link, &files);
369
370 return stream;
371}
372
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 }
381
382 stream->fd = fd;
383 stream->pos = 0;
384 stream->error = false;
385 stream->eof = false;
386 stream->ops = &stdio_vfs_ops;
387 stream->arg = NULL;
388 stream->sess = NULL;
389 stream->need_sync = false;
390 _setvbuf(stream);
391 stream->ungetc_chars = 0;
392
393 list_append(&stream->link, &files);
394
395 return stream;
396}
397
398
399static int _fclose_nofree(FILE *stream)
400{
401 errno_t rc = 0;
402
403 fflush(stream);
404
405 if (stream->sess != NULL)
406 async_hangup(stream->sess);
407
408 if (stream->fd >= 0)
409 rc = vfs_put(stream->fd);
410
411 list_remove(&stream->link);
412
413 if (rc != EOK) {
414 errno = rc;
415 return EOF;
416 }
417
418 return 0;
419}
420
421int fclose(FILE *stream)
422{
423 int rc = _fclose_nofree(stream);
424
425 if ((stream != &stdin_null) &&
426 (stream != &stdout_kio) &&
427 (stream != &stderr_kio))
428 free(stream);
429
430 return rc;
431}
432
433FILE *freopen(const char *path, const char *mode, FILE *stream)
434{
435 FILE *nstr;
436
437 if (path == NULL) {
438 /* Changing mode is not supported */
439 return NULL;
440 }
441
442 (void) _fclose_nofree(stream);
443 nstr = fopen(path, mode);
444 if (nstr == NULL) {
445 free(stream);
446 return NULL;
447 }
448
449 list_remove(&nstr->link);
450 *stream = *nstr;
451 list_append(&stream->link, &files);
452
453 free(nstr);
454
455 return stream;
456}
457
458/** Read from a stream (unbuffered).
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.
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.
467 */
468static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
469{
470 return stream->ops->read(buf, size, nmemb, stream);
471}
472
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.
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.
482 */
483static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
484{
485 size_t nwritten;
486
487 if (size == 0 || nmemb == 0)
488 return 0;
489
490 nwritten = stream->ops->write(buf, size, nmemb, stream);
491
492 if (nwritten > 0)
493 stream->need_sync = true;
494
495 return (nwritten / size);
496}
497
498/** Read some data in stream buffer.
499 *
500 * On error, stream error indicator is set and errno is set.
501 */
502static void _ffillbuf(FILE *stream)
503{
504 errno_t rc;
505 size_t nread;
506
507 stream->buf_head = stream->buf_tail = stream->buf;
508
509 rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
510 &nread);
511 if (rc != EOK) {
512 errno = rc;
513 stream->error = true;
514 return;
515 }
516
517 if (nread == 0) {
518 stream->eof = true;
519 return;
520 }
521
522 stream->buf_head += nread;
523 stream->buf_state = _bs_read;
524}
525
526/** Write out stream buffer, do not sync stream. */
527static void _fflushbuf(FILE *stream)
528{
529 size_t bytes_used;
530
531 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
532 return;
533
534 bytes_used = stream->buf_head - stream->buf_tail;
535
536 /* If buffer has prefetched read data, we need to seek back. */
537 if (bytes_used > 0 && stream->buf_state == _bs_read)
538 stream->pos -= bytes_used;
539
540 /* If buffer has unwritten data, we need to write them out. */
541 if (bytes_used > 0 && stream->buf_state == _bs_write) {
542 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
543 /* On error stream error indicator and errno are set by _fwrite */
544 if (stream->error)
545 return;
546 }
547
548 stream->buf_head = stream->buf;
549 stream->buf_tail = stream->buf;
550 stream->buf_state = _bs_empty;
551}
552
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
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;
581 --bytes_left;
582 }
583
584 /* If not buffered stream, read in directly. */
585 if (stream->btype == _IONBF) {
586 total_read += _fread(dest, 1, bytes_left, stream);
587 return total_read / size;
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
604 if (stream->error || stream->eof) {
605 /* On error errno was set by _ffillbuf() */
606 break;
607 }
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
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;
648
649 if (size == 0 || nmemb == 0)
650 return 0;
651
652 /* If not buffered stream, write out directly. */
653 if (stream->btype == _IONBF) {
654 now = _fwrite(buf, size, nmemb, stream);
655 fflush(stream);
656 return now;
657 }
658
659 /* Make sure buffer contains no prefetched data. */
660 if (stream->buf_state == _bs_read)
661 _fflushbuf(stream);
662
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 }
668
669 data = (uint8_t *) buf;
670 bytes_left = size * nmemb;
671 total_written = 0;
672 need_flush = false;
673
674 while ((!stream->error) && (bytes_left > 0)) {
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;
680
681 for (i = 0; i < now; i++) {
682 b = data[i];
683 stream->buf_head[i] = b;
684
685 if ((b == '\n') && (stream->btype == _IOLBF))
686 need_flush = true;
687 }
688
689 data += now;
690 stream->buf_head += now;
691 buf_free -= now;
692 bytes_left -= now;
693 total_written += now;
694 stream->buf_state = _bs_write;
695
696 if (buf_free == 0) {
697 /* Only need to drain buffer. */
698 _fflushbuf(stream);
699 if (!stream->error)
700 need_flush = false;
701 }
702 }
703
704 if (need_flush)
705 fflush(stream);
706
707 return (total_written / size);
708}
709
710wint_t fputwc(wchar_t wc, FILE *stream)
711{
712 char buf[STR_BOUNDS(1)];
713 size_t sz = 0;
714
715 if (chr_encode(wc, buf, &sz, STR_BOUNDS(1)) != EOK) {
716 errno = EILSEQ;
717 return WEOF;
718 }
719
720 size_t wr = fwrite(buf, 1, sz, stream);
721 if (wr < sz)
722 return WEOF;
723
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;
741
742 return b;
743}
744
745int putchar(int c)
746{
747 return fputc(c, stdout);
748}
749
750int fputs(const char *str, FILE *stream)
751{
752 (void) fwrite(str, str_size(str), 1, stream);
753 if (ferror(stream))
754 return EOF;
755 return 0;
756}
757
758int puts(const char *str)
759{
760 if (fputs(str, stdout) < 0)
761 return EOF;
762 return putchar('\n');
763}
764
765int fgetc(FILE *stream)
766{
767 char c;
768
769 /* This could be made faster by only flushing when needed. */
770 if (stdout)
771 fflush(stdout);
772 if (stderr)
773 fflush(stderr);
774
775 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
776 return EOF;
777
778 return (int) c;
779}
780
781char *fgets(char *str, int size, FILE *stream)
782{
783 int c;
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
808int getchar(void)
809{
810 return fgetc(stdin);
811}
812
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
828int fseek64(FILE *stream, off64_t offset, int whence)
829{
830 errno_t rc;
831
832 if (stream->error)
833 return -1;
834
835 _fflushbuf(stream);
836 if (stream->error) {
837 /* errno was set by _fflushbuf() */
838 return -1;
839 }
840
841 stream->ungetc_chars = 0;
842
843 vfs_stat_t st;
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:
852 rc = vfs_stat(stream->fd, &st);
853 if (rc != EOK) {
854 errno = rc;
855 stream->error = true;
856 return -1;
857 }
858 stream->pos = st.size + offset;
859 break;
860 }
861
862 stream->eof = false;
863 return 0;
864}
865
866off64_t ftell64(FILE *stream)
867{
868 if (stream->error)
869 return EOF;
870
871 _fflushbuf(stream);
872 if (stream->error) {
873 /* errno was set by _fflushbuf() */
874 return EOF;
875 }
876
877 return stream->pos - stream->ungetc_chars;
878}
879
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
896void rewind(FILE *stream)
897{
898 (void) fseek(stream, 0, SEEK_SET);
899}
900
901int fflush(FILE *stream)
902{
903 if (stream->error)
904 return EOF;
905
906 _fflushbuf(stream);
907 if (stream->error) {
908 /* errno was set by _fflushbuf() */
909 return EOF;
910 }
911
912 if (stream->need_sync) {
913 /**
914 * Better than syncing always, but probably still not the
915 * right thing to do.
916 */
917 if (stream->ops->flush(stream) == EOF)
918 return EOF;
919
920 stream->need_sync = false;
921 }
922
923 return 0;
924}
925
926int feof(FILE *stream)
927{
928 return stream->eof;
929}
930
931int ferror(FILE *stream)
932{
933 return stream->error;
934}
935
936void clearerr(FILE *stream)
937{
938 stream->eof = false;
939 stream->error = false;
940}
941
942int fileno(FILE *stream)
943{
944 if (stream->ops != &stdio_vfs_ops) {
945 errno = EBADF;
946 return EOF;
947 }
948
949 return stream->fd;
950}
951
952async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
953{
954 if (stream->fd >= 0) {
955 if (stream->sess == NULL)
956 stream->sess = vfs_fd_session(stream->fd, iface);
957
958 return stream->sess;
959 }
960
961 return NULL;
962}
963
964errno_t vfs_fhandle(FILE *stream, int *handle)
965{
966 if (stream->fd >= 0) {
967 *handle = stream->fd;
968 return EOK;
969 }
970
971 return ENOENT;
972}
973
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
1054/** @}
1055 */
Note: See TracBrowser for help on using the repository browser.