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