]> pd.if.org Git - pdclib/blob - functions/stdio/fwrite.c
PDCLib includes with quotes, not <>.
[pdclib] / functions / stdio / fwrite.c
1 /* fwrite( const void *, size_t, size_t, FILE * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8
9 #ifndef REGTEST
10 #include "_PDCLIB_io.h"
11 #include "_PDCLIB_glue.h"
12
13 #include <stdbool.h>
14 #include <string.h>
15
16 size_t _PDCLIB_fwrite_unlocked( const void *restrict vptr,
17                size_t size, size_t nmemb,
18                FILE * _PDCLIB_restrict stream )
19 {
20     if ( _PDCLIB_prepwrite( stream ) == EOF )
21     {
22         return 0;
23     }
24
25     const char *restrict ptr = vptr;
26     size_t nmemb_i;
27     for ( nmemb_i = 0; nmemb_i < nmemb; ++nmemb_i )
28     {
29         for ( size_t size_i = 0; size_i < size; ++size_i )
30         {
31             char c = ptr[ nmemb_i * size + size_i ];
32             stream->buffer[ stream->bufidx++ ] = c;
33
34             if ( stream->bufidx == stream->bufsize || ( c == '\n' && stream->status & _IOLBF ) )
35             {
36                 if ( _PDCLIB_flushbuffer( stream ) == EOF )
37                 {
38                     /* Returning number of objects completely buffered */
39                     return nmemb_i;
40                 }
41             }
42         }
43
44         if ( stream->status & _IONBF )
45         {
46             if ( _PDCLIB_flushbuffer( stream ) == EOF )
47             {
48                 /* Returning number of objects completely buffered */
49                 return nmemb_i;
50             }
51         }
52     }
53     return nmemb_i;
54 }
55
56 size_t fwrite( const void * _PDCLIB_restrict ptr,
57                size_t size, size_t nmemb,
58                FILE * _PDCLIB_restrict stream )
59 {
60     _PDCLIB_flockfile( stream );
61     size_t r = _PDCLIB_fwrite_unlocked( ptr, size, nmemb, stream );
62     _PDCLIB_funlockfile( stream );
63     return r;
64 }
65
66 #endif
67
68 #ifdef TEST
69 #include "_PDCLIB_test.h"
70
71 int main( void )
72 {
73     /* Testing covered by fread(). */
74     return TEST_RESULTS;
75 }
76
77 #endif
78