home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 1.ddi / CLIBSRC1.ZIP / FCLOSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  1.8 KB  |  71 lines

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