home *** CD-ROM | disk | FTP | other *** search
/ CD Shareware Magazine 1996 December / CD_shareware_12-96.iso / DOS / Programa / CCDL122.ZIP / CLIBS / IO / FCLOSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-27  |  1.2 KB  |  66 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <libp.h>
  5.  
  6. extern int _abterm;
  7.  
  8. FILE *_pstreams[_NFILE_];
  9. char *_filenames[_NFILE_];
  10. int maxfiles;
  11.  
  12. #pragma startup fileinit 120
  13. #pragma rundown closeall 10
  14.  
  15. void fileinit(void)
  16. {
  17.     _ll_init();
  18. }
  19.  
  20. void closeall(void)
  21. {
  22.     int i;
  23.     if (!_abterm)
  24.         for (i=0; i < maxfiles; i++) {
  25.             fflush(_pstreams[i]);
  26.             fclose(_pstreams[i]);
  27.         }
  28. }
  29. int _basefclose(FILE *stream,int release)
  30. {
  31.     int rv,i;
  32.     if (stream->token == FILTOK && maxfiles) {
  33.         int tempflag = stream->istemp;
  34.         char *fname;
  35.         fflush(stream);
  36.         stream->token = (short)-1;
  37.         if (maxfiles > 1) {
  38.             for (i=0; i < maxfiles; i++)
  39.                 if (_pstreams[i] == stream) {
  40.                     fname = _filenames[i];
  41.                     _pstreams[i] = _pstreams[maxfiles-1];
  42.                     _filenames[i] = _filenames[maxfiles-1];
  43.                 }
  44.             maxfiles--;
  45.         }
  46.         rv = _ll_close(stream->fd);
  47.         if (tempflag && fname)
  48.             rv &= remove(fname);
  49.         if (fname)
  50.             free(fname);
  51.         if (stream->flags & _F_BUF)
  52.             free(stream->buffer);
  53.         if (release)
  54.             free(stream);
  55.         if (!rv)
  56.             return 0;
  57.         else {
  58.             return EOF;
  59.         }
  60.     }
  61.     else return EOF;
  62. }
  63. int fclose(FILE *stream)
  64. {
  65.     return _basefclose(stream,1);
  66. }