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

  1.  
  2. /*
  3.  *  FFLUSH.C
  4.  *
  5.  *  (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  fflush is also called by other stdio routines to put everything into
  8.  *  a known state.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14. int
  15. fflush(fi)
  16. FILE *fi;
  17. {
  18.     int n;
  19.     int error = 0;
  20.  
  21.     /*
  22.      *    note, if we need to allocate a buffer we are guarenteed not to be in
  23.      *    read or write mode.
  24.      */
  25.  
  26.     chkabort();
  27.     if (fi && (fi->sd_Flags & __SIF_OPEN)) {
  28.     fi->sd_UC = -1;
  29.     if (fi->sd_BufSiz && fi->sd_WBuf == NULL) {
  30.         fi->sd_WBuf = malloc(fi->sd_BufSiz);
  31.         fi->sd_Flags |= __SIF_MYBUF;
  32.         if (fi->sd_WBuf == NULL)     /*  if couldn't malloc, make unbuf'd */
  33.         fi->sd_BufSiz = 0;
  34.         fi->sd_WLeft = fi->sd_BufSiz;
  35.     }
  36.  
  37.     /*
  38.      *  if a normal file reset the read buffer
  39.      */
  40.  
  41.     if (fi->sd_RLeft >= 0 && (fi->sd_Flags & __SIF_FILE)) {
  42.         if (fi->sd_RLeft > 0)       /*  seek to the right place */
  43.         fi->sd_Offset = lseek(fi->sd_Fd, fi->sd_Offset - fi->sd_RLeft, 0);
  44.         fi->sd_RLeft = -1;
  45.         /*fi->sd_WLeft = fi->sd_BufSiz;*/
  46.     }
  47.     if (fi->sd_WLeft >= 0 && (n = fi->sd_BufSiz - fi->sd_WLeft)) {
  48.         if (write(fi->sd_Fd, fi->sd_WBuf, n) != n)
  49.         fi->sd_Error = EOF;
  50.         fi->sd_Offset += n;
  51.         fi->sd_WLeft = fi->sd_BufSiz;
  52.     }
  53.     if (fi->sd_WLeft < 0 && fi->sd_BufSiz)
  54.         fi->sd_WLeft = fi->sd_BufSiz;
  55.     fi->sd_WPtr = fi->sd_WBuf;
  56.     }
  57.     return(error);
  58. }
  59.  
  60.  
  61.