ungetc.c 855 B

12345678910111213141516171819202122232425262728293031323334353637
  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 -- ungetc
  11. */
  12. #include "iolib.h"
  13. int ungetc(int c, FILE *f){
  14. if(c==EOF) return EOF;
  15. switch(f->state){
  16. default: /* WR */
  17. f->state=ERR;
  18. return EOF;
  19. case CLOSED:
  20. case ERR:
  21. return EOF;
  22. case OPEN:
  23. _IO_setvbuf(f);
  24. case RDWR:
  25. case END:
  26. f->rp=f->wp=f->buf+(f->bufl==0?1:f->bufl);
  27. f->state=RD;
  28. case RD:
  29. if(f->rp==f->buf) return EOF;
  30. if(f->flags&STRING)
  31. f->rp--;
  32. else
  33. *--f->rp=c;
  34. return (char)c;
  35. }
  36. }