source: mainline/uspace/lib/c/generic/io/io.c@ 96b02eb9

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

implement fileno()

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