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:
John Schneiderman 2024-09-29 11:44:35 +02:00
parent 581137ade7
commit cfd2e5142a

View File

@ -1,6 +1,7 @@
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "stdbool.h"
void putchar(char c)
{
@ -29,13 +30,29 @@ char * gets(char * str)
char * gets_s(char * str, size_t n)
{
if (str == NULL)
return NULL;
if (n == 0 || n == 1)
return NULL;
str[0] = '\0';
char i = 0, t = n - 1;
bool isTruncated = false;
while ((char ch = getpch()) != '\n')
{
if (i < t)
str[i++] = ch;
else
{
isTruncated = true;
break;
}
}
str[i] = 0;
if (isTruncated)
return NULL;
return str;
}