1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "os_account.h"
16 #include <string>
17 #include <securec.h>
18 #include "account_log_wrapper.h"
19 #include "os_account_common.h"
20 #include "os_account_manager.h"
21
22 using namespace OHOS;
23 using namespace OHOS::AccountSA;
OH_OsAccount_GetName(char * buffer,size_t buffer_size)24 OsAccount_ErrCode OH_OsAccount_GetName(char *buffer, size_t buffer_size)
25 {
26 if ((buffer == nullptr) || (buffer_size == 0)) {
27 ACCOUNT_LOGE("Buffer is nullptr or length is zero.");
28 return OsAccount_ErrCode::OS_ACCOUNT_ERR_INVALID_PARAMETER;
29 }
30 std::string accountName;
31 ErrCode err = AccountSA::OsAccountManager::GetOsAccountName(accountName);
32 if (err != ERR_OK) {
33 ACCOUNT_LOGE("Internal error(%{public}d).", err);
34 return OsAccount_ErrCode::OS_ACCOUNT_ERR_INTERNAL_ERROR;
35 }
36 size_t accountSize = accountName.size();
37 if (buffer_size <= accountSize) {
38 ACCOUNT_LOGE(
39 "Buffer size(%{public}zu) is less than length of account name(%{public}zu).", buffer_size, accountSize);
40 return OsAccount_ErrCode::OS_ACCOUNT_ERR_INVALID_PARAMETER;
41 }
42 err = strncpy_s(buffer, buffer_size, accountName.c_str(), accountSize);
43 if (err != EOK) {
44 ACCOUNT_LOGE("Failed to strncpy_s, err(%{public}d).", err);
45 return OsAccount_ErrCode::OS_ACCOUNT_ERR_INTERNAL_ERROR;
46 }
47 buffer[accountSize] = '\0';
48 return OsAccount_ErrCode::OS_ACCOUNT_ERR_OK;
49 }