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>
7 #include <msvcrt/msvcrtdbg.h>
13 size_t fwrite(const void *vptr, size_t size, size_t count, FILE *iop)
15 size_t to_write, n_written;
16 unsigned char *ptr = (unsigned char *)vptr;
19 DPRINT("fwrite(%x, %d, %d, %x)\n", vptr, size, count, iop);
21 to_write = size*count;
22 if (!OPEN4WRITING(iop))
36 if (vptr == NULL || to_write == 0)
39 if (iop->_base == NULL && !(iop->_flag&_IONBF))
41 if (EOF == _flsbuf(*ptr++, iop))
47 if (iop->_flag & _IOLBF)
51 if (EOF == putc(*ptr++, iop))
61 if (iop->_cnt > 0 && to_write > 0)
63 copy = min(iop->_cnt, to_write);
64 memcpy(iop->_ptr, ptr, copy);
69 iop->_flag |= _IODIRTY;
74 // if the buffer is dirty it will have to be written now
75 // otherwise the file pointer won't match anymore.
77 if (to_write >= iop->_bufsiz)
81 n_written = _write(fileno(iop), ptr, to_write);
87 to_write -= n_written;
91 // check to see if this will work with in combination with ungetc
93 // the file buffer is empty and there is no read ahead information anymore.
94 iop->_flag &= ~_IOAHEAD;
98 if (EOF != _flsbuf(*ptr++, iop))
102 memcpy(iop->_ptr, ptr, to_write);
103 iop->_ptr += to_write;
104 iop->_cnt -= to_write;
105 iop->_flag |= _IODIRTY;
113 return count - (to_write/size);