1#include <errno.h>2#include <stdio.h>3#include <stdlib.h>45#include "../libc.h"67static void8cleanup(void)9{10 fflush(NULL);11}1213int14__putc(int ch, FILE *fp)15{16 if (fp->flags & _IOERR)17 return EOF;1819 if (fp->flags & _IOREAD) {20 fp->flags |= _IOERR;21 errno = EBADF;22 return EOF;23 }2425 if (fp->flags & _IOSTRG)26 return ch & 0xFF;2728 if (fp->buf == NULL && _allocbuf(fp))29 return EOF;30 _flushall = cleanup;3132 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 }4849 fp->flags |= _IOWRITE;50 return ch & 0xFF;51}