From: Art Cancro Date: Mon, 12 Jun 2023 04:02:19 +0000 (-0900) Subject: ForEachUser() convert to array X-Git-Tag: v977~2 X-Git-Url: https://code.citadel.org/?a=commitdiff_plain;h=4c4c60eb90b5a7ebb381b1b62b59802565cc39d9;p=citadel.git ForEachUser() convert to array --- diff --git a/citadel/server/user_ops.c b/citadel/server/user_ops.c index 1a0aadf99..a801c0e8b 100644 --- a/citadel/server/user_ops.c +++ b/citadel/server/user_ops.c @@ -1063,43 +1063,30 @@ void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data) { struct cdbdata *cdbus; struct ctdluser *usptr; - struct feu { - struct feu *next; - char username[USERNAME_SIZE]; - }; - struct feu *ufirst = NULL; - struct feu *ulast = NULL; - struct feu *f = NULL; + Array *all_users = array_new(USERNAME_SIZE); + if (all_users == NULL) { + syslog(LOG_ERR, "user_ops: alloc failed, ForEachUser() exiting"); + return; + } + cdb_rewind(CDB_USERS); - // Phase 1 : build a linked list of all our user account names + // Phase 1 : build an array of all our user account names while (cdbus = cdb_next_item(CDB_USERS), cdbus != NULL) { usptr = (struct ctdluser *) cdbus->ptr; - if (strlen(usptr->fullname) > 0) { - f = malloc(sizeof(struct feu)); - f->next = NULL; - strncpy(f->username, usptr->fullname, USERNAME_SIZE); - - if (ufirst == NULL) { - ufirst = f; - ulast = f; - } - else { - ulast->next = f; - ulast = f; - } + array_append(all_users, usptr->fullname); } } // Phase 2 : perform the callback for each user while de-allocating the list - while (ufirst != NULL) { - (*CallBack) (ufirst->username, in_data); - f = ufirst; - ufirst = ufirst->next; - free(f); + for (int i=0; i