home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 387b.lha / dice_v2.02 / lib / stdio / filbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-05-30  |  1.1 KB  |  58 lines

  1.  
  2. /*
  3.  *  _FILBUF.C
  4.  *
  5.  *  (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <errno.h>
  11.  
  12. int
  13. _filbuf(fi)
  14. FILE *fi;
  15. {
  16.     int n;
  17.     int error = 0;
  18.  
  19.     chkabort();
  20.     if (fi && (fi->sd_Flags & __SIF_OPEN) && fi->sd_RLeft <= 0) {
  21.     /*
  22.      *  if a normal file flush pending write data
  23.      */
  24.     if (fi->sd_WLeft >= 0 && (fi->sd_Flags & __SIF_FILE)) {
  25.         fflush(fi);
  26.         fi->sd_WLeft = -1;
  27.         fi->sd_RLeft = 0;
  28.     }
  29.     if (fi->sd_BufSiz && fi->sd_RBuf == NULL) {
  30.         fi->sd_RBuf = malloc(fi->sd_BufSiz);
  31.         fi->sd_Flags |= __SIF_MYBUF;
  32.         if (fi->sd_RBuf == NULL)     /*  if couldn't malloc, make unbuf'd */
  33.         fi->sd_BufSiz = 0;
  34.     }
  35.     if (fi->sd_BufSiz) {            /*  else we might be unbuffered!     */
  36.         n = read(fi->sd_Fd, fi->sd_RBuf, fi->sd_BufSiz);
  37.         if (n < 0) {
  38.         if (errno != EAGAIN) {
  39.             fi->sd_Error = EOF;
  40.             error = EOF;
  41.         }
  42.         } else {
  43.         fi->sd_RLeft = n;
  44.         fi->sd_Offset += n;
  45.         if (n == 0) {
  46.             error = EOF;
  47.             fi->sd_Flags |= __SIF_EOF;
  48.         } else {
  49.             fi->sd_Flags &= ~__SIF_EOF;
  50.         }
  51.         }
  52.     }
  53.     fi->sd_RPtr = fi->sd_RBuf;
  54.     }
  55.     return(error);
  56. }
  57.  
  58.