home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / ansi / stdio / fclose.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-25  |  773 b   |  41 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <libc/file.h>
  9.  
  10. int
  11. fclose(FILE *f)
  12. {
  13.   int r;
  14.  
  15.   r = EOF;
  16.   if (!f)
  17.     return r;
  18.   if (f->_flag & (_IOREAD|_IOWRT|_IORW)
  19.       && !(f->_flag&_IOSTRG))
  20.   {
  21.     r = fflush(f);
  22.     if (close(fileno(f)) < 0)
  23.       r = EOF;
  24.     if (f->_flag&_IOMYBUF)
  25.       free(f->_base);
  26.   }
  27.   if (f->_flag & _IORMONCL && f->_name_to_remove)
  28.   {
  29.     remove(f->_name_to_remove);
  30.     free(f->_name_to_remove);
  31.     f->_name_to_remove = 0;
  32.   }
  33.   f->_cnt = 0;
  34.   f->_base = 0;
  35.   f->_ptr = 0;
  36.   f->_bufsiz = 0;
  37.   f->_flag = 0;
  38.   f->_file = -1;
  39.   return r;
  40. }
  41.