fputc.c 799 B

123456789101112131415161718192021222324252627282930313233
  1. #include <stdio.h>
  2. #include <peekpoke.h>
  3. #include <alloc.h>
  4. __ATTR_TIOS_CALLBACK__ short fputc(short c, FILE *f)
  5. {
  6. short tmode=!(f->flags&_F_BIN);
  7. char *base=f->base,*oldbase=base;
  8. if(f->flags&_F_ERR) return EOF;
  9. if(!(f->flags&_F_WRIT)) __FERROR(f);
  10. if(peek_w(base)+10>f->alloc)
  11. {
  12. HeapUnlock(f->handle);
  13. if(!HeapRealloc(f->handle,f->alloc+=f->buffincrement)) __FERROR(f);
  14. base=f->base=HLock(f->handle);
  15. f->fpos+=base-oldbase;
  16. oldbase=base;
  17. }
  18. if(feof(f)) (*(short*)base)++;
  19. if(c=='\n'&&tmode) c='\r';
  20. poke(f->fpos++,c);
  21. if(c=='\r'&&tmode) fputc(' ',f);
  22. if(base+peek_w(base)+(tmode?0:2)==f->fpos)
  23. {
  24. f->flags|=_F_EOF;
  25. if(tmode)
  26. {
  27. poke(f->fpos,0);
  28. poke(f->fpos+1,0xE0);
  29. }
  30. }
  31. return c;
  32. }