X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstdio%2Ffgets.c;h=8752892bedc1a26103fe375089a5fff82b4b111b;hp=44518a3e0b0f2552a8d5b1a47c81fb7fd3bf37f0;hb=da0f3f353d417fed71f358a48d5d5394145e460d;hpb=b41576197133c1211d6ec353faf93f505f573b8a diff --git a/functions/stdio/fgets.c b/functions/stdio/fgets.c index 44518a3..8752892 100644 --- a/functions/stdio/fgets.c +++ b/functions/stdio/fgets.c @@ -1,5 +1,3 @@ -/* $Id$ */ - /* fgets( char *, int, FILE * ) This file is part of the Public Domain C Library (PDCLib). @@ -9,10 +7,9 @@ #include #ifndef REGTEST +#include "_PDCLIB_io.h" -#include <_PDCLIB_glue.h> - -char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream ) +char * _PDCLIB_fgets_unlocked( char * _PDCLIB_restrict s, int size, FILE * _PDCLIB_restrict stream ) { if ( size == 0 ) { @@ -28,28 +25,26 @@ char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCL return NULL; } char * dest = s; - while ( ( ( *dest++ = stream->buffer[stream->bufidx++] ) != '\n' ) && --size > 0 ) - { - if ( stream->bufidx == stream->bufend ) - { - if ( _PDCLIB_fillbuffer( stream ) == EOF ) - { - /* In case of error / EOF before a character is read, this - will lead to a \0 be written anyway. Since the results - are "indeterminate" by definition, this does not hurt. - */ - break; - } - } - } + + dest += _PDCLIB_getchars( dest, size - 1, '\n', stream ); + *dest = '\0'; return ( dest == s ) ? NULL : s; } +char * fgets( char * _PDCLIB_restrict s, int size, + FILE * _PDCLIB_restrict stream ) +{ + _PDCLIB_flockfile( stream ); + char* r = _PDCLIB_fgets_unlocked( s, size, stream ); + _PDCLIB_funlockfile( stream ); + return r; +} + #endif #ifdef TEST -#include <_PDCLIB_test.h> +#include "_PDCLIB_test.h" #include int main( void )