1 /* fwrite( void *, size_t, size_t, 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>
15 size_t _PDCLIB_fread_unlocked(
16 void * _PDCLIB_restrict ptr,
17 size_t size, size_t nmemb,
18 FILE * _PDCLIB_restrict stream
21 if ( _PDCLIB_prepread( stream ) == EOF )
25 char * dest = (char *)ptr;
27 for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
29 size_t numread = _PDCLIB_getchars( &dest[ nmemb_i * size ], size, EOF,
37 size_t fread( void * _PDCLIB_restrict ptr,
38 size_t size, size_t nmemb,
39 FILE * _PDCLIB_restrict stream )
41 _PDCLIB_flockfile( stream );
42 size_t r = _PDCLIB_fread_unlocked( ptr, size, nmemb, stream );
43 _PDCLIB_funlockfile( stream );
50 #include <_PDCLIB_test.h>
55 char const * message = "Testing fwrite()...\n";
58 TESTCASE( ( fh = tmpfile() ) != NULL );
59 /* fwrite() / readback */
60 TESTCASE( fwrite( message, 1, 20, fh ) == 20 );
62 TESTCASE( fread( buffer, 1, 20, fh ) == 20 );
63 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
64 TESTCASE( buffer[20] == 'x' );
65 /* same, different nmemb / size settings */
67 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
68 TESTCASE( fwrite( message, 5, 4, fh ) == 4 );
70 TESTCASE( fread( buffer, 5, 4, fh ) == 4 );
71 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
72 TESTCASE( buffer[20] == 'x' );
75 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
76 TESTCASE( fwrite( message, 20, 1, fh ) == 1 );
78 TESTCASE( fread( buffer, 20, 1, fh ) == 1 );
79 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
80 TESTCASE( buffer[20] == 'x' );
82 TESTCASE( fclose( fh ) == 0 );