home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- *
- * $Source: /unixb/home/unixlib/source/unixlib37/src/stdio/c/RCS/fread,v $
- * $Date: 1996/05/06 09:01:34 $
- * $Revision: 1.2 $
- * $State: Rel $
- * $Author: unixlib $
- *
- * $Log: fread,v $
- * Revision 1.2 1996/05/06 09:01:34 unixlib
- * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
- * Saved for 3.7a release.
- *
- * Revision 1.1 1996/04/19 21:32:42 simon
- * Initial revision
- *
- ***************************************************************************/
-
- static const char rcs_id[] = "$Id: fread,v 1.2 1996/05/06 09:01:34 unixlib Rel $";
-
- #include <stdlib.h>
- #include <string.h>
- #include <stdio.h>
- #include <unistd.h>
-
- __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);
- }
-