Ignore:
File:
1 edited

Legend:

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

    re86a617a r7699c21  
    4242#include <malloc.h>
    4343#include <async.h>
    44 #include <io/kio.h>
     44#include <io/klog.h>
    4545#include <vfs/vfs.h>
    4646#include <vfs/vfs_sess.h>
     
    5757        .error = true,
    5858        .eof = true,
    59         .kio = false,
     59        .klog = false,
    6060        .sess = NULL,
    6161        .btype = _IONBF,
     
    6767};
    6868
    69 static FILE stdout_kio = {
     69static FILE stdout_klog = {
    7070        .fd = -1,
    7171        .error = false,
    7272        .eof = false,
    73         .kio = true,
     73        .klog = true,
    7474        .sess = NULL,
    7575        .btype = _IOLBF,
     
    8181};
    8282
    83 static FILE stderr_kio = {
     83static FILE stderr_klog = {
    8484        .fd = -1,
    8585        .error = false,
    8686        .eof = false,
    87         .kio = true,
     87        .klog = true,
    8888        .sess = NULL,
    8989        .btype = _IONBF,
     
    113113                stdout = fdopen(1, "w");
    114114        } else {
    115                 stdout = &stdout_kio;
     115                stdout = &stdout_klog;
    116116                list_append(&stdout->link, &files);
    117117        }
     
    120120                stderr = fdopen(2, "w");
    121121        } else {
    122                 stderr = &stderr_kio;
     122                stderr = &stderr_klog;
    123123                list_append(&stderr->link, &files);
    124124        }
     
    231231        if (stream->buf == NULL) {
    232232                errno = ENOMEM;
    233                 return EOF;
     233                return -1;
    234234        }
    235235       
     
    267267        stream->error = false;
    268268        stream->eof = false;
    269         stream->kio = false;
     269        stream->klog = false;
    270270        stream->sess = NULL;
    271271        stream->need_sync = false;
    272272        _setvbuf(stream);
    273         stream->ungetc_chars = 0;
    274273       
    275274        list_append(&stream->link, &files);
     
    290289        stream->error = false;
    291290        stream->eof = false;
    292         stream->kio = false;
     291        stream->klog = false;
    293292        stream->sess = NULL;
    294293        stream->need_sync = false;
    295294        _setvbuf(stream);
    296         stream->ungetc_chars = 0;
    297295       
    298296        list_append(&stream->link, &files);
     
    301299}
    302300
    303 
    304 static int _fclose_nofree(FILE *stream)
     301int fclose(FILE *stream)
    305302{
    306303        int rc = 0;
     
    315312       
    316313        list_remove(&stream->link);
     314       
     315        if ((stream != &stdin_null)
     316            && (stream != &stdout_klog)
     317            && (stream != &stderr_klog))
     318                free(stream);
     319       
     320        stream = NULL;
    317321       
    318322        if (rc != 0) {
     
    322326       
    323327        return 0;
    324 }
    325 
    326 int fclose(FILE *stream)
    327 {
    328         int rc = _fclose_nofree(stream);
    329        
    330         if ((stream != &stdin_null)
    331             && (stream != &stdout_kio)
    332             && (stream != &stderr_kio))
    333                 free(stream);
    334        
    335         return rc;
    336 }
    337 
    338 FILE *freopen(const char *path, const char *mode, FILE *stream)
    339 {
    340         FILE *nstr;
    341        
    342         if (path == NULL) {
    343                 /* Changing mode is not supported */
    344                 return NULL;
    345         }
    346        
    347         (void) _fclose_nofree(stream);
    348         nstr = fopen(path, mode);
    349         if (nstr == NULL) {
    350                 free(stream);
    351                 return NULL;
    352         }
    353        
    354         list_remove(&nstr->link);
    355         *stream = *nstr;
    356         list_append(&stream->link, &files);
    357        
    358         free(nstr);
    359        
    360         return stream;
    361328}
    362329
     
    367334 * @param nmemb  Number of records to read.
    368335 * @param stream Pointer to the stream.
    369  *
    370  * @return Number of elements successfully read. On error this is less than
    371  *         nmemb, stream error indicator is set and errno is set.
    372336 */
    373337static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
     
    384348                ssize_t rd = read(stream->fd, buf + done, left);
    385349               
    386                 if (rd < 0) {
    387                         /* errno was set by read() */
     350                if (rd < 0)
    388351                        stream->error = true;
    389                 } else if (rd == 0) {
     352                else if (rd == 0)
    390353                        stream->eof = true;
    391                 } else {
     354                else {
    392355                        left -= rd;
    393356                        done += rd;
     
    404367 * @param nmemb  Number of records to write.
    405368 * @param stream Pointer to the stream.
    406  *
    407  * @return Number of elements successfully written. On error this is less than
    408  *         nmemb, stream error indicator is set and errno is set.
    409369 */
    410370static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
     
    412372        size_t left;
    413373        size_t done;
    414         int rc;
    415374
    416375        if (size == 0 || nmemb == 0)
     
    422381        while ((left > 0) && (!stream->error)) {
    423382                ssize_t wr;
    424                 size_t uwr;
    425                
    426                 if (stream->kio) {
    427                         uwr = 0;
    428                         rc = kio_write(buf + done, left, &uwr);
    429                         if (rc != EOK)
    430                                 errno = rc;
    431                 } else {
     383               
     384                if (stream->klog)
     385                        wr = klog_write(buf + done, left);
     386                else
    432387                        wr = write(stream->fd, buf + done, left);
    433                         if (wr >= 0) {
    434                                 uwr = (size_t)wr;
    435                                 rc = EOK;
    436                         } else {
    437                                 /* errno was set by write */
    438                                 uwr = 0;
    439                                 rc = errno;
    440                         }
    441                 }
    442                
    443                 if (rc != EOK) {
    444                         /* errno was set above */
     388               
     389                if (wr <= 0)
    445390                        stream->error = true;
    446                 } else {
    447                         left -= uwr;
    448                         done += uwr;
     391                else {
     392                        left -= wr;
     393                        done += wr;
    449394                }
    450395        }
     
    456401}
    457402
    458 /** Read some data in stream buffer.
    459  *
    460  * On error, stream error indicator is set and errno is set.
    461  */
     403/** Read some data in stream buffer. */
    462404static void _ffillbuf(FILE *stream)
    463405{
     
    468410        rc = read(stream->fd, stream->buf, stream->buf_size);
    469411        if (rc < 0) {
    470                 /* errno was set by read() */
    471412                stream->error = true;
    472413                return;
     
    493434
    494435        /* If buffer has prefetched read data, we need to seek back. */
    495         if (bytes_used > 0 && stream->buf_state == _bs_read) {
    496                 off64_t rc;
    497                 rc = lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR);
    498                 if (rc == (off64_t)-1) {
    499                         /* errno was set by lseek */
    500                         stream->error = 1;
    501                         return;
    502                 }
    503         }
     436        if (bytes_used > 0 && stream->buf_state == _bs_read)
     437                lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR);
    504438
    505439        /* If buffer has unwritten data, we need to write them out. */
    506         if (bytes_used > 0 && stream->buf_state == _bs_write) {
     440        if (bytes_used > 0 && stream->buf_state == _bs_write)
    507441                (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
    508                 /* On error stream error indicator and errno are set by _fwrite */
    509                 if (stream->error)
    510                         return;
    511         }
    512442
    513443        stream->buf_head = stream->buf;
     
    536466                return 0;
    537467
    538         bytes_left = size * nmemb;
    539         total_read = 0;
    540         dp = (uint8_t *) dest;
    541 
    542         /* Bytes from ungetc() buffer */
    543         while (stream->ungetc_chars > 0 && bytes_left > 0) {
    544                 *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
    545                 ++total_read;
    546                 --bytes_left;
    547         }
    548 
    549468        /* If not buffered stream, read in directly. */
    550469        if (stream->btype == _IONBF) {
    551                 total_read += _fread(dest, 1, bytes_left, stream);
    552                 return total_read / size;
     470                now = _fread(dest, size, nmemb, stream);
     471                return now;
    553472        }
    554473
     
    563482        }
    564483
     484        bytes_left = size * nmemb;
     485        total_read = 0;
     486        dp = (uint8_t *) dest;
     487
    565488        while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
    566489                if (stream->buf_head == stream->buf_tail)
    567490                        _ffillbuf(stream);
    568491
    569                 if (stream->error || stream->eof) {
    570                         /* On error errno was set by _ffillbuf() */
     492                if (stream->error || stream->eof)
    571493                        break;
    572                 }
    573494
    574495                data_avail = stream->buf_head - stream->buf_tail;
     
    626547                _fflushbuf(stream);
    627548
     549
    628550        /* Perform lazy allocation of stream buffer. */
    629551        if (stream->buf == NULL) {
     
    662584                        /* Only need to drain buffer. */
    663585                        _fflushbuf(stream);
    664                         if (!stream->error)
    665                                 need_flush = false;
     586                        need_flush = false;
    666587                }
    667588        }
     
    697618int fputs(const char *str, FILE *stream)
    698619{
    699         (void) fwrite(str, str_size(str), 1, stream);
    700         if (ferror(stream))
    701                 return EOF;
    702         return 0;
     620        return fwrite(str, str_size(str), 1, stream);
    703621}
    704622
     
    756674}
    757675
    758 int ungetc(int c, FILE *stream)
    759 {
    760         if (c == EOF)
    761                 return EOF;
    762 
    763         if (stream->ungetc_chars >= UNGETC_MAX)
    764                 return EOF;
    765 
    766         stream->ungetc_buf[stream->ungetc_chars++] =
    767             (uint8_t)c;
    768 
    769         stream->eof = false;
    770         return (uint8_t)c;
    771 }
    772 
    773676int fseek(FILE *stream, off64_t offset, int whence)
    774677{
    775678        off64_t rc;
    776679
    777         if (stream->error)
    778                 return EOF;
    779 
    780680        _fflushbuf(stream);
    781         if (stream->error) {
    782                 /* errno was set by _fflushbuf() */
    783                 return EOF;
    784         }
    785 
    786         stream->ungetc_chars = 0;
    787681
    788682        rc = lseek(stream->fd, offset, whence);
    789683        if (rc == (off64_t) (-1)) {
    790                 /* errno has been set by lseek() */
    791                 return EOF;
     684                /* errno has been set by lseek64. */
     685                return -1;
    792686        }
    793687
     
    798692off64_t ftell(FILE *stream)
    799693{
    800         off64_t pos;
    801        
    802         if (stream->error)
    803                 return EOF;
    804        
    805694        _fflushbuf(stream);
    806         if (stream->error) {
    807                 /* errno was set by _fflushbuf() */
    808                 return EOF;
    809         }
    810 
    811         pos = lseek(stream->fd, 0, SEEK_CUR);
    812         if (pos == (off64_t) -1) {
    813                 /* errno was set by lseek */
    814                 return (off64_t) -1;
    815         }
    816        
    817         return pos - stream->ungetc_chars;
     695        return lseek(stream->fd, 0, SEEK_CUR);
    818696}
    819697
     
    825703int fflush(FILE *stream)
    826704{
    827         if (stream->error)
    828                 return EOF;
    829        
    830705        _fflushbuf(stream);
    831         if (stream->error) {
    832                 /* errno was set by _fflushbuf() */
    833                 return EOF;
    834         }
    835        
    836         if (stream->kio) {
    837                 kio_update();
    838                 return 0;
     706       
     707        if (stream->klog) {
     708                klog_update();
     709                return EOK;
    839710        }
    840711       
     
    845716                 */
    846717                stream->need_sync = false;
    847                 if (fsync(stream->fd) != 0) {
    848                         /* errno was set by fsync() */
    849                         return EOF;
    850                 }
    851 
    852                 return 0;
    853         }
    854        
    855         return 0;
     718                return fsync(stream->fd);
     719        }
     720       
     721        return ENOENT;
    856722}
    857723
     
    874740int fileno(FILE *stream)
    875741{
    876         if (stream->kio) {
     742        if (stream->klog) {
    877743                errno = EBADF;
    878                 return EOF;
     744                return -1;
    879745        }
    880746       
     
    882748}
    883749
    884 async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
     750async_sess_t *fsession(exch_mgmt_t mgmt, FILE *stream)
    885751{
    886752        if (stream->fd >= 0) {
    887753                if (stream->sess == NULL)
    888                         stream->sess = vfs_fd_session(stream->fd, iface);
     754                        stream->sess = fd_session(mgmt, stream->fd);
    889755               
    890756                return stream->sess;
     
    894760}
    895761
    896 int vfs_fhandle(FILE *stream, int *handle)
     762int fhandle(FILE *stream, int *handle)
    897763{
    898764        if (stream->fd >= 0) {
Note: See TracChangeset for help on using the changeset viewer.