/* nothing like the original from * from Dale Schumacher's dLibs */ #include #include #include #include #include #include #include "lib.h" size_t fread(_data, size, count, fp) void *_data; size_t size; size_t count; register FILE *fp; { register size_t n; register long l, cnt; register unsigned int f; char *data=_data; char *ptr; 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)); if( f&_IOBIN ) { 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 = 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, (unsigned 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 */ if((cnt = _read(fp->_file, data, (unsigned long)n)) != n) { /* EOF or error */ fp->_flag |= ((cnt >= 0) ? _IOEOF : _IOERR); } if( cnt>0 ) l += cnt; } } } else { while( n>0 ) { if( (cnt=fp->_cnt)>0 ) { ptr=(char*)fp->_ptr; while( n>0 && cnt>0 ) { if( *ptr!='\r' ) { *data++=*ptr++; cnt--; n--; l++; } else { ptr++; cnt--; } } fp->_cnt=cnt; fp->_ptr=(unsigned char*)ptr; } if( n==0 ) break; /* done */ /* wanna have n more bytes */ if( n_bsiz ) { /* read in fp->_bsiz bytes into fp->_base and do it again */ fp->_ptr = fp->_base; if((cnt = _read(fp->_file, fp->_base, (unsigned long)fp->_bsiz)) <= 0) { /* EOF or error */ fp->_flag |= ((cnt == 0) ? _IOEOF : _IOERR); goto ret; } fp->_cnt = cnt; } else { /* read in n bytes into data */ if((cnt = _read(fp->_file, data, (unsigned long)n)) != n) { /* EOF or error */ fp->_flag |= ((cnt >= 0) ? _IOEOF : _IOERR); /* if EOF but read bytes still convert CRLF */ if( cnt<=0 ) goto ret; } ptr=data; while( cnt>0 ) { if( *ptr!='\r' ) { *data++=*ptr++; cnt--; n--; l++; } else { ptr++; cnt--; } } if( fp->_flag&_IOEOF ) goto ret; } } } ret: return((l > 0) ? ((size_t)l / size) : 0); }