The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | #ifndef __CUTILS_PROPERTIES_H |
| 18 | #define __CUTILS_PROPERTIES_H |
| 19 | |
Nick Kralevich | 0e54ec8 | 2013-05-23 10:01:42 -0700 | [diff] [blame] | 20 | #include <sys/cdefs.h> |
| 21 | #include <stddef.h> |
Nick Kralevich | 53df3ad | 2013-05-23 09:55:41 -0700 | [diff] [blame] | 22 | #include <sys/system_properties.h> |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 23 | #include <stdint.h> |
Nick Kralevich | 53df3ad | 2013-05-23 09:55:41 -0700 | [diff] [blame] | 24 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | /* System properties are *small* name value pairs managed by the |
| 30 | ** property service. If your data doesn't fit in the provided |
| 31 | ** space it is not appropriate for a system property. |
| 32 | ** |
| 33 | ** WARNING: system/bionic/include/sys/system_properties.h also defines |
| 34 | ** these, but with different names. (TODO: fix that) |
| 35 | */ |
Nick Kralevich | 53df3ad | 2013-05-23 09:55:41 -0700 | [diff] [blame] | 36 | #define PROPERTY_KEY_MAX PROP_NAME_MAX |
| 37 | #define PROPERTY_VALUE_MAX PROP_VALUE_MAX |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | |
| 39 | /* property_get: returns the length of the value which will never be |
| 40 | ** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated. |
| 41 | ** (the length does not include the terminating zero). |
| 42 | ** |
| 43 | ** If the property read fails or returns an empty value, the default |
| 44 | ** value is used (if nonnull). |
| 45 | */ |
| 46 | int property_get(const char *key, char *value, const char *default_value); |
| 47 | |
Igor Murashkin | d8f2a8d | 2014-04-08 11:22:42 -0700 | [diff] [blame] | 48 | /* property_get_bool: returns the value of key coerced into a |
| 49 | ** boolean. If the property is not set, then the default value is returned. |
| 50 | ** |
| 51 | * The following is considered to be true (1): |
| 52 | ** "1", "true", "y", "yes", "on" |
| 53 | ** |
| 54 | ** The following is considered to be false (0): |
| 55 | ** "0", "false", "n", "no", "off" |
| 56 | ** |
| 57 | ** The conversion is whitespace-sensitive (e.g. " off" will not be false). |
| 58 | ** |
| 59 | ** If no property with this key is set (or the key is NULL) or the boolean |
| 60 | ** conversion fails, the default value is returned. |
| 61 | **/ |
| 62 | int8_t property_get_bool(const char *key, int8_t default_value); |
| 63 | |
| 64 | /* property_get_int64: returns the value of key truncated and coerced into a |
| 65 | ** int64_t. If the property is not set, then the default value is used. |
| 66 | ** |
| 67 | ** The numeric conversion is identical to strtoimax with the base inferred: |
| 68 | ** - All digits up to the first non-digit characters are read |
| 69 | ** - The longest consecutive prefix of digits is converted to a long |
| 70 | ** |
| 71 | ** Valid strings of digits are: |
| 72 | ** - An optional sign character + or - |
| 73 | ** - An optional prefix indicating the base (otherwise base 10 is assumed) |
| 74 | ** -- 0 prefix is octal |
| 75 | ** -- 0x / 0X prefix is hex |
| 76 | ** |
| 77 | ** Leading/trailing whitespace is ignored. Overflow/underflow will cause |
| 78 | ** numeric conversion to fail. |
| 79 | ** |
| 80 | ** If no property with this key is set (or the key is NULL) or the numeric |
| 81 | ** conversion fails, the default value is returned. |
| 82 | **/ |
| 83 | int64_t property_get_int64(const char *key, int64_t default_value); |
| 84 | |
| 85 | /* property_get_int32: returns the value of key truncated and coerced into an |
| 86 | ** int32_t. If the property is not set, then the default value is used. |
| 87 | ** |
| 88 | ** The numeric conversion is identical to strtoimax with the base inferred: |
| 89 | ** - All digits up to the first non-digit characters are read |
| 90 | ** - The longest consecutive prefix of digits is converted to a long |
| 91 | ** |
| 92 | ** Valid strings of digits are: |
| 93 | ** - An optional sign character + or - |
| 94 | ** - An optional prefix indicating the base (otherwise base 10 is assumed) |
| 95 | ** -- 0 prefix is octal |
| 96 | ** -- 0x / 0X prefix is hex |
| 97 | ** |
| 98 | ** Leading/trailing whitespace is ignored. Overflow/underflow will cause |
| 99 | ** numeric conversion to fail. |
| 100 | ** |
| 101 | ** If no property with this key is set (or the key is NULL) or the numeric |
| 102 | ** conversion fails, the default value is returned. |
| 103 | **/ |
| 104 | int32_t property_get_int32(const char *key, int32_t default_value); |
| 105 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 106 | /* property_set: returns 0 on success, < 0 on failure |
| 107 | */ |
| 108 | int property_set(const char *key, const char *value); |
| 109 | |
| 110 | int property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie); |
| 111 | |
Nick Kralevich | 0e54ec8 | 2013-05-23 10:01:42 -0700 | [diff] [blame] | 112 | #if defined(__BIONIC_FORTIFY) |
| 113 | |
| 114 | extern int __property_get_real(const char *, char *, const char *) |
| 115 | __asm__(__USER_LABEL_PREFIX__ "property_get"); |
Nick Kralevich | bda25e4 | 2013-06-18 10:57:25 -0700 | [diff] [blame] | 116 | __errordecl(__property_get_too_small_error, "property_get() called with too small of a buffer"); |
Nick Kralevich | 0e54ec8 | 2013-05-23 10:01:42 -0700 | [diff] [blame] | 117 | |
| 118 | __BIONIC_FORTIFY_INLINE |
| 119 | int property_get(const char *key, char *value, const char *default_value) { |
| 120 | size_t bos = __bos(value); |
| 121 | if (bos < PROPERTY_VALUE_MAX) { |
| 122 | __property_get_too_small_error(); |
| 123 | } |
| 124 | return __property_get_real(key, value, default_value); |
| 125 | } |
| 126 | |
| 127 | #endif |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 128 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 129 | #ifdef __cplusplus |
| 130 | } |
| 131 | #endif |
| 132 | |
| 133 | #endif |