properties.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #include <cutils/properties.h>
  17. #define LOG_TAG "properties"
  18. // #define LOG_NDEBUG 0
  19. #include <assert.h>
  20. #include <ctype.h>
  21. #include <errno.h>
  22. #include <inttypes.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <cutils/sockets.h>
  27. #include <log/log.h>
  28. int8_t property_get_bool(const char *key, int8_t default_value) {
  29. if (!key) {
  30. return default_value;
  31. }
  32. int8_t result = default_value;
  33. char buf[PROPERTY_VALUE_MAX] = {'\0'};
  34. int len = property_get(key, buf, "");
  35. if (len == 1) {
  36. char ch = buf[0];
  37. if (ch == '0' || ch == 'n') {
  38. result = false;
  39. } else if (ch == '1' || ch == 'y') {
  40. result = true;
  41. }
  42. } else if (len > 1) {
  43. if (!strcmp(buf, "no") || !strcmp(buf, "false") || !strcmp(buf, "off")) {
  44. result = false;
  45. } else if (!strcmp(buf, "yes") || !strcmp(buf, "true") || !strcmp(buf, "on")) {
  46. result = true;
  47. }
  48. }
  49. return result;
  50. }
  51. // Convert string property to int (default if fails); return default value if out of bounds
  52. static intmax_t property_get_imax(const char *key, intmax_t lower_bound, intmax_t upper_bound,
  53. intmax_t default_value) {
  54. if (!key) {
  55. return default_value;
  56. }
  57. intmax_t result = default_value;
  58. char buf[PROPERTY_VALUE_MAX] = {'\0'};
  59. char *end = NULL;
  60. int len = property_get(key, buf, "");
  61. if (len > 0) {
  62. int tmp = errno;
  63. errno = 0;
  64. // Infer base automatically
  65. result = strtoimax(buf, &end, /*base*/ 0);
  66. if ((result == INTMAX_MIN || result == INTMAX_MAX) && errno == ERANGE) {
  67. // Over or underflow
  68. result = default_value;
  69. ALOGV("%s(%s,%" PRIdMAX ") - overflow", __FUNCTION__, key, default_value);
  70. } else if (result < lower_bound || result > upper_bound) {
  71. // Out of range of requested bounds
  72. result = default_value;
  73. ALOGV("%s(%s,%" PRIdMAX ") - out of range", __FUNCTION__, key, default_value);
  74. } else if (end == buf) {
  75. // Numeric conversion failed
  76. result = default_value;
  77. ALOGV("%s(%s,%" PRIdMAX ") - numeric conversion failed", __FUNCTION__, key,
  78. default_value);
  79. }
  80. errno = tmp;
  81. }
  82. return result;
  83. }
  84. int64_t property_get_int64(const char *key, int64_t default_value) {
  85. return (int64_t)property_get_imax(key, INT64_MIN, INT64_MAX, default_value);
  86. }
  87. int32_t property_get_int32(const char *key, int32_t default_value) {
  88. return (int32_t)property_get_imax(key, INT32_MIN, INT32_MAX, default_value);
  89. }
  90. #define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
  91. #include <sys/_system_properties.h>
  92. int property_set(const char *key, const char *value) {
  93. return __system_property_set(key, value);
  94. }
  95. int property_get(const char *key, char *value, const char *default_value) {
  96. int len = __system_property_get(key, value);
  97. if (len > 0) {
  98. return len;
  99. }
  100. if (default_value) {
  101. len = strnlen(default_value, PROPERTY_VALUE_MAX - 1);
  102. memcpy(value, default_value, len);
  103. value[len] = '\0';
  104. }
  105. return len;
  106. }
  107. struct callback_data {
  108. void (*callback)(const char* name, const char* value, void* cookie);
  109. void* cookie;
  110. };
  111. static void trampoline(void* raw_data, const char* name, const char* value, unsigned /*serial*/) {
  112. callback_data* data = reinterpret_cast<callback_data*>(raw_data);
  113. data->callback(name, value, data->cookie);
  114. }
  115. static void property_list_callback(const prop_info* pi, void* data) {
  116. __system_property_read_callback(pi, trampoline, data);
  117. }
  118. int property_list(void (*fn)(const char* name, const char* value, void* cookie), void* cookie) {
  119. callback_data data = { fn, cookie };
  120. return __system_property_foreach(property_list_callback, &data);
  121. }