From d1892d8a4cf2f125cd5cfb6da9ae46feb46ae1cd Mon Sep 17 00:00:00 2001 From: John Schneiderman Date: Sun, 29 Sep 2024 11:22:19 +0200 Subject: [PATCH] Changes from being mostly assembly to be based upon a C version. Adds the additional error conditions for get_s: truncation and too small buffer. --- include/stdio.c | 53 ++++++++++--------------------------------------- include/stdio.h | 2 -- 2 files changed, 11 insertions(+), 44 deletions(-) diff --git a/include/stdio.c b/include/stdio.c index 84bcce0..0c7ec1d 100644 --- a/include/stdio.c +++ b/include/stdio.c @@ -29,18 +29,6 @@ char * gets(char * str) } char * gets_s(char * str, size_t n) -{ - char i = 0, t = n - 1; - while ((char ch = getpch()) != '\n') - { - if (i < t) - str[i++] = ch; - } - str[i] = 0; - return str; -} - -char* gets_s(char* str, size_t n) { if (str == NULL) return NULL; @@ -48,39 +36,20 @@ char* gets_s(char* str, size_t n) if (n == 0 || n == 1) return NULL; str[0] = '\0'; - size_t leftToStore = n - 1; + char i = 0, t = n - 1; bool isTruncated = false; - __asm { - ldy #0 - - read_loop: - jsr getpch - - // Check if the read in character is the line feed. - cmp #10 - beq read_done - - // Check to see if the maximum characters is reached. - ldx leftToStore - cpx #0 - beq read_max - - // Store the read in character & prepare for the next read. - sta (str), Y - iny - dec leftToStore - jmp read_loop - - read_max: - ldx #1 - stx isTruncated - - read_done: - // Place a null character at the end of the C-string. - lda #0 - sta (str), Y + while ((char ch = getpch()) != '\n') + { + if (i < t) + str[i++] = ch; + else + { + isTruncated = true; + break; + } } + str[i] = 0; if (isTruncated) return NULL; diff --git a/include/stdio.h b/include/stdio.h index 431c8b9..7b1d059 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -14,8 +14,6 @@ void puts(const char * str); char * gets(char * str); char* gets_s(char* str, size_t n); -char * gets_s(char * str, size_t n); - void printf(const char * fmt, ...); int sprintf(char * str, const char * fmt, ...);