source: mainline/uspace/lib/c/generic/io/io.c@ ffa2c8ef

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ffa2c8ef was 64d2b10, checked in by Martin Decky <martin@…>, 14 years ago

libc: do not intermix low-level IPC methods with async framework methods

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