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>
10 size_t fwrite(const void *vptr, size_t size, size_t count, FILE *iop)
12 size_t to_write, n_written;
13 unsigned char *ptr = (unsigned char *)vptr;
16 DPRINT("fwrite(%x, %d, %d, %x)\n", vptr, size, count, iop);
18 to_write = size*count;
19 if (!OPEN4WRITING(iop))
33 if (vptr == NULL || to_write == 0)
36 if (iop->_base == NULL && !(iop->_flag&_IONBF))
38 if (EOF == _flsbuf(*ptr++, iop))
44 if (iop->_flag & _IOLBF)
48 if (EOF == putc(*ptr++, iop))
58 if (iop->_cnt > 0 && to_write > 0)
60 copy = min(iop->_cnt, to_write);
61 memcpy(iop->_ptr, ptr, copy);
66 iop->_flag |= _IODIRTY;
71 // if the buffer is dirty it will have to be written now
72 // otherwise the file pointer won't match anymore.
74 if (to_write >= iop->_bufsiz)
78 n_written = _write(fileno(iop), ptr, to_write);
84 to_write -= n_written;
88 // check to see if this will work with in combination with ungetc
90 // the file buffer is empty and there is no read ahead information anymore.
91 iop->_flag &= ~_IOAHEAD;
95 if (EOF != _flsbuf(*ptr++, iop))
99 memcpy(iop->_ptr, ptr, to_write);
100 iop->_ptr += to_write;
101 iop->_cnt -= to_write;
102 iop->_flag |= _IODIRTY;
110 return count - (to_write/size);