source: mainline/uspace/lib/c/generic/io/io.c@ 38d150e

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

Prefer to get memory allocation functions through the standard stdlib header.

  • Property mode set to 100644
File size: 17.6 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>
[47b7006]48#include "../private/io.h"
[79ae36dd]49#include "../private/stdio.h"
[b861b58]50
[facebd56]51static void _ffillbuf(FILE *stream);
[ef8bcc6]52static void _fflushbuf(FILE *stream);
53
[a68f737]54static FILE stdin_null = {
[2595dab]55 .fd = -1,
[58898d1d]56 .pos = 0,
[2595dab]57 .error = true,
58 .eof = true,
[6fa9a99d]59 .kio = false,
[79ae36dd]60 .sess = NULL,
[ef8bcc6]61 .btype = _IONBF,
62 .buf = NULL,
63 .buf_size = 0,
[facebd56]64 .buf_head = NULL,
65 .buf_tail = NULL,
[c70703a]66 .buf_state = _bs_empty
[2595dab]67};
[350514c]68
[6fa9a99d]69static FILE stdout_kio = {
[2595dab]70 .fd = -1,
[58898d1d]71 .pos = 0,
[2595dab]72 .error = false,
73 .eof = false,
[6fa9a99d]74 .kio = true,
[79ae36dd]75 .sess = NULL,
[ef8bcc6]76 .btype = _IOLBF,
77 .buf = NULL,
78 .buf_size = BUFSIZ,
[facebd56]79 .buf_head = NULL,
80 .buf_tail = NULL,
81 .buf_state = _bs_empty
[2595dab]82};
83
[6fa9a99d]84static FILE stderr_kio = {
[a68f737]85 .fd = -1,
[58898d1d]86 .pos = 0,
[a68f737]87 .error = false,
88 .eof = false,
[6fa9a99d]89 .kio = true,
[79ae36dd]90 .sess = NULL,
[ef8bcc6]91 .btype = _IONBF,
92 .buf = NULL,
93 .buf_size = 0,
[facebd56]94 .buf_head = NULL,
95 .buf_tail = NULL,
96 .buf_state = _bs_empty
[a68f737]97};
98
99FILE *stdin = NULL;
100FILE *stdout = NULL;
101FILE *stderr = NULL;
102
103static LIST_INITIALIZE(files);
104
[bb9ec2d]105void __stdio_init(void)
[a68f737]106{
[bb9ec2d]107 /* The first three standard file descriptors are assigned for compatibility.
108 * This will probably be removed later.
109 */
110
111 int infd = inbox_get("stdin");
112 if (infd >= 0) {
[fcab7ef]113 int stdinfd = vfs_clone(infd, -1, false);
[bb9ec2d]114 assert(stdinfd == 0);
[b19e892]115 vfs_open(stdinfd, MODE_READ);
[bb9ec2d]116 stdin = fdopen(stdinfd, "r");
[a68f737]117 } else {
118 stdin = &stdin_null;
119 list_append(&stdin->link, &files);
120 }
121
[bb9ec2d]122 int outfd = inbox_get("stdout");
123 if (outfd >= 0) {
[fcab7ef]124 int stdoutfd = vfs_clone(outfd, -1, false);
[bb9ec2d]125 assert(stdoutfd <= 1);
[fcab7ef]126 while (stdoutfd < 1)
127 stdoutfd = vfs_clone(outfd, -1, false);
[b19e892]128 vfs_open(stdoutfd, MODE_APPEND);
[bb9ec2d]129 stdout = fdopen(stdoutfd, "a");
[a68f737]130 } else {
[6fa9a99d]131 stdout = &stdout_kio;
[a68f737]132 list_append(&stdout->link, &files);
133 }
134
[bb9ec2d]135 int errfd = inbox_get("stderr");
136 if (errfd >= 0) {
[fcab7ef]137 int stderrfd = vfs_clone(errfd, -1, false);
[bb9ec2d]138 assert(stderrfd <= 2);
[fcab7ef]139 while (stderrfd < 2)
140 stderrfd = vfs_clone(errfd, -1, false);
[b19e892]141 vfs_open(stderrfd, MODE_APPEND);
[bb9ec2d]142 stderr = fdopen(stderrfd, "a");
[a68f737]143 } else {
[6fa9a99d]144 stderr = &stderr_kio;
[a68f737]145 list_append(&stderr->link, &files);
146 }
147}
148
[db24058]149void __stdio_done(void)
[a68f737]150{
[b72efe8]151 while (!list_empty(&files)) {
152 FILE *file = list_get_instance(list_first(&files), FILE, link);
[a68f737]153 fclose(file);
154 }
155}
[2595dab]156
[b19e892]157static bool parse_mode(const char *fmode, int *mode, bool *create, bool *truncate)
[b861b58]158{
[2595dab]159 /* Parse mode except first character. */
[b19e892]160 const char *mp = fmode;
[2595dab]161 if (*mp++ == 0) {
162 errno = EINVAL;
163 return false;
164 }
[cc6f688]165
[2595dab]166 if ((*mp == 'b') || (*mp == 't'))
167 mp++;
[c9857c6]168
[2595dab]169 bool plus;
170 if (*mp == '+') {
171 mp++;
172 plus = true;
173 } else
174 plus = false;
[566f4cfb]175
[2595dab]176 if (*mp != 0) {
177 errno = EINVAL;
178 return false;
[350514c]179 }
[b19e892]180
181 *create = false;
182 *truncate = false;
[350514c]183
[b19e892]184 /* Parse first character of fmode and determine mode for vfs_open(). */
185 switch (fmode[0]) {
[2595dab]186 case 'r':
[b19e892]187 *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
[2595dab]188 break;
189 case 'w':
[b19e892]190 *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
191 *create = true;
192 if (!plus)
193 *truncate = true;
[2595dab]194 break;
195 case 'a':
196 /* TODO: a+ must read from beginning, append to the end. */
197 if (plus) {
198 errno = ENOTSUP;
199 return false;
200 }
[b19e892]201
202 *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
203 *create = true;
[82582e4]204 break;
[2595dab]205 default:
206 errno = EINVAL;
207 return false;
208 }
209
210 return true;
[cc6f688]211}
[b861b58]212
[ef8bcc6]213/** Set stream buffer. */
214void setvbuf(FILE *stream, void *buf, int mode, size_t size)
215{
216 stream->btype = mode;
217 stream->buf = buf;
218 stream->buf_size = size;
219 stream->buf_head = stream->buf;
[facebd56]220 stream->buf_tail = stream->buf;
221 stream->buf_state = _bs_empty;
[ef8bcc6]222}
223
[7699c21]224/** Set stream buffer.
225 *
226 * When @p buf is NULL, the stream is set as unbuffered, otherwise
227 * full buffering is enabled.
228 */
229void setbuf(FILE *stream, void *buf)
230{
231 if (buf == NULL) {
232 setvbuf(stream, NULL, _IONBF, BUFSIZ);
233 } else {
234 setvbuf(stream, buf, _IOFBF, BUFSIZ);
235 }
236}
237
[bfd247f]238static void _setvbuf(FILE *stream)
239{
240 /* FIXME: Use more complex rules for setting buffering options. */
241
242 switch (stream->fd) {
243 case 1:
244 setvbuf(stream, NULL, _IOLBF, BUFSIZ);
245 break;
246 case 0:
247 case 2:
248 setvbuf(stream, NULL, _IONBF, 0);
249 break;
250 default:
251 setvbuf(stream, NULL, _IOFBF, BUFSIZ);
252 }
253}
254
[ef8bcc6]255/** Allocate stream buffer. */
256static int _fallocbuf(FILE *stream)
257{
258 assert(stream->buf == NULL);
[bfd247f]259
[ef8bcc6]260 stream->buf = malloc(stream->buf_size);
261 if (stream->buf == NULL) {
262 errno = ENOMEM;
[6afc9d7]263 return EOF;
[ef8bcc6]264 }
[bfd247f]265
[ef8bcc6]266 stream->buf_head = stream->buf;
[facebd56]267 stream->buf_tail = stream->buf;
[ef8bcc6]268 return 0;
269}
270
[2595dab]271/** Open a stream.
272 *
273 * @param path Path of the file to open.
274 * @param mode Mode string, (r|w|a)[b|t][+].
275 *
[4e2cf8b]276 */
[b19e892]277FILE *fopen(const char *path, const char *fmode)
[4e2cf8b]278{
[b19e892]279 int mode;
280 bool create;
281 bool truncate;
282
283 if (!parse_mode(fmode, &mode, &create, &truncate))
[2595dab]284 return NULL;
[4e2cf8b]285
[2595dab]286 /* Open file. */
287 FILE *stream = malloc(sizeof(FILE));
288 if (stream == NULL) {
289 errno = ENOMEM;
290 return NULL;
291 }
[b19e892]292
293 int flags = WALK_REGULAR;
294 if (create)
295 flags |= WALK_MAY_CREATE;
296 int file = vfs_lookup(path, flags);
297 if (file < 0) {
298 errno = file;
299 free(stream);
300 return NULL;
301 }
302
303 int rc = vfs_open(file, mode);
304 if (rc != EOK) {
305 errno = rc;
[9c4cf0d]306 vfs_put(file);
[2595dab]307 free(stream);
308 return NULL;
309 }
310
[b19e892]311 if (truncate) {
312 rc = vfs_resize(file, 0);
313 if (rc != EOK) {
314 errno = rc;
[9c4cf0d]315 vfs_put(file);
[b19e892]316 free(stream);
317 return NULL;
318 }
319 }
320
321 stream->fd = file;
[58898d1d]322 stream->pos = 0;
[2595dab]323 stream->error = false;
324 stream->eof = false;
[6fa9a99d]325 stream->kio = false;
[79ae36dd]326 stream->sess = NULL;
[facebd56]327 stream->need_sync = false;
[bfd247f]328 _setvbuf(stream);
[e86a617a]329 stream->ungetc_chars = 0;
[2595dab]330
[a68f737]331 list_append(&stream->link, &files);
332
[2595dab]333 return stream;
334}
335
[080ad7f]336FILE *fdopen(int fd, const char *mode)
337{
338 /* Open file. */
339 FILE *stream = malloc(sizeof(FILE));
340 if (stream == NULL) {
341 errno = ENOMEM;
342 return NULL;
343 }
344
345 stream->fd = fd;
[58898d1d]346 stream->pos = 0;
[080ad7f]347 stream->error = false;
348 stream->eof = false;
[6fa9a99d]349 stream->kio = false;
[79ae36dd]350 stream->sess = NULL;
[facebd56]351 stream->need_sync = false;
[bfd247f]352 _setvbuf(stream);
[e86a617a]353 stream->ungetc_chars = 0;
[080ad7f]354
355 list_append(&stream->link, &files);
356
357 return stream;
358}
359
[80bee81]360
361static int _fclose_nofree(FILE *stream)
[2595dab]362{
363 int rc = 0;
364
365 fflush(stream);
366
[79ae36dd]367 if (stream->sess != NULL)
368 async_hangup(stream->sess);
[2595dab]369
370 if (stream->fd >= 0)
[9c4cf0d]371 rc = vfs_put(stream->fd);
[2595dab]372
[a68f737]373 list_remove(&stream->link);
374
[80bee81]375 if (rc != 0) {
376 /* errno was set by close() */
377 return EOF;
378 }
379
380 return 0;
381}
382
383int fclose(FILE *stream)
384{
385 int rc = _fclose_nofree(stream);
386
[a68f737]387 if ((stream != &stdin_null)
[6fa9a99d]388 && (stream != &stdout_kio)
389 && (stream != &stderr_kio))
[2595dab]390 free(stream);
391
[80bee81]392 return rc;
393}
394
395FILE *freopen(const char *path, const char *mode, FILE *stream)
396{
397 FILE *nstr;
[2595dab]398
[80bee81]399 if (path == NULL) {
400 /* Changing mode is not supported */
401 return NULL;
[2595dab]402 }
403
[80bee81]404 (void) _fclose_nofree(stream);
405 nstr = fopen(path, mode);
406 if (nstr == NULL) {
407 free(stream);
408 return NULL;
409 }
410
411 list_remove(&nstr->link);
412 *stream = *nstr;
413 list_append(&stream->link, &files);
414
415 free(nstr);
416
417 return stream;
[4e2cf8b]418}
419
[facebd56]420/** Read from a stream (unbuffered).
[2595dab]421 *
422 * @param buf Destination buffer.
423 * @param size Size of each record.
424 * @param nmemb Number of records to read.
425 * @param stream Pointer to the stream.
[6afc9d7]426 *
427 * @return Number of elements successfully read. On error this is less than
428 * nmemb, stream error indicator is set and errno is set.
[4e2cf8b]429 */
[facebd56]430static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
[4e2cf8b]431{
[002252a]432 if (size == 0 || nmemb == 0)
433 return 0;
434
[ce04ea44]435 ssize_t rd = vfs_read(stream->fd, &stream->pos, buf, size * nmemb);
[58898d1d]436 if (rd < 0) {
[ce04ea44]437 errno = rd;
[58898d1d]438 stream->error = true;
439 rd = 0;
440 } else if (rd == 0) {
441 stream->eof = true;
[2595dab]442 }
[4e2cf8b]443
[58898d1d]444 return (rd / size);
[2595dab]445}
[55cff86]446
[facebd56]447/** Write to a stream (unbuffered).
448 *
449 * @param buf Source buffer.
450 * @param size Size of each record.
451 * @param nmemb Number of records to write.
452 * @param stream Pointer to the stream.
[6afc9d7]453 *
454 * @return Number of elements successfully written. On error this is less than
455 * nmemb, stream error indicator is set and errno is set.
[facebd56]456 */
[ef8bcc6]457static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
[2595dab]458{
[002252a]459 if (size == 0 || nmemb == 0)
460 return 0;
461
[58898d1d]462 ssize_t wr;
463 if (stream->kio) {
464 size_t nwritten;
465 wr = kio_write(buf, size * nmemb, &nwritten);
466 if (wr != EOK) {
467 stream->error = true;
468 wr = 0;
[6afc9d7]469 } else {
[58898d1d]470 wr = nwritten;
[6afc9d7]471 }
[58898d1d]472 } else {
[ce04ea44]473 wr = vfs_write(stream->fd, &stream->pos, buf, size * nmemb);
[58898d1d]474 if (wr < 0) {
[ce04ea44]475 errno = wr;
[2595dab]476 stream->error = true;
[58898d1d]477 wr = 0;
[2595dab]478 }
479 }
[facebd56]480
[58898d1d]481 if (wr > 0)
[facebd56]482 stream->need_sync = true;
[2595dab]483
[58898d1d]484 return (wr / size);
[4e2cf8b]485}
486
[6afc9d7]487/** Read some data in stream buffer.
488 *
489 * On error, stream error indicator is set and errno is set.
490 */
[facebd56]491static void _ffillbuf(FILE *stream)
492{
493 ssize_t rc;
494
495 stream->buf_head = stream->buf_tail = stream->buf;
496
[ce04ea44]497 rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size);
[facebd56]498 if (rc < 0) {
[ce04ea44]499 errno = rc;
[facebd56]500 stream->error = true;
501 return;
502 }
503
504 if (rc == 0) {
505 stream->eof = true;
506 return;
507 }
508
509 stream->buf_head += rc;
510 stream->buf_state = _bs_read;
511}
512
513/** Write out stream buffer, do not sync stream. */
[ef8bcc6]514static void _fflushbuf(FILE *stream)
515{
516 size_t bytes_used;
[facebd56]517
[bfd247f]518 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
[ef8bcc6]519 return;
[facebd56]520
521 bytes_used = stream->buf_head - stream->buf_tail;
522
523 /* If buffer has prefetched read data, we need to seek back. */
[58898d1d]524 if (bytes_used > 0 && stream->buf_state == _bs_read)
525 stream->pos -= bytes_used;
[facebd56]526
527 /* If buffer has unwritten data, we need to write them out. */
[6afc9d7]528 if (bytes_used > 0 && stream->buf_state == _bs_write) {
[facebd56]529 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
[6afc9d7]530 /* On error stream error indicator and errno are set by _fwrite */
531 if (stream->error)
532 return;
533 }
[facebd56]534
[ef8bcc6]535 stream->buf_head = stream->buf;
[facebd56]536 stream->buf_tail = stream->buf;
537 stream->buf_state = _bs_empty;
[ef8bcc6]538}
539
[facebd56]540/** Read from a stream.
541 *
542 * @param dest Destination buffer.
543 * @param size Size of each record.
544 * @param nmemb Number of records to read.
545 * @param stream Pointer to the stream.
546 *
547 */
548size_t fread(void *dest, size_t size, size_t nmemb, FILE *stream)
549{
550 uint8_t *dp;
551 size_t bytes_left;
552 size_t now;
553 size_t data_avail;
554 size_t total_read;
555 size_t i;
556
557 if (size == 0 || nmemb == 0)
558 return 0;
559
[bd5414e]560 bytes_left = size * nmemb;
561 total_read = 0;
562 dp = (uint8_t *) dest;
563
564 /* Bytes from ungetc() buffer */
565 while (stream->ungetc_chars > 0 && bytes_left > 0) {
566 *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
567 ++total_read;
[a955fcc]568 --bytes_left;
[bd5414e]569 }
570
[facebd56]571 /* If not buffered stream, read in directly. */
572 if (stream->btype == _IONBF) {
[bd5414e]573 total_read += _fread(dest, 1, bytes_left, stream);
574 return total_read / size;
[facebd56]575 }
576
577 /* Make sure no data is pending write. */
578 if (stream->buf_state == _bs_write)
579 _fflushbuf(stream);
580
581 /* Perform lazy allocation of stream buffer. */
582 if (stream->buf == NULL) {
583 if (_fallocbuf(stream) != 0)
584 return 0; /* Errno set by _fallocbuf(). */
585 }
586
587 while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
588 if (stream->buf_head == stream->buf_tail)
589 _ffillbuf(stream);
590
[6afc9d7]591 if (stream->error || stream->eof) {
592 /* On error errno was set by _ffillbuf() */
[facebd56]593 break;
[6afc9d7]594 }
[facebd56]595
596 data_avail = stream->buf_head - stream->buf_tail;
597
598 if (bytes_left > data_avail)
599 now = data_avail;
600 else
601 now = bytes_left;
602
603 for (i = 0; i < now; i++) {
604 dp[i] = stream->buf_tail[i];
605 }
606
607 dp += now;
608 stream->buf_tail += now;
609 bytes_left -= now;
610 total_read += now;
611 }
612
613 return (total_read / size);
614}
615
616
[ef8bcc6]617/** Write to a stream.
618 *
619 * @param buf Source buffer.
620 * @param size Size of each record.
621 * @param nmemb Number of records to write.
622 * @param stream Pointer to the stream.
623 *
624 */
625size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
626{
627 uint8_t *data;
628 size_t bytes_left;
629 size_t now;
630 size_t buf_free;
631 size_t total_written;
632 size_t i;
633 uint8_t b;
634 bool need_flush;
[002252a]635
636 if (size == 0 || nmemb == 0)
637 return 0;
638
[ef8bcc6]639 /* If not buffered stream, write out directly. */
[bfd247f]640 if (stream->btype == _IONBF) {
641 now = _fwrite(buf, size, nmemb, stream);
642 fflush(stream);
643 return now;
644 }
[facebd56]645
646 /* Make sure buffer contains no prefetched data. */
647 if (stream->buf_state == _bs_read)
648 _fflushbuf(stream);
649
[ef8bcc6]650 /* Perform lazy allocation of stream buffer. */
651 if (stream->buf == NULL) {
652 if (_fallocbuf(stream) != 0)
653 return 0; /* Errno set by _fallocbuf(). */
654 }
[bfd247f]655
[ef8bcc6]656 data = (uint8_t *) buf;
657 bytes_left = size * nmemb;
658 total_written = 0;
659 need_flush = false;
[bfd247f]660
661 while ((!stream->error) && (bytes_left > 0)) {
[ef8bcc6]662 buf_free = stream->buf_size - (stream->buf_head - stream->buf);
663 if (bytes_left > buf_free)
664 now = buf_free;
665 else
666 now = bytes_left;
[bfd247f]667
[ef8bcc6]668 for (i = 0; i < now; i++) {
669 b = data[i];
670 stream->buf_head[i] = b;
[bfd247f]671
672 if ((b == '\n') && (stream->btype == _IOLBF))
[ef8bcc6]673 need_flush = true;
674 }
[bfd247f]675
[0803134]676 data += now;
[ef8bcc6]677 stream->buf_head += now;
678 buf_free -= now;
679 bytes_left -= now;
680 total_written += now;
[0803134]681 stream->buf_state = _bs_write;
[bfd247f]682
[ef8bcc6]683 if (buf_free == 0) {
684 /* Only need to drain buffer. */
685 _fflushbuf(stream);
[6afc9d7]686 if (!stream->error)
687 need_flush = false;
[ef8bcc6]688 }
689 }
[facebd56]690
[ef8bcc6]691 if (need_flush)
692 fflush(stream);
[bfd247f]693
[ef8bcc6]694 return (total_written / size);
695}
696
[2595dab]697int fputc(wchar_t c, FILE *stream)
[c9857c6]698{
[56fa418]699 char buf[STR_BOUNDS(1)];
[2595dab]700 size_t sz = 0;
701
702 if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
[2a5af223]703 size_t wr = fwrite(buf, 1, sz, stream);
[2595dab]704
705 if (wr < sz)
706 return EOF;
707
708 return (int) c;
709 }
710
711 return EOF;
712}
[56fa418]713
[2595dab]714int putchar(wchar_t c)
715{
716 return fputc(c, stdout);
717}
[56fa418]718
[2595dab]719int fputs(const char *str, FILE *stream)
720{
[7db5cfd]721 (void) fwrite(str, str_size(str), 1, stream);
722 if (ferror(stream))
723 return EOF;
724 return 0;
[2595dab]725}
[56fa418]726
[2595dab]727int puts(const char *str)
728{
729 return fputs(str, stdout);
[c9857c6]730}
[b27a97bb]731
[2595dab]732int fgetc(FILE *stream)
[b27a97bb]733{
[2595dab]734 char c;
[bfd247f]735
[bac82eeb]736 /* This could be made faster by only flushing when needed. */
[b8e57e8c]737 if (stdout)
738 fflush(stdout);
739 if (stderr)
740 fflush(stderr);
[bfd247f]741
[2595dab]742 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
743 return EOF;
[b27a97bb]744
[2595dab]745 return (int) c;
746}
747
[c62d2e1]748char *fgets(char *str, int size, FILE *stream)
749{
[a634485]750 int c;
[c62d2e1]751 int idx;
752
753 idx = 0;
754 while (idx < size - 1) {
755 c = fgetc(stream);
756 if (c == EOF)
757 break;
758
759 str[idx++] = c;
760
761 if (c == '\n')
762 break;
763 }
764
765 if (ferror(stream))
766 return NULL;
767
768 if (idx == 0)
769 return NULL;
770
771 str[idx] = '\0';
772 return str;
773}
774
[2595dab]775int getchar(void)
776{
777 return fgetc(stdin);
[b27a97bb]778}
779
[bd5414e]780int ungetc(int c, FILE *stream)
781{
782 if (c == EOF)
783 return EOF;
784
785 if (stream->ungetc_chars >= UNGETC_MAX)
786 return EOF;
787
788 stream->ungetc_buf[stream->ungetc_chars++] =
789 (uint8_t)c;
790
791 stream->eof = false;
792 return (uint8_t)c;
793}
794
[456c086]795int fseek(FILE *stream, long offset, int whence)
[d2cc7e1]796{
[23a0368]797 int rc;
798
[6afc9d7]799 if (stream->error)
[58898d1d]800 return -1;
[6afc9d7]801
[facebd56]802 _fflushbuf(stream);
[6afc9d7]803 if (stream->error) {
804 /* errno was set by _fflushbuf() */
[58898d1d]805 return -1;
[6afc9d7]806 }
807
[bd5414e]808 stream->ungetc_chars = 0;
[facebd56]809
[58898d1d]810 struct stat st;
811 switch (whence) {
812 case SEEK_SET:
813 stream->pos = offset;
814 break;
815 case SEEK_CUR:
816 stream->pos += offset;
817 break;
818 case SEEK_END:
[23a0368]819 rc = vfs_stat(stream->fd, &st);
820 if (rc != EOK) {
821 errno = rc;
[58898d1d]822 stream->error = true;
823 return -1;
824 }
825 stream->pos = st.size + offset;
826 break;
[2595dab]827 }
[facebd56]828
[2595dab]829 stream->eof = false;
[566f4cfb]830 return 0;
[d2cc7e1]831}
832
[456c086]833long ftell(FILE *stream)
[08232ee]834{
[59f388a]835 /* The native position is too large for the C99-ish interface. */
[456c086]836 if (stream->pos - stream->ungetc_chars > LONG_MAX)
[59f388a]837 return EOF;
838
[6afc9d7]839 if (stream->error)
840 return EOF;
841
[8ad496d]842 _fflushbuf(stream);
[6afc9d7]843 if (stream->error) {
844 /* errno was set by _fflushbuf() */
845 return EOF;
846 }
847
[58898d1d]848 return stream->pos - stream->ungetc_chars;
[08232ee]849}
850
[080ad7f]851void rewind(FILE *stream)
852{
853 (void) fseek(stream, 0, SEEK_SET);
854}
855
[2595dab]856int fflush(FILE *stream)
857{
[6afc9d7]858 if (stream->error)
859 return EOF;
860
[ef8bcc6]861 _fflushbuf(stream);
[6afc9d7]862 if (stream->error) {
863 /* errno was set by _fflushbuf() */
864 return EOF;
865 }
[bfd247f]866
[6fa9a99d]867 if (stream->kio) {
868 kio_update();
[6afc9d7]869 return 0;
[2595dab]870 }
871
[79ae36dd]872 if ((stream->fd >= 0) && (stream->need_sync)) {
[a56cef9]873 int rc;
874
[facebd56]875 /**
876 * Better than syncing always, but probably still not the
877 * right thing to do.
878 */
879 stream->need_sync = false;
[a56cef9]880 rc = vfs_sync(stream->fd);
881 if (rc != EOK) {
882 errno = rc;
[6afc9d7]883 return EOF;
884 }
885
886 return 0;
[facebd56]887 }
[2595dab]888
[6afc9d7]889 return 0;
[2595dab]890}
891
892int feof(FILE *stream)
893{
894 return stream->eof;
895}
896
897int ferror(FILE *stream)
898{
899 return stream->error;
900}
901
[c77a64f]902void clearerr(FILE *stream)
903{
904 stream->eof = false;
905 stream->error = false;
906}
907
[3629481]908int fileno(FILE *stream)
909{
[6fa9a99d]910 if (stream->kio) {
[3629481]911 errno = EBADF;
[6afc9d7]912 return EOF;
[3629481]913 }
914
915 return stream->fd;
916}
917
[6afc9d7]918async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
[2595dab]919{
920 if (stream->fd >= 0) {
[79ae36dd]921 if (stream->sess == NULL)
[6afc9d7]922 stream->sess = vfs_fd_session(stream->fd, iface);
[2595dab]923
[79ae36dd]924 return stream->sess;
[2595dab]925 }
926
[79ae36dd]927 return NULL;
[2595dab]928}
929
[6afc9d7]930int vfs_fhandle(FILE *stream, int *handle)
[2595dab]931{
[7171760]932 if (stream->fd >= 0) {
933 *handle = stream->fd;
934 return EOK;
935 }
[a68f737]936
937 return ENOENT;
[2595dab]938}
939
[fadd381]940/** @}
[b2951e2]941 */
Note: See TracBrowser for help on using the repository browser.