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 #define _PDCLIB_GLUE_H _PDCLIB_GLUE_H
14 #include <_PDCLIB_glue.h>
16 char * fgets( char * _PDCLIB_restrict s, int size, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
27 if ( _PDCLIB_prepread( stream ) == EOF )
32 while ( ( ( *dest++ = stream->buffer[stream->bufidx++] ) != '\n' ) && --size > 0 )
34 if ( stream->bufidx == stream->bufend )
36 if ( _PDCLIB_fillbuffer( stream ) == EOF )
38 /* In case of error / EOF before a character is read, this
39 will lead to a \0 be written anyway. Since the results
40 are "indeterminate" by definition, this does not hurt.
47 return ( dest == s ) ? NULL : s;
53 #include <_PDCLIB_test.h>
60 char const * fgets_test = "foo\nbar\0baz\nweenie";
61 TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL );
62 TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 );
64 TESTCASE( fgets( buffer, 10, fh ) == buffer );
65 TESTCASE( strcmp( buffer, "foo\n" ) == 0 );
66 TESTCASE( fgets( buffer, 10, fh ) == buffer );
67 TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 );
68 TESTCASE( fgets( buffer, 10, fh ) == buffer );
69 TESTCASE( strcmp( buffer, "weenie" ) == 0 );
70 TESTCASE( feof( fh ) );
71 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
72 TESTCASE( fgets( buffer, 1, fh ) == buffer );
73 TESTCASE( strcmp( buffer, "" ) == 0 );
74 TESTCASE( fgets( buffer, 0, fh ) == NULL );
75 TESTCASE( ! feof( fh ) );
76 TESTCASE( fgets( buffer, 1, fh ) == buffer );
77 TESTCASE( strcmp( buffer, "" ) == 0 );
78 TESTCASE( ! feof( fh ) );
79 TESTCASE( fgets( buffer, 2, fh ) == buffer );
80 TESTCASE( strcmp( buffer, "e" ) == 0 );
81 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
82 TESTCASE( fgets( buffer, 2, fh ) == NULL );
83 TESTCASE( feof( fh ) );
84 TESTCASE( fclose( fh ) == 0 );
85 TESTCASE( remove( testfile ) == 0 );