ForEachUser() linked list , keep track of last item so we don't have to traverse...
authorArt Cancro <ajc@citadel.org>
Sat, 28 Sep 2019 20:54:46 +0000 (16:54 -0400)
committerArt Cancro <ajc@citadel.org>
Sat, 28 Sep 2019 20:54:46 +0000 (16:54 -0400)
citadel/user_ops.c

index 9a39b06b6754b7b4c940fbd0e06acf0959476411..c215e734a6706940472766ac502b366385868c4d 100644 (file)
@@ -1202,32 +1202,39 @@ void ForEachUser(void (*CallBack) (char *, void *out_data), void *in_data)
                struct feu *next;
                char username[USERNAME_SIZE];
        };
-       struct feu *usernames = NULL;
+       struct feu *ufirst = NULL;
+       struct feu *ulast = NULL;
        struct feu *f = NULL;
 
        cdb_rewind(CDB_USERS);
 
-       // Phase 1 : load list of usernames
+       // Phase 1 : build a linked list 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 = usernames;
+                       f->next = NULL;
                        strncpy(f->username, usptr->fullname, USERNAME_SIZE);
-                       usernames = f;
+
+                       if (ufirst == NULL) {
+                               ufirst = f;
+                               ulast = f;
+                       }
+                       else {
+                               ulast->next = f;
+                               ulast = f;
+                       }
                }
        }
 
-       // Phase 2 : perform the callback for each username
-       while (usernames != NULL) {
-               (*CallBack) (usernames->username, in_data);
-               f = usernames;
-               usernames = usernames->next;
+       // 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);
        }
-
-       free(usernames);
 }