1 | /*
|
---|
2 | * Copyright (c) 2005 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <assert.h>
|
---|
37 | #include <str.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <stdbool.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <async.h>
|
---|
42 | #include <io/kio.h>
|
---|
43 | #include <vfs/vfs.h>
|
---|
44 | #include <vfs/vfs_sess.h>
|
---|
45 | #include <vfs/inbox.h>
|
---|
46 | #include <ipc/loc.h>
|
---|
47 | #include <adt/list.h>
|
---|
48 | #include "../private/io.h"
|
---|
49 | #include "../private/stdio.h"
|
---|
50 |
|
---|
51 | static void _ffillbuf(FILE *stream);
|
---|
52 | static void _fflushbuf(FILE *stream);
|
---|
53 |
|
---|
54 | static FILE stdin_null = {
|
---|
55 | .fd = -1,
|
---|
56 | .pos = 0,
|
---|
57 | .error = true,
|
---|
58 | .eof = true,
|
---|
59 | .kio = false,
|
---|
60 | .sess = NULL,
|
---|
61 | .btype = _IONBF,
|
---|
62 | .buf = NULL,
|
---|
63 | .buf_size = 0,
|
---|
64 | .buf_head = NULL,
|
---|
65 | .buf_tail = NULL,
|
---|
66 | .buf_state = _bs_empty
|
---|
67 | };
|
---|
68 |
|
---|
69 | static FILE stdout_kio = {
|
---|
70 | .fd = -1,
|
---|
71 | .pos = 0,
|
---|
72 | .error = false,
|
---|
73 | .eof = false,
|
---|
74 | .kio = true,
|
---|
75 | .sess = NULL,
|
---|
76 | .btype = _IOLBF,
|
---|
77 | .buf = NULL,
|
---|
78 | .buf_size = BUFSIZ,
|
---|
79 | .buf_head = NULL,
|
---|
80 | .buf_tail = NULL,
|
---|
81 | .buf_state = _bs_empty
|
---|
82 | };
|
---|
83 |
|
---|
84 | static FILE stderr_kio = {
|
---|
85 | .fd = -1,
|
---|
86 | .pos = 0,
|
---|
87 | .error = false,
|
---|
88 | .eof = false,
|
---|
89 | .kio = true,
|
---|
90 | .sess = NULL,
|
---|
91 | .btype = _IONBF,
|
---|
92 | .buf = NULL,
|
---|
93 | .buf_size = 0,
|
---|
94 | .buf_head = NULL,
|
---|
95 | .buf_tail = NULL,
|
---|
96 | .buf_state = _bs_empty
|
---|
97 | };
|
---|
98 |
|
---|
99 | FILE *stdin = NULL;
|
---|
100 | FILE *stdout = NULL;
|
---|
101 | FILE *stderr = NULL;
|
---|
102 |
|
---|
103 | static LIST_INITIALIZE(files);
|
---|
104 |
|
---|
105 | void __stdio_init(void)
|
---|
106 | {
|
---|
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) {
|
---|
113 | int stdinfd = vfs_clone(infd, -1, false);
|
---|
114 | assert(stdinfd == 0);
|
---|
115 | vfs_open(stdinfd, MODE_READ);
|
---|
116 | stdin = fdopen(stdinfd, "r");
|
---|
117 | } else {
|
---|
118 | stdin = &stdin_null;
|
---|
119 | list_append(&stdin->link, &files);
|
---|
120 | }
|
---|
121 |
|
---|
122 | int outfd = inbox_get("stdout");
|
---|
123 | if (outfd >= 0) {
|
---|
124 | int stdoutfd = vfs_clone(outfd, -1, false);
|
---|
125 | assert(stdoutfd <= 1);
|
---|
126 | while (stdoutfd < 1)
|
---|
127 | stdoutfd = vfs_clone(outfd, -1, false);
|
---|
128 | vfs_open(stdoutfd, MODE_APPEND);
|
---|
129 | stdout = fdopen(stdoutfd, "a");
|
---|
130 | } else {
|
---|
131 | stdout = &stdout_kio;
|
---|
132 | list_append(&stdout->link, &files);
|
---|
133 | }
|
---|
134 |
|
---|
135 | int errfd = inbox_get("stderr");
|
---|
136 | if (errfd >= 0) {
|
---|
137 | int stderrfd = vfs_clone(errfd, -1, false);
|
---|
138 | assert(stderrfd <= 2);
|
---|
139 | while (stderrfd < 2)
|
---|
140 | stderrfd = vfs_clone(errfd, -1, false);
|
---|
141 | vfs_open(stderrfd, MODE_APPEND);
|
---|
142 | stderr = fdopen(stderrfd, "a");
|
---|
143 | } else {
|
---|
144 | stderr = &stderr_kio;
|
---|
145 | list_append(&stderr->link, &files);
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | void __stdio_done(void)
|
---|
150 | {
|
---|
151 | while (!list_empty(&files)) {
|
---|
152 | FILE *file = list_get_instance(list_first(&files), FILE, link);
|
---|
153 | fclose(file);
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | static bool parse_mode(const char *fmode, int *mode, bool *create, bool *truncate)
|
---|
158 | {
|
---|
159 | /* Parse mode except first character. */
|
---|
160 | const char *mp = fmode;
|
---|
161 | if (*mp++ == 0) {
|
---|
162 | errno = EINVAL;
|
---|
163 | return false;
|
---|
164 | }
|
---|
165 |
|
---|
166 | if ((*mp == 'b') || (*mp == 't'))
|
---|
167 | mp++;
|
---|
168 |
|
---|
169 | bool plus;
|
---|
170 | if (*mp == '+') {
|
---|
171 | mp++;
|
---|
172 | plus = true;
|
---|
173 | } else
|
---|
174 | plus = false;
|
---|
175 |
|
---|
176 | if (*mp != 0) {
|
---|
177 | errno = EINVAL;
|
---|
178 | return false;
|
---|
179 | }
|
---|
180 |
|
---|
181 | *create = false;
|
---|
182 | *truncate = false;
|
---|
183 |
|
---|
184 | /* Parse first character of fmode and determine mode for vfs_open(). */
|
---|
185 | switch (fmode[0]) {
|
---|
186 | case 'r':
|
---|
187 | *mode = plus ? MODE_READ | MODE_WRITE : MODE_READ;
|
---|
188 | break;
|
---|
189 | case 'w':
|
---|
190 | *mode = plus ? MODE_READ | MODE_WRITE : MODE_WRITE;
|
---|
191 | *create = true;
|
---|
192 | if (!plus)
|
---|
193 | *truncate = true;
|
---|
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 | }
|
---|
201 |
|
---|
202 | *mode = MODE_APPEND | (plus ? MODE_READ | MODE_WRITE : MODE_WRITE);
|
---|
203 | *create = true;
|
---|
204 | break;
|
---|
205 | default:
|
---|
206 | errno = EINVAL;
|
---|
207 | return false;
|
---|
208 | }
|
---|
209 |
|
---|
210 | return true;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /** Set stream buffer. */
|
---|
214 | void 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;
|
---|
220 | stream->buf_tail = stream->buf;
|
---|
221 | stream->buf_state = _bs_empty;
|
---|
222 | }
|
---|
223 |
|
---|
224 | /** Set stream buffer.
|
---|
225 | *
|
---|
226 | * When @p buf is NULL, the stream is set as unbuffered, otherwise
|
---|
227 | * full buffering is enabled.
|
---|
228 | */
|
---|
229 | void 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 |
|
---|
238 | static 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 |
|
---|
255 | /** Allocate stream buffer. */
|
---|
256 | static int _fallocbuf(FILE *stream)
|
---|
257 | {
|
---|
258 | assert(stream->buf == NULL);
|
---|
259 |
|
---|
260 | stream->buf = malloc(stream->buf_size);
|
---|
261 | if (stream->buf == NULL) {
|
---|
262 | errno = ENOMEM;
|
---|
263 | return EOF;
|
---|
264 | }
|
---|
265 |
|
---|
266 | stream->buf_head = stream->buf;
|
---|
267 | stream->buf_tail = stream->buf;
|
---|
268 | return 0;
|
---|
269 | }
|
---|
270 |
|
---|
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 | *
|
---|
276 | */
|
---|
277 | FILE *fopen(const char *path, const char *fmode)
|
---|
278 | {
|
---|
279 | int mode;
|
---|
280 | bool create;
|
---|
281 | bool truncate;
|
---|
282 |
|
---|
283 | if (!parse_mode(fmode, &mode, &create, &truncate))
|
---|
284 | return NULL;
|
---|
285 |
|
---|
286 | /* Open file. */
|
---|
287 | FILE *stream = malloc(sizeof(FILE));
|
---|
288 | if (stream == NULL) {
|
---|
289 | errno = ENOMEM;
|
---|
290 | return NULL;
|
---|
291 | }
|
---|
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;
|
---|
306 | vfs_put(file);
|
---|
307 | free(stream);
|
---|
308 | return NULL;
|
---|
309 | }
|
---|
310 |
|
---|
311 | if (truncate) {
|
---|
312 | rc = vfs_resize(file, 0);
|
---|
313 | if (rc != EOK) {
|
---|
314 | errno = rc;
|
---|
315 | vfs_put(file);
|
---|
316 | free(stream);
|
---|
317 | return NULL;
|
---|
318 | }
|
---|
319 | }
|
---|
320 |
|
---|
321 | stream->fd = file;
|
---|
322 | stream->pos = 0;
|
---|
323 | stream->error = false;
|
---|
324 | stream->eof = false;
|
---|
325 | stream->kio = false;
|
---|
326 | stream->sess = NULL;
|
---|
327 | stream->need_sync = false;
|
---|
328 | _setvbuf(stream);
|
---|
329 | stream->ungetc_chars = 0;
|
---|
330 |
|
---|
331 | list_append(&stream->link, &files);
|
---|
332 |
|
---|
333 | return stream;
|
---|
334 | }
|
---|
335 |
|
---|
336 | FILE *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;
|
---|
346 | stream->pos = 0;
|
---|
347 | stream->error = false;
|
---|
348 | stream->eof = false;
|
---|
349 | stream->kio = false;
|
---|
350 | stream->sess = NULL;
|
---|
351 | stream->need_sync = false;
|
---|
352 | _setvbuf(stream);
|
---|
353 | stream->ungetc_chars = 0;
|
---|
354 |
|
---|
355 | list_append(&stream->link, &files);
|
---|
356 |
|
---|
357 | return stream;
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | static int _fclose_nofree(FILE *stream)
|
---|
362 | {
|
---|
363 | int rc = 0;
|
---|
364 |
|
---|
365 | fflush(stream);
|
---|
366 |
|
---|
367 | if (stream->sess != NULL)
|
---|
368 | async_hangup(stream->sess);
|
---|
369 |
|
---|
370 | if (stream->fd >= 0)
|
---|
371 | rc = vfs_put(stream->fd);
|
---|
372 |
|
---|
373 | list_remove(&stream->link);
|
---|
374 |
|
---|
375 | if (rc != 0) {
|
---|
376 | /* errno was set by close() */
|
---|
377 | return EOF;
|
---|
378 | }
|
---|
379 |
|
---|
380 | return 0;
|
---|
381 | }
|
---|
382 |
|
---|
383 | int fclose(FILE *stream)
|
---|
384 | {
|
---|
385 | int rc = _fclose_nofree(stream);
|
---|
386 |
|
---|
387 | if ((stream != &stdin_null)
|
---|
388 | && (stream != &stdout_kio)
|
---|
389 | && (stream != &stderr_kio))
|
---|
390 | free(stream);
|
---|
391 |
|
---|
392 | return rc;
|
---|
393 | }
|
---|
394 |
|
---|
395 | FILE *freopen(const char *path, const char *mode, FILE *stream)
|
---|
396 | {
|
---|
397 | FILE *nstr;
|
---|
398 |
|
---|
399 | if (path == NULL) {
|
---|
400 | /* Changing mode is not supported */
|
---|
401 | return NULL;
|
---|
402 | }
|
---|
403 |
|
---|
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;
|
---|
418 | }
|
---|
419 |
|
---|
420 | /** Read from a stream (unbuffered).
|
---|
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.
|
---|
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.
|
---|
429 | */
|
---|
430 | static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
431 | {
|
---|
432 | if (size == 0 || nmemb == 0)
|
---|
433 | return 0;
|
---|
434 |
|
---|
435 | ssize_t rd = vfs_read(stream->fd, &stream->pos, buf, size * nmemb);
|
---|
436 | if (rd < 0) {
|
---|
437 | errno = rd;
|
---|
438 | stream->error = true;
|
---|
439 | rd = 0;
|
---|
440 | } else if (rd == 0) {
|
---|
441 | stream->eof = true;
|
---|
442 | }
|
---|
443 |
|
---|
444 | return (rd / size);
|
---|
445 | }
|
---|
446 |
|
---|
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.
|
---|
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.
|
---|
456 | */
|
---|
457 | static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
458 | {
|
---|
459 | if (size == 0 || nmemb == 0)
|
---|
460 | return 0;
|
---|
461 |
|
---|
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;
|
---|
469 | } else {
|
---|
470 | wr = nwritten;
|
---|
471 | }
|
---|
472 | } else {
|
---|
473 | wr = vfs_write(stream->fd, &stream->pos, buf, size * nmemb);
|
---|
474 | if (wr < 0) {
|
---|
475 | errno = wr;
|
---|
476 | stream->error = true;
|
---|
477 | wr = 0;
|
---|
478 | }
|
---|
479 | }
|
---|
480 |
|
---|
481 | if (wr > 0)
|
---|
482 | stream->need_sync = true;
|
---|
483 |
|
---|
484 | return (wr / size);
|
---|
485 | }
|
---|
486 |
|
---|
487 | /** Read some data in stream buffer.
|
---|
488 | *
|
---|
489 | * On error, stream error indicator is set and errno is set.
|
---|
490 | */
|
---|
491 | static void _ffillbuf(FILE *stream)
|
---|
492 | {
|
---|
493 | ssize_t rc;
|
---|
494 |
|
---|
495 | stream->buf_head = stream->buf_tail = stream->buf;
|
---|
496 |
|
---|
497 | rc = vfs_read(stream->fd, &stream->pos, stream->buf, stream->buf_size);
|
---|
498 | if (rc < 0) {
|
---|
499 | errno = rc;
|
---|
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. */
|
---|
514 | static void _fflushbuf(FILE *stream)
|
---|
515 | {
|
---|
516 | size_t bytes_used;
|
---|
517 |
|
---|
518 | if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
|
---|
519 | return;
|
---|
520 |
|
---|
521 | bytes_used = stream->buf_head - stream->buf_tail;
|
---|
522 |
|
---|
523 | /* If buffer has prefetched read data, we need to seek back. */
|
---|
524 | if (bytes_used > 0 && stream->buf_state == _bs_read)
|
---|
525 | stream->pos -= bytes_used;
|
---|
526 |
|
---|
527 | /* If buffer has unwritten data, we need to write them out. */
|
---|
528 | if (bytes_used > 0 && stream->buf_state == _bs_write) {
|
---|
529 | (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
|
---|
530 | /* On error stream error indicator and errno are set by _fwrite */
|
---|
531 | if (stream->error)
|
---|
532 | return;
|
---|
533 | }
|
---|
534 |
|
---|
535 | stream->buf_head = stream->buf;
|
---|
536 | stream->buf_tail = stream->buf;
|
---|
537 | stream->buf_state = _bs_empty;
|
---|
538 | }
|
---|
539 |
|
---|
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 | */
|
---|
548 | size_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 |
|
---|
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;
|
---|
568 | --bytes_left;
|
---|
569 | }
|
---|
570 |
|
---|
571 | /* If not buffered stream, read in directly. */
|
---|
572 | if (stream->btype == _IONBF) {
|
---|
573 | total_read += _fread(dest, 1, bytes_left, stream);
|
---|
574 | return total_read / size;
|
---|
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 |
|
---|
591 | if (stream->error || stream->eof) {
|
---|
592 | /* On error errno was set by _ffillbuf() */
|
---|
593 | break;
|
---|
594 | }
|
---|
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 |
|
---|
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 | */
|
---|
625 | size_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;
|
---|
635 |
|
---|
636 | if (size == 0 || nmemb == 0)
|
---|
637 | return 0;
|
---|
638 |
|
---|
639 | /* If not buffered stream, write out directly. */
|
---|
640 | if (stream->btype == _IONBF) {
|
---|
641 | now = _fwrite(buf, size, nmemb, stream);
|
---|
642 | fflush(stream);
|
---|
643 | return now;
|
---|
644 | }
|
---|
645 |
|
---|
646 | /* Make sure buffer contains no prefetched data. */
|
---|
647 | if (stream->buf_state == _bs_read)
|
---|
648 | _fflushbuf(stream);
|
---|
649 |
|
---|
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 | }
|
---|
655 |
|
---|
656 | data = (uint8_t *) buf;
|
---|
657 | bytes_left = size * nmemb;
|
---|
658 | total_written = 0;
|
---|
659 | need_flush = false;
|
---|
660 |
|
---|
661 | while ((!stream->error) && (bytes_left > 0)) {
|
---|
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;
|
---|
667 |
|
---|
668 | for (i = 0; i < now; i++) {
|
---|
669 | b = data[i];
|
---|
670 | stream->buf_head[i] = b;
|
---|
671 |
|
---|
672 | if ((b == '\n') && (stream->btype == _IOLBF))
|
---|
673 | need_flush = true;
|
---|
674 | }
|
---|
675 |
|
---|
676 | data += now;
|
---|
677 | stream->buf_head += now;
|
---|
678 | buf_free -= now;
|
---|
679 | bytes_left -= now;
|
---|
680 | total_written += now;
|
---|
681 | stream->buf_state = _bs_write;
|
---|
682 |
|
---|
683 | if (buf_free == 0) {
|
---|
684 | /* Only need to drain buffer. */
|
---|
685 | _fflushbuf(stream);
|
---|
686 | if (!stream->error)
|
---|
687 | need_flush = false;
|
---|
688 | }
|
---|
689 | }
|
---|
690 |
|
---|
691 | if (need_flush)
|
---|
692 | fflush(stream);
|
---|
693 |
|
---|
694 | return (total_written / size);
|
---|
695 | }
|
---|
696 |
|
---|
697 | int fputc(wchar_t c, FILE *stream)
|
---|
698 | {
|
---|
699 | char buf[STR_BOUNDS(1)];
|
---|
700 | size_t sz = 0;
|
---|
701 |
|
---|
702 | if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
|
---|
703 | size_t wr = fwrite(buf, 1, sz, stream);
|
---|
704 |
|
---|
705 | if (wr < sz)
|
---|
706 | return EOF;
|
---|
707 |
|
---|
708 | return (int) c;
|
---|
709 | }
|
---|
710 |
|
---|
711 | return EOF;
|
---|
712 | }
|
---|
713 |
|
---|
714 | int putchar(wchar_t c)
|
---|
715 | {
|
---|
716 | return fputc(c, stdout);
|
---|
717 | }
|
---|
718 |
|
---|
719 | int fputs(const char *str, FILE *stream)
|
---|
720 | {
|
---|
721 | (void) fwrite(str, str_size(str), 1, stream);
|
---|
722 | if (ferror(stream))
|
---|
723 | return EOF;
|
---|
724 | return 0;
|
---|
725 | }
|
---|
726 |
|
---|
727 | int puts(const char *str)
|
---|
728 | {
|
---|
729 | return fputs(str, stdout);
|
---|
730 | }
|
---|
731 |
|
---|
732 | int fgetc(FILE *stream)
|
---|
733 | {
|
---|
734 | char c;
|
---|
735 |
|
---|
736 | /* This could be made faster by only flushing when needed. */
|
---|
737 | if (stdout)
|
---|
738 | fflush(stdout);
|
---|
739 | if (stderr)
|
---|
740 | fflush(stderr);
|
---|
741 |
|
---|
742 | if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
|
---|
743 | return EOF;
|
---|
744 |
|
---|
745 | return (int) c;
|
---|
746 | }
|
---|
747 |
|
---|
748 | char *fgets(char *str, int size, FILE *stream)
|
---|
749 | {
|
---|
750 | int c;
|
---|
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 |
|
---|
775 | int getchar(void)
|
---|
776 | {
|
---|
777 | return fgetc(stdin);
|
---|
778 | }
|
---|
779 |
|
---|
780 | int 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 |
|
---|
795 | int fseek(FILE *stream, long offset, int whence)
|
---|
796 | {
|
---|
797 | int rc;
|
---|
798 |
|
---|
799 | if (stream->error)
|
---|
800 | return -1;
|
---|
801 |
|
---|
802 | _fflushbuf(stream);
|
---|
803 | if (stream->error) {
|
---|
804 | /* errno was set by _fflushbuf() */
|
---|
805 | return -1;
|
---|
806 | }
|
---|
807 |
|
---|
808 | stream->ungetc_chars = 0;
|
---|
809 |
|
---|
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:
|
---|
819 | rc = vfs_stat(stream->fd, &st);
|
---|
820 | if (rc != EOK) {
|
---|
821 | errno = rc;
|
---|
822 | stream->error = true;
|
---|
823 | return -1;
|
---|
824 | }
|
---|
825 | stream->pos = st.size + offset;
|
---|
826 | break;
|
---|
827 | }
|
---|
828 |
|
---|
829 | stream->eof = false;
|
---|
830 | return 0;
|
---|
831 | }
|
---|
832 |
|
---|
833 | long ftell(FILE *stream)
|
---|
834 | {
|
---|
835 | /* The native position is too large for the C99-ish interface. */
|
---|
836 | if (stream->pos - stream->ungetc_chars > LONG_MAX)
|
---|
837 | return EOF;
|
---|
838 |
|
---|
839 | if (stream->error)
|
---|
840 | return EOF;
|
---|
841 |
|
---|
842 | _fflushbuf(stream);
|
---|
843 | if (stream->error) {
|
---|
844 | /* errno was set by _fflushbuf() */
|
---|
845 | return EOF;
|
---|
846 | }
|
---|
847 |
|
---|
848 | return stream->pos - stream->ungetc_chars;
|
---|
849 | }
|
---|
850 |
|
---|
851 | void rewind(FILE *stream)
|
---|
852 | {
|
---|
853 | (void) fseek(stream, 0, SEEK_SET);
|
---|
854 | }
|
---|
855 |
|
---|
856 | int fflush(FILE *stream)
|
---|
857 | {
|
---|
858 | if (stream->error)
|
---|
859 | return EOF;
|
---|
860 |
|
---|
861 | _fflushbuf(stream);
|
---|
862 | if (stream->error) {
|
---|
863 | /* errno was set by _fflushbuf() */
|
---|
864 | return EOF;
|
---|
865 | }
|
---|
866 |
|
---|
867 | if (stream->kio) {
|
---|
868 | kio_update();
|
---|
869 | return 0;
|
---|
870 | }
|
---|
871 |
|
---|
872 | if ((stream->fd >= 0) && (stream->need_sync)) {
|
---|
873 | int rc;
|
---|
874 |
|
---|
875 | /**
|
---|
876 | * Better than syncing always, but probably still not the
|
---|
877 | * right thing to do.
|
---|
878 | */
|
---|
879 | stream->need_sync = false;
|
---|
880 | rc = vfs_sync(stream->fd);
|
---|
881 | if (rc != EOK) {
|
---|
882 | errno = rc;
|
---|
883 | return EOF;
|
---|
884 | }
|
---|
885 |
|
---|
886 | return 0;
|
---|
887 | }
|
---|
888 |
|
---|
889 | return 0;
|
---|
890 | }
|
---|
891 |
|
---|
892 | int feof(FILE *stream)
|
---|
893 | {
|
---|
894 | return stream->eof;
|
---|
895 | }
|
---|
896 |
|
---|
897 | int ferror(FILE *stream)
|
---|
898 | {
|
---|
899 | return stream->error;
|
---|
900 | }
|
---|
901 |
|
---|
902 | void clearerr(FILE *stream)
|
---|
903 | {
|
---|
904 | stream->eof = false;
|
---|
905 | stream->error = false;
|
---|
906 | }
|
---|
907 |
|
---|
908 | int fileno(FILE *stream)
|
---|
909 | {
|
---|
910 | if (stream->kio) {
|
---|
911 | errno = EBADF;
|
---|
912 | return EOF;
|
---|
913 | }
|
---|
914 |
|
---|
915 | return stream->fd;
|
---|
916 | }
|
---|
917 |
|
---|
918 | async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
|
---|
919 | {
|
---|
920 | if (stream->fd >= 0) {
|
---|
921 | if (stream->sess == NULL)
|
---|
922 | stream->sess = vfs_fd_session(stream->fd, iface);
|
---|
923 |
|
---|
924 | return stream->sess;
|
---|
925 | }
|
---|
926 |
|
---|
927 | return NULL;
|
---|
928 | }
|
---|
929 |
|
---|
930 | int vfs_fhandle(FILE *stream, int *handle)
|
---|
931 | {
|
---|
932 | if (stream->fd >= 0) {
|
---|
933 | *handle = stream->fd;
|
---|
934 | return EOK;
|
---|
935 | }
|
---|
936 |
|
---|
937 | return ENOENT;
|
---|
938 | }
|
---|
939 |
|
---|
940 | /** @}
|
---|
941 | */
|
---|