From b852731d2bc40ce2208264cdb55cc8e27944c890 Mon Sep 17 00:00:00 2001 From: Owen Shepherd Date: Tue, 11 Dec 2012 02:00:22 +0000 Subject: [PATCH] PDCLIB-18: Unify fgetc under _PDCLIB_getchars. Also unify gets under _PDCLIB_getchars (gets also suffered from the bug) --- functions/stdio/fgetc.c | 11 ++++++----- functions/stdio/gets.c | 20 +++++++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/functions/stdio/fgetc.c b/functions/stdio/fgetc.c index ae7a835..61f52fd 100644 --- a/functions/stdio/fgetc.c +++ b/functions/stdio/fgetc.c @@ -18,11 +18,12 @@ int fgetc_unlocked( struct _PDCLIB_file_t * stream ) { return EOF; } - if ( stream->ungetidx > 0 ) - { - return (unsigned char)stream->ungetbuf[ --(stream->ungetidx) ]; - } - return (unsigned char)stream->buffer[stream->bufidx++]; + + char c; + + size_t n = _PDCLIB_getchars( &c, 1, EOF, stream ); + + return n == 0 ? EOF : (unsigned char) c; } int fgetc( struct _PDCLIB_file_t * stream ) diff --git a/functions/stdio/gets.c b/functions/stdio/gets.c index 9bf3a26..3185396 100644 --- a/functions/stdio/gets.c +++ b/functions/stdio/gets.c @@ -10,6 +10,7 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> +#include char * gets( char * s ) { @@ -18,18 +19,15 @@ char * gets( char * s ) return NULL; } char * dest = s; - while ( ( *dest = stdin->buffer[stdin->bufidx++] ) != '\n' ) - { - ++dest; - if ( stdin->bufidx == stdin->bufend ) - { - if ( _PDCLIB_fillbuffer( stdin ) == EOF ) - { - break; - } - } + + dest += _PDCLIB_getchars( dest, SIZE_MAX, '\n', stdin ); + + if(*(dest - 1) == '\n') { + *(--dest) = '\0'; + } else { + *dest = '\0'; } - *dest = '\0'; + return ( dest == s ) ? NULL : s; } -- 2.40.0