diff --git a/nx/include/switch/services/acc.h b/nx/include/switch/services/acc.h index 7e4568fc..e2d50a63 100644 --- a/nx/include/switch/services/acc.h +++ b/nx/include/switch/services/acc.h @@ -39,10 +39,12 @@ Service* accountGetService(void); Result accountGetUserCount(s32* user_count); /** - * @brief Get a list of all user IDs - * @param userIDs Pointer to array of user IDs, array must be exactly ACC_USER_LIST_SIZE + * @brief Get a list of all user IDs. The returned list will never be larger than ACC_USER_LIST_SIZE + * @param userIDs Pointer to array of user IDs + * @param max_userIDs Maximum number of user IDs to return + * @param actual_total The actual total number of user IDs found */ -Result accountListAllUsers(u128* userIDs); +Result accountListAllUsers(u128* userIDs, size_t max_userIDs, size_t *actual_total); /// Get the userID for the currently active user. The output userID is only valid when the output account_selected==1, otherwise no user is currently selected. /// An user is only selected when the user-account selection applet was used to select an user at least once before. diff --git a/nx/source/services/acc.c b/nx/source/services/acc.c index 30d91a93..8db33a65 100644 --- a/nx/source/services/acc.c +++ b/nx/source/services/acc.c @@ -1,4 +1,5 @@ #include +#include #include "types.h" #include "arm/atomics.h" @@ -70,7 +71,7 @@ Result accountGetUserCount(s32* user_count) return rc; } -Result accountListAllUsers(u128* userIDs) +static Result _accountListAllUsers(u128* userIDs) { IpcCommand c; ipcInitialize(&c); @@ -108,6 +109,38 @@ Result accountListAllUsers(u128* userIDs) return rc; } +Result accountListAllUsers(u128* userIDs, size_t max_userIDs, size_t *actual_total) +{ + Result rc=0; + + u128 *temp_userIDs; + temp_userIDs = malloc(sizeof(u128) * ACC_USER_LIST_SIZE); + + rc = _accountListAllUsers(temp_userIDs); + + if (R_SUCCEEDED(rc)) { + size_t total_userIDs = 0; + + for (int i = 0; i < ACC_USER_LIST_SIZE; i++) { + if (temp_userIDs[i]) { + total_userIDs++; + } + } + + if (max_userIDs > total_userIDs) { + max_userIDs = total_userIDs; + } + + for (int i = 0; i < max_userIDs; i++) { + userIDs[i] = temp_userIDs[i]; + } + + *actual_total = total_userIDs; + } + + return rc; +} + Result accountGetActiveUser(u128 *userID, bool *account_selected) { IpcCommand c;