update for HEAD-2003091401
[reactos.git] / lib / crtdll / stdio / fwrite.c
1 #include <msvcrt/stdio.h>
2 #include <msvcrt/stdlib.h>
3 #include <msvcrt/string.h>
4 #include <msvcrt/errno.h>
5 #include <msvcrt/internal/file.h>
6
7 /*
8  * @implemented
9  */
10 #if 0
11 size_t
12 fwrite(const void *p, size_t size, size_t count, FILE *iop)
13 {
14   char *ptr = (char *)p;
15   size_t to_write;
16
17
18   to_write = size * count;
19  
20
21
22   while ( to_write > 0 ) {
23         if ( putc(*ptr,iop) == EOF )
24                 break;
25         to_write--;
26         ptr++;
27   }
28         
29
30
31   return count -to_write/size;
32  
33 }
34
35
36 #else
37 size_t fwrite(const void *vptr, size_t size, size_t count, FILE *iop)
38  {
39         size_t to_write, n_written;
40         char *ptr = (char *)vptr;
41         
42         to_write = size*count;
43         if (!OPEN4WRITING(iop) )
44         {
45                 __set_errno (EINVAL);
46                 return 0;
47         }
48
49
50         if (iop == NULL  )
51         {
52                 __set_errno (EINVAL);
53                 return 0;
54         }
55
56         if (ferror (iop))
57                 return 0;
58         if (vptr == NULL || to_write == 0)
59                 return 0;
60
61         
62         while(iop->_cnt > 0 && to_write > 0 ) {     
63                 to_write--;
64                 putc(*ptr++,iop);
65         }
66
67         // if the buffer is dirty it will have to be written now
68         // otherwise the file pointer won't match anymore.
69   
70         fflush(iop);       
71       
72         n_written = _write(fileno(iop), ptr,to_write);
73         if ( n_written != -1 )
74                 to_write -= n_written;
75         
76         // check to see if this will work with in combination with ungetc
77
78
79         // the file buffer is empty and there is no read ahead information anymore.
80         
81         iop->_flag &= ~_IOAHEAD;
82         
83         return count - (to_write/size);      
84   
85 }
86
87 #endif