home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_01 / 1001014d < prev    next >
Text File  |  1991-11-18  |  849b  |  47 lines

  1.  
  2. Listing 5 -- the file fclose.c
  3.  
  4.  
  5. /* fclose function */
  6. #include <stdlib.h>
  7. #include "xstdio.h"
  8. #include "yfuns.h"
  9.  
  10. int (fclose)(FILE *str)
  11.     {    /* close a stream */
  12.     int stat = fflush(str);
  13.  
  14.     if (str->_Mode & _MALBUF)
  15.         free(str->_Buf);
  16.     str->_Buf = NULL;
  17.     if (0 <= str->_Handle && _Fclose(str))
  18.         stat = EOF;
  19.     if (str->_Tmpnam)
  20.         {    /* remove temp file */
  21.         if (remove(str->_Tmpnam))
  22.             stat = EOF;
  23.         free(str->_Tmpnam);
  24.         str->_Tmpnam = NULL;
  25.         }
  26.     str->_Mode = 0;
  27.     str->_Next = &str->_Cbuf;
  28.     str->_Rend = &str->_Cbuf;
  29.     str->_Wend = &str->_Cbuf;
  30.     str->_Nback = 0;
  31.     if (str->_Mode & _MALFIL)
  32.         {    /* find _Files[i] entry and free */
  33.         size_t i;
  34.  
  35.         for (i = 0; i < FOPEN_MAX; ++i)
  36.             if (_Files[i] == str)
  37.                 {    /* found entry */
  38.                 _Files[i] = NULL;
  39.                 break;
  40.                 }
  41.         free(str);
  42.         }
  43.     return (stat);
  44.     }
  45.  
  46.  
  47.