/*
 * Print the usernames of all users in the list starting at curr.
 * Names should be printed to standard output, one per line.
 * Return a dynamically allocated string containing the list of user names.
 */
char* list_users(const User *curr) {
    char* result = NULL;
    int len = 0;
    while (curr != NULL) {
        len += strlen(curr->name) + 1; // Add 1 for newline character
        curr = curr->next;
    }

    result = (char*) malloc(len + 1); // Add 1 for null terminator
    if (result == NULL) {
        perror("malloc");
        exit(1);
    }

    char* ptr = result;
    curr = curr->next;
    while (curr != NULL) {
        int n = snprintf(ptr, len, "%s\n", curr->name);
        if (n < 0) {
            perror("snprintf");
            free(result);
            exit(1);
        }
        ptr += n;
        len -= n;
        curr = curr->next;
    }

    return result;
}
