scc

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

__putc.c (923B)


      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 	static int first = 1;
     17 
     18 	if (fp->flags & _IOERR)
     19 		return EOF;
     20 
     21 	if (fp->flags & _IOREAD) {
     22 		fp->flags |= _IOERR;
     23 		errno = EBADF;
     24 		return EOF;
     25 	}
     26 
     27 	if (fp->flags & _IOSTRG) {
     28 		fp->flags |= _IOERR;
     29 		return EOF;
     30 	}
     31 
     32 	if (fp->buf == NULL && _allocbuf(fp))
     33 		return EOF;
     34 
     35 	if (first) {
     36 		if (atexit(cleanup)) {
     37 			fp->flags |= _IOERR;
     38 			errno = ENOMEM;
     39 			return EOF;
     40 		}
     41 		first = 0;
     42 	}
     43 
     44 	if (fp->flags & _IOLBF) {
     45 		if (fp->wp == fp->lp && _flsbuf(fp))
     46 			return EOF;
     47 		*fp->wp++ = ch;
     48 		if (ch == '\n' && _flsbuf(fp))
     49 			return EOF;
     50 	} else if (fp->flags & _IOFBF) {
     51 		if (_flsbuf(fp))
     52 			return EOF;
     53 		*fp->wp++ = ch;
     54 		fp->rp = fp->buf + fp->len;
     55 	} else {
     56 		*fp->wp++ = ch;
     57 		if (_flsbuf(fp))
     58 			return EOF;
     59 	}
     60 
     61 	fp->flags |= _IOWRITE;
     62 	return ch & 0xFF;
     63 }