반응형
size_t
fread(void *buf, size_t size, size_t count, FILE *fp)
#include <stdio.h> #include <string.h> #include "local.h" size_t fread(void *buf, size_t size, size_t count, FILE *fp) { size_t resid; char *p; int r; size_t total; /* * The ANSI standard requires a return value of 0 for a count * or a size of 0. Peculiarily, it imposes no such requirements * on fwrite; it only requires fread to be broken. */ if ((resid = count * size) == 0) return (0); if (fp->_r < 0) fp->_r = 0; total = resid; p = buf; while (resid > (r = fp->_r)) { (void)memcpy((void *)p, (void *)fp->_p, (size_t)r); fp->_p += r; /* fp->_r = 0 ... done in __srefill */ p += r; resid -= r; if (__srefill(fp)) { /* no more input: return partial result */ return ((total - resid) / size); } } (void)memcpy((void *)p, (void *)fp->_p, resid); fp->_r -= resid; fp->_p += resid; return (count); }
반응형