Replace inSize with 'i' and use a standard for loop to handle incrementing.

modified:   src/nms.c
This commit is contained in:
Brian Barto 2017-09-20 09:15:04 -04:00
parent 40da2058b4
commit 65ded7d75b

View File

@ -16,7 +16,7 @@
#define INPUT_GROWTH_FACTOR 2 #define INPUT_GROWTH_FACTOR 2
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
int c, o, inSize = 0, inCapacity = INITIAL_CAPACITY; int c, o, i, inCapacity = INITIAL_CAPACITY;
char *input = NULL; char *input = NULL;
// Processing command arguments // Processing command arguments
@ -46,15 +46,15 @@ int main(int argc, char *argv[]) {
} }
} }
// Allocate memory for our input buffer
if ((input = malloc(inCapacity + 1)) == NULL) { if ((input = malloc(inCapacity + 1)) == NULL) {
fprintf (stderr, "Memory Allocation Error! Quitting...\n"); fprintf (stderr, "Memory Allocation Error! Quitting...\n");
return 1; return 1;
} }
// Geting input // Geting input
while ((c = getchar()) != EOF) { for (i = 0; (c = getchar()) != EOF; ++i) {
++inSize; if (i >= inCapacity) {
if (inSize > inCapacity) {
inCapacity *= INPUT_GROWTH_FACTOR; inCapacity *= INPUT_GROWTH_FACTOR;
input = realloc(input, inCapacity + 1); input = realloc(input, inCapacity + 1);
if (input == NULL) { if (input == NULL) {
@ -62,8 +62,8 @@ int main(int argc, char *argv[]) {
return 1; return 1;
} }
} }
input[inSize - 1] = c; input[i] = c;
input[inSize] = '\0'; input[i+1] = '\0';
} }
// Execute effect // Execute effect