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

Last change on this file since eec201d was 3f7fe9e, checked in by Jiří Zárevúcky <jiri.zarevucky@…>, 7 years ago

Clean up headers

Depends on <limits.h> and <stdint.h> being provided, which is a step up from
depending on mostly undocumented predefined macros.
In principle, <limits.h> and <stdint.h> mostly describe properties of
the compiler, so even though we depend on certain values for their contents,
actually defining them in the library is kind of reversal of concerns.

  • 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>
[3f7fe9e]39#include <limits.h>
[3e6a98c5]40#include <stdbool.h>
[38d150e]41#include <stdlib.h>
[64d2b10]42#include <async.h>
[6fa9a99d]43#include <io/kio.h>
[2595dab]44#include <vfs/vfs.h>
[79ae36dd]45#include <vfs/vfs_sess.h>
[bb9ec2d]46#include <vfs/inbox.h>
[15f3c3f]47#include <ipc/loc.h>
[d9c8c81]48#include <adt/list.h>
[ed88c8e]49#include <wchar.h>
[47b7006]50#include "../private/io.h"
[79ae36dd]51#include "../private/stdio.h"
[b861b58]52
[facebd56]53static void _ffillbuf(FILE *stream);
[ef8bcc6]54static void _fflushbuf(FILE *stream);
55
[01cc7b4]56static size_t stdio_kio_read(void *, size_t, size_t, FILE *);
57static size_t stdio_kio_write(const void *, size_t, size_t, FILE *);
58static int stdio_kio_flush(FILE *);
59
60static size_t stdio_vfs_read(void *, size_t, size_t, FILE *);
61static size_t stdio_vfs_write(const void *, size_t, size_t, FILE *);
62
63static int stdio_vfs_flush(FILE *);
64
65/** KIO stream ops */
66static __stream_ops_t stdio_kio_ops = {
67 .read = stdio_kio_read,
68 .write = stdio_kio_write,
69 .flush = stdio_kio_flush
70};
71
72/** VFS stream ops */
73static __stream_ops_t stdio_vfs_ops = {
74 .read = stdio_vfs_read,
75 .write = stdio_vfs_write,
76 .flush = stdio_vfs_flush
77};
78
[a68f737]79static FILE stdin_null = {
[2595dab]80 .fd = -1,
[1b20da0]81 .pos = 0,
[2595dab]82 .error = true,
83 .eof = true,
[01cc7b4]84 .ops = &stdio_vfs_ops,
85 .arg = NULL,
[79ae36dd]86 .sess = NULL,
[ef8bcc6]87 .btype = _IONBF,
88 .buf = NULL,
89 .buf_size = 0,
[facebd56]90 .buf_head = NULL,
91 .buf_tail = NULL,
[c70703a]92 .buf_state = _bs_empty
[2595dab]93};
[350514c]94
[6fa9a99d]95static FILE stdout_kio = {
[2595dab]96 .fd = -1,
[1b20da0]97 .pos = 0,
[2595dab]98 .error = false,
99 .eof = false,
[01cc7b4]100 .ops = &stdio_kio_ops,
101 .arg = NULL,
[79ae36dd]102 .sess = NULL,
[ef8bcc6]103 .btype = _IOLBF,
104 .buf = NULL,
105 .buf_size = BUFSIZ,
[facebd56]106 .buf_head = NULL,
107 .buf_tail = NULL,
108 .buf_state = _bs_empty
[2595dab]109};
110
[6fa9a99d]111static FILE stderr_kio = {
[a68f737]112 .fd = -1,
[1b20da0]113 .pos = 0,
[a68f737]114 .error = false,
115 .eof = false,
[01cc7b4]116 .ops = &stdio_kio_ops,
117 .arg = NULL,
[79ae36dd]118 .sess = NULL,
[ef8bcc6]119 .btype = _IONBF,
120 .buf = NULL,
121 .buf_size = 0,
[facebd56]122 .buf_head = NULL,
123 .buf_tail = NULL,
124 .buf_state = _bs_empty
[a68f737]125};
126
127FILE *stdin = NULL;
128FILE *stdout = NULL;
129FILE *stderr = NULL;
130
131static LIST_INITIALIZE(files);
132
[bb9ec2d]133void __stdio_init(void)
[a68f737]134{
[7c3fb9b]135 /*
136 * The first three standard file descriptors are assigned for compatibility.
[bb9ec2d]137 * This will probably be removed later.
138 */
139 int infd = inbox_get("stdin");
140 if (infd >= 0) {
[f77c1c9]141 int stdinfd = -1;
142 (void) vfs_clone(infd, -1, false, &stdinfd);
[bb9ec2d]143 assert(stdinfd == 0);
[b19e892]144 vfs_open(stdinfd, MODE_READ);
[bb9ec2d]145 stdin = fdopen(stdinfd, "r");
[a68f737]146 } else {
147 stdin = &stdin_null;
148 list_append(&stdin->link, &files);
149 }
[a35b458]150
[bb9ec2d]151 int outfd = inbox_get("stdout");
152 if (outfd >= 0) {
[f77c1c9]153 int stdoutfd = -1;
154 (void) vfs_clone(outfd, -1, false, &stdoutfd);
[bb9ec2d]155 assert(stdoutfd <= 1);
[fcab7ef]156 while (stdoutfd < 1)
[f77c1c9]157 (void) vfs_clone(outfd, -1, false, &stdoutfd);
[b19e892]158 vfs_open(stdoutfd, MODE_APPEND);
[bb9ec2d]159 stdout = fdopen(stdoutfd, "a");
[a68f737]160 } else {
[6fa9a99d]161 stdout = &stdout_kio;
[a68f737]162 list_append(&stdout->link, &files);
163 }
[a35b458]164
[bb9ec2d]165 int errfd = inbox_get("stderr");
166 if (errfd >= 0) {
[f77c1c9]167 int stderrfd = -1;
168 (void) vfs_clone(errfd, -1, false, &stderrfd);
[bb9ec2d]169 assert(stderrfd <= 2);
[fcab7ef]170 while (stderrfd < 2)
[f77c1c9]171 (void) vfs_clone(errfd, -1, false, &stderrfd);
[b19e892]172 vfs_open(stderrfd, MODE_APPEND);
[bb9ec2d]173 stderr = fdopen(stderrfd, "a");
[a68f737]174 } else {
[6fa9a99d]175 stderr = &stderr_kio;
[a68f737]176 list_append(&stderr->link, &files);
177 }
178}
179
[db24058]180void __stdio_done(void)
[a68f737]181{
[b72efe8]182 while (!list_empty(&files)) {
183 FILE *file = list_get_instance(list_first(&files), FILE, link);
[a68f737]184 fclose(file);
185 }
186}
[2595dab]187
[7ab7075f]188static bool parse_mode(const char *fmode, int *mode, bool *create, bool *excl,
189 bool *truncate)
[b861b58]190{
[2595dab]191 /* Parse mode except first character. */
[b19e892]192 const char *mp = fmode;
[7ab7075f]193
194 if (*mp++ == '\0') {
[2595dab]195 errno = EINVAL;
196 return false;
197 }
[a35b458]198
[2595dab]199 if ((*mp == 'b') || (*mp == 't'))
200 mp++;
[a35b458]201
[2595dab]202 bool plus;
203 if (*mp == '+') {
204 mp++;
205 plus = true;
[7ab7075f]206 } else {
[2595dab]207 plus = false;
[7ab7075f]208 }
209
210 bool ex;
211 if (*mp == 'x') {
212 mp++;
213 ex = true;
214 } else {
215 ex = false;
216 }
[a35b458]217
[7ab7075f]218 if (*mp != '\0') {
[2595dab]219 errno = EINVAL;
220 return false;
[350514c]221 }
[b19e892]222
223 *create = false;
224 *truncate = false;
[7ab7075f]225 *excl = false;
[a35b458]226
[b19e892]227 /* Parse first character of fmode and determine mode for vfs_open(). */
228 switch (fmode[0]) {
[2595dab]229 case 'r':
[b19e892]230 *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
[7ab7075f]231 if (ex) {
232 errno = EINVAL;
233 return false;
234 }
[2595dab]235 break;
236 case 'w':
[b19e892]237 *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
238 *create = true;
[7ab7075f]239 *excl = ex;
[b19e892]240 if (!plus)
241 *truncate = true;
[2595dab]242 break;
243 case 'a':
244 /* TODO: a+ must read from beginning, append to the end. */
245 if (plus) {
246 errno = ENOTSUP;
247 return false;
248 }
[b19e892]249
[7ab7075f]250 if (ex) {
251 errno = EINVAL;
252 return false;
253 }
254
[b19e892]255 *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
256 *create = true;
[82582e4]257 break;
[2595dab]258 default:
259 errno = EINVAL;
260 return false;
261 }
[a35b458]262
[2595dab]263 return true;
[cc6f688]264}
[b861b58]265
[ef8bcc6]266/** Set stream buffer. */
[58daded]267int setvbuf(FILE *stream, void *buf, int mode, size_t size)
[ef8bcc6]268{
[58daded]269 if (mode != _IONBF && mode != _IOLBF && mode != _IOFBF)
270 return -1;
271
[ef8bcc6]272 stream->btype = mode;
273 stream->buf = buf;
274 stream->buf_size = size;
275 stream->buf_head = stream->buf;
[facebd56]276 stream->buf_tail = stream->buf;
277 stream->buf_state = _bs_empty;
[58daded]278
279 return 0;
[ef8bcc6]280}
281
[7699c21]282/** Set stream buffer.
283 *
284 * When @p buf is NULL, the stream is set as unbuffered, otherwise
285 * full buffering is enabled.
286 */
287void setbuf(FILE *stream, void *buf)
288{
289 if (buf == NULL) {
290 setvbuf(stream, NULL, _IONBF, BUFSIZ);
291 } else {
292 setvbuf(stream, buf, _IOFBF, BUFSIZ);
293 }
294}
295
[bfd247f]296static void _setvbuf(FILE *stream)
297{
298 /* FIXME: Use more complex rules for setting buffering options. */
[a35b458]299
[bfd247f]300 switch (stream->fd) {
301 case 1:
302 setvbuf(stream, NULL, _IOLBF, BUFSIZ);
303 break;
304 case 0:
305 case 2:
306 setvbuf(stream, NULL, _IONBF, 0);
307 break;
308 default:
309 setvbuf(stream, NULL, _IOFBF, BUFSIZ);
310 }
311}
312
[ef8bcc6]313/** Allocate stream buffer. */
314static int _fallocbuf(FILE *stream)
315{
316 assert(stream->buf == NULL);
[a35b458]317
[ef8bcc6]318 stream->buf = malloc(stream->buf_size);
319 if (stream->buf == NULL) {
320 errno = ENOMEM;
[6afc9d7]321 return EOF;
[ef8bcc6]322 }
[a35b458]323
[ef8bcc6]324 stream->buf_head = stream->buf;
[facebd56]325 stream->buf_tail = stream->buf;
[ef8bcc6]326 return 0;
327}
328
[2595dab]329/** Open a stream.
330 *
331 * @param path Path of the file to open.
[7ab7075f]332 * @param mode Mode string, (r|w|a)[b|t][+][x].
[2595dab]333 *
[4e2cf8b]334 */
[b19e892]335FILE *fopen(const char *path, const char *fmode)
[4e2cf8b]336{
[b19e892]337 int mode;
338 bool create;
[7ab7075f]339 bool excl;
[b19e892]340 bool truncate;
341
[7ab7075f]342 if (!parse_mode(fmode, &mode, &create, &excl, &truncate))
[2595dab]343 return NULL;
[a35b458]344
[2595dab]345 /* Open file. */
346 FILE *stream = malloc(sizeof(FILE));
347 if (stream == NULL) {
348 errno = ENOMEM;
349 return NULL;
350 }
[b19e892]351
352 int flags = WALK_REGULAR;
[7ab7075f]353 if (create && excl)
354 flags |= WALK_MUST_CREATE;
355 else if (create)
[b19e892]356 flags |= WALK_MAY_CREATE;
[f77c1c9]357 int file;
[b7fd2a0]358 errno_t rc = vfs_lookup(path, flags, &file);
[f77c1c9]359 if (rc != EOK) {
360 errno = rc;
[b19e892]361 free(stream);
362 return NULL;
363 }
364
[f77c1c9]365 rc = vfs_open(file, mode);
[b19e892]366 if (rc != EOK) {
367 errno = rc;
[9c4cf0d]368 vfs_put(file);
[2595dab]369 free(stream);
370 return NULL;
371 }
[a35b458]372
[b19e892]373 if (truncate) {
374 rc = vfs_resize(file, 0);
375 if (rc != EOK) {
376 errno = rc;
[9c4cf0d]377 vfs_put(file);
[b19e892]378 free(stream);
379 return NULL;
380 }
381 }
382
383 stream->fd = file;
[58898d1d]384 stream->pos = 0;
[2595dab]385 stream->error = false;
386 stream->eof = false;
[01cc7b4]387 stream->ops = &stdio_vfs_ops;
388 stream->arg = NULL;
[79ae36dd]389 stream->sess = NULL;
[facebd56]390 stream->need_sync = false;
[bfd247f]391 _setvbuf(stream);
[e86a617a]392 stream->ungetc_chars = 0;
[a35b458]393
[a68f737]394 list_append(&stream->link, &files);
[a35b458]395
[2595dab]396 return stream;
397}
398
[080ad7f]399FILE *fdopen(int fd, const char *mode)
400{
401 /* Open file. */
402 FILE *stream = malloc(sizeof(FILE));
403 if (stream == NULL) {
404 errno = ENOMEM;
405 return NULL;
406 }
[a35b458]407
[080ad7f]408 stream->fd = fd;
[58898d1d]409 stream->pos = 0;
[080ad7f]410 stream->error = false;
411 stream->eof = false;
[01cc7b4]412 stream->ops = &stdio_vfs_ops;
413 stream->arg = NULL;
[79ae36dd]414 stream->sess = NULL;
[facebd56]415 stream->need_sync = false;
[bfd247f]416 _setvbuf(stream);
[e86a617a]417 stream->ungetc_chars = 0;
[a35b458]418
[080ad7f]419 list_append(&stream->link, &files);
[a35b458]420
[080ad7f]421 return stream;
422}
423
[80bee81]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
[ef8bcc6]654/** Write to a stream.
655 *
656 * @param buf Source buffer.
657 * @param size Size of each record.
658 * @param nmemb Number of records to write.
659 * @param stream Pointer to the stream.
660 *
661 */
662size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
663{
664 uint8_t *data;
665 size_t bytes_left;
666 size_t now;
667 size_t buf_free;
668 size_t total_written;
669 size_t i;
670 uint8_t b;
671 bool need_flush;
[002252a]672
673 if (size == 0 || nmemb == 0)
674 return 0;
675
[ef8bcc6]676 /* If not buffered stream, write out directly. */
[bfd247f]677 if (stream->btype == _IONBF) {
678 now = _fwrite(buf, size, nmemb, stream);
679 fflush(stream);
680 return now;
681 }
[facebd56]682
683 /* Make sure buffer contains no prefetched data. */
684 if (stream->buf_state == _bs_read)
685 _fflushbuf(stream);
686
[ef8bcc6]687 /* Perform lazy allocation of stream buffer. */
688 if (stream->buf == NULL) {
689 if (_fallocbuf(stream) != 0)
690 return 0; /* Errno set by _fallocbuf(). */
691 }
[a35b458]692
[ef8bcc6]693 data = (uint8_t *) buf;
694 bytes_left = size * nmemb;
695 total_written = 0;
696 need_flush = false;
[a35b458]697
[bfd247f]698 while ((!stream->error) && (bytes_left > 0)) {
[ef8bcc6]699 buf_free = stream->buf_size - (stream->buf_head - stream->buf);
700 if (bytes_left > buf_free)
701 now = buf_free;
702 else
703 now = bytes_left;
[a35b458]704
[ef8bcc6]705 for (i = 0; i < now; i++) {
706 b = data[i];
707 stream->buf_head[i] = b;
[a35b458]708
[bfd247f]709 if ((b == '\n') && (stream->btype == _IOLBF))
[ef8bcc6]710 need_flush = true;
711 }
[a35b458]712
[0803134]713 data += now;
[ef8bcc6]714 stream->buf_head += now;
715 buf_free -= now;
716 bytes_left -= now;
717 total_written += now;
[0803134]718 stream->buf_state = _bs_write;
[a35b458]719
[ef8bcc6]720 if (buf_free == 0) {
721 /* Only need to drain buffer. */
722 _fflushbuf(stream);
[6afc9d7]723 if (!stream->error)
724 need_flush = false;
[ef8bcc6]725 }
726 }
[facebd56]727
[ef8bcc6]728 if (need_flush)
729 fflush(stream);
[a35b458]730
[ef8bcc6]731 return (total_written / size);
732}
733
[ed88c8e]734wint_t fputwc(wchar_t wc, FILE *stream)
[c9857c6]735{
[56fa418]736 char buf[STR_BOUNDS(1)];
[2595dab]737 size_t sz = 0;
[a35b458]738
[ed88c8e]739 if (chr_encode(wc, buf, &sz, STR_BOUNDS(1)) != EOK) {
740 errno = EILSEQ;
741 return WEOF;
742 }
[a35b458]743
[ed88c8e]744 size_t wr = fwrite(buf, 1, sz, stream);
745 if (wr < sz)
746 return WEOF;
[a35b458]747
[ed88c8e]748 return wc;
749}
750
751wint_t putwchar(wchar_t wc)
752{
753 return fputwc(wc, stdout);
754}
755
756int fputc(int c, FILE *stream)
757{
758 unsigned char b;
759 size_t wr;
760
761 b = (unsigned char) c;
762 wr = fwrite(&b, sizeof(b), 1, stream);
763 if (wr < 1)
764 return EOF;
[a35b458]765
[ed88c8e]766 return b;
[2595dab]767}
[56fa418]768
[ed88c8e]769int putchar(int c)
[2595dab]770{
771 return fputc(c, stdout);
772}
[56fa418]773
[2595dab]774int fputs(const char *str, FILE *stream)
775{
[7db5cfd]776 (void) fwrite(str, str_size(str), 1, stream);
777 if (ferror(stream))
778 return EOF;
779 return 0;
[2595dab]780}
[56fa418]781
[2595dab]782int puts(const char *str)
783{
[1abcf1d]784 if (fputs(str, stdout) < 0)
785 return EOF;
786 return putchar('\n');
[c9857c6]787}
[b27a97bb]788
[2595dab]789int fgetc(FILE *stream)
[b27a97bb]790{
[2595dab]791 char c;
[a35b458]792
[bac82eeb]793 /* This could be made faster by only flushing when needed. */
[b8e57e8c]794 if (stdout)
795 fflush(stdout);
796 if (stderr)
797 fflush(stderr);
[a35b458]798
[2595dab]799 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
800 return EOF;
[a35b458]801
[2595dab]802 return (int) c;
803}
804
[c62d2e1]805char *fgets(char *str, int size, FILE *stream)
806{
[a634485]807 int c;
[c62d2e1]808 int idx;
809
810 idx = 0;
811 while (idx < size - 1) {
812 c = fgetc(stream);
813 if (c == EOF)
814 break;
815
816 str[idx++] = c;
817
818 if (c == '\n')
819 break;
820 }
821
822 if (ferror(stream))
823 return NULL;
824
825 if (idx == 0)
826 return NULL;
827
828 str[idx] = '\0';
829 return str;
830}
831
[2595dab]832int getchar(void)
833{
834 return fgetc(stdin);
[b27a97bb]835}
836
[bd5414e]837int ungetc(int c, FILE *stream)
838{
839 if (c == EOF)
840 return EOF;
841
842 if (stream->ungetc_chars >= UNGETC_MAX)
843 return EOF;
844
845 stream->ungetc_buf[stream->ungetc_chars++] =
846 (uint8_t)c;
847
848 stream->eof = false;
849 return (uint8_t)c;
850}
851
[1c7f381]852int fseek64(FILE *stream, off64_t offset, int whence)
[d2cc7e1]853{
[b7fd2a0]854 errno_t rc;
[23a0368]855
[6afc9d7]856 if (stream->error)
[58898d1d]857 return -1;
[6afc9d7]858
[facebd56]859 _fflushbuf(stream);
[6afc9d7]860 if (stream->error) {
861 /* errno was set by _fflushbuf() */
[58898d1d]862 return -1;
[6afc9d7]863 }
864
[bd5414e]865 stream->ungetc_chars = 0;
[facebd56]866
[39330200]867 vfs_stat_t st;
[58898d1d]868 switch (whence) {
869 case SEEK_SET:
870 stream->pos = offset;
871 break;
872 case SEEK_CUR:
873 stream->pos += offset;
874 break;
875 case SEEK_END:
[23a0368]876 rc = vfs_stat(stream->fd, &st);
877 if (rc != EOK) {
878 errno = rc;
[58898d1d]879 stream->error = true;
[1b20da0]880 return -1;
[58898d1d]881 }
882 stream->pos = st.size + offset;
883 break;
[2595dab]884 }
[facebd56]885
[2595dab]886 stream->eof = false;
[566f4cfb]887 return 0;
[d2cc7e1]888}
889
[1c7f381]890off64_t ftell64(FILE *stream)
[08232ee]891{
[6afc9d7]892 if (stream->error)
893 return EOF;
[a35b458]894
[8ad496d2]895 _fflushbuf(stream);
[6afc9d7]896 if (stream->error) {
897 /* errno was set by _fflushbuf() */
898 return EOF;
899 }
900
[58898d1d]901 return stream->pos - stream->ungetc_chars;
[08232ee]902}
903
[1c7f381]904int fseek(FILE *stream, long offset, int whence)
905{
906 return fseek64(stream, offset, whence);
907}
908
909long ftell(FILE *stream)
910{
911 off64_t off = ftell64(stream);
912
913 /* The native position is too large for the C99-ish interface. */
[ef84413]914 if (off > LONG_MAX) {
915 errno = EOVERFLOW;
916 return -1;
917 }
[1c7f381]918
919 return off;
920}
921
[080ad7f]922void rewind(FILE *stream)
923{
924 (void) fseek(stream, 0, SEEK_SET);
925}
926
[2595dab]927int fflush(FILE *stream)
928{
[6afc9d7]929 if (stream->error)
930 return EOF;
[a35b458]931
[ef8bcc6]932 _fflushbuf(stream);
[6afc9d7]933 if (stream->error) {
934 /* errno was set by _fflushbuf() */
935 return EOF;
936 }
[a35b458]937
[01cc7b4]938 if (stream->need_sync) {
[facebd56]939 /**
940 * Better than syncing always, but probably still not the
941 * right thing to do.
942 */
[01cc7b4]943 if (stream->ops->flush(stream) == EOF)
[6afc9d7]944 return EOF;
945
[01cc7b4]946 stream->need_sync = false;
[facebd56]947 }
[a35b458]948
[6afc9d7]949 return 0;
[2595dab]950}
951
952int feof(FILE *stream)
953{
954 return stream->eof;
955}
956
957int ferror(FILE *stream)
958{
959 return stream->error;
960}
961
[c77a64f]962void clearerr(FILE *stream)
963{
964 stream->eof = false;
965 stream->error = false;
966}
967
[3629481]968int fileno(FILE *stream)
969{
[01cc7b4]970 if (stream->ops != &stdio_vfs_ops) {
[3629481]971 errno = EBADF;
[6afc9d7]972 return EOF;
[3629481]973 }
[a35b458]974
[3629481]975 return stream->fd;
976}
977
[6afc9d7]978async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
[2595dab]979{
980 if (stream->fd >= 0) {
[79ae36dd]981 if (stream->sess == NULL)
[6afc9d7]982 stream->sess = vfs_fd_session(stream->fd, iface);
[a35b458]983
[79ae36dd]984 return stream->sess;
[2595dab]985 }
[a35b458]986
[79ae36dd]987 return NULL;
[2595dab]988}
989
[b7fd2a0]990errno_t vfs_fhandle(FILE *stream, int *handle)
[2595dab]991{
[7171760]992 if (stream->fd >= 0) {
993 *handle = stream->fd;
994 return EOK;
995 }
[a35b458]996
[a68f737]997 return ENOENT;
[2595dab]998}
999
[01cc7b4]1000/** Read from KIO stream. */
1001static size_t stdio_kio_read(void *buf, size_t size, size_t nmemb, FILE *stream)
1002{
1003 stream->eof = true;
1004 return 0;
1005}
1006
1007/** Write to KIO stream. */
1008static size_t stdio_kio_write(const void *buf, size_t size, size_t nmemb,
1009 FILE *stream)
1010{
1011 errno_t rc;
1012 size_t nwritten;
1013
1014 rc = kio_write(buf, size * nmemb, &nwritten);
1015 if (rc != EOK) {
1016 stream->error = true;
1017 nwritten = 0;
1018 }
1019
1020 return nwritten / size;
1021}
1022
1023/** Flush KIO stream. */
1024static int stdio_kio_flush(FILE *stream)
1025{
1026 kio_update();
1027 return 0;
1028}
1029
1030/** Read from VFS stream. */
1031static size_t stdio_vfs_read(void *buf, size_t size, size_t nmemb, FILE *stream)
1032{
1033 errno_t rc;
1034 size_t nread;
1035
1036 if (size == 0 || nmemb == 0)
1037 return 0;
1038
1039 rc = vfs_read(stream->fd, &stream->pos, buf, size * nmemb, &nread);
1040 if (rc != EOK) {
1041 errno = rc;
1042 stream->error = true;
1043 } else if (nread == 0) {
1044 stream->eof = true;
1045 }
1046
1047 return (nread / size);
1048}
1049
1050/** Write to VFS stream. */
1051static size_t stdio_vfs_write(const void *buf, size_t size, size_t nmemb,
1052 FILE *stream)
1053{
1054 errno_t rc;
1055 size_t nwritten;
1056
1057 rc = vfs_write(stream->fd, &stream->pos, buf, size * nmemb, &nwritten);
1058 if (rc != EOK) {
1059 errno = rc;
1060 stream->error = true;
1061 }
1062
1063 return nwritten / size;
1064}
1065
1066/** Flush VFS stream. */
1067static int stdio_vfs_flush(FILE *stream)
1068{
1069 errno_t rc;
1070
1071 rc = vfs_sync(stream->fd);
1072 if (rc != EOK) {
1073 errno = rc;
1074 return EOF;
1075 }
1076
1077 return 0;
1078}
1079
[fadd381]1080/** @}
[b2951e2]1081 */
Note: See TracBrowser for help on using the repository browser.