fxlibc/src/libc/stdio/fputc.c

35 lines
520 B
C
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include "fileutil.h"
int fputc(int c_int, FILE *fp)
{
unsigned char c = c_int;
if(!fp->writable) {
fp->error = 1;
return 0;
}
if(fp->append && fseek(fp, 0, SEEK_END) == EOF) {
fp->error = 1;
return 0;
}
if(!fp->buf) {
ssize_t rc = __fp_write(fp, &c, 1);
return (rc == 1) ? c : EOF;
}
__fp_buffer_mode_write(fp);
if(fp->bufpos >= fp->bufsize)
fflush(fp);
fp->buf[fp->bufpos++] = c;
if(fp->bufmode == _IOLBF && c == '\n')
fflush(fp);
return c;
}