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