ref: 1582b3f523dd18147b2c32003584b990b6e58bfe
parent: 1291405b38fc406e752908cf5ff5a7299e44e07a
author: phil9 <telephil9@gmail.com>
date: Fri Mar 11 15:29:10 EST 2022
do not limit input lines it seems it is possible to dynamically allocate memory which allows growing arrays as needed and thus remove the need to fix an arbitrary limit on the number of input lines...what times we are living!
--- a/main.c
+++ b/main.c
@@ -19,7 +19,6 @@
enum
{
- Maxlines = 4096,
Tickw = 3,
Padding = 4,
Scrollwidth = 12,
@@ -42,14 +41,37 @@
int scrollsize;
int pmode;
int plumbfd;
-char* lines[Maxlines];
+char** lines;
int nlines;
-Match matches[Maxlines];
+int maxlines;
+Match* matches;
int nmatches;
char input[255] = {0};
int ninput = 0;
char pwd[255] = {0};
+void*
+emalloc(ulong size)
+{
+ void *p;
+
+ p = malloc(size);
+ if(p == nil)
+ sysfatal("malloc: %r");
+ return p;
+}
+
+void*
+erealloc(void *p, ulong size)
+{
+ void *q;
+
+ q = realloc(p, size);
+ if(q == nil)
+ sysfatal("realloc: %r");
+ return q;
+}
+
int
matchcmp(void *a, void *b)
{
@@ -85,6 +107,10 @@
char *s;
nlines = 0;
+ nmatches = 0;
+ maxlines = 4096;
+ lines = emalloc(maxlines * sizeof *lines);
+ matches = emalloc(maxlines * sizeof *matches);
bp = Bfdopen(0, OREAD);
for(;;){
s = Brdstr(bp, '\n', 1);
@@ -92,8 +118,11 @@
break;
lines[nlines++] = s;
matches[nmatches++].name = s;
- if(nlines >= Maxlines)
- break;
+ if(nlines >= maxlines || nmatches >= maxlines){
+ maxlines *= 1.5;
+ lines = erealloc(lines, maxlines * sizeof *lines);
+ matches = erealloc(matches, maxlines * sizeof *matches);
+ }
}
Bterm(bp);
}