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
|
|
|
|
2007-04-21 23:18:14 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "loc_incl.h"
|
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;
|
2007-04-21 23:18:14 +00:00
|
|
|
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
|
|
|
}
|