scc

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

fclose.c (550B)


      1 #include <stdlib.h>
      2 #include <stdio.h>
      3 
      4 #include "../syscall.h"
      5 #include "../libc.h"
      6 #undef fclose
      7 
      8 int
      9 fclose(FILE *fp)
     10 {
     11 	int r = EOF;
     12 
     13 	if ((fp->flags & _IOSTRG) == 0 &&
     14 	    fp->flags & (_IOWRITE | _IOREAD | _IORW)) {
     15 		r = 0;
     16 		if (_flsbuf(fp) == EOF)
     17 			r = EOF;
     18 		if (_close(fp->fd) < 0)
     19 			r = EOF;
     20 	}
     21 
     22 	if (fp->flags & _IOALLOC) {
     23 		free(fp->buf);
     24 		fp->buf = NULL;
     25 	}
     26 
     27 	fp->flags &= ~(_IOWRITE | _IOREAD | _IORW |
     28 	               _IOERR | _IOEOF |
     29 	               _IOALLOC |
     30 	               _IOTXT |
     31 	               _IOSTRG);
     32 
     33 	return r;
     34 }