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 )
32 dest += _PDCLIB_getchars( dest, size - 1, '\n', stream );
35 return ( dest == s ) ? NULL : s;
38 char * fgets( char * _PDCLIB_restrict s, int size,
39 struct _PDCLIB_file_t * _PDCLIB_restrict stream )
42 char* r = fgets_unlocked( s, size, stream );
43 funlockfile( stream );
50 #include <_PDCLIB_test.h>
57 char const * fgets_test = "foo\nbar\0baz\nweenie";
58 TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL );
59 TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 );
61 TESTCASE( fgets( buffer, 10, fh ) == buffer );
62 TESTCASE( strcmp( buffer, "foo\n" ) == 0 );
63 TESTCASE( fgets( buffer, 10, fh ) == buffer );
64 TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 );
65 TESTCASE( fgets( buffer, 10, fh ) == buffer );
66 TESTCASE( strcmp( buffer, "weenie" ) == 0 );
67 TESTCASE( feof( fh ) );
68 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
69 TESTCASE( fgets( buffer, 1, fh ) == buffer );
70 TESTCASE( strcmp( buffer, "" ) == 0 );
71 TESTCASE( fgets( buffer, 0, fh ) == NULL );
72 TESTCASE( ! feof( fh ) );
73 TESTCASE( fgets( buffer, 1, fh ) == buffer );
74 TESTCASE( strcmp( buffer, "" ) == 0 );
75 TESTCASE( ! feof( fh ) );
76 TESTCASE( fgets( buffer, 2, fh ) == buffer );
77 TESTCASE( strcmp( buffer, "e" ) == 0 );
78 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
79 TESTCASE( fgets( buffer, 2, fh ) == NULL );
80 TESTCASE( feof( fh ) );
81 TESTCASE( fclose( fh ) == 0 );
82 TESTCASE( remove( testfile ) == 0 );