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 <fcntl.h>
|
---|
38 | #include <assert.h>
|
---|
39 | #include <str.h>
|
---|
40 | #include <errno.h>
|
---|
41 | #include <bool.h>
|
---|
42 | #include <malloc.h>
|
---|
43 | #include <async.h>
|
---|
44 | #include <io/klog.h>
|
---|
45 | #include <vfs/vfs.h>
|
---|
46 | #include <ipc/devmap.h>
|
---|
47 | #include <adt/list.h>
|
---|
48 |
|
---|
49 | static void _ffillbuf(FILE *stream);
|
---|
50 | static void _fflushbuf(FILE *stream);
|
---|
51 |
|
---|
52 | static FILE stdin_null = {
|
---|
53 | .fd = -1,
|
---|
54 | .error = true,
|
---|
55 | .eof = true,
|
---|
56 | .klog = false,
|
---|
57 | .phone = -1,
|
---|
58 | .btype = _IONBF,
|
---|
59 | .buf = NULL,
|
---|
60 | .buf_size = 0,
|
---|
61 | .buf_head = NULL,
|
---|
62 | .buf_tail = NULL,
|
---|
63 | .buf_state = _bs_empty
|
---|
64 | };
|
---|
65 |
|
---|
66 | static FILE stdout_klog = {
|
---|
67 | .fd = -1,
|
---|
68 | .error = false,
|
---|
69 | .eof = false,
|
---|
70 | .klog = true,
|
---|
71 | .phone = -1,
|
---|
72 | .btype = _IOLBF,
|
---|
73 | .buf = NULL,
|
---|
74 | .buf_size = BUFSIZ,
|
---|
75 | .buf_head = NULL,
|
---|
76 | .buf_tail = NULL,
|
---|
77 | .buf_state = _bs_empty
|
---|
78 | };
|
---|
79 |
|
---|
80 | static FILE stderr_klog = {
|
---|
81 | .fd = -1,
|
---|
82 | .error = false,
|
---|
83 | .eof = false,
|
---|
84 | .klog = true,
|
---|
85 | .phone = -1,
|
---|
86 | .btype = _IONBF,
|
---|
87 | .buf = NULL,
|
---|
88 | .buf_size = 0,
|
---|
89 | .buf_head = NULL,
|
---|
90 | .buf_tail = NULL,
|
---|
91 | .buf_state = _bs_empty
|
---|
92 | };
|
---|
93 |
|
---|
94 | FILE *stdin = NULL;
|
---|
95 | FILE *stdout = NULL;
|
---|
96 | FILE *stderr = NULL;
|
---|
97 |
|
---|
98 | static LIST_INITIALIZE(files);
|
---|
99 |
|
---|
100 | void __stdio_init(int filc, fdi_node_t *filv[])
|
---|
101 | {
|
---|
102 | if (filc > 0) {
|
---|
103 | stdin = fopen_node(filv[0], "r");
|
---|
104 | } else {
|
---|
105 | stdin = &stdin_null;
|
---|
106 | list_append(&stdin->link, &files);
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (filc > 1) {
|
---|
110 | stdout = fopen_node(filv[1], "w");
|
---|
111 | } else {
|
---|
112 | stdout = &stdout_klog;
|
---|
113 | list_append(&stdout->link, &files);
|
---|
114 | }
|
---|
115 |
|
---|
116 | if (filc > 2) {
|
---|
117 | stderr = fopen_node(filv[2], "w");
|
---|
118 | } else {
|
---|
119 | stderr = &stderr_klog;
|
---|
120 | list_append(&stderr->link, &files);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | void __stdio_done(void)
|
---|
125 | {
|
---|
126 | link_t *link = files.next;
|
---|
127 |
|
---|
128 | while (link != &files) {
|
---|
129 | FILE *file = list_get_instance(link, FILE, link);
|
---|
130 | fclose(file);
|
---|
131 | link = files.next;
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | static bool parse_mode(const char *mode, int *flags)
|
---|
136 | {
|
---|
137 | /* Parse mode except first character. */
|
---|
138 | const char *mp = mode;
|
---|
139 | if (*mp++ == 0) {
|
---|
140 | errno = EINVAL;
|
---|
141 | return false;
|
---|
142 | }
|
---|
143 |
|
---|
144 | if ((*mp == 'b') || (*mp == 't'))
|
---|
145 | mp++;
|
---|
146 |
|
---|
147 | bool plus;
|
---|
148 | if (*mp == '+') {
|
---|
149 | mp++;
|
---|
150 | plus = true;
|
---|
151 | } else
|
---|
152 | plus = false;
|
---|
153 |
|
---|
154 | if (*mp != 0) {
|
---|
155 | errno = EINVAL;
|
---|
156 | return false;
|
---|
157 | }
|
---|
158 |
|
---|
159 | /* Parse first character of mode and determine flags for open(). */
|
---|
160 | switch (mode[0]) {
|
---|
161 | case 'r':
|
---|
162 | *flags = plus ? O_RDWR : O_RDONLY;
|
---|
163 | break;
|
---|
164 | case 'w':
|
---|
165 | *flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
|
---|
166 | break;
|
---|
167 | case 'a':
|
---|
168 | /* TODO: a+ must read from beginning, append to the end. */
|
---|
169 | if (plus) {
|
---|
170 | errno = ENOTSUP;
|
---|
171 | return false;
|
---|
172 | }
|
---|
173 | *flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY);
|
---|
174 | default:
|
---|
175 | errno = EINVAL;
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 |
|
---|
179 | return true;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Set stream buffer. */
|
---|
183 | void setvbuf(FILE *stream, void *buf, int mode, size_t size)
|
---|
184 | {
|
---|
185 | stream->btype = mode;
|
---|
186 | stream->buf = buf;
|
---|
187 | stream->buf_size = size;
|
---|
188 | stream->buf_head = stream->buf;
|
---|
189 | stream->buf_tail = stream->buf;
|
---|
190 | stream->buf_state = _bs_empty;
|
---|
191 | }
|
---|
192 |
|
---|
193 | static void _setvbuf(FILE *stream)
|
---|
194 | {
|
---|
195 | /* FIXME: Use more complex rules for setting buffering options. */
|
---|
196 |
|
---|
197 | switch (stream->fd) {
|
---|
198 | case 1:
|
---|
199 | setvbuf(stream, NULL, _IOLBF, BUFSIZ);
|
---|
200 | break;
|
---|
201 | case 0:
|
---|
202 | case 2:
|
---|
203 | setvbuf(stream, NULL, _IONBF, 0);
|
---|
204 | break;
|
---|
205 | default:
|
---|
206 | setvbuf(stream, NULL, _IOFBF, BUFSIZ);
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | /** Allocate stream buffer. */
|
---|
211 | static int _fallocbuf(FILE *stream)
|
---|
212 | {
|
---|
213 | assert(stream->buf == NULL);
|
---|
214 |
|
---|
215 | stream->buf = malloc(stream->buf_size);
|
---|
216 | if (stream->buf == NULL) {
|
---|
217 | errno = ENOMEM;
|
---|
218 | return -1;
|
---|
219 | }
|
---|
220 |
|
---|
221 | stream->buf_head = stream->buf;
|
---|
222 | stream->buf_tail = stream->buf;
|
---|
223 | return 0;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /** Open a stream.
|
---|
227 | *
|
---|
228 | * @param path Path of the file to open.
|
---|
229 | * @param mode Mode string, (r|w|a)[b|t][+].
|
---|
230 | *
|
---|
231 | */
|
---|
232 | FILE *fopen(const char *path, const char *mode)
|
---|
233 | {
|
---|
234 | int flags;
|
---|
235 | if (!parse_mode(mode, &flags))
|
---|
236 | return NULL;
|
---|
237 |
|
---|
238 | /* Open file. */
|
---|
239 | FILE *stream = malloc(sizeof(FILE));
|
---|
240 | if (stream == NULL) {
|
---|
241 | errno = ENOMEM;
|
---|
242 | return NULL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | stream->fd = open(path, flags, 0666);
|
---|
246 | if (stream->fd < 0) {
|
---|
247 | /* errno was set by open() */
|
---|
248 | free(stream);
|
---|
249 | return NULL;
|
---|
250 | }
|
---|
251 |
|
---|
252 | stream->error = false;
|
---|
253 | stream->eof = false;
|
---|
254 | stream->klog = false;
|
---|
255 | stream->phone = -1;
|
---|
256 | stream->need_sync = false;
|
---|
257 | _setvbuf(stream);
|
---|
258 |
|
---|
259 | list_append(&stream->link, &files);
|
---|
260 |
|
---|
261 | return stream;
|
---|
262 | }
|
---|
263 |
|
---|
264 | FILE *fdopen(int fd, const char *mode)
|
---|
265 | {
|
---|
266 | /* Open file. */
|
---|
267 | FILE *stream = malloc(sizeof(FILE));
|
---|
268 | if (stream == NULL) {
|
---|
269 | errno = ENOMEM;
|
---|
270 | return NULL;
|
---|
271 | }
|
---|
272 |
|
---|
273 | stream->fd = fd;
|
---|
274 | stream->error = false;
|
---|
275 | stream->eof = false;
|
---|
276 | stream->klog = false;
|
---|
277 | stream->phone = -1;
|
---|
278 | stream->need_sync = false;
|
---|
279 | _setvbuf(stream);
|
---|
280 |
|
---|
281 | list_append(&stream->link, &files);
|
---|
282 |
|
---|
283 | return stream;
|
---|
284 | }
|
---|
285 |
|
---|
286 | FILE *fopen_node(fdi_node_t *node, const char *mode)
|
---|
287 | {
|
---|
288 | int flags;
|
---|
289 | if (!parse_mode(mode, &flags))
|
---|
290 | return NULL;
|
---|
291 |
|
---|
292 | /* Open file. */
|
---|
293 | FILE *stream = malloc(sizeof(FILE));
|
---|
294 | if (stream == NULL) {
|
---|
295 | errno = ENOMEM;
|
---|
296 | return NULL;
|
---|
297 | }
|
---|
298 |
|
---|
299 | stream->fd = open_node(node, flags);
|
---|
300 | if (stream->fd < 0) {
|
---|
301 | /* errno was set by open_node() */
|
---|
302 | free(stream);
|
---|
303 | return NULL;
|
---|
304 | }
|
---|
305 |
|
---|
306 | stream->error = false;
|
---|
307 | stream->eof = false;
|
---|
308 | stream->klog = false;
|
---|
309 | stream->phone = -1;
|
---|
310 | stream->need_sync = false;
|
---|
311 | _setvbuf(stream);
|
---|
312 |
|
---|
313 | list_append(&stream->link, &files);
|
---|
314 |
|
---|
315 | return stream;
|
---|
316 | }
|
---|
317 |
|
---|
318 | int fclose(FILE *stream)
|
---|
319 | {
|
---|
320 | int rc = 0;
|
---|
321 |
|
---|
322 | fflush(stream);
|
---|
323 |
|
---|
324 | if (stream->phone >= 0)
|
---|
325 | async_hangup(stream->phone);
|
---|
326 |
|
---|
327 | if (stream->fd >= 0)
|
---|
328 | rc = close(stream->fd);
|
---|
329 |
|
---|
330 | list_remove(&stream->link);
|
---|
331 |
|
---|
332 | if ((stream != &stdin_null)
|
---|
333 | && (stream != &stdout_klog)
|
---|
334 | && (stream != &stderr_klog))
|
---|
335 | free(stream);
|
---|
336 |
|
---|
337 | stream = NULL;
|
---|
338 |
|
---|
339 | if (rc != 0) {
|
---|
340 | /* errno was set by close() */
|
---|
341 | return EOF;
|
---|
342 | }
|
---|
343 |
|
---|
344 | return 0;
|
---|
345 | }
|
---|
346 |
|
---|
347 | /** Read from a stream (unbuffered).
|
---|
348 | *
|
---|
349 | * @param buf Destination buffer.
|
---|
350 | * @param size Size of each record.
|
---|
351 | * @param nmemb Number of records to read.
|
---|
352 | * @param stream Pointer to the stream.
|
---|
353 | */
|
---|
354 | static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
355 | {
|
---|
356 | size_t left, done;
|
---|
357 |
|
---|
358 | if (size == 0 || nmemb == 0)
|
---|
359 | return 0;
|
---|
360 |
|
---|
361 | left = size * nmemb;
|
---|
362 | done = 0;
|
---|
363 |
|
---|
364 | while ((left > 0) && (!stream->error) && (!stream->eof)) {
|
---|
365 | ssize_t rd = read(stream->fd, buf + done, left);
|
---|
366 |
|
---|
367 | if (rd < 0)
|
---|
368 | stream->error = true;
|
---|
369 | else if (rd == 0)
|
---|
370 | stream->eof = true;
|
---|
371 | else {
|
---|
372 | left -= rd;
|
---|
373 | done += rd;
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 | return (done / size);
|
---|
378 | }
|
---|
379 |
|
---|
380 | /** Write to a stream (unbuffered).
|
---|
381 | *
|
---|
382 | * @param buf Source buffer.
|
---|
383 | * @param size Size of each record.
|
---|
384 | * @param nmemb Number of records to write.
|
---|
385 | * @param stream Pointer to the stream.
|
---|
386 | */
|
---|
387 | static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
388 | {
|
---|
389 | size_t left;
|
---|
390 | size_t done;
|
---|
391 |
|
---|
392 | if (size == 0 || nmemb == 0)
|
---|
393 | return 0;
|
---|
394 |
|
---|
395 | left = size * nmemb;
|
---|
396 | done = 0;
|
---|
397 |
|
---|
398 | while ((left > 0) && (!stream->error)) {
|
---|
399 | ssize_t wr;
|
---|
400 |
|
---|
401 | if (stream->klog)
|
---|
402 | wr = klog_write(buf + done, left);
|
---|
403 | else
|
---|
404 | wr = write(stream->fd, buf + done, left);
|
---|
405 |
|
---|
406 | if (wr <= 0)
|
---|
407 | stream->error = true;
|
---|
408 | else {
|
---|
409 | left -= wr;
|
---|
410 | done += wr;
|
---|
411 | }
|
---|
412 | }
|
---|
413 |
|
---|
414 | if (done > 0)
|
---|
415 | stream->need_sync = true;
|
---|
416 |
|
---|
417 | return (done / size);
|
---|
418 | }
|
---|
419 |
|
---|
420 | /** Read some data in stream buffer. */
|
---|
421 | static void _ffillbuf(FILE *stream)
|
---|
422 | {
|
---|
423 | ssize_t rc;
|
---|
424 |
|
---|
425 | stream->buf_head = stream->buf_tail = stream->buf;
|
---|
426 |
|
---|
427 | rc = read(stream->fd, stream->buf, stream->buf_size);
|
---|
428 | if (rc < 0) {
|
---|
429 | stream->error = true;
|
---|
430 | return;
|
---|
431 | }
|
---|
432 |
|
---|
433 | if (rc == 0) {
|
---|
434 | stream->eof = true;
|
---|
435 | return;
|
---|
436 | }
|
---|
437 |
|
---|
438 | stream->buf_head += rc;
|
---|
439 | stream->buf_state = _bs_read;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /** Write out stream buffer, do not sync stream. */
|
---|
443 | static void _fflushbuf(FILE *stream)
|
---|
444 | {
|
---|
445 | size_t bytes_used;
|
---|
446 |
|
---|
447 | if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
|
---|
448 | return;
|
---|
449 |
|
---|
450 | bytes_used = stream->buf_head - stream->buf_tail;
|
---|
451 | if (bytes_used == 0)
|
---|
452 | return;
|
---|
453 |
|
---|
454 | /* If buffer has prefetched read data, we need to seek back. */
|
---|
455 | if (stream->buf_state == _bs_read)
|
---|
456 | lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR);
|
---|
457 |
|
---|
458 | /* If buffer has unwritten data, we need to write them out. */
|
---|
459 | if (stream->buf_state == _bs_write)
|
---|
460 | (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
|
---|
461 |
|
---|
462 | stream->buf_head = stream->buf;
|
---|
463 | stream->buf_tail = stream->buf;
|
---|
464 | stream->buf_state = _bs_empty;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /** Read from a stream.
|
---|
468 | *
|
---|
469 | * @param dest Destination buffer.
|
---|
470 | * @param size Size of each record.
|
---|
471 | * @param nmemb Number of records to read.
|
---|
472 | * @param stream Pointer to the stream.
|
---|
473 | *
|
---|
474 | */
|
---|
475 | size_t fread(void *dest, size_t size, size_t nmemb, FILE *stream)
|
---|
476 | {
|
---|
477 | uint8_t *dp;
|
---|
478 | size_t bytes_left;
|
---|
479 | size_t now;
|
---|
480 | size_t data_avail;
|
---|
481 | size_t total_read;
|
---|
482 | size_t i;
|
---|
483 |
|
---|
484 | if (size == 0 || nmemb == 0)
|
---|
485 | return 0;
|
---|
486 |
|
---|
487 | /* If not buffered stream, read in directly. */
|
---|
488 | if (stream->btype == _IONBF) {
|
---|
489 | now = _fread(dest, size, nmemb, stream);
|
---|
490 | return now;
|
---|
491 | }
|
---|
492 |
|
---|
493 | /* Make sure no data is pending write. */
|
---|
494 | if (stream->buf_state == _bs_write)
|
---|
495 | _fflushbuf(stream);
|
---|
496 |
|
---|
497 | /* Perform lazy allocation of stream buffer. */
|
---|
498 | if (stream->buf == NULL) {
|
---|
499 | if (_fallocbuf(stream) != 0)
|
---|
500 | return 0; /* Errno set by _fallocbuf(). */
|
---|
501 | }
|
---|
502 |
|
---|
503 | bytes_left = size * nmemb;
|
---|
504 | total_read = 0;
|
---|
505 | dp = (uint8_t *) dest;
|
---|
506 |
|
---|
507 | while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
|
---|
508 | if (stream->buf_head == stream->buf_tail)
|
---|
509 | _ffillbuf(stream);
|
---|
510 |
|
---|
511 | if (stream->error || stream->eof)
|
---|
512 | break;
|
---|
513 |
|
---|
514 | data_avail = stream->buf_head - stream->buf_tail;
|
---|
515 |
|
---|
516 | if (bytes_left > data_avail)
|
---|
517 | now = data_avail;
|
---|
518 | else
|
---|
519 | now = bytes_left;
|
---|
520 |
|
---|
521 | for (i = 0; i < now; i++) {
|
---|
522 | dp[i] = stream->buf_tail[i];
|
---|
523 | }
|
---|
524 |
|
---|
525 | dp += now;
|
---|
526 | stream->buf_tail += now;
|
---|
527 | bytes_left -= now;
|
---|
528 | total_read += now;
|
---|
529 | }
|
---|
530 |
|
---|
531 | return (total_read / size);
|
---|
532 | }
|
---|
533 |
|
---|
534 |
|
---|
535 | /** Write to a stream.
|
---|
536 | *
|
---|
537 | * @param buf Source buffer.
|
---|
538 | * @param size Size of each record.
|
---|
539 | * @param nmemb Number of records to write.
|
---|
540 | * @param stream Pointer to the stream.
|
---|
541 | *
|
---|
542 | */
|
---|
543 | size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
|
---|
544 | {
|
---|
545 | uint8_t *data;
|
---|
546 | size_t bytes_left;
|
---|
547 | size_t now;
|
---|
548 | size_t buf_free;
|
---|
549 | size_t total_written;
|
---|
550 | size_t i;
|
---|
551 | uint8_t b;
|
---|
552 | bool need_flush;
|
---|
553 |
|
---|
554 | if (size == 0 || nmemb == 0)
|
---|
555 | return 0;
|
---|
556 |
|
---|
557 | /* If not buffered stream, write out directly. */
|
---|
558 | if (stream->btype == _IONBF) {
|
---|
559 | now = _fwrite(buf, size, nmemb, stream);
|
---|
560 | fflush(stream);
|
---|
561 | return now;
|
---|
562 | }
|
---|
563 |
|
---|
564 | /* Make sure buffer contains no prefetched data. */
|
---|
565 | if (stream->buf_state == _bs_read)
|
---|
566 | _fflushbuf(stream);
|
---|
567 |
|
---|
568 |
|
---|
569 | /* Perform lazy allocation of stream buffer. */
|
---|
570 | if (stream->buf == NULL) {
|
---|
571 | if (_fallocbuf(stream) != 0)
|
---|
572 | return 0; /* Errno set by _fallocbuf(). */
|
---|
573 | }
|
---|
574 |
|
---|
575 | data = (uint8_t *) buf;
|
---|
576 | bytes_left = size * nmemb;
|
---|
577 | total_written = 0;
|
---|
578 | need_flush = false;
|
---|
579 |
|
---|
580 | while ((!stream->error) && (bytes_left > 0)) {
|
---|
581 | buf_free = stream->buf_size - (stream->buf_head - stream->buf);
|
---|
582 | if (bytes_left > buf_free)
|
---|
583 | now = buf_free;
|
---|
584 | else
|
---|
585 | now = bytes_left;
|
---|
586 |
|
---|
587 | for (i = 0; i < now; i++) {
|
---|
588 | b = data[i];
|
---|
589 | stream->buf_head[i] = b;
|
---|
590 |
|
---|
591 | if ((b == '\n') && (stream->btype == _IOLBF))
|
---|
592 | need_flush = true;
|
---|
593 | }
|
---|
594 |
|
---|
595 | buf += now;
|
---|
596 | stream->buf_head += now;
|
---|
597 | buf_free -= now;
|
---|
598 | bytes_left -= now;
|
---|
599 | total_written += now;
|
---|
600 |
|
---|
601 | if (buf_free == 0) {
|
---|
602 | /* Only need to drain buffer. */
|
---|
603 | _fflushbuf(stream);
|
---|
604 | need_flush = false;
|
---|
605 | }
|
---|
606 | }
|
---|
607 |
|
---|
608 | if (total_written > 0)
|
---|
609 | stream->buf_state = _bs_write;
|
---|
610 |
|
---|
611 | if (need_flush)
|
---|
612 | fflush(stream);
|
---|
613 |
|
---|
614 | return (total_written / size);
|
---|
615 | }
|
---|
616 |
|
---|
617 | int fputc(wchar_t c, FILE *stream)
|
---|
618 | {
|
---|
619 | char buf[STR_BOUNDS(1)];
|
---|
620 | size_t sz = 0;
|
---|
621 |
|
---|
622 | if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
|
---|
623 | size_t wr = fwrite(buf, 1, sz, stream);
|
---|
624 |
|
---|
625 | if (wr < sz)
|
---|
626 | return EOF;
|
---|
627 |
|
---|
628 | return (int) c;
|
---|
629 | }
|
---|
630 |
|
---|
631 | return EOF;
|
---|
632 | }
|
---|
633 |
|
---|
634 | int putchar(wchar_t c)
|
---|
635 | {
|
---|
636 | return fputc(c, stdout);
|
---|
637 | }
|
---|
638 |
|
---|
639 | int fputs(const char *str, FILE *stream)
|
---|
640 | {
|
---|
641 | return fwrite(str, str_size(str), 1, stream);
|
---|
642 | }
|
---|
643 |
|
---|
644 | int puts(const char *str)
|
---|
645 | {
|
---|
646 | return fputs(str, stdout);
|
---|
647 | }
|
---|
648 |
|
---|
649 | int fgetc(FILE *stream)
|
---|
650 | {
|
---|
651 | char c;
|
---|
652 |
|
---|
653 | /* This could be made faster by only flushing when needed. */
|
---|
654 | if (stdout)
|
---|
655 | fflush(stdout);
|
---|
656 | if (stderr)
|
---|
657 | fflush(stderr);
|
---|
658 |
|
---|
659 | if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
|
---|
660 | return EOF;
|
---|
661 |
|
---|
662 | return (int) c;
|
---|
663 | }
|
---|
664 |
|
---|
665 | char *fgets(char *str, int size, FILE *stream)
|
---|
666 | {
|
---|
667 | int c;
|
---|
668 | int idx;
|
---|
669 |
|
---|
670 | idx = 0;
|
---|
671 | while (idx < size - 1) {
|
---|
672 | c = fgetc(stream);
|
---|
673 | if (c == EOF)
|
---|
674 | break;
|
---|
675 |
|
---|
676 | str[idx++] = c;
|
---|
677 |
|
---|
678 | if (c == '\n')
|
---|
679 | break;
|
---|
680 | }
|
---|
681 |
|
---|
682 | if (ferror(stream))
|
---|
683 | return NULL;
|
---|
684 |
|
---|
685 | if (idx == 0)
|
---|
686 | return NULL;
|
---|
687 |
|
---|
688 | str[idx] = '\0';
|
---|
689 | return str;
|
---|
690 | }
|
---|
691 |
|
---|
692 | int getchar(void)
|
---|
693 | {
|
---|
694 | return fgetc(stdin);
|
---|
695 | }
|
---|
696 |
|
---|
697 | int fseek(FILE *stream, off64_t offset, int whence)
|
---|
698 | {
|
---|
699 | off64_t rc;
|
---|
700 |
|
---|
701 | _fflushbuf(stream);
|
---|
702 |
|
---|
703 | rc = lseek(stream->fd, offset, whence);
|
---|
704 | if (rc == (off64_t) (-1)) {
|
---|
705 | /* errno has been set by lseek64. */
|
---|
706 | return -1;
|
---|
707 | }
|
---|
708 |
|
---|
709 | stream->eof = false;
|
---|
710 | return 0;
|
---|
711 | }
|
---|
712 |
|
---|
713 | off64_t ftell(FILE *stream)
|
---|
714 | {
|
---|
715 | return lseek(stream->fd, 0, SEEK_CUR);
|
---|
716 | }
|
---|
717 |
|
---|
718 | void rewind(FILE *stream)
|
---|
719 | {
|
---|
720 | (void) fseek(stream, 0, SEEK_SET);
|
---|
721 | }
|
---|
722 |
|
---|
723 | int fflush(FILE *stream)
|
---|
724 | {
|
---|
725 | _fflushbuf(stream);
|
---|
726 |
|
---|
727 | if (stream->klog) {
|
---|
728 | klog_update();
|
---|
729 | return EOK;
|
---|
730 | }
|
---|
731 |
|
---|
732 | if (stream->fd >= 0 && stream->need_sync) {
|
---|
733 | /**
|
---|
734 | * Better than syncing always, but probably still not the
|
---|
735 | * right thing to do.
|
---|
736 | */
|
---|
737 | stream->need_sync = false;
|
---|
738 | return fsync(stream->fd);
|
---|
739 | }
|
---|
740 |
|
---|
741 | return ENOENT;
|
---|
742 | }
|
---|
743 |
|
---|
744 | int feof(FILE *stream)
|
---|
745 | {
|
---|
746 | return stream->eof;
|
---|
747 | }
|
---|
748 |
|
---|
749 | int ferror(FILE *stream)
|
---|
750 | {
|
---|
751 | return stream->error;
|
---|
752 | }
|
---|
753 |
|
---|
754 | void clearerr(FILE *stream)
|
---|
755 | {
|
---|
756 | stream->eof = false;
|
---|
757 | stream->error = false;
|
---|
758 | }
|
---|
759 |
|
---|
760 | int fileno(FILE *stream)
|
---|
761 | {
|
---|
762 | if (stream->klog) {
|
---|
763 | errno = EBADF;
|
---|
764 | return -1;
|
---|
765 | }
|
---|
766 |
|
---|
767 | return stream->fd;
|
---|
768 | }
|
---|
769 |
|
---|
770 | int fphone(FILE *stream)
|
---|
771 | {
|
---|
772 | if (stream->fd >= 0) {
|
---|
773 | if (stream->phone < 0)
|
---|
774 | stream->phone = fd_phone(stream->fd);
|
---|
775 |
|
---|
776 | return stream->phone;
|
---|
777 | }
|
---|
778 |
|
---|
779 | return -1;
|
---|
780 | }
|
---|
781 |
|
---|
782 | int fnode(FILE *stream, fdi_node_t *node)
|
---|
783 | {
|
---|
784 | if (stream->fd >= 0)
|
---|
785 | return fd_node(stream->fd, node);
|
---|
786 |
|
---|
787 | return ENOENT;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /** @}
|
---|
791 | */
|
---|