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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ef84413 was ef84413, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

libposix: Correctly disambiguate other uses of off_t

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