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