ack/lang/cem/libcc.ansi/stdio/fclose.c
1989-05-30 13:34:25 +00:00

32 lines
591 B
C

/*
* fclose.c - flush a stream and close the file
*/
/* $Header$ */
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
int close(int d);
int
fclose(FILE *fp)
{
register int i, retval = 0;
for (i=0; i<FOPEN_MAX; i++)
if (fp == _iotable[i]) {
_iotable[i] = 0;
break;
}
if (i >= FOPEN_MAX)
return(EOF);
if (fflush(fp)) retval = EOF; /* ??? */
if (close(fileno(fp))) retval = EOF; /* ??? */
if ( io_testflag(fp,_IOMYBUF) && fp->_buf )
free((void *)fp->_buf);
if (fp != &_stdin && fp != &_stdout && fp != &_stderr)
free((void *)fp);
return(retval);
}