fputc.c 963 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. unsigned short minalloc;
  8. char *base=f->base,*oldbase=base;
  9. if(f->flags&_F_ERR) return EOF;
  10. if(!(f->flags&_F_WRIT)) __FERROR(f);
  11. minalloc=peek_w(base)+3;
  12. if(minalloc>65520u) __FERROR(f);
  13. if(minalloc>f->alloc)
  14. {
  15. HeapUnlock(f->handle);
  16. if(f->alloc<=65520u-f->buffincrement) f->alloc+=f->buffincrement;
  17. else f->alloc=65520u;
  18. if(!HeapRealloc(f->handle,f->alloc)) __FERROR(f);
  19. base=f->base=HLock(f->handle);
  20. f->fpos+=base-oldbase;
  21. oldbase=base;
  22. }
  23. if(feof(f)) (*(short*)base)++;
  24. if(c=='\n'&&tmode) c='\r';
  25. poke(f->fpos++,c);
  26. if(c=='\r'&&tmode) fputc(' ',f);
  27. if(base+peek_w(base)+(tmode?0:2)==f->fpos)
  28. {
  29. f->flags|=_F_EOF;
  30. if(tmode)
  31. {
  32. poke(f->fpos,0);
  33. poke(f->fpos+1,0xE0);
  34. }
  35. }
  36. return c;
  37. }