implement handling of maxsize param

This commit is contained in:
Roland W-H 2022-11-19 16:50:53 +00:00
parent 236dbce306
commit 1fde10d2f5
1 changed files with 33 additions and 26 deletions

View File

@ -27,8 +27,12 @@ char* dyninput_str(int_least64_t maxsize)
do
{
c = getchar();
if (i + 2 == size)
if (size == maxsize)
{
printf("Character limit of %lld reached!\n", maxsize);
break;
}
c = (char)getchar();
if (i + 1 == size)
{
@ -45,7 +49,10 @@ char* dyninput_str(int_least64_t maxsize)
str[i] = c;
i++;
} while (c && c != '\n');
if (str[i - 1] != '\n') str[i - 1] = '\n';
str[i] = '\0';
// Sanitise stdin
while (c != '\n') c = (char)getchar();
return str;
}