Changeset bfd247f in mainline for uspace/lib/libc/generic/io/io.c


Ignore:
Timestamp:
2009-06-29T09:20:10Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
7fcb74c
Parents:
415c7e0d
Message:

set buffering according to what file descriptor we are opening
(this still does not cover the full complexity of stdio buffering)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/io/io.c

    r415c7e0d rbfd247f  
    181181}
    182182
     183static void _setvbuf(FILE *stream)
     184{
     185        /* FIXME: Use more complex rules for setting buffering options. */
     186       
     187        switch (stream->fd) {
     188        case 1:
     189                setvbuf(stream, NULL, _IOLBF, BUFSIZ);
     190                break;
     191        case 0:
     192        case 2:
     193                setvbuf(stream, NULL, _IONBF, 0);
     194                break;
     195        default:
     196                setvbuf(stream, NULL, _IOFBF, BUFSIZ);
     197        }
     198}
     199
    183200/** Allocate stream buffer. */
    184201static int _fallocbuf(FILE *stream)
    185202{
    186203        assert(stream->buf == NULL);
    187 
     204       
    188205        stream->buf = malloc(stream->buf_size);
    189206        if (stream->buf == NULL) {
     
    191208                return -1;
    192209        }
    193 
     210       
    194211        stream->buf_head = stream->buf;
    195212        return 0;
     
    226243        stream->klog = false;
    227244        stream->phone = -1;
    228 
    229         /* FIXME: Should select buffering type based on what was opened. */
    230         setvbuf(stream, NULL, _IOFBF, BUFSIZ);
     245        _setvbuf(stream);
    231246       
    232247        list_append(&stream->link, &files);
     
    249264        stream->klog = false;
    250265        stream->phone = -1;
    251 
    252         /* FIXME: Should select buffering type based on what was opened. */
    253         setvbuf(stream, NULL, _IOLBF, BUFSIZ);
     266        _setvbuf(stream);
    254267       
    255268        list_append(&stream->link, &files);
     
    282295        stream->klog = false;
    283296        stream->phone = -1;
    284 
    285         /* FIXME: Should select buffering type based on what was opened. */
    286         setvbuf(stream, NULL, _IOLBF, BUFSIZ);
     297        _setvbuf(stream);
    287298       
    288299        list_append(&stream->link, &files);
     
    332343        size_t left = size * nmemb;
    333344        size_t done = 0;
    334 
     345       
    335346        /* Make sure no data is pending write. */
    336347        _fflushbuf(stream);
    337 
     348       
    338349        while ((left > 0) && (!stream->error) && (!stream->eof)) {
    339350                ssize_t rd = read(stream->fd, buf + done, left);
     
    380391{
    381392        size_t bytes_used;
    382 
    383         if (!stream->buf || stream->btype == _IONBF || stream->error)
     393       
     394        if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
    384395                return;
    385 
     396       
    386397        bytes_used = stream->buf_head - stream->buf;
    387398        if (bytes_used == 0)
    388399                return;
    389 
     400       
    390401        (void) _fwrite(stream->buf, 1, bytes_used, stream);
    391402        stream->buf_head = stream->buf;
     
    410421        uint8_t b;
    411422        bool need_flush;
    412 
     423       
    413424        /* If not buffered stream, write out directly. */
    414         if (stream->btype == _IONBF)
    415                 return _fwrite(buf, size, nmemb, stream);
    416 
     425        if (stream->btype == _IONBF) {
     426                now = _fwrite(buf, size, nmemb, stream);
     427                fflush(stream);
     428                return now;
     429        }
     430       
    417431        /* Perform lazy allocation of stream buffer. */
    418432        if (stream->buf == NULL) {
     
    420434                        return 0; /* Errno set by _fallocbuf(). */
    421435        }
    422 
     436       
    423437        data = (uint8_t *) buf;
    424438        bytes_left = size * nmemb;
    425439        total_written = 0;
    426440        need_flush = false;
    427 
    428         while (!stream->error && bytes_left > 0) {
    429 
     441       
     442        while ((!stream->error) && (bytes_left > 0)) {
    430443                buf_free = stream->buf_size - (stream->buf_head - stream->buf);
    431444                if (bytes_left > buf_free)
     
    433446                else
    434447                        now = bytes_left;
    435 
     448               
    436449                for (i = 0; i < now; i++) {
    437450                        b = data[i];
    438451                        stream->buf_head[i] = b;
    439 
    440                         if (b == '\n' && stream->btype == _IOLBF)
     452                       
     453                        if ((b == '\n') && (stream->btype == _IOLBF))
    441454                                need_flush = true;
    442455                }
    443 
     456               
    444457                buf += now;
    445458                stream->buf_head += now;
     
    447460                bytes_left -= now;
    448461                total_written += now;
    449 
     462               
    450463                if (buf_free == 0) {
    451464                        /* Only need to drain buffer. */
     
    454467                }
    455468        }
    456 
     469       
    457470        if (need_flush)
    458471                fflush(stream);
    459 
     472       
    460473        return (total_written / size);
    461474}
     
    496509{
    497510        char c;
    498 
     511       
    499512        /* This could be made faster by only flushing when needed. */
    500513        if (stdout)
     
    502515        if (stderr)
    503516                fflush(stderr);
    504 
     517       
    505518        if (fread(&c, sizeof(char), 1, stream) < sizeof(char))
    506519                return EOF;
     
    535548{
    536549        _fflushbuf(stream);
    537 
     550       
    538551        if (stream->klog) {
    539552                klog_update();
Note: See TracChangeset for help on using the changeset viewer.