3 /* fwrite( void *, size_t, size_t, 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>
18 size_t fread_unlocked( void * _PDCLIB_restrict ptr,
19 size_t size, size_t nmemb,
20 struct _PDCLIB_file_t * _PDCLIB_restrict stream )
22 if ( _PDCLIB_prepread( stream ) == EOF )
26 char * dest = (char *)ptr;
28 for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
30 for ( size_t size_i = 0; size_i < size; ++size_i )
32 if ( stream->bufidx == stream->bufend )
34 if ( _PDCLIB_fillbuffer( stream ) == EOF )
36 /* Could not read requested data */
40 dest[ nmemb_i * size + size_i ] = stream->buffer[ stream->bufidx++ ];
46 size_t fread( void * _PDCLIB_restrict ptr,
47 size_t size, size_t nmemb,
48 struct _PDCLIB_file_t * _PDCLIB_restrict stream )
51 size_t r = fread_unlocked( ptr, size, nmemb, stream );
52 funlockfile( stream );
59 #include <_PDCLIB_test.h>
64 char const * message = "Testing fwrite()...\n";
67 TESTCASE( ( fh = tmpfile() ) != NULL );
68 /* fwrite() / readback */
69 TESTCASE( fwrite( message, 1, 20, fh ) == 20 );
71 TESTCASE( fread( buffer, 1, 20, fh ) == 20 );
72 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
73 TESTCASE( buffer[20] == 'x' );
74 /* same, different nmemb / size settings */
76 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
77 TESTCASE( fwrite( message, 5, 4, fh ) == 4 );
79 TESTCASE( fread( buffer, 5, 4, fh ) == 4 );
80 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
81 TESTCASE( buffer[20] == 'x' );
84 TESTCASE( memset( buffer, '\0', 20 ) == buffer );
85 TESTCASE( fwrite( message, 20, 1, fh ) == 1 );
87 TESTCASE( fread( buffer, 20, 1, fh ) == 1 );
88 TESTCASE( memcmp( buffer, message, 20 ) == 0 );
89 TESTCASE( buffer[20] == 'x' );
91 TESTCASE( fclose( fh ) == 0 );