Pristine Ack-5.5
[Ack-5.5.git] / lang / cem / libcc.ansi / stdio / fclose.c
1 /*
2  * fclose.c - flush a stream and close the file
3  */
4 /* $Id: fclose.c,v 1.5 1994/06/24 11:48:38 ceriel Exp $ */
5
6 #include        <stdio.h>
7 #include        <stdlib.h>
8 #include        "loc_incl.h"
9
10 int _close(int d);
11
12 int
13 fclose(FILE *fp)
14 {
15         register int i, retval = 0;
16
17         for (i=0; i<FOPEN_MAX; i++)
18                 if (fp == __iotab[i]) {
19                         __iotab[i] = 0;
20                         break;
21                 }
22         if (i >= FOPEN_MAX)
23                 return EOF;
24         if (fflush(fp)) retval = EOF;
25         if (_close(fileno(fp))) retval = EOF;
26         if ( io_testflag(fp,_IOMYBUF) && fp->_buf )
27                 free((void *)fp->_buf);
28         if (fp != stdin && fp != stdout && fp != stderr)
29                 free((void *)fp);
30         return retval;
31 }