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