From 1e58a85f02c064dc600c13555d20f923cd273020 Mon Sep 17 00:00:00 2001 From: bumbread Date: Fri, 24 Jun 2022 13:43:47 +1100 Subject: [PATCH] fputc --- inc/errno.h | 1 + src/code/string.c | 1 + src/code/uchar.c | 4 ++-- src/win/win_stdio.c | 19 +++++++++++++++++-- test/test_io.c | 7 +++++++ 5 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 test/test_io.c diff --git a/inc/errno.h b/inc/errno.h index ce351d6..648071e 100644 --- a/inc/errno.h +++ b/inc/errno.h @@ -4,5 +4,6 @@ #define EDOM 1 #define EILSEQ 2 #define ERANGE 3 +#define EIO 4 extern _Thread_local int errno; diff --git a/src/code/string.c b/src/code/string.c index 69798cb..dddcc39 100644 --- a/src/code/string.c +++ b/src/code/string.c @@ -216,6 +216,7 @@ char *strerror(int errnum) { case EDOM: return "Value is out of domain of the function"; case EILSEQ: return "Illegal byte sequence"; case ERANGE: return "Value is out of range"; + case EIO: return "I/O error"; } return "Unkown error"; } diff --git a/src/code/uchar.c b/src/code/uchar.c index 78bf4ac..eb634d1 100644 --- a/src/code/uchar.c +++ b/src/code/uchar.c @@ -75,7 +75,7 @@ size_t c16rtomb( char16_t c16, mbstate_t *restrict ps ) { - if(*s == NULL) { + if(s == NULL) { *ps = (mbstate_t) {0}; return 0; } @@ -178,7 +178,7 @@ size_t c32rtomb( char32_t c32, mbstate_t *restrict ps ) { - if(*s == NULL) { + if(s == NULL) { *ps = (mbstate_t) {0}; return 0; } diff --git a/src/win/win_stdio.c b/src/win/win_stdio.c index 6cabc13..2c6ec34 100644 --- a/src/win/win_stdio.c +++ b/src/win/win_stdio.c @@ -6,6 +6,7 @@ #include #include #include +#include enum str_type { STR_R, @@ -69,6 +70,7 @@ static inline FILE *new_file( file_list_last->next = file; file->prev = file_list_last; file->next = NULL; + file_list_last = file; } else { file_list_last = file; @@ -94,6 +96,7 @@ static inline void dispose_file(FILE *file) { if(next == NULL) file_list_last = prev; free(file); mtx_unlock(&lock); + mtx_destroy(&lock); } void _setup_io() { @@ -147,15 +150,27 @@ int setvbuf( } int fflush(FILE *stream) { - if(mode == _IOFBF) { + if(stream->mode == _IOFBF) { } - else if(mode == _IONBF) { + else if(stream->mode == _IONBF) { } return 0; } +int fputc(int c, FILE *stream) { + mtx_lock(&stream->lock); + DWORD written; + BOOL ok = WriteFile(stream->handle, &c, 1, &written, NULL); + mtx_unlock(&stream->lock); + if(!ok) { + errno = EIO; + return EOF; + } + return c; +} + void setbuf(FILE *restrict stream, char *restrict buf) { if(buf != NULL) setvbuf(stream, buf, _IOFBF, BUFSIZ); diff --git a/test/test_io.c b/test/test_io.c new file mode 100644 index 0000000..cb3034f --- /dev/null +++ b/test/test_io.c @@ -0,0 +1,7 @@ + +#include + +int main() { + fputc('Z', stdout); + return 0; +}