xref: /OK3568_Linux_fs/external/mpp/osal/windows/os_env.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #if defined(_WIN32)
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include "os_env.h"
21 
22 #define ENV_BUF_SIZE_WIN    1024
23 
os_get_env_u32(const char * name,RK_U32 * value,RK_U32 default_value)24 RK_S32 os_get_env_u32(const char *name, RK_U32 *value, RK_U32 default_value)
25 {
26     char *ptr = getenv(name);
27     if (NULL == ptr) {
28         *value = default_value;
29     } else {
30         char *endptr;
31         int base = (ptr[0] == '0' && ptr[1] == 'x') ? (16) : (10);
32         errno = 0;
33         *value = strtoul(ptr, &endptr, base);
34         if (errno || (ptr == endptr)) {
35             errno = 0;
36             *value = default_value;
37         }
38     }
39     return 0;
40 }
41 
os_get_env_str(const char * name,const char ** value,const char * default_value)42 RK_S32 os_get_env_str(const char *name, const char **value, const char *default_value)
43 {
44     *value = getenv(name);
45     if (NULL == *value) {
46         *value = default_value;
47     }
48     return 0;
49 }
50 
os_set_env_u32(const char * name,RK_U32 value)51 RK_S32 os_set_env_u32(const char *name, RK_U32 value)
52 {
53     char buf[ENV_BUF_SIZE_WIN];
54     _snprintf(buf, sizeof(buf), "%s=%lu", name, value);
55     return _putenv(buf);
56 }
57 
os_set_env_str(const char * name,char * value)58 RK_S32 os_set_env_str(const char *name, char *value)
59 {
60     char buf[ENV_BUF_SIZE_WIN];
61     _snprintf(buf, sizeof(buf), "%s=%s", name, value);
62     return _putenv(buf);
63 }
64 
65 #endif
66