home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 2 / DATAFILE_PDCD2.iso / utilities2 / unixlib36d / UnixLib36d / src / stdio / c / filbuf < prev    next >
Encoding:
Text File  |  1994-03-08  |  806 b   |  49 lines

  1. static char sccs_id[] = "@(#) filbuf.c 2.1 " __DATE__ " HJR";
  2.  
  3. /* filbuf.c (c) Copyright 1990 H.Rogers */
  4.  
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. #include "fcntl.h"
  9.  
  10. extern int read (int, void *, int);
  11.  
  12. __STDIOLIB__
  13.  
  14. int
  15. __filbuf (register FILE * f)
  16. {
  17.   register int n, b, g = f->flag;
  18.   register unsigned char *a;
  19.  
  20.   if ((g & (_IOREAD | _IOERR | _IOEOF)) != _IOREAD)
  21.     return (-1);
  22.  
  23.   b = (g & _IONBF) ? 1 : f->bufsiz;
  24.  
  25.   if (!(a = f->i_base))
  26.     {
  27.       if (!(a = f->i_base = malloc (b + 1)))
  28.     {
  29.       f->flag = g | _IOERR;
  30.       return (-1);
  31.     }
  32.     }
  33.  
  34.   ++a;                /* for ungetc() */
  35.  
  36.   if ((n = read (f->fd, a, b)) <= 0)
  37.     {
  38.       f->flag |= ((n) ? _IOERR : _IOEOF);
  39.       f->i_cnt = 0;
  40.       f->i_ptr = a;
  41.       return (-1);
  42.     }
  43.  
  44.   f->pos += n;
  45.   f->i_cnt = n - 1;
  46.   f->i_ptr = a + 1;
  47.   return (*a);
  48. }
  49.