1#include <stdlib.h>2#include <stdio.h>34#include "../libc.h"5#include "../syscall.h"67#undef fclose89int10fclose(FILE *fp)11{12 int r = EOF;1314 if ((fp->flags & _IOSTRG) == 0 &&15 fp->flags & (_IOWRITE | _IOREAD | _IORW)) {16 r = 0;17 if (_flsbuf(fp) == EOF)18 r = EOF;19 if (_close(fp->fd) < 0)20 r = EOF;21 }2223 if (fp->flags & _IOALLOC) {24 free(fp->buf);25 fp->rp = fp->wp = fp->lp = fp->buf = NULL;26 }2728 fp->flags &= ~(_IOWRITE | _IOREAD | _IORW |29 _IOERR | _IOEOF |30 _IOALLOC |31 _IOTXT |32 _IOSTRG);3334 return r;35}