vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

fwrite.c (783B)


      1 
      2 #include <string.h>
      3 
      4 #include "ioprivate.h"
      5 
      6 
      7 size_t fwrite(const void *restrict buf, size_t eltsize, size_t nelts,
      8 		FILE *restrict f)
      9 {
     10 	size_t totsize = eltsize * nelts;
     11 
     12 	// Check for space in the output buffer.
     13 	if (f->opos + totsize > f->omax) {
     14 
     15 		// Create/flush the output buffer.
     16 		if (__getospace(f) < 0)
     17 			return EOF;
     18 
     19 		if (totsize >= f->omax) {
     20 
     21 			// Bigger than the buffer - just write it directly.
     22 			if (__writebuf(f, buf, totsize) < 0)
     23 				return 0;
     24 			return nelts;
     25 		}
     26 	}
     27 
     28 	// Copy the data to the output buffer.
     29 	memcpy(&f->obuf[f->opos], buf, totsize);
     30 	f->opos += totsize;
     31 
     32 	// Flush the buffer if appropriate.
     33 	if ((f->bufmode == _IOLBF && memchr(buf, '\n', totsize)) ||
     34 			(f->bufmode == _IONBF)) {
     35 		if (fflush(f) < 0)
     36 			return 0;
     37 	}
     38 
     39 	return nelts;
     40 }
     41