1 /* fgets( char *, int, FILE * )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
10 #include "_PDCLIB_io.h"
12 char * _PDCLIB_fgets_unlocked( char * _PDCLIB_restrict s, int size, FILE * _PDCLIB_restrict stream )
23 if ( _PDCLIB_prepread( stream ) == EOF )
29 dest += _PDCLIB_getchars( dest, size - 1, '\n', stream );
32 return ( dest == s ) ? NULL : s;
35 char * fgets( char * _PDCLIB_restrict s, int size,
36 FILE * _PDCLIB_restrict stream )
38 _PDCLIB_flockfile( stream );
39 char* r = _PDCLIB_fgets_unlocked( s, size, stream );
40 _PDCLIB_funlockfile( stream );
47 #include "_PDCLIB_test.h"
54 char const * fgets_test = "foo\nbar\0baz\nweenie";
55 TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL );
56 TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 );
58 TESTCASE( fgets( buffer, 10, fh ) == buffer );
59 TESTCASE( strcmp( buffer, "foo\n" ) == 0 );
60 TESTCASE( fgets( buffer, 10, fh ) == buffer );
61 TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 );
62 TESTCASE( fgets( buffer, 10, fh ) == buffer );
63 TESTCASE( strcmp( buffer, "weenie" ) == 0 );
64 TESTCASE( feof( fh ) );
65 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
66 TESTCASE( fgets( buffer, 1, fh ) == buffer );
67 TESTCASE( strcmp( buffer, "" ) == 0 );
68 TESTCASE( fgets( buffer, 0, fh ) == NULL );
69 TESTCASE( ! feof( fh ) );
70 TESTCASE( fgets( buffer, 1, fh ) == buffer );
71 TESTCASE( strcmp( buffer, "" ) == 0 );
72 TESTCASE( ! feof( fh ) );
73 TESTCASE( fgets( buffer, 2, fh ) == buffer );
74 TESTCASE( strcmp( buffer, "e" ) == 0 );
75 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
76 TESTCASE( fgets( buffer, 2, fh ) == NULL );
77 TESTCASE( feof( fh ) );
78 TESTCASE( fclose( fh ) == 0 );
79 TESTCASE( remove( testfile ) == 0 );