fflush.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 -- fflush
  11. */
  12. #include "iolib.h"
  13. /*
  14. * pANS stdio -- data (put here, since loader won't load a separate file)
  15. */
  16. FILE _IO_stream[]={
  17. /* fd flags state buf rp wp lp bufl unbuf */
  18. 0, 0, OPEN, 0, 0, 0, 0, 0, 0,
  19. 1, 0, OPEN, 0, 0, 0, 0, 0, 0,
  20. 2, 0, OPEN, 0, 0, 0, 0, 0, 0,
  21. };
  22. int _fflush(FILE *f){
  23. int error, cnt;
  24. if(f==NULL){
  25. error=0;
  26. for(f=_IO_stream;f!=&_IO_stream[FOPEN_MAX];f++)
  27. if(f->state==WR && _fflush(f)==EOF)
  28. error=EOF;
  29. return error;
  30. }
  31. if(f->flags&STRING) return EOF;
  32. switch(f->state){
  33. default: /* OPEN RDWR EOF RD */
  34. return 0;
  35. case CLOSED:
  36. case ERR:
  37. return EOF;
  38. case WR:
  39. cnt=(f->flags&LINEBUF?f->lp:f->wp)-f->buf;
  40. if(cnt && write(f->fd, f->buf, cnt)!=cnt){
  41. f->state=ERR;
  42. return EOF;
  43. }
  44. f->rp=f->wp=f->buf;
  45. f->state=RDWR;
  46. return 0;
  47. }
  48. }
  49. int fflush(FILE *f)
  50. {
  51. int r;
  52. qlock(&_stdiolk);
  53. r = _fflush(f);
  54. qunlock(&_stdiolk);
  55. return r;
  56. }