diff --git a/src/userinput.c b/src/userinput.c index e161a63..8fe6660 100644 --- a/src/userinput.c +++ b/src/userinput.c @@ -2,6 +2,7 @@ #include #include +#include void trimLeft(char *input) { size_t length = strlen(input); @@ -21,4 +22,28 @@ void trimRight(char *input) { input[index] = '\0'; index--; } +} + +char *readInput() { + size_t bufferSize = 100; + char *buffer = malloc(bufferSize*sizeof(char)); + char c; + int index = 0; + do { + c = fgetc(stdin); + if (!(bufferSize > index)) { + char *newBuffer = malloc(2*bufferSize*sizeof(char)); + for (int i = 0; i < bufferSize; i++) { + newBuffer[i] = buffer[i]; + } + bufferSize *= 2; + } + buffer[index] = c; + index++; + } + while(c != '\n' && c != EOF); + buffer[index - 1] = '\0'; + trimLeft(buffer); + trimRight(buffer); + return buffer; } \ No newline at end of file