diff options
author | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-10 10:33:20 -0400 |
---|---|---|
committer | bpc2003 <wpesfriendnva@gmail.com> | 2025-03-10 10:33:20 -0400 |
commit | 021fe93cf7656b3e3e7eb7fc01a30a4727bc359c (patch) | |
tree | 00f089e249152c1cfd529a9855a5b1de7f4eb697 /src/keytab.c | |
parent | 7caa00ef467acdb7b874d4283b3bc58730bf803d (diff) |
Offloaded reallocing list to setkey
Diffstat (limited to 'src/keytab.c')
-rw-r--r-- | src/keytab.c | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/keytab.c b/src/keytab.c index 160e50a..6fcf303 100644 --- a/src/keytab.c +++ b/src/keytab.c @@ -35,8 +35,12 @@ struct keytab getkey(struct keytablist *list, int id, char *key) return list[id].tab[idx]; } -void setkey(struct keytablist *list, int id, char *pair) +void setkey(struct keytablist **list, int *len, int id, char *pair) { + if (id >= *len) { + *len += (id - *len) + 1; + *list = realloc(*list, (*len) * sizeof(struct keytablist)); + } char *tok = strtok(pair, ":"); char *key = calloc(strlen(tok) + 1, sizeof(char)); strcpy(key, tok); @@ -61,18 +65,20 @@ void setkey(struct keytablist *list, int id, char *pair) } int idx = hash(key); - while (list[id].tab[idx].key != NULL && strcmp(list[id].tab[idx].key, key) && idx < TABLEN) + while ((*list)[id].tab[idx].key != NULL && + strcmp((*list)[id].tab[idx].key, key) && + idx < TABLEN) idx++; if (idx >= TABLEN) { fprintf(stderr, "No more room in table\n"); return; } - if (!list[id].tab[idx].key) - list[id].tab[idx].key = key; + if (!(*list)[id].tab[idx].key) + (*list)[id].tab[idx].key = key; else free(key); - list[id].tab[idx].v = v; - list[id].tab[idx].flag = flag; + (*list)[id].tab[idx].v = v; + (*list)[id].tab[idx].flag = flag; } void delkey(struct keytablist *list, int id, char *key) |