fix: resize arrays as much as needed

This commit is contained in:
2026-10-10 01:04:11 +02:00
parent a7cbe7b32c
commit a1df9fca4f

View File

@@ -17,8 +17,9 @@
#define HISTORY_FILE_NAME "/history.log"
#define LOCAL_SHARE_DIRNAME "/.local/share/msh"
#define DEFAULTLINECAPACITY 128
#define DEFAULTTOKENSCAPACITY 32
#define DEFAULTLINECAPACITY 256
#define DEFAULTTOKENSCAPACITY 64
#define DEFAULTHISTORYCAPACITY 128
const int LINESIZE = 512;
@@ -267,8 +268,13 @@ int main(int argc, char** argv) {
}
tokenworkcapacity = DEFAULTLINECAPACITY;
historycapacity = 16;
historycapacity = DEFAULTHISTORYCAPACITY;
history = calloc(historycapacity, sizeof(char*));
if (history == NULL) {
retval = 1;
goto cleanup;
}
memset(history, 0, historycapacity);
// Main loop
while(1) {
@@ -379,7 +385,7 @@ int main(int argc, char** argv) {
linelen++;
// Are we out of capacity? Double line length
if (linelen >= linecapacity - 1) {
while (linelen >= linecapacity - 1) {
linecapacity *= 2;
line = realloc(line, linecapacity);
if (line == NULL) {
@@ -409,7 +415,7 @@ print_prompt:
fprintf(historyfile, "%s\n", line);
fflush(historyfile);
if (tokenworkcapacity < linelen - 1) {
while (tokenworkcapacity < linelen - 1) {
tokenworkcapacity *= 2;
tokenwork = realloc(tokenwork, tokenworkcapacity);
}
@@ -422,8 +428,7 @@ print_prompt:
}
// TEMPORARY
if (historyi >= historycapacity) {
while (historyi >= historycapacity) {
historycapacity *= 2;
history = reallocarray(history, historycapacity, sizeof(char*));
@@ -431,6 +436,7 @@ print_prompt:
retval = 1;
goto cleanup;
}
memset(history + historyi, 0, (historycapacity - historyi) * sizeof(char*));
}
// change ownership of line to history
history[historyi++] = line;
@@ -448,7 +454,7 @@ print_prompt:
tokens[ntokens] = token;
if (ntokens >= tokenscapacity - 1) {
while (ntokens >= tokenscapacity - 1) {
tokenscapacity *= 2;
tokens = reallocarray(tokens, tokenscapacity, sizeof(char*));
}