home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
GEMini Atari
/
GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso
/
zip
/
mint
/
mntlib16.lzh
/
MNTLIB16
/
FREAD.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-03
|
2KB
|
75 lines
/* nothing like the original from
* Dale Schumacher's dLibs
*/
#include <stddef.h>
#include <stdio.h>
#include <limits.h>
#include <assert.h>
#include <string.h>
#include "lib.h"
size_t fread(data, size, count, fp)
register void *data;
size_t size;
size_t count;
register FILE *fp;
{
register size_t n;
register long l, cnt;
register unsigned int f;
assert((data != NULL));
assert((size != 0));
f = fp->_flag;
if(f & _IORW) f = (fp->_flag |= _IOREAD);
if(!(f & _IOREAD) || (f & (_IOERR | _IOEOF)))
return(0);
l = 0;
n = count * size;
#if 0
if(fflush(fp)) /* re-sync file pointers */
return 0;
#endif
assert((n <= (size_t)LONG_MAX));
again:
if((cnt = fp->_cnt) > 0)
{
cnt = (cnt < n) ? cnt : n;
bcopy(fp->_ptr, data, cnt);
fp->_cnt -= cnt;
fp->_ptr += cnt;
l += cnt;
data += cnt;
n -= cnt;
}
/* n == how much more */
if(n > 0)
{
if(n < fp->_bsiz)
{ /* read in fp->_bsiz bytes into fp->_base and do it again */
fp->_ptr = fp->_base;
if((cnt = _read(fp->_file, fp->_base, (long)fp->_bsiz)) <= 0)
{ /* EOF or error */
fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
goto ret;
}
fp->_cnt = cnt;
goto again;
}
else
{ /* read in n bytes into data */
cnt = _read(fp->_file, data, (long)n);
if (cnt <= 0) {
fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR);
} else {
l += cnt;
}
}
}
ret:
return((l > 0) ? ((size_t)l / size) : 0);
}