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.
12 #include <_PDCLIB_io.h>
14 char * fgets_unlocked( char * _PDCLIB_restrict s, int size, FILE * _PDCLIB_restrict stream )
25 if ( _PDCLIB_prepread( stream ) == EOF )
31 dest += _PDCLIB_getchars( dest, size - 1, '\n', stream );
34 return ( dest == s ) ? NULL : s;
37 char * fgets( char * _PDCLIB_restrict s, int size,
38 FILE * _PDCLIB_restrict stream )
41 char* r = fgets_unlocked( s, size, stream );
42 funlockfile( stream );
49 #include <_PDCLIB_test.h>
56 char const * fgets_test = "foo\nbar\0baz\nweenie";
57 TESTCASE( ( fh = fopen( testfile, "wb+" ) ) != NULL );
58 TESTCASE( fwrite( fgets_test, 1, 18, fh ) == 18 );
60 TESTCASE( fgets( buffer, 10, fh ) == buffer );
61 TESTCASE( strcmp( buffer, "foo\n" ) == 0 );
62 TESTCASE( fgets( buffer, 10, fh ) == buffer );
63 TESTCASE( memcmp( buffer, "bar\0baz\n", 8 ) == 0 );
64 TESTCASE( fgets( buffer, 10, fh ) == buffer );
65 TESTCASE( strcmp( buffer, "weenie" ) == 0 );
66 TESTCASE( feof( fh ) );
67 TESTCASE( fseek( fh, -1, SEEK_END ) == 0 );
68 TESTCASE( fgets( buffer, 1, fh ) == buffer );
69 TESTCASE( strcmp( buffer, "" ) == 0 );
70 TESTCASE( fgets( buffer, 0, fh ) == NULL );
71 TESTCASE( ! feof( fh ) );
72 TESTCASE( fgets( buffer, 1, fh ) == buffer );
73 TESTCASE( strcmp( buffer, "" ) == 0 );
74 TESTCASE( ! feof( fh ) );
75 TESTCASE( fgets( buffer, 2, fh ) == buffer );
76 TESTCASE( strcmp( buffer, "e" ) == 0 );
77 TESTCASE( fseek( fh, 0, SEEK_END ) == 0 );
78 TESTCASE( fgets( buffer, 2, fh ) == NULL );
79 TESTCASE( feof( fh ) );
80 TESTCASE( fclose( fh ) == 0 );
81 TESTCASE( remove( testfile ) == 0 );