Changes in / [2070570:5782081] in mainline


Ignore:
Location:
uspace/lib/libc
Files:
2 edited

Legend:

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

    r2070570 r5782081  
    4646#include <adt/list.h>
    4747
    48 static void _ffillbuf(FILE *stream);
    4948static void _fflushbuf(FILE *stream);
    5049
     
    5857        .buf = NULL,
    5958        .buf_size = 0,
    60         .buf_head = NULL,
    61         .buf_tail = NULL,
    62         .buf_state = _bs_empty
     59        .buf_head = NULL
    6360};
    6461
     
    7269        .buf = NULL,
    7370        .buf_size = BUFSIZ,
    74         .buf_head = NULL,
    75         .buf_tail = NULL,
    76         .buf_state = _bs_empty
     71        .buf_head = NULL
    7772};
    7873
     
    8681        .buf = NULL,
    8782        .buf_size = 0,
    88         .buf_head = NULL,
    89         .buf_tail = NULL,
    90         .buf_state = _bs_empty
     83        .buf_head = NULL
    9184};
    9285
     
    186179        stream->buf_size = size;
    187180        stream->buf_head = stream->buf;
    188         stream->buf_tail = stream->buf;
    189         stream->buf_state = _bs_empty;
    190181}
    191182
     
    219210       
    220211        stream->buf_head = stream->buf;
    221         stream->buf_tail = stream->buf;
    222212        return 0;
    223213}
     
    253243        stream->klog = false;
    254244        stream->phone = -1;
    255         stream->need_sync = false;
    256245        _setvbuf(stream);
    257246       
     
    275264        stream->klog = false;
    276265        stream->phone = -1;
    277         stream->need_sync = false;
    278266        _setvbuf(stream);
    279267       
     
    307295        stream->klog = false;
    308296        stream->phone = -1;
    309         stream->need_sync = false;
    310297        _setvbuf(stream);
    311298       
     
    344331}
    345332
    346 /** Read from a stream (unbuffered).
     333/** Read from a stream.
    347334 *
    348335 * @param buf    Destination buffer.
     
    350337 * @param nmemb  Number of records to read.
    351338 * @param stream Pointer to the stream.
     339 *
    352340 */
    353 static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
     341size_t fread(void *buf, size_t size, size_t nmemb, FILE *stream)
    354342{
    355343        size_t left, done;
     
    357345        if (size == 0 || nmemb == 0)
    358346                return 0;
     347
     348        /* Make sure no data is pending write. */
     349        _fflushbuf(stream);
    359350
    360351        left = size * nmemb;
     
    377368}
    378369
    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  */
    386370static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
    387371{
     
    410394                }
    411395        }
    412 
    413         if (done > 0)
    414                 stream->need_sync = true;
    415396       
    416397        return (done / size);
    417398}
    418399
    419 /** Read some data in stream buffer. */
    420 static 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. */
     400/** Drain stream buffer, do not sync stream. */
    442401static void _fflushbuf(FILE *stream)
    443402{
    444403        size_t bytes_used;
    445 
     404       
    446405        if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error))
    447406                return;
    448 
    449         bytes_used = stream->buf_head - stream->buf_tail;
     407       
     408        bytes_used = stream->buf_head - stream->buf;
    450409        if (bytes_used == 0)
    451410                return;
    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 
     411       
     412        (void) _fwrite(stream->buf, 1, bytes_used, stream);
    461413        stream->buf_head = stream->buf;
    462         stream->buf_tail = stream->buf;
    463         stream->buf_state = _bs_empty;
    464 }
    465 
    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  */
    474 size_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 
     414}
    533415
    534416/** Write to a stream.
     
    560442                return now;
    561443        }
    562 
    563         /* Make sure buffer contains no prefetched data. */
    564         if (stream->buf_state == _bs_read)
    565                 _fflushbuf(stream);
    566 
    567 
     444       
    568445        /* Perform lazy allocation of stream buffer. */
    569446        if (stream->buf == NULL) {
     
    605482        }
    606483       
    607         if (total_written > 0)
    608                 stream->buf_state = _bs_write;
    609 
    610484        if (need_flush)
    611485                fflush(stream);
     
    696570int fseek(FILE *stream, off64_t offset, int whence)
    697571{
    698         off64_t rc;
    699 
    700         _fflushbuf(stream);
    701 
    702         rc = lseek(stream->fd, offset, whence);
     572        off64_t rc = lseek(stream->fd, offset, whence);
    703573        if (rc == (off64_t) (-1)) {
    704574                /* errno has been set by lseek64. */
    705575                return -1;
    706576        }
    707 
     577       
    708578        stream->eof = false;
     579       
    709580        return 0;
    710581}
     
    729600        }
    730601       
    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;
     602        if (stream->fd >= 0)
    737603                return fsync(stream->fd);
    738         }
    739604       
    740605        return ENOENT;
  • uspace/lib/libc/include/stdio.h

    r2070570 r5782081  
    7575};
    7676
    77 enum _buffer_state {
    78         /** Buffer is empty */
    79         _bs_empty,
    80 
    81         /** Buffer contains data to be written */
    82         _bs_write,
    83 
    84         /** Buffer contains prefetched data for reading */
    85         _bs_read
    86 };
    87 
    8877typedef struct {
    8978        /** Linked list pointer. */
     
    10594        int phone;
    10695
    107         /**
    108          * Non-zero if the stream needs sync on fflush(). XXX change
    109          * console semantics so that sync is not needed.
    110          */
    111         int need_sync;
    112 
    11396        /** Buffering type */
    11497        enum _buffer_type btype;
    115 
    11698        /** Buffer */
    11799        uint8_t *buf;
    118 
    119100        /** Buffer size */
    120101        size_t buf_size;
    121 
    122         /** Buffer state */
    123         enum _buffer_state buf_state;
    124 
    125102        /** Buffer I/O pointer */
    126103        uint8_t *buf_head;
    127 
    128         /** Points to end of occupied space when in read mode. */
    129         uint8_t *buf_tail;
    130104} FILE;
    131105
Note: See TracChangeset for help on using the changeset viewer.