Dynamically allocating size for input buffer. This will probably improve in a later commit.

modified:   nms.c
This commit is contained in:
Brian Barto 2016-04-04 22:36:00 -04:00
parent 077cc1d1bb
commit 3dc7cb41d4

19
nms.c
View File

@ -1,5 +1,6 @@
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
@ -13,18 +14,24 @@ void clearTermWindow(int, int);
int main(void) { int main(void) {
int termSizeRows = getTermSizeRows(); int termSizeRows = getTermSizeRows();
int termSizeCols = getTermSizeCols(); int termSizeCols = getTermSizeCols();
char inputBuffer[termSizeRows * termSizeCols]; char *inputBuffer;
inputBuffer = malloc(sizeof(char));
clearTermWindow(termSizeRows, termSizeCols); clearTermWindow(termSizeRows, termSizeCols);
// Geting input // Geting input
int c; int c, i = 0;
while ((c = getchar()) != EOF) { while ((c = getchar()) != EOF) {
if (isspace(c)) inputBuffer = realloc(inputBuffer, sizeof(char) * (i + 2));
putchar(c); inputBuffer[i] = c;
else ++i;
putchar('x');
} }
inputBuffer[i] = '\0';
printf("%s", inputBuffer);
free(inputBuffer);
return 0; return 0;
} }