Stub out most of stdio on AltOS
[fw/pdclib] / platform / altos / functions / stdio / fread.c
1 /* $Id$ */
2
3 /* fwrite( void *, size_t, size_t, FILE * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10
11 #ifndef REGTEST
12
13 #include <_PDCLIB_glue.h>
14
15 #include <stdbool.h>
16 #include <string.h>
17
18 size_t fread( void * _PDCLIB_restrict ptr, size_t size, size_t nmemb, struct _PDCLIB_file_t * _PDCLIB_restrict stream )
19 {
20     char * dest = (char *)ptr;
21     size_t nmemb_i;
22     for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
23     {
24         for ( size_t size_i = 0; size_i < size; ++size_i )
25         {
26             int c = getc(stream);
27             if (c == EOF)
28             {
29                 /* Could not read requested data */
30                 return nmemb_i;
31             }
32             *dest++ = c;
33         }
34     }
35     return nmemb_i;
36 }
37
38 #endif
39
40 #ifdef TEST
41 #include <_PDCLIB_test.h>
42
43 int main( void )
44 {
45     FILE * fh;
46     char const * message = "Testing fwrite()...\n";
47     char buffer[21];
48     buffer[20] = 'x';
49     TESTCASE( ( fh = tmpfile() ) != NULL );
50     /* fwrite() / readback */
51     TESTCASE( fwrite( message, 1, 20, fh ) == 20 );
52     rewind( fh );
53     TESTCASE( fread( buffer, 1, 20, fh ) == 20 );
54     TESTCASE( memcmp( buffer, message, 20 ) == 0 );
55     TESTCASE( buffer[20] == 'x' );
56     /* same, different nmemb / size settings */
57     rewind( fh );
58     TESTCASE( memset( buffer, '\0', 20 ) == buffer );
59     TESTCASE( fwrite( message, 5, 4, fh ) == 4 );
60     rewind( fh );
61     TESTCASE( fread( buffer, 5, 4, fh ) == 4 );
62     TESTCASE( memcmp( buffer, message, 20 ) == 0 );
63     TESTCASE( buffer[20] == 'x' );
64     /* same... */
65     rewind( fh );
66     TESTCASE( memset( buffer, '\0', 20 ) == buffer );
67     TESTCASE( fwrite( message, 20, 1, fh ) == 1 );
68     rewind( fh );
69     TESTCASE( fread( buffer, 20, 1, fh ) == 1 );
70     TESTCASE( memcmp( buffer, message, 20 ) == 0 );
71     TESTCASE( buffer[20] == 'x' );
72     /* Done. */
73     TESTCASE( fclose( fh ) == 0 );
74     return TEST_RESULTS;
75 }
76
77 #endif
78