scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

__putc.c (781B)


      1 #include <errno.h>
      2 #include <stdio.h>
      3 #include <stdlib.h>
      4 
      5 #include "../libc.h"
      6 
      7 static void
      8 cleanup(void)
      9 {
     10 	fflush(NULL);
     11 }
     12 
     13 int
     14 __putc(int ch, FILE *fp)
     15 {
     16 	if (fp->flags & _IOERR)
     17 		return EOF;
     18 
     19 	if (fp->flags & _IOREAD) {
     20 		fp->flags |= _IOERR;
     21 		errno = EBADF;
     22 		return EOF;
     23 	}
     24 
     25 	if (fp->flags & _IOSTRG)
     26 		return ch & 0xFF;
     27 
     28 	if (fp->buf == NULL && _allocbuf(fp))
     29 		return EOF;
     30 	_flushall = cleanup;
     31 
     32 	if (fp->flags & _IOLBF) {
     33 		if (fp->wp == fp->lp && _flsbuf(fp))
     34 			return EOF;
     35 		*fp->wp++ = ch;
     36 		if (ch == '\n' && _flsbuf(fp))
     37 			return EOF;
     38 	} else if (fp->flags & _IOFBF) {
     39 		if (_flsbuf(fp))
     40 			return EOF;
     41 		*fp->wp++ = ch;
     42 		fp->rp = fp->buf + fp->len;
     43 	} else {
     44 		*fp->wp++ = ch;
     45 		if (_flsbuf(fp))
     46 			return EOF;
     47 	}
     48 
     49 	fp->flags |= _IOWRITE;
     50 	return ch & 0xFF;
     51 }