xref: /OK3568_Linux_fs/external/mpp/osal/linux/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(linux) && !defined(__ANDROID__)
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include "os_env.h"
22 
23 #define ENV_BUF_SIZE_LINUX  1024
24 
os_get_env_u32(const char * name,RK_U32 * value,RK_U32 default_value)25 RK_S32 os_get_env_u32(const char *name, RK_U32 *value, RK_U32 default_value)
26 {
27     char *ptr = getenv(name);
28     if (NULL == ptr) {
29         *value = default_value;
30     } else {
31         char *endptr;
32         int base = (ptr[0] == '0' && ptr[1] == 'x') ? (16) : (10);
33         errno = 0;
34         *value = strtoul(ptr, &endptr, base);
35         if (errno || (ptr == endptr)) {
36             errno = 0;
37             *value = default_value;
38         }
39     }
40     return 0;
41 }
42 
os_get_env_str(const char * name,const char ** value,const char * default_value)43 RK_S32 os_get_env_str(const char *name, const char **value, const char *default_value)
44 {
45     *value = getenv(name);
46     if (NULL == *value) {
47         *value = default_value;
48     }
49     return 0;
50 }
51 
os_set_env_u32(const char * name,RK_U32 value)52 RK_S32 os_set_env_u32(const char *name, RK_U32 value)
53 {
54     char buf[ENV_BUF_SIZE_LINUX];
55     snprintf(buf, sizeof(buf) - 1, "%u", value);
56     return setenv(name, buf, 1);
57 }
58 
os_set_env_str(const char * name,char * value)59 RK_S32 os_set_env_str(const char *name, char *value)
60 {
61     return setenv(name, value, 1);
62 }
63 
64 #endif
65