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;
|
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)
|
char * gets_s(char * str, size_t n)
|
||||||
{
|
{
|
||||||
if (str == NULL)
|
if (str == NULL)
|
||||||
|
@ -48,39 +36,20 @@ char* gets_s(char* str, size_t n)
|
||||||
if (n == 0 || n == 1)
|
if (n == 0 || n == 1)
|
||||||
return NULL;
|
return NULL;
|
||||||
str[0] = '\0';
|
str[0] = '\0';
|
||||||
size_t leftToStore = n - 1;
|
char i = 0, t = n - 1;
|
||||||
bool isTruncated = false;
|
bool isTruncated = false;
|
||||||
|
|
||||||
__asm {
|
while ((char ch = getpch()) != '\n')
|
||||||
ldy #0
|
{
|
||||||
|
if (i < t)
|
||||||
read_loop:
|
str[i++] = ch;
|
||||||
jsr getpch
|
else
|
||||||
|
{
|
||||||
// Check if the read in character is the line feed.
|
isTruncated = true;
|
||||||
cmp #10
|
break;
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
str[i] = 0;
|
||||||
|
|
||||||
if (isTruncated)
|
if (isTruncated)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
|
@ -14,8 +14,6 @@ void puts(const char * str);
|
||||||
char * gets(char * str);
|
char * gets(char * str);
|
||||||
char* gets_s(char* str, size_t n);
|
char* gets_s(char* str, size_t n);
|
||||||
|
|
||||||
char * gets_s(char * str, size_t n);
|
|
||||||
|
|
||||||
void printf(const char * fmt, ...);
|
void printf(const char * fmt, ...);
|
||||||
|
|
||||||
int sprintf(char * str, const char * fmt, ...);
|
int sprintf(char * str, const char * fmt, ...);
|
||||||
|
|
Loading…
Reference in New Issue