scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

fclose.c (578B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 
      4 #include "../libc.h"
      5 #include "../syscall.h"
      6 
      7 #undef fclose
      8 
      9 int
     10 fclose(FILE *fp)
     11 {
     12 	int r = EOF;
     13 
     14 	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 	}
     22 
     23 	if (fp->flags & _IOALLOC) {
     24 		free(fp->buf);
     25 		fp->rp = fp->wp = fp->lp = fp->buf = NULL;
     26 	}
     27 
     28 	fp->flags &= ~(_IOWRITE | _IOREAD | _IORW |
     29 	               _IOERR | _IOEOF |
     30 	               _IOALLOC |
     31 	               _IOTXT |
     32 	               _IOSTRG);
     33 
     34 	return r;
     35 }