home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 1.ddi / CLIB1.ZIP / FCLOSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  1.9 KB  |  70 lines

  1. /*------------------------------------------------------------------------
  2.  * filename - fclose.c
  3.  *
  4.  * function(s)
  5.  *        fclose - closes a stream
  6.  *-----------------------------------------------------------------------*/
  7.  
  8. /*[]------------------------------------------------------------[]*/
  9. /*|                                                              |*/
  10. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  11. /*|                                                              |*/
  12. /*|                                                              |*/
  13. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  14. /*|     All Rights Reserved.                                     |*/
  15. /*|                                                              |*/
  16. /*[]------------------------------------------------------------[]*/
  17.  
  18. #include <stdio.h>
  19. #include <alloc.h>
  20. #include <io.h>
  21. #include <_stdio.h>
  22.  
  23.  
  24. /*--------------------------------------------------------------------------*
  25.  
  26. Name        fclose - closes a stream
  27.  
  28. Usage        int fclose(FILE *stream);
  29.  
  30. Prototype in    stdio.h
  31.  
  32. Description    closes the named stream; generally, all buffers associated
  33.         with stream are flushed before closing.  System-allocated
  34.         buffers are freed upon closing.  Buffers assigned with setvbuf
  35.         and setbuf are not automatically freed.
  36.  
  37. Return value    success : 0
  38.         failure : EOF
  39.  
  40. *---------------------------------------------------------------------------*/
  41. int fclose(register FILE *fp)
  42. {
  43.     register    res = EOF;
  44.  
  45.         if (fp->token != (short) fp)  return(res);  /* invalid pointer */
  46.  
  47.     if (fp->bsize)
  48.     {
  49.                 if (fp->level < 0 && fflush (fp))  return(res);
  50.  
  51.         if (fp->flags & _F_BUF)  free (fp->buffer);
  52.     }
  53.  
  54.     if (fp->fd >= 0)  res = close (fp->fd);
  55.  
  56.     fp->flags = 0;
  57.     fp->bsize = 0;
  58.     fp->level = 0;
  59.     fp->fd = -1;
  60.  
  61.     if (fp->istemp != 0)
  62.     {
  63.         unlink(__mkname(NULL, fp->istemp));
  64.         fp->istemp = 0;
  65.     }
  66.  
  67.     return res;
  68. }
  69.  
  70.