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.
This commit is contained in:
parent
0711764fef
commit
d1892d8a4c
|
@ -28,18 +28,6 @@ char * gets(char * str)
|
|||
return 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)
|
||||
|
@ -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;
|
||||
|
|
|
@ -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, ...);
|
||||
|
|
Loading…
Reference in New Issue