source: mainline/uspace/lib/c/generic/io/io.c@ 82582e4

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 82582e4 was 82582e4, checked in by Jakub Jermar <jakub@…>, 14 years ago

Fix fopen(_, a) by adding a missing break statement.

  • Property mode set to 100644
File size: 14.5 KB
RevLine 
[b861b58]1/*
[df4ed85]2 * Copyright (c) 2005 Martin Decky
[b861b58]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.
[b2951e2]27 */
28
[fadd381]29/** @addtogroup libc
[b2951e2]30 * @{
31 */
32/** @file
[2595dab]33 */
[b861b58]34
[cc6f688]35#include <stdio.h>
[2595dab]36#include <unistd.h>
37#include <fcntl.h>
[ef8bcc6]38#include <assert.h>
[19f857a]39#include <str.h>
[56fa418]40#include <errno.h>
[2595dab]41#include <bool.h>
42#include <malloc.h>
[64d2b10]43#include <async.h>
[2595dab]44#include <io/klog.h>
45#include <vfs/vfs.h>
46#include <ipc/devmap.h>
[d9c8c81]47#include <adt/list.h>
[47b7006]48#include "../private/io.h"
[b861b58]49
[facebd56]50static void _ffillbuf(FILE *stream);
[ef8bcc6]51static void _fflushbuf(FILE *stream);
52
[a68f737]53static FILE stdin_null = {
[2595dab]54 .fd = -1,
55 .error = true,
56 .eof = true,
57 .klog = false,
[ef8bcc6]58 .phone = -1,
59 .btype = _IONBF,
60 .buf = NULL,
61 .buf_size = 0,
[facebd56]62 .buf_head = NULL,
63 .buf_tail = NULL,
64 .buf_state = _bs_empty
[2595dab]65};
[350514c]66
[a68f737]67static FILE stdout_klog = {
[2595dab]68 .fd = -1,
69 .error = false,
70 .eof = false,
71 .klog = true,
[ef8bcc6]72 .phone = -1,
73 .btype = _IOLBF,
74 .buf = NULL,
75 .buf_size = BUFSIZ,
[facebd56]76 .buf_head = NULL,
77 .buf_tail = NULL,
78 .buf_state = _bs_empty
[2595dab]79};
80
[a68f737]81static FILE stderr_klog = {
82 .fd = -1,
83 .error = false,
84 .eof = false,
85 .klog = true,
[ef8bcc6]86 .phone = -1,
87 .btype = _IONBF,
88 .buf = NULL,
89 .buf_size = 0,
[facebd56]90 .buf_head = NULL,
91 .buf_tail = NULL,
92 .buf_state = _bs_empty
[a68f737]93};
94
95FILE *stdin = NULL;
96FILE *stdout = NULL;
97FILE *stderr = NULL;
98
99static LIST_INITIALIZE(files);
100
[db24058]101void __stdio_init(int filc, fdi_node_t *filv[])
[a68f737]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
[db24058]125void __stdio_done(void)
[a68f737]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}
[2595dab]135
136static bool parse_mode(const char *mode, int *flags)
[b861b58]137{
[2595dab]138 /* Parse mode except first character. */
139 const char *mp = mode;
140 if (*mp++ == 0) {
141 errno = EINVAL;
142 return false;
143 }
[cc6f688]144
[2595dab]145 if ((*mp == 'b') || (*mp == 't'))
146 mp++;
[c9857c6]147
[2595dab]148 bool plus;
149 if (*mp == '+') {
150 mp++;
151 plus = true;
152 } else
153 plus = false;
[566f4cfb]154
[2595dab]155 if (*mp != 0) {
156 errno = EINVAL;
157 return false;
[350514c]158 }
159
[2595dab]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);
[82582e4]175 break;
[2595dab]176 default:
177 errno = EINVAL;
178 return false;
179 }
180
181 return true;
[cc6f688]182}
[b861b58]183
[ef8bcc6]184/** Set stream buffer. */
185void 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;
[facebd56]191 stream->buf_tail = stream->buf;
192 stream->buf_state = _bs_empty;
[ef8bcc6]193}
194
[bfd247f]195static 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
[ef8bcc6]212/** Allocate stream buffer. */
213static int _fallocbuf(FILE *stream)
214{
215 assert(stream->buf == NULL);
[bfd247f]216
[ef8bcc6]217 stream->buf = malloc(stream->buf_size);
218 if (stream->buf == NULL) {
219 errno = ENOMEM;
220 return -1;
221 }
[bfd247f]222
[ef8bcc6]223 stream->buf_head = stream->buf;
[facebd56]224 stream->buf_tail = stream->buf;
[ef8bcc6]225 return 0;
226}
227
[2595dab]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 *
[4e2cf8b]233 */
[2595dab]234FILE *fopen(const char *path, const char *mode)
[4e2cf8b]235{
[2595dab]236 int flags;
237 if (!parse_mode(mode, &flags))
238 return NULL;
[4e2cf8b]239
[2595dab]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;
[facebd56]258 stream->need_sync = false;
[bfd247f]259 _setvbuf(stream);
[2595dab]260
[a68f737]261 list_append(&stream->link, &files);
262
[2595dab]263 return stream;
264}
265
[080ad7f]266FILE *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;
[facebd56]280 stream->need_sync = false;
[bfd247f]281 _setvbuf(stream);
[080ad7f]282
283 list_append(&stream->link, &files);
284
285 return stream;
286}
287
[99272a3]288FILE *fopen_node(fdi_node_t *node, const char *mode)
[2595dab]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;
[facebd56]312 stream->need_sync = false;
[bfd247f]313 _setvbuf(stream);
[2595dab]314
[a68f737]315 list_append(&stream->link, &files);
316
[2595dab]317 return stream;
318}
319
320int fclose(FILE *stream)
321{
322 int rc = 0;
323
324 fflush(stream);
325
326 if (stream->phone >= 0)
[64d2b10]327 async_hangup(stream->phone);
[2595dab]328
329 if (stream->fd >= 0)
330 rc = close(stream->fd);
331
[a68f737]332 list_remove(&stream->link);
333
334 if ((stream != &stdin_null)
335 && (stream != &stdout_klog)
336 && (stream != &stderr_klog))
[2595dab]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;
[4e2cf8b]347}
348
[facebd56]349/** Read from a stream (unbuffered).
[2595dab]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.
[4e2cf8b]355 */
[facebd56]356static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
[4e2cf8b]357{
[002252a]358 size_t left, done;
359
360 if (size == 0 || nmemb == 0)
361 return 0;
362
363 left = size * nmemb;
364 done = 0;
[bfd247f]365
[2595dab]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 }
[4e2cf8b]378
[2595dab]379 return (done / size);
380}
[55cff86]381
[facebd56]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 */
[ef8bcc6]389static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
[2595dab]390{
[002252a]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
[2595dab]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 }
[facebd56]415
416 if (done > 0)
417 stream->need_sync = true;
[2595dab]418
419 return (done / size);
[4e2cf8b]420}
421
[facebd56]422/** Read some data in stream buffer. */
423static 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. */
[ef8bcc6]445static void _fflushbuf(FILE *stream)
446{
447 size_t bytes_used;
[facebd56]448
[bfd247f]449 if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
[ef8bcc6]450 return;
[facebd56]451
452 bytes_used = stream->buf_head - stream->buf_tail;
[ef8bcc6]453 if (bytes_used == 0)
454 return;
[facebd56]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
[ef8bcc6]464 stream->buf_head = stream->buf;
[facebd56]465 stream->buf_tail = stream->buf;
466 stream->buf_state = _bs_empty;
[ef8bcc6]467}
468
[facebd56]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 */
477size_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
[ef8bcc6]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 */
545size_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;
[002252a]555
556 if (size == 0 || nmemb == 0)
557 return 0;
558
[ef8bcc6]559 /* If not buffered stream, write out directly. */
[bfd247f]560 if (stream->btype == _IONBF) {
561 now = _fwrite(buf, size, nmemb, stream);
562 fflush(stream);
563 return now;
564 }
[facebd56]565
566 /* Make sure buffer contains no prefetched data. */
567 if (stream->buf_state == _bs_read)
568 _fflushbuf(stream);
569
570
[ef8bcc6]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 }
[bfd247f]576
[ef8bcc6]577 data = (uint8_t *) buf;
578 bytes_left = size * nmemb;
579 total_written = 0;
580 need_flush = false;
[bfd247f]581
582 while ((!stream->error) && (bytes_left > 0)) {
[ef8bcc6]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;
[bfd247f]588
[ef8bcc6]589 for (i = 0; i < now; i++) {
590 b = data[i];
591 stream->buf_head[i] = b;
[bfd247f]592
593 if ((b == '\n') && (stream->btype == _IOLBF))
[ef8bcc6]594 need_flush = true;
595 }
[bfd247f]596
[ef8bcc6]597 buf += now;
598 stream->buf_head += now;
599 buf_free -= now;
600 bytes_left -= now;
601 total_written += now;
[bfd247f]602
[ef8bcc6]603 if (buf_free == 0) {
604 /* Only need to drain buffer. */
605 _fflushbuf(stream);
606 need_flush = false;
607 }
608 }
[bfd247f]609
[facebd56]610 if (total_written > 0)
611 stream->buf_state = _bs_write;
612
[ef8bcc6]613 if (need_flush)
614 fflush(stream);
[bfd247f]615
[ef8bcc6]616 return (total_written / size);
617}
618
[2595dab]619int fputc(wchar_t c, FILE *stream)
[c9857c6]620{
[56fa418]621 char buf[STR_BOUNDS(1)];
[2595dab]622 size_t sz = 0;
623
624 if (chr_encode(c, buf, &sz, STR_BOUNDS(1)) == EOK) {
[2a5af223]625 size_t wr = fwrite(buf, 1, sz, stream);
[2595dab]626
627 if (wr < sz)
628 return EOF;
629
630 return (int) c;
631 }
632
633 return EOF;
634}
[56fa418]635
[2595dab]636int putchar(wchar_t c)
637{
638 return fputc(c, stdout);
639}
[56fa418]640
[2595dab]641int fputs(const char *str, FILE *stream)
642{
643 return fwrite(str, str_size(str), 1, stream);
644}
[56fa418]645
[2595dab]646int puts(const char *str)
647{
648 return fputs(str, stdout);
[c9857c6]649}
[b27a97bb]650
[2595dab]651int fgetc(FILE *stream)
[b27a97bb]652{
[2595dab]653 char c;
[bfd247f]654
[bac82eeb]655 /* This could be made faster by only flushing when needed. */
[b8e57e8c]656 if (stdout)
657 fflush(stdout);
658 if (stderr)
659 fflush(stderr);
[bfd247f]660
[2595dab]661 if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
662 return EOF;
[b27a97bb]663
[2595dab]664 return (int) c;
665}
666
[c62d2e1]667char *fgets(char *str, int size, FILE *stream)
668{
[a634485]669 int c;
[c62d2e1]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
[2595dab]694int getchar(void)
695{
696 return fgetc(stdin);
[b27a97bb]697}
698
[ed903174]699int fseek(FILE *stream, off64_t offset, int whence)
[d2cc7e1]700{
[facebd56]701 off64_t rc;
702
703 _fflushbuf(stream);
704
705 rc = lseek(stream->fd, offset, whence);
[ed903174]706 if (rc == (off64_t) (-1)) {
707 /* errno has been set by lseek64. */
[2595dab]708 return -1;
709 }
[facebd56]710
[2595dab]711 stream->eof = false;
[566f4cfb]712 return 0;
[d2cc7e1]713}
714
[ed903174]715off64_t ftell(FILE *stream)
[08232ee]716{
[ed903174]717 return lseek(stream->fd, 0, SEEK_CUR);
[08232ee]718}
719
[080ad7f]720void rewind(FILE *stream)
721{
722 (void) fseek(stream, 0, SEEK_SET);
723}
724
[2595dab]725int fflush(FILE *stream)
726{
[ef8bcc6]727 _fflushbuf(stream);
[bfd247f]728
[2595dab]729 if (stream->klog) {
730 klog_update();
731 return EOK;
732 }
733
[facebd56]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;
[2595dab]740 return fsync(stream->fd);
[facebd56]741 }
[2595dab]742
743 return ENOENT;
744}
745
746int feof(FILE *stream)
747{
748 return stream->eof;
749}
750
751int ferror(FILE *stream)
752{
753 return stream->error;
754}
755
[c77a64f]756void clearerr(FILE *stream)
757{
758 stream->eof = false;
759 stream->error = false;
760}
761
[3629481]762int fileno(FILE *stream)
763{
764 if (stream->klog) {
765 errno = EBADF;
766 return -1;
767 }
768
769 return stream->fd;
770}
771
[2595dab]772int 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
[a68f737]784int fnode(FILE *stream, fdi_node_t *node)
[2595dab]785{
[a68f737]786 if (stream->fd >= 0)
787 return fd_node(stream->fd, node);
788
789 return ENOENT;
[2595dab]790}
791
[fadd381]792/** @}
[b2951e2]793 */
Note: See TracBrowser for help on using the repository browser.