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

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

Merge from lp:~jakub/helenos/camp2011.

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