source: mainline/uspace/lib/c/generic/io/io.c@ 7ab7075f

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

Add support for 'x' fopen file mode modifier from C11.

  • 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]423
424static int _fclose_nofree(FILE *stream)
[2595dab]425{
[b7fd2a0]426 errno_t rc = 0;
[a35b458]427
[2595dab]428 fflush(stream);
[a35b458]429
[79ae36dd]430 if (stream->sess != NULL)
431 async_hangup(stream->sess);
[a35b458]432
[2595dab]433 if (stream->fd >= 0)
[9c4cf0d]434 rc = vfs_put(stream->fd);
[a35b458]435
[a68f737]436 list_remove(&stream->link);
[a35b458]437
[f77c1c9]438 if (rc != EOK) {
439 errno = rc;
[80bee81]440 return EOF;
441 }
[a35b458]442
[80bee81]443 return 0;
444}
445
446int fclose(FILE *stream)
447{
448 int rc = _fclose_nofree(stream);
[a35b458]449
[3bacee1]450 if ((stream != &stdin_null) &&
451 (stream != &stdout_kio) &&
452 (stream != &stderr_kio))
[2595dab]453 free(stream);
[a35b458]454
[80bee81]455 return rc;
456}
457
458FILE *freopen(const char *path, const char *mode, FILE *stream)
459{
460 FILE *nstr;
[a35b458]461
[80bee81]462 if (path == NULL) {
463 /* Changing mode is not supported */
464 return NULL;
[2595dab]465 }
[a35b458]466
[80bee81]467 (void) _fclose_nofree(stream);
468 nstr = fopen(path, mode);
469 if (nstr == NULL) {
470 free(stream);
471 return NULL;
472 }
[a35b458]473
[80bee81]474 list_remove(&nstr->link);
475 *stream = *nstr;
476 list_append(&stream->link, &files);
[a35b458]477
[80bee81]478 free(nstr);
[a35b458]479
[80bee81]480 return stream;
[4e2cf8b]481}
482
[facebd56]483/** Read from a stream (unbuffered).
[2595dab]484 *
485 * @param buf Destination buffer.
486 * @param size Size of each record.
487 * @param nmemb Number of records to read.
488 * @param stream Pointer to the stream.
[6afc9d7]489 *
490 * @return Number of elements successfully read. On error this is less than
491 * nmemb, stream error indicator is set and errno is set.
[4e2cf8b]492 */
[facebd56]493static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
[4e2cf8b]494{
[01cc7b4]495 return stream->ops->read(buf, size, nmemb, stream);
[2595dab]496}
[55cff86]497
[facebd56]498/** Write to a stream (unbuffered).
499 *
500 * @param buf Source buffer.
501 * @param size Size of each record.
502 * @param nmemb Number of records to write.
503 * @param stream Pointer to the stream.
[6afc9d7]504 *
505 * @return Number of elements successfully written. On error this is less than
506 * nmemb, stream error indicator is set and errno is set.
[facebd56]507 */
[ef8bcc6]508static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
[2595dab]509{
[8e3498b]510 size_t nwritten;
511
[002252a]512 if (size == 0 || nmemb == 0)
513 return 0;
514
[01cc7b4]515 nwritten = stream->ops->write(buf, size, nmemb, stream);
[facebd56]516
[8e3498b]517 if (nwritten > 0)
[facebd56]518 stream->need_sync = true;
[8e3498b]519
520 return (nwritten / size);
[4e2cf8b]521}
522
[6afc9d7]523/** Read some data in stream buffer.
524 *
525 * On error, stream error indicator is set and errno is set.
526 */
[facebd56]527static void _ffillbuf(FILE *stream)
528{
[b7fd2a0]529 errno_t rc;
[8e3498b]530 size_t nread;
[facebd56]531
532 stream->buf_head = stream->buf_tail = stream->buf;
533
[8e3498b]534 rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size,
535 &nread);
536 if (rc != EOK) {
[ce04ea44]537 errno = rc;
[facebd56]538 stream->error = true;
539 return;
540 }
541
[8e3498b]542 if (nread == 0) {
[facebd56]543 stream->eof = true;
544 return;
545 }
546
[8e3498b]547 stream->buf_head += nread;
[facebd56]548 stream->buf_state = _bs_read;
549}
550
551/** Write out stream buffer, do not sync stream. */
[ef8bcc6]552static void _fflushbuf(FILE *stream)
553{
554 size_t bytes_used;
[facebd56]555
[bfd247f]556 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
[ef8bcc6]557 return;
[facebd56]558
559 bytes_used = stream->buf_head - stream->buf_tail;
560
561 /* If buffer has prefetched read data, we need to seek back. */
[58898d1d]562 if (bytes_used > 0 && stream->buf_state == _bs_read)
563 stream->pos -= bytes_used;
[facebd56]564
565 /* If buffer has unwritten data, we need to write them out. */
[6afc9d7]566 if (bytes_used > 0 && stream->buf_state == _bs_write) {
[facebd56]567 (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
[6afc9d7]568 /* On error stream error indicator and errno are set by _fwrite */
569 if (stream->error)
570 return;
571 }
[facebd56]572
[ef8bcc6]573 stream->buf_head = stream->buf;
[facebd56]574 stream->buf_tail = stream->buf;
575 stream->buf_state = _bs_empty;
[ef8bcc6]576}
577
[facebd56]578/** Read from a stream.
579 *
580 * @param dest Destination buffer.
581 * @param size Size of each record.
582 * @param nmemb Number of records to read.
583 * @param stream Pointer to the stream.
584 *
585 */
586size_t fread(void *dest, size_t size, size_t nmemb, FILE *stream)
587{
588 uint8_t *dp;
589 size_t bytes_left;
590 size_t now;
591 size_t data_avail;
592 size_t total_read;
593 size_t i;
594
595 if (size == 0 || nmemb == 0)
596 return 0;
597
[bd5414e]598 bytes_left = size * nmemb;
599 total_read = 0;
600 dp = (uint8_t *) dest;
601
602 /* Bytes from ungetc() buffer */
603 while (stream->ungetc_chars > 0 && bytes_left > 0) {
604 *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
605 ++total_read;
[a955fcc]606 --bytes_left;
[bd5414e]607 }
608
[facebd56]609 /* If not buffered stream, read in directly. */
610 if (stream->btype == _IONBF) {
[bd5414e]611 total_read += _fread(dest, 1, bytes_left, stream);
612 return total_read / size;
[facebd56]613 }
614
615 /* Make sure no data is pending write. */
616 if (stream->buf_state == _bs_write)
617 _fflushbuf(stream);
618
619 /* Perform lazy allocation of stream buffer. */
620 if (stream->buf == NULL) {
621 if (_fallocbuf(stream) != 0)
622 return 0; /* Errno set by _fallocbuf(). */
623 }
624
625 while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
626 if (stream->buf_head == stream->buf_tail)
627 _ffillbuf(stream);
628
[6afc9d7]629 if (stream->error || stream->eof) {
630 /* On error errno was set by _ffillbuf() */
[facebd56]631 break;
[6afc9d7]632 }
[facebd56]633
634 data_avail = stream->buf_head - stream->buf_tail;
635
636 if (bytes_left > data_avail)
637 now = data_avail;
638 else
639 now = bytes_left;
640
641 for (i = 0; i < now; i++) {
642 dp[i] = stream->buf_tail[i];
643 }
644
645 dp += now;
646 stream->buf_tail += now;
647 bytes_left -= now;
648 total_read += now;
649 }
650
651 return (total_read / size);
652}
653
654
[ef8bcc6]655/** Write to a stream.
656 *
657 * @param buf Source buffer.
658 * @param size Size of each record.
659 * @param nmemb Number of records to write.
660 * @param stream Pointer to the stream.
661 *
662 */
663size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
664{
665 uint8_t *data;
666 size_t bytes_left;
667 size_t now;
668 size_t buf_free;
669 size_t total_written;
670 size_t i;
671 uint8_t b;
672 bool need_flush;
[002252a]673
674 if (size == 0 || nmemb == 0)
675 return 0;
676
[ef8bcc6]677 /* If not buffered stream, write out directly. */
[bfd247f]678 if (stream->btype == _IONBF) {
679 now = _fwrite(buf, size, nmemb, stream);
680 fflush(stream);
681 return now;
682 }
[facebd56]683
684 /* Make sure buffer contains no prefetched data. */
685 if (stream->buf_state == _bs_read)
686 _fflushbuf(stream);
687
[ef8bcc6]688 /* Perform lazy allocation of stream buffer. */
689 if (stream->buf == NULL) {
690 if (_fallocbuf(stream) != 0)
691 return 0; /* Errno set by _fallocbuf(). */
692 }
[a35b458]693
[ef8bcc6]694 data = (uint8_t *) buf;
695 bytes_left = size * nmemb;
696 total_written = 0;
697 need_flush = false;
[a35b458]698
[bfd247f]699 while ((!stream->error) && (bytes_left > 0)) {
[ef8bcc6]700 buf_free = stream->buf_size - (stream->buf_head - stream->buf);
701 if (bytes_left > buf_free)
702 now = buf_free;
703 else
704 now = bytes_left;
[a35b458]705
[ef8bcc6]706 for (i = 0; i < now; i++) {
707 b = data[i];
708 stream->buf_head[i] = b;
[a35b458]709
[bfd247f]710 if ((b == '\n') && (stream->btype == _IOLBF))
[ef8bcc6]711 need_flush = true;
712 }
[a35b458]713
[0803134]714 data += now;
[ef8bcc6]715 stream->buf_head += now;
716 buf_free -= now;
717 bytes_left -= now;
718 total_written += now;
[0803134]719 stream->buf_state = _bs_write;
[a35b458]720
[ef8bcc6]721 if (buf_free == 0) {
722 /* Only need to drain buffer. */
723 _fflushbuf(stream);
[6afc9d7]724 if (!stream->error)
725 need_flush = false;
[ef8bcc6]726 }
727 }
[facebd56]728
[ef8bcc6]729 if (need_flush)
730 fflush(stream);
[a35b458]731
[ef8bcc6]732 return (total_written / size);
733}
734
[ed88c8e]735wint_t fputwc(wchar_t wc, FILE *stream)
[c9857c6]736{
[56fa418]737 char buf[STR_BOUNDS(1)];
[2595dab]738 size_t sz = 0;
[a35b458]739
[ed88c8e]740 if (chr_encode(wc, buf, &sz, STR_BOUNDS(1)) != EOK) {
741 errno = EILSEQ;
742 return WEOF;
743 }
[a35b458]744
[ed88c8e]745 size_t wr = fwrite(buf, 1, sz, stream);
746 if (wr < sz)
747 return WEOF;
[a35b458]748
[ed88c8e]749 return wc;
750}
751
752wint_t putwchar(wchar_t wc)
753{
754 return fputwc(wc, stdout);
755}
756
757int fputc(int c, FILE *stream)
758{
759 unsigned char b;
760 size_t wr;
761
762 b = (unsigned char) c;
763 wr = fwrite(&b, sizeof(b), 1, stream);
764 if (wr < 1)
765 return EOF;
[a35b458]766
[ed88c8e]767 return b;
[2595dab]768}
[56fa418]769
[ed88c8e]770int putchar(int c)
[2595dab]771{
772 return fputc(c, stdout);
773}
[56fa418]774
[2595dab]775int fputs(const char *str, FILE *stream)
776{
[7db5cfd]777 (void) fwrite(str, str_size(str), 1, stream);
778 if (ferror(stream))
779 return EOF;
780 return 0;
[2595dab]781}
[56fa418]782
[2595dab]783int puts(const char *str)
784{
[1abcf1d]785 if (fputs(str, stdout) < 0)
786 return EOF;
787 return putchar('\n');
[c9857c6]788}
[b27a97bb]789
[2595dab]790int fgetc(FILE *stream)
[b27a97bb]791{
[2595dab]792 char c;
[a35b458]793
[bac82eeb]794 /* This could be made faster by only flushing when needed. */
[b8e57e8c]795 if (stdout)
796 fflush(stdout);
797 if (stderr)
798 fflush(stderr);
[a35b458]799
[2595dab]800 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
801 return EOF;
[a35b458]802
[2595dab]803 return (int) c;
804}
805
[c62d2e1]806char *fgets(char *str, int size, FILE *stream)
807{
[a634485]808 int c;
[c62d2e1]809 int idx;
810
811 idx = 0;
812 while (idx < size - 1) {
813 c = fgetc(stream);
814 if (c == EOF)
815 break;
816
817 str[idx++] = c;
818
819 if (c == '\n')
820 break;
821 }
822
823 if (ferror(stream))
824 return NULL;
825
826 if (idx == 0)
827 return NULL;
828
829 str[idx] = '\0';
830 return str;
831}
832
[2595dab]833int getchar(void)
834{
835 return fgetc(stdin);
[b27a97bb]836}
837
[bd5414e]838int ungetc(int c, FILE *stream)
839{
840 if (c == EOF)
841 return EOF;
842
843 if (stream->ungetc_chars >= UNGETC_MAX)
844 return EOF;
845
846 stream->ungetc_buf[stream->ungetc_chars++] =
847 (uint8_t)c;
848
849 stream->eof = false;
850 return (uint8_t)c;
851}
852
[1c7f381]853int fseek64(FILE *stream, off64_t offset, int whence)
[d2cc7e1]854{
[b7fd2a0]855 errno_t rc;
[23a0368]856
[6afc9d7]857 if (stream->error)
[58898d1d]858 return -1;
[6afc9d7]859
[facebd56]860 _fflushbuf(stream);
[6afc9d7]861 if (stream->error) {
862 /* errno was set by _fflushbuf() */
[58898d1d]863 return -1;
[6afc9d7]864 }
865
[bd5414e]866 stream->ungetc_chars = 0;
[facebd56]867
[39330200]868 vfs_stat_t st;
[58898d1d]869 switch (whence) {
870 case SEEK_SET:
871 stream->pos = offset;
872 break;
873 case SEEK_CUR:
874 stream->pos += offset;
875 break;
876 case SEEK_END:
[23a0368]877 rc = vfs_stat(stream->fd, &st);
878 if (rc != EOK) {
879 errno = rc;
[58898d1d]880 stream->error = true;
[1b20da0]881 return -1;
[58898d1d]882 }
883 stream->pos = st.size + offset;
884 break;
[2595dab]885 }
[facebd56]886
[2595dab]887 stream->eof = false;
[566f4cfb]888 return 0;
[d2cc7e1]889}
890
[1c7f381]891off64_t ftell64(FILE *stream)
[08232ee]892{
[6afc9d7]893 if (stream->error)
894 return EOF;
[a35b458]895
[8ad496d2]896 _fflushbuf(stream);
[6afc9d7]897 if (stream->error) {
898 /* errno was set by _fflushbuf() */
899 return EOF;
900 }
901
[58898d1d]902 return stream->pos - stream->ungetc_chars;
[08232ee]903}
904
[1c7f381]905int fseek(FILE *stream, long offset, int whence)
906{
907 return fseek64(stream, offset, whence);
908}
909
910long ftell(FILE *stream)
911{
912 off64_t off = ftell64(stream);
913
914 /* The native position is too large for the C99-ish interface. */
915 if (off > LONG_MAX)
916 return EOF;
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.