home *** CD-ROM | disk | FTP | other *** search
- static char sccs_id[] = "@(#) fread.c 3.2 " __DATE__ " HJR";
-
- /* fread.c (c) Copyright 1990 H.Rogers */
-
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
-
- extern int read (int, void *, int);
-
- __STDIOLIB__
-
- int
- __fread (register FILE * f, register char *s, int _n)
- {
- register int n, i, b, g = f->flag;
-
- if ((g & (_IOREAD | _IOERR | _IOEOF)) != _IOREAD)
- return (-1);
-
- b = (g & _IONBF) ? 1 : f->bufsiz;
-
- n = _n;
-
- while (n)
- {
- if (i = ((n > f->i_cnt) ? f->i_cnt : n)) /* read buffer */
- {
- memcpy (s, f->i_ptr, i);
- f->i_cnt -= i, f->i_ptr += i;
- n -= i, s += i;
- }
- while (n >= b) /* direct read() */
- {
- if ((i = read (f->fd, s, b)) <= 0)
- {
- f->flag |= ((i) ? _IOERR : _IOEOF);
- i = _n - n;
- return (i ? i : -1);
- }
- f->pos += i, n -= i, s += i;
- }
- if (n)
- {
- if ((i = __filbuf (f)) < 0) /* fill buffer */
- {
- i = _n - n;
- return (i ? i : -1);
- }
- --n, *s++ = i;
- }
- }
- return (_n - n);
- }
-