sclose.c 943 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /*
  10. * pANS stdio -- sclose
  11. */
  12. #include "iolib.h"
  13. char *sclose(FILE *f){
  14. switch(f->state){
  15. default: /* ERR CLOSED */
  16. if(f->buf && f->flags&BALLOC)
  17. free(f->buf);
  18. f->flags=0;
  19. return NULL;
  20. case OPEN:
  21. f->buf=malloc(1);
  22. f->buf[0]='\0';
  23. break;
  24. case RD:
  25. case END:
  26. f->flags=0;
  27. break;
  28. case RDWR:
  29. case WR:
  30. if(f->wp==f->rp){
  31. if(f->flags&BALLOC)
  32. f->buf=realloc(f->buf, f->bufl+1);
  33. if(f->buf==NULL) return NULL;
  34. }
  35. *f->wp='\0';
  36. f->flags=0;
  37. break;
  38. }
  39. f->state=CLOSED;
  40. f->flags=0;
  41. return f->buf;
  42. }