ack/lang/cem/libcc.ansi/sys/stdio/fclose.c
David Given 9109d7af7f First stage in modularising FILE*. Refactor so that printf/scanf don't rely on
FILE* innards; allow plats to replace the entire emulated FILE* system.
2019-06-15 13:07:10 +02:00

37 lines
622 B
C

/*
* fclose.c - flush a stream and close the file
*/
/* $Id$ */
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "loc_incl.h"
#if ACKCONF_WANT_STDIO && ACKCONF_WANT_EMULATED_FILE
int fclose(FILE* fp)
{
register int i, retval = 0;
for (i = 0; i < FOPEN_MAX; i++)
if (fp == __iotab[i])
{
__iotab[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;
}
#endif