source: mainline/uspace/lib/c/generic/io/io.c@ 221ab41a

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

Rework the way how open files are passed from parent task to child.

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