ack/lang/cem/libcc.ansi/stdio/fclose.c

32 lines
554 B
C
Raw Normal View History

1989-05-30 13:34:25 +00:00
/*
* fclose.c - flush a stream and close the file
*/
1994-06-24 14:02:31 +00:00
/* $Id$ */
1989-05-30 13:34:25 +00:00
#include <stdio.h>
#include <stdlib.h>
#include "loc_incl.h"
int _close(int d);
1989-05-30 13:34:25 +00:00
int
fclose(FILE *fp)
{
register int i, retval = 0;
for (i=0; i<FOPEN_MAX; i++)
1989-12-18 15:04:14 +00:00
if (fp == __iotab[i]) {
__iotab[i] = 0;
1989-05-30 13:34:25 +00:00
break;
}
if (i >= FOPEN_MAX)
1989-06-26 10:37:05 +00:00
return EOF;
if (fflush(fp)) retval = EOF;
if (_close(fileno(fp))) retval = EOF;
1989-05-30 13:34:25 +00:00
if ( io_testflag(fp,_IOMYBUF) && fp->_buf )
free((void *)fp->_buf);
1989-06-26 10:37:05 +00:00
if (fp != stdin && fp != stdout && fp != stderr)
1989-05-30 13:34:25 +00:00
free((void *)fp);
1989-06-26 10:37:05 +00:00
return retval;
1989-05-30 13:34:25 +00:00
}