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
RevLine 
[b861b58]1/*
[df4ed85]2 * Copyright (c) 2005 Martin Decky
[b861b58]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
[b2951e2]27 */
28
[fadd381]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
[2595dab]33 */
[b861b58]34
[cc6f688]35#include <stdio.h>
[ef8bcc6]36#include <assert.h>
[19f857a]37#include <str.h>
[56fa418]38#include <errno.h>
[3e6a98c5]39#include <stdbool.h>
[38d150e]40#include <stdlib.h>
[64d2b10]41#include <async.h>
[6fa9a99d]42#include <io/kio.h>
[2595dab]43#include <vfs/vfs.h>
[79ae36dd]44#include <vfs/vfs_sess.h>
[bb9ec2d]45#include <vfs/inbox.h>
[15f3c3f]46#include <ipc/loc.h>
[d9c8c81]47#include <adt/list.h>
[ed88c8e]48#include <wchar.h>
[47b7006]49#include "../private/io.h"
[79ae36dd]50#include "../private/stdio.h"
[b861b58]51
[facebd56]52static void _ffillbuf(FILE *stream);
[ef8bcc6]53static void _fflushbuf(FILE *stream);
54
[01cc7b4]55static size_t stdio_kio_read(void *, size_t, size_t, FILE *);
56static size_t stdio_kio_write(const void *, size_t, size_t, FILE *);
57static int stdio_kio_flush(FILE *);
58
59static size_t stdio_vfs_read(void *, size_t, size_t, FILE *);
60static size_t stdio_vfs_write(const void *, size_t, size_t, FILE *);
61
62static int stdio_vfs_flush(FILE *);
63
64/** KIO stream ops */
65static __stream_ops_t stdio_kio_ops = {
66 .read = stdio_kio_read,
67 .write = stdio_kio_write,
68 .flush = stdio_kio_flush
69};
70
71/** VFS stream ops */
72static __stream_ops_t stdio_vfs_ops = {
73 .read = stdio_vfs_read,
74 .write = stdio_vfs_write,
75 .flush = stdio_vfs_flush
76};
77
[a68f737]78static FILE stdin_null = {
[2595dab]79 .fd = -1,
[1b20da0]80 .pos = 0,
[2595dab]81 .error = true,
82 .eof = true,
[01cc7b4]83 .ops = &stdio_vfs_ops,
84 .arg = NULL,
[79ae36dd]85 .sess = NULL,
[ef8bcc6]86 .btype = _IONBF,
87 .buf = NULL,
88 .buf_size = 0,
[facebd56]89 .buf_head = NULL,
90 .buf_tail = NULL,
[c70703a]91 .buf_state = _bs_empty
[2595dab]92};
[350514c]93
[6fa9a99d]94static FILE stdout_kio = {
[2595dab]95 .fd = -1,
[1b20da0]96 .pos = 0,
[2595dab]97 .error = false,
98 .eof = false,
[01cc7b4]99 .ops = &stdio_kio_ops,
100 .arg = NULL,
[79ae36dd]101 .sess = NULL,
[ef8bcc6]102 .btype = _IOLBF,
103 .buf = NULL,
104 .buf_size = BUFSIZ,
[facebd56]105 .buf_head = NULL,
106 .buf_tail = NULL,
107 .buf_state = _bs_empty
[2595dab]108};
109
[6fa9a99d]110static FILE stderr_kio = {
[a68f737]111 .fd = -1,
[1b20da0]112 .pos = 0,
[a68f737]113 .error = false,
114 .eof = false,
[01cc7b4]115 .ops = &stdio_kio_ops,
116 .arg = NULL,
[79ae36dd]117 .sess = NULL,
[ef8bcc6]118 .btype = _IONBF,
119 .buf = NULL,
120 .buf_size = 0,
[facebd56]121 .buf_head = NULL,
122 .buf_tail = NULL,
123 .buf_state = _bs_empty
[a68f737]124};
125
126FILE *stdin = NULL;
127FILE *stdout = NULL;
128FILE *stderr = NULL;
129
130static LIST_INITIALIZE(files);
131
[bb9ec2d]132void __stdio_init(void)
[a68f737]133{
[7c3fb9b]134 /*
135 * The first three standard file descriptors are assigned for compatibility.
[bb9ec2d]136 * This will probably be removed later.
137 */
138 int infd = inbox_get("stdin");
139 if (infd >= 0) {
[f77c1c9]140 int stdinfd = -1;
141 (void) vfs_clone(infd, -1, false, &stdinfd);
[bb9ec2d]142 assert(stdinfd == 0);
[b19e892]143 vfs_open(stdinfd, MODE_READ);
[bb9ec2d]144 stdin = fdopen(stdinfd, "r");
[a68f737]145 } else {
146 stdin = &stdin_null;
147 list_append(&stdin->link, &files);
148 }
[a35b458]149
[bb9ec2d]150 int outfd = inbox_get("stdout");
151 if (outfd >= 0) {
[f77c1c9]152 int stdoutfd = -1;
153 (void) vfs_clone(outfd, -1, false, &stdoutfd);
[bb9ec2d]154 assert(stdoutfd <= 1);
[fcab7ef]155 while (stdoutfd < 1)
[f77c1c9]156 (void) vfs_clone(outfd, -1, false, &stdoutfd);
[b19e892]157 vfs_open(stdoutfd, MODE_APPEND);
[bb9ec2d]158 stdout = fdopen(stdoutfd, "a");
[a68f737]159 } else {
[6fa9a99d]160 stdout = &stdout_kio;
[a68f737]161 list_append(&stdout->link, &files);
162 }
[a35b458]163
[bb9ec2d]164 int errfd = inbox_get("stderr");
165 if (errfd >= 0) {
[f77c1c9]166 int stderrfd = -1;
167 (void) vfs_clone(errfd, -1, false, &stderrfd);
[bb9ec2d]168 assert(stderrfd <= 2);
[fcab7ef]169 while (stderrfd < 2)
[f77c1c9]170 (void) vfs_clone(errfd, -1, false, &stderrfd);
[b19e892]171 vfs_open(stderrfd, MODE_APPEND);
[bb9ec2d]172 stderr = fdopen(stderrfd, "a");
[a68f737]173 } else {
[6fa9a99d]174 stderr = &stderr_kio;
[a68f737]175 list_append(&stderr->link, &files);
176 }
177}
178
[db24058]179void __stdio_done(void)
[a68f737]180{
[b72efe8]181 while (!list_empty(&files)) {
182 FILE *file = list_get_instance(list_first(&files), FILE, link);
[a68f737]183 fclose(file);
184 }
185}
[2595dab]186
[7ab7075f]187static bool parse_mode(const char *fmode, int *mode, bool *create, bool *excl,
188 bool *truncate)
[b861b58]189{
[2595dab]190 /* Parse mode except first character. */
[b19e892]191 const char *mp = fmode;
[7ab7075f]192
193 if (*mp++ == '\0') {
[2595dab]194 errno = EINVAL;
195 return false;
196 }
[a35b458]197
[2595dab]198 if ((*mp == 'b') || (*mp == 't'))
199 mp++;
[a35b458]200
[2595dab]201 bool plus;
202 if (*mp == '+') {
203 mp++;
204 plus = true;
[7ab7075f]205 } else {
[2595dab]206 plus = false;
[7ab7075f]207 }
208
209 bool ex;
210 if (*mp == 'x') {
211 mp++;
212 ex = true;
213 } else {
214 ex = false;
215 }
[a35b458]216
[7ab7075f]217 if (*mp != '\0') {
[2595dab]218 errno = EINVAL;
219 return false;
[350514c]220 }
[b19e892]221
222 *create = false;
223 *truncate = false;
[7ab7075f]224 *excl = false;
[a35b458]225
[b19e892]226 /* Parse first character of fmode and determine mode for vfs_open(). */
227 switch (fmode[0]) {
[2595dab]228 case 'r':
[b19e892]229 *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
[7ab7075f]230 if (ex) {
231 errno = EINVAL;
232 return false;
233 }
[2595dab]234 break;
235 case 'w':
[b19e892]236 *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
237 *create = true;
[7ab7075f]238 *excl = ex;
[b19e892]239 if (!plus)
240 *truncate = true;
[2595dab]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 }
[b19e892]248
[7ab7075f]249 if (ex) {
250 errno = EINVAL;
251 return false;
252 }
253
[b19e892]254 *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
255 *create = true;
[82582e4]256 break;
[2595dab]257 default:
258 errno = EINVAL;
259 return false;
260 }
[a35b458]261
[2595dab]262 return true;
[cc6f688]263}
[b861b58]264
[ef8bcc6]265/** Set stream buffer. */
[58daded]266int setvbuf(FILE *stream, void *buf, int mode, size_t size)
[ef8bcc6]267{
[58daded]268 if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF)
269 return -1;
270
[ef8bcc6]271 stream->btype = mode;
272 stream->buf = buf;
273 stream->buf_size = size;
274 stream->buf_head = stream->buf;
[facebd56]275 stream->buf_tail = stream->buf;
276 stream->buf_state = _bs_empty;
[58daded]277
278 return 0;
[ef8bcc6]279}
280
[7699c21]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
[bfd247f]295static void _setvbuf(FILE *stream)
296{
297 /* FIXME: Use more complex rules for setting buffering options. */
[a35b458]298
[bfd247f]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
[ef8bcc6]312/** Allocate stream buffer. */
313static int _fallocbuf(FILE *stream)
314{
315 assert(stream->buf == NULL);
[a35b458]316
[ef8bcc6]317 stream->buf = malloc(stream->buf_size);
318 if (stream->buf == NULL) {
319 errno = ENOMEM;
[6afc9d7]320 return EOF;
[ef8bcc6]321 }
[a35b458]322
[ef8bcc6]323 stream->buf_head = stream->buf;
[facebd56]324 stream->buf_tail = stream->buf;
[ef8bcc6]325 return 0;
326}
327
[2595dab]328/** Open a stream.
329 *
330 * @param path Path of the file to open.
[7ab7075f]331 * @param mode Mode string, (r|w|a)[b|t][+][x].
[2595dab]332 *
[4e2cf8b]333 */
[b19e892]334FILE *fopen(const char *path, const char *fmode)
[4e2cf8b]335{
[b19e892]336 int mode;
337 bool create;
[7ab7075f]338 bool excl;
[b19e892]339 bool truncate;
340
[7ab7075f]341 if (!parse_mode(fmode, &mode, &create, &excl, &truncate))
[2595dab]342 return NULL;
[a35b458]343
[2595dab]344 /* Open file. */
345 FILE *stream = malloc(sizeof(FILE));
346 if (stream == NULL) {
347 errno = ENOMEM;
348 return NULL;
349 }
[b19e892]350
351 int flags = WALK_REGULAR;
[7ab7075f]352 if (create && excl)
353 flags |= WALK_MUST_CREATE;
354 else if (create)
[b19e892]355 flags |= WALK_MAY_CREATE;
[f77c1c9]356 int file;
[b7fd2a0]357 errno_t rc = vfs_lookup(path, flags, &file);
[f77c1c9]358 if (rc != EOK) {
359 errno = rc;
[b19e892]360 free(stream);
361 return NULL;
362 }
363
[f77c1c9]364 rc = vfs_open(file, mode);
[b19e892]365 if (rc != EOK) {
366 errno = rc;
[9c4cf0d]367 vfs_put(file);
[2595dab]368 free(stream);
369 return NULL;
370 }
[a35b458]371
[b19e892]372 if (truncate) {
373 rc = vfs_resize(file, 0);
374 if (rc != EOK) {
375 errno = rc;
[9c4cf0d]376 vfs_put(file);
[b19e892]377 free(stream);
378 return NULL;
379 }
380 }
381
382 stream->fd = file;
[58898d1d]383 stream->pos = 0;
[2595dab]384 stream->error = false;
385 stream->eof = false;
[01cc7b4]386 stream->ops = &stdio_vfs_ops;
387 stream->arg = NULL;
[79ae36dd]388 stream->sess = NULL;
[facebd56]389 stream->need_sync = false;
[bfd247f]390 _setvbuf(stream);
[e86a617a]391 stream->ungetc_chars = 0;
[a35b458]392
[a68f737]393 list_append(&stream->link, &files);
[a35b458]394
[2595dab]395 return stream;
396}
397
[080ad7f]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 }
[a35b458]406
[080ad7f]407 stream->fd = fd;
[58898d1d]408 stream->pos = 0;
[080ad7f]409 stream->error = false;
410 stream->eof = false;
[01cc7b4]411 stream->ops = &stdio_vfs_ops;
412 stream->arg = NULL;
[79ae36dd]413 stream->sess = NULL;
[facebd56]414 stream->need_sync = false;
[bfd247f]415 _setvbuf(stream);
[e86a617a]416 stream->ungetc_chars = 0;
[a35b458]417
[080ad7f]418 list_append(&stream->link, &files);
[a35b458]419
[080ad7f]420 return stream;
421}
422
[80bee81]423static int _fclose_nofree(FILE *stream)
[2595dab]424{
[b7fd2a0]425 errno_t rc = 0;
[a35b458]426
[2595dab]427 fflush(stream);
[a35b458]428
[79ae36dd]429 if (stream->sess != NULL)
430 async_hangup(stream->sess);
[a35b458]431
[2595dab]432 if (stream->fd >= 0)
[9c4cf0d]433 rc = vfs_put(stream->fd);
[a35b458]434
[a68f737]435 list_remove(&stream->link);
[a35b458]436
[f77c1c9]437 if (rc != EOK) {
438 errno = rc;
[80bee81]439 return EOF;
440 }
[a35b458]441
[80bee81]442 return 0;
443}
444
445int fclose(FILE *stream)
446{
447 int rc = _fclose_nofree(stream);
[a35b458]448
[3bacee1]449 if ((stream != &stdin_null) &&
450 (stream != &stdout_kio) &&
451 (stream != &stderr_kio))
[2595dab]452 free(stream);
[a35b458]453
[80bee81]454 return rc;
455}
456
457FILE *freopen(const char *path, const char *mode, FILE *stream)
458{
459 FILE *nstr;
[a35b458]460
[80bee81]461 if (path == NULL) {
462 /* Changing mode is not supported */
463 return NULL;
[2595dab]464 }
[a35b458]465
[80bee81]466 (void) _fclose_nofree(stream);
467 nstr = fopen(path, mode);
468 if (nstr == NULL) {
469 free(stream);
470 return NULL;
471 }
[a35b458]472
[80bee81]473 list_remove(&nstr->link);
474 *stream = *nstr;
475 list_append(&stream->link, &files);
[a35b458]476
[80bee81]477 free(nstr);
[a35b458]478
[80bee81]479 return stream;
[4e2cf8b]480}
481
[facebd56]482/** Read from a stream (unbuffered).
[2595dab]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.
[6afc9d7]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.
[4e2cf8b]491 */
[facebd56]492static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
[4e2cf8b]493{
[01cc7b4]494 return stream->ops->read(buf, size, nmemb, stream);
[2595dab]495}
[55cff86]496
[facebd56]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.
[6afc9d7]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.
[facebd56]506 */
[ef8bcc6]507static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
[2595dab]508{
[8e3498b]509 size_t nwritten;
510
[002252a]511 if (size == 0 || nmemb == 0)
512 return 0;
513
[01cc7b4]514 nwritten = stream->ops->write(buf, size, nmemb, stream);
[facebd56]515
[8e3498b]516 if (nwritten > 0)
[facebd56]517 stream->need_sync = true;
[8e3498b]518
519 return (nwritten / size);
[4e2cf8b]520}
521
[6afc9d7]522/** Read some data in stream buffer.
523 *
524 * On error, stream error indicator is set and errno is set.
525 */
[facebd56]526static void _ffillbuf(FILE *stream)
527{
[b7fd2a0]528 errno_t rc;
[8e3498b]529 size_t nread;
[facebd56]530
531 stream->buf_head = stream->buf_tail = stream->buf;
532
[8e3498b]533 rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
534 &nread);
535 if (rc != EOK) {
[ce04ea44]536 errno = rc;
[facebd56]537 stream->error = true;
538 return;
539 }
540
[8e3498b]541 if (nread == 0) {
[facebd56]542 stream->eof = true;
543 return;
544 }
545
[8e3498b]546 stream->buf_head += nread;
[facebd56]547 stream->buf_state = _bs_read;
548}
549
550/** Write out stream buffer, do not sync stream. */
[ef8bcc6]551static void _fflushbuf(FILE *stream)
552{
553 size_t bytes_used;
[facebd56]554
[bfd247f]555 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
[ef8bcc6]556 return;
[facebd56]557
558 bytes_used = stream->buf_head - stream->buf_tail;
559
560 /* If buffer has prefetched read data, we need to seek back. */
[58898d1d]561 if (bytes_used > 0 && stream->buf_state == _bs_read)
562 stream->pos -= bytes_used;
[facebd56]563
564 /* If buffer has unwritten data, we need to write them out. */
[6afc9d7]565 if (bytes_used > 0 && stream->buf_state == _bs_write) {
[facebd56]566 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
[6afc9d7]567 /* On error stream error indicator and errno are set by _fwrite */
568 if (stream->error)
569 return;
570 }
[facebd56]571
[ef8bcc6]572 stream->buf_head = stream->buf;
[facebd56]573 stream->buf_tail = stream->buf;
574 stream->buf_state = _bs_empty;
[ef8bcc6]575}
576
[facebd56]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
[bd5414e]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;
[a955fcc]605 --bytes_left;
[bd5414e]606 }
607
[facebd56]608 /* If not buffered stream, read in directly. */
609 if (stream->btype == _IONBF) {
[bd5414e]610 total_read += _fread(dest, 1, bytes_left, stream);
611 return total_read / size;
[facebd56]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
[6afc9d7]628 if (stream->error || stream->eof) {
629 /* On error errno was set by _ffillbuf() */
[facebd56]630 break;
[6afc9d7]631 }
[facebd56]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
[ef8bcc6]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;
[002252a]671
672 if (size == 0 || nmemb == 0)
673 return 0;
674
[ef8bcc6]675 /* If not buffered stream, write out directly. */
[bfd247f]676 if (stream->btype == _IONBF) {
677 now = _fwrite(buf, size, nmemb, stream);
678 fflush(stream);
679 return now;
680 }
[facebd56]681
682 /* Make sure buffer contains no prefetched data. */
683 if (stream->buf_state == _bs_read)
684 _fflushbuf(stream);
685
[ef8bcc6]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 }
[a35b458]691
[ef8bcc6]692 data = (uint8_t *) buf;
693 bytes_left = size * nmemb;
694 total_written = 0;
695 need_flush = false;
[a35b458]696
[bfd247f]697 while ((!stream->error) && (bytes_left > 0)) {
[ef8bcc6]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;
[a35b458]703
[ef8bcc6]704 for (i = 0; i < now; i++) {
705 b = data[i];
706 stream->buf_head[i] = b;
[a35b458]707
[bfd247f]708 if ((b == '\n') && (stream->btype == _IOLBF))
[ef8bcc6]709 need_flush = true;
710 }
[a35b458]711
[0803134]712 data += now;
[ef8bcc6]713 stream->buf_head += now;
714 buf_free -= now;
715 bytes_left -= now;
716 total_written += now;
[0803134]717 stream->buf_state = _bs_write;
[a35b458]718
[ef8bcc6]719 if (buf_free == 0) {
720 /* Only need to drain buffer. */
721 _fflushbuf(stream);
[6afc9d7]722 if (!stream->error)
723 need_flush = false;
[ef8bcc6]724 }
725 }
[facebd56]726
[ef8bcc6]727 if (need_flush)
728 fflush(stream);
[a35b458]729
[ef8bcc6]730 return (total_written / size);
731}
732
[ed88c8e]733wint_t fputwc(wchar_t wc, FILE *stream)
[c9857c6]734{
[56fa418]735 char buf[STR_BOUNDS(1)];
[2595dab]736 size_t sz = 0;
[a35b458]737
[ed88c8e]738 if (chr_encode(wc, buf, &sz, STR_BOUNDS(1)) != EOK) {
739 errno = EILSEQ;
740 return WEOF;
741 }
[a35b458]742
[ed88c8e]743 size_t wr = fwrite(buf, 1, sz, stream);
744 if (wr < sz)
745 return WEOF;
[a35b458]746
[ed88c8e]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;
[a35b458]764
[ed88c8e]765 return b;
[2595dab]766}
[56fa418]767
[ed88c8e]768int putchar(int c)
[2595dab]769{
770 return fputc(c, stdout);
771}
[56fa418]772
[2595dab]773int fputs(const char *str, FILE *stream)
774{
[7db5cfd]775 (void) fwrite(str, str_size(str), 1, stream);
776 if (ferror(stream))
777 return EOF;
778 return 0;
[2595dab]779}
[56fa418]780
[2595dab]781int puts(const char *str)
782{
[1abcf1d]783 if (fputs(str, stdout) < 0)
784 return EOF;
785 return putchar('\n');
[c9857c6]786}
[b27a97bb]787
[2595dab]788int fgetc(FILE *stream)
[b27a97bb]789{
[2595dab]790 char c;
[a35b458]791
[bac82eeb]792 /* This could be made faster by only flushing when needed. */
[b8e57e8c]793 if (stdout)
794 fflush(stdout);
795 if (stderr)
796 fflush(stderr);
[a35b458]797
[2595dab]798 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
799 return EOF;
[a35b458]800
[2595dab]801 return (int) c;
802}
803
[c62d2e1]804char *fgets(char *str, int size, FILE *stream)
805{
[a634485]806 int c;
[c62d2e1]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
[2595dab]831int getchar(void)
832{
833 return fgetc(stdin);
[b27a97bb]834}
835
[bd5414e]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
[1c7f381]851int fseek64(FILE *stream, off64_t offset, int whence)
[d2cc7e1]852{
[b7fd2a0]853 errno_t rc;
[23a0368]854
[6afc9d7]855 if (stream->error)
[58898d1d]856 return -1;
[6afc9d7]857
[facebd56]858 _fflushbuf(stream);
[6afc9d7]859 if (stream->error) {
860 /* errno was set by _fflushbuf() */
[58898d1d]861 return -1;
[6afc9d7]862 }
863
[bd5414e]864 stream->ungetc_chars = 0;
[facebd56]865
[39330200]866 vfs_stat_t st;
[58898d1d]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:
[23a0368]875 rc = vfs_stat(stream->fd, &st);
876 if (rc != EOK) {
877 errno = rc;
[58898d1d]878 stream->error = true;
[1b20da0]879 return -1;
[58898d1d]880 }
881 stream->pos = st.size + offset;
882 break;
[2595dab]883 }
[facebd56]884
[2595dab]885 stream->eof = false;
[566f4cfb]886 return 0;
[d2cc7e1]887}
888
[1c7f381]889off64_t ftell64(FILE *stream)
[08232ee]890{
[6afc9d7]891 if (stream->error)
892 return EOF;
[a35b458]893
[8ad496d2]894 _fflushbuf(stream);
[6afc9d7]895 if (stream->error) {
896 /* errno was set by _fflushbuf() */
897 return EOF;
898 }
899
[58898d1d]900 return stream->pos - stream->ungetc_chars;
[08232ee]901}
902
[1c7f381]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. */
[ef84413]913 if (off > LONG_MAX) {
914 errno = EOVERFLOW;
915 return -1;
916 }
[1c7f381]917
918 return off;
919}
920
[080ad7f]921void rewind(FILE *stream)
922{
923 (void) fseek(stream, 0, SEEK_SET);
924}
925
[2595dab]926int fflush(FILE *stream)
927{
[6afc9d7]928 if (stream->error)
929 return EOF;
[a35b458]930
[ef8bcc6]931 _fflushbuf(stream);
[6afc9d7]932 if (stream->error) {
933 /* errno was set by _fflushbuf() */
934 return EOF;
935 }
[a35b458]936
[01cc7b4]937 if (stream->need_sync) {
[facebd56]938 /**
939 * Better than syncing always, but probably still not the
940 * right thing to do.
941 */
[01cc7b4]942 if (stream->ops->flush(stream) == EOF)
[6afc9d7]943 return EOF;
944
[01cc7b4]945 stream->need_sync = false;
[facebd56]946 }
[a35b458]947
[6afc9d7]948 return 0;
[2595dab]949}
950
951int feof(FILE *stream)
952{
953 return stream->eof;
954}
955
956int ferror(FILE *stream)
957{
958 return stream->error;
959}
960
[c77a64f]961void clearerr(FILE *stream)
962{
963 stream->eof = false;
964 stream->error = false;
965}
966
[3629481]967int fileno(FILE *stream)
968{
[01cc7b4]969 if (stream->ops != &stdio_vfs_ops) {
[3629481]970 errno = EBADF;
[6afc9d7]971 return EOF;
[3629481]972 }
[a35b458]973
[3629481]974 return stream->fd;
975}
976
[6afc9d7]977async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
[2595dab]978{
979 if (stream->fd >= 0) {
[79ae36dd]980 if (stream->sess == NULL)
[6afc9d7]981 stream->sess = vfs_fd_session(stream->fd, iface);
[a35b458]982
[79ae36dd]983 return stream->sess;
[2595dab]984 }
[a35b458]985
[79ae36dd]986 return NULL;
[2595dab]987}
988
[b7fd2a0]989errno_t vfs_fhandle(FILE *stream, int *handle)
[2595dab]990{
[7171760]991 if (stream->fd >= 0) {
992 *handle = stream->fd;
993 return EOK;
994 }
[a35b458]995
[a68f737]996 return ENOENT;
[2595dab]997}
998
[01cc7b4]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
[fadd381]1079/** @}
[b2951e2]1080 */
Note: See TracBrowser for help on using the repository browser.