1 /*
2 * Copyright (C) 2006 The Android Open Source Project
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 #include <cutils/properties.h>
18
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include <android-base/properties.h>
26
property_get_bool(const char * key,int8_t default_value)27 int8_t property_get_bool(const char* key, int8_t default_value) {
28 if (!key) return default_value;
29
30 int8_t result = default_value;
31 char buf[PROPERTY_VALUE_MAX] = {};
32
33 int len = property_get(key, buf, "");
34 if (len == 1) {
35 char ch = buf[0];
36 if (ch == '0' || ch == 'n') {
37 result = false;
38 } else if (ch == '1' || ch == 'y') {
39 result = true;
40 }
41 } else if (len > 1) {
42 if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
43 result = false;
44 } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
45 result = true;
46 }
47 }
48
49 return result;
50 }
51
52 template <typename T>
property_get_int(const char * key,T default_value)53 static T property_get_int(const char* key, T default_value) {
54 if (!key) return default_value;
55
56 char value[PROPERTY_VALUE_MAX] = {};
57 if (property_get(key, value, "") < 1) return default_value;
58
59 // libcutils unwisely allows octal, which libbase doesn't.
60 T result = default_value;
61 int saved_errno = errno;
62 errno = 0;
63 char* end = nullptr;
64 intmax_t v = strtoimax(value, &end, 0);
65 if (errno != ERANGE && end != value && v >= std::numeric_limits<T>::min() &&
66 v <= std::numeric_limits<T>::max()) {
67 result = v;
68 }
69 errno = saved_errno;
70 return result;
71 }
72
property_get_int64(const char * key,int64_t default_value)73 int64_t property_get_int64(const char* key, int64_t default_value) {
74 return property_get_int<int64_t>(key, default_value);
75 }
76
property_get_int32(const char * key,int32_t default_value)77 int32_t property_get_int32(const char* key, int32_t default_value) {
78 return property_get_int<int32_t>(key, default_value);
79 }
80
property_set(const char * key,const char * value)81 int property_set(const char* key, const char* value) {
82 return __system_property_set(key, value);
83 }
84
property_get(const char * key,char * value,const char * default_value)85 int property_get(const char* key, char* value, const char* default_value) {
86 int len = __system_property_get(key, value);
87 if (len < 1 && default_value) {
88 snprintf(value, PROPERTY_VALUE_MAX, "%s", default_value);
89 return strlen(value);
90 }
91 return len;
92 }
93
94 #if __has_include(<sys/system_properties.h>)
95
96 #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
97 #include <sys/_system_properties.h>
98
99 struct callback_data {
100 void (*callback)(const char* name, const char* value, void* cookie);
101 void* cookie;
102 };
103
trampoline(void * raw_data,const char * name,const char * value,unsigned)104 static void trampoline(void* raw_data, const char* name, const char* value, unsigned /*serial*/) {
105 callback_data* data = reinterpret_cast<callback_data*>(raw_data);
106 data->callback(name, value, data->cookie);
107 }
108
property_list_callback(const prop_info * pi,void * data)109 static void property_list_callback(const prop_info* pi, void* data) {
110 __system_property_read_callback(pi, trampoline, data);
111 }
112
property_list(void (* fn)(const char * name,const char * value,void * cookie),void * cookie)113 int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) {
114 callback_data data = {fn, cookie};
115 return __system_property_foreach(property_list_callback, &data);
116 }
117
118 #endif
119