Changeset bd5414e in mainline for uspace/lib/c/generic/io/io.c


Ignore:
Timestamp:
2015-09-21T21:05:26Z (10 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c70703a
Parents:
bea710f
Message:

Proper ungetc() in libc.

File:
1 edited

Legend:

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

    rbea710f rbd5414e  
    6464        .buf_head = NULL,
    6565        .buf_tail = NULL,
    66         .buf_state = _bs_empty
     66        .buf_state = _bs_empty,
    6767};
    6868
     
    466466                return 0;
    467467
     468        bytes_left = size * nmemb;
     469        total_read = 0;
     470        dp = (uint8_t *) dest;
     471
     472        /* Bytes from ungetc() buffer */
     473        while (stream->ungetc_chars > 0 && bytes_left > 0) {
     474                *dp++ = stream->ungetc_buf[--stream->ungetc_chars];
     475                ++total_read;
     476        }
     477
    468478        /* If not buffered stream, read in directly. */
    469479        if (stream->btype == _IONBF) {
    470                 now = _fread(dest, size, nmemb, stream);
    471                 return now;
     480                total_read += _fread(dest, 1, bytes_left, stream);
     481                return total_read / size;
    472482        }
    473483
     
    481491                        return 0; /* Errno set by _fallocbuf(). */
    482492        }
    483 
    484         bytes_left = size * nmemb;
    485         total_read = 0;
    486         dp = (uint8_t *) dest;
    487493
    488494        while ((!stream->error) && (!stream->eof) && (bytes_left > 0)) {
     
    674680}
    675681
     682int ungetc(int c, FILE *stream)
     683{
     684        if (c == EOF)
     685                return EOF;
     686
     687        if (stream->ungetc_chars >= UNGETC_MAX)
     688                return EOF;
     689
     690        stream->ungetc_buf[stream->ungetc_chars++] =
     691            (uint8_t)c;
     692
     693        stream->eof = false;
     694        return (uint8_t)c;
     695}
     696
    676697int fseek(FILE *stream, off64_t offset, int whence)
    677698{
     
    679700
    680701        _fflushbuf(stream);
     702        stream->ungetc_chars = 0;
    681703
    682704        rc = lseek(stream->fd, offset, whence);
     
    693715{
    694716        _fflushbuf(stream);
    695         return lseek(stream->fd, 0, SEEK_CUR);
     717        return lseek(stream->fd, 0, SEEK_CUR) - stream->ungetc_chars;
    696718}
    697719
Note: See TracChangeset for help on using the changeset viewer.