3 /* fgets( char *, int, FILE * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
13 #include <_PDCLIB_glue.h>
15 char * fgets_unlocked( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
26 if ( _PDCLIB_prepread( stream ) == EOF )
31 while ( ( ( *dest++ = stream->buffer[stream->bufidx++] ) != '\n' ) && --size > 0 )
33 if ( stream->bufidx == stream->bufend )
35 if ( _PDCLIB_fillbuffer( stream ) == EOF )
37 /* In case of error / EOF before a character is read, this
38 will lead to a \0 be written anyway. Since the results
39 are "indeterminate" by definition, this does not hurt.
46 return ( dest == s ) ? NULL : s;
49 char * fgets( char * _PDCLIB_restrict s, int size,
50 struct _PDCLIB_file_t * _PDCLIB_restrict stream )
53 char* r = fgets_unlocked( s, size, stream );
54 funlockfile( stream );
61 #include <_PDCLIB_test.h>
68 char const * fgets_test = "foo\nbar\0baz\nweenie";
69 TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL );
70 TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 );
72 TESTCASE( fgets( buffer, 10, fh ) == buffer );
73 TESTCASE( strcmp( buffer, "foo\n" ) == 0 );
74 TESTCASE( fgets( buffer, 10, fh ) == buffer );
75 TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 );
76 TESTCASE( fgets( buffer, 10, fh ) == buffer );
77 TESTCASE( strcmp( buffer, "weenie" ) == 0 );
78 TESTCASE( feof( fh ) );
79 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
80 TESTCASE( fgets( buffer, 1, fh ) == buffer );
81 TESTCASE( strcmp( buffer, "" ) == 0 );
82 TESTCASE( fgets( buffer, 0, fh ) == NULL );
83 TESTCASE( ! feof( fh ) );
84 TESTCASE( fgets( buffer, 1, fh ) == buffer );
85 TESTCASE( strcmp( buffer, "" ) == 0 );
86 TESTCASE( ! feof( fh ) );
87 TESTCASE( fgets( buffer, 2, fh ) == buffer );
88 TESTCASE( strcmp( buffer, "e" ) == 0 );
89 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
90 TESTCASE( fgets( buffer, 2, fh ) == NULL );
91 TESTCASE( feof( fh ) );
92 TESTCASE( fclose( fh ) == 0 );
93 TESTCASE( remove( testfile ) == 0 );