From 785dcb334199c55fc3bbaf92dc81e82ae9e00361 Mon Sep 17 00:00:00 2001 From: turbocat Date: Thu, 26 Aug 2021 20:35:11 +0000 Subject: [PATCH] libc.obj: - fixed return fwrite and fread as well as eof git-svn-id: svn://kolibrios.org@9156 a494cfbc-eb01-0410-851d-a64ba20cac60 --- .../ktcc/trunk/libc.obj/source/stdio/fread.c | 23 +++++++++---- .../ktcc/trunk/libc.obj/source/stdio/fwrite.c | 32 +++++++++++-------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c index 35eff033fb..6010ad123e 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fread.c @@ -12,6 +12,12 @@ size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict strea return 0; } + if(size<=0 || nmemb<=0){ + errno = EINVAL; + stream->error=errno; + return 0; + } + if(stream==stdin){ con_init(); con_gets((char*)ptr, bytes_count+1); @@ -25,14 +31,17 @@ size_t fread(void *restrict ptr, size_t size, size_t nmemb, FILE *restrict strea //debug_printf("Ungetc: %x\n", ((char*) ptr)[0]); } unsigned status = _ksys_file_read_file(stream->name, stream->position, bytes_count, ptr , &bytes_read); - if (status) { - errno = EIO; - stream->error = errno; - return 0; - }else { - stream->position+=bytes_read; + if (status != KSYS_FS_ERR_SUCCESS) { + if(status == KSYS_FS_ERR_EOF){ + stream->eof=1; + }else{ + errno = EIO; + stream->error = errno; + return 0; + } } + stream->position+=bytes_read; } } - return bytes_read; + return bytes_read/size; } diff --git a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fwrite.c b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fwrite.c index 0e807aacda..c2e095223e 100644 --- a/programs/develop/ktcc/trunk/libc.obj/source/stdio/fwrite.c +++ b/programs/develop/ktcc/trunk/libc.obj/source/stdio/fwrite.c @@ -12,28 +12,34 @@ size_t fwrite(const void *restrict ptr, size_t size, size_t nmemb, FILE *restric return 0; } + if(size<=0 || nmemb<=0){ + errno = EINVAL; + stream->error=errno; + return 0; + } + if(stream==stdout){ con_init(); con_write_string((char*)ptr, bytes_count); return nmemb; } - else if(stream==stderr){ + + if(stream==stderr){ for (size_t i = 0; i < bytes_count; i++) { char c = *(char*)(ptr+i); _ksys_debug_putc(c); } + return nmemb; } - else{ - if(stream->mode != _FILEMODE_R){ - unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written); - if (status != KSYS_FS_ERR_SUCCESS) { - errno = EIO; - stream->error = errno; - return 0; - }else { - stream->position+=bytes_written; - } + + if(stream->mode != _FILEMODE_R){ + unsigned status = _ksys_file_write_file(stream->name, stream->position, bytes_count, ptr, &bytes_written); + if (status != KSYS_FS_ERR_SUCCESS) { + errno = EIO; + stream->error = errno; + return 0; } + stream->position+=bytes_written; } - return bytes_written; -} \ No newline at end of file + return bytes_written/size; +}