1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2006 The Android Open Source Project
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
5bf215546Sopenharmony_ci * you may not use this file except in compliance with the License.
6bf215546Sopenharmony_ci * You may obtain a copy of the License at
7bf215546Sopenharmony_ci *
8bf215546Sopenharmony_ci *      http://www.apache.org/licenses/LICENSE-2.0
9bf215546Sopenharmony_ci *
10bf215546Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
11bf215546Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
12bf215546Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13bf215546Sopenharmony_ci * See the License for the specific language governing permissions and
14bf215546Sopenharmony_ci * limitations under the License.
15bf215546Sopenharmony_ci */
16bf215546Sopenharmony_ci
17bf215546Sopenharmony_ci#pragma once
18bf215546Sopenharmony_ci
19bf215546Sopenharmony_ci#include <sys/cdefs.h>
20bf215546Sopenharmony_ci#include <stddef.h>
21bf215546Sopenharmony_ci#include <stdint.h>
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci#if __has_include(<sys/system_properties.h>)
24bf215546Sopenharmony_ci#include <sys/system_properties.h>
25bf215546Sopenharmony_ci#else
26bf215546Sopenharmony_ci#define PROP_VALUE_MAX 92
27bf215546Sopenharmony_ci#endif
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#ifdef __cplusplus
30bf215546Sopenharmony_ciextern "C" {
31bf215546Sopenharmony_ci#endif
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci//
34bf215546Sopenharmony_ci// Deprecated.
35bf215546Sopenharmony_ci//
36bf215546Sopenharmony_ci// See <android-base/properties.h> for better API.
37bf215546Sopenharmony_ci//
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#define PROPERTY_KEY_MAX PROP_NAME_MAX
40bf215546Sopenharmony_ci#define PROPERTY_VALUE_MAX PROP_VALUE_MAX
41bf215546Sopenharmony_ci
42bf215546Sopenharmony_ci/* property_get: returns the length of the value which will never be
43bf215546Sopenharmony_ci** greater than PROPERTY_VALUE_MAX - 1 and will always be zero terminated.
44bf215546Sopenharmony_ci** (the length does not include the terminating zero).
45bf215546Sopenharmony_ci**
46bf215546Sopenharmony_ci** If the property read fails or returns an empty value, the default
47bf215546Sopenharmony_ci** value is used (if nonnull).
48bf215546Sopenharmony_ci*/
49bf215546Sopenharmony_ciint property_get(const char* key, char* value, const char* default_value);
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci/* property_get_bool: returns the value of key coerced into a
52bf215546Sopenharmony_ci** boolean. If the property is not set, then the default value is returned.
53bf215546Sopenharmony_ci**
54bf215546Sopenharmony_ci* The following is considered to be true (1):
55bf215546Sopenharmony_ci**   "1", "true", "y", "yes", "on"
56bf215546Sopenharmony_ci**
57bf215546Sopenharmony_ci** The following is considered to be false (0):
58bf215546Sopenharmony_ci**   "0", "false", "n", "no", "off"
59bf215546Sopenharmony_ci**
60bf215546Sopenharmony_ci** The conversion is whitespace-sensitive (e.g. " off" will not be false).
61bf215546Sopenharmony_ci**
62bf215546Sopenharmony_ci** If no property with this key is set (or the key is NULL) or the boolean
63bf215546Sopenharmony_ci** conversion fails, the default value is returned.
64bf215546Sopenharmony_ci**/
65bf215546Sopenharmony_ciint8_t property_get_bool(const char *key, int8_t default_value);
66bf215546Sopenharmony_ci
67bf215546Sopenharmony_ci/* property_get_int64: returns the value of key truncated and coerced into a
68bf215546Sopenharmony_ci** int64_t. If the property is not set, then the default value is used.
69bf215546Sopenharmony_ci**
70bf215546Sopenharmony_ci** The numeric conversion is identical to strtoimax with the base inferred:
71bf215546Sopenharmony_ci** - All digits up to the first non-digit characters are read
72bf215546Sopenharmony_ci** - The longest consecutive prefix of digits is converted to a long
73bf215546Sopenharmony_ci**
74bf215546Sopenharmony_ci** Valid strings of digits are:
75bf215546Sopenharmony_ci** - An optional sign character + or -
76bf215546Sopenharmony_ci** - An optional prefix indicating the base (otherwise base 10 is assumed)
77bf215546Sopenharmony_ci**   -- 0 prefix is octal
78bf215546Sopenharmony_ci**   -- 0x / 0X prefix is hex
79bf215546Sopenharmony_ci**
80bf215546Sopenharmony_ci** Leading/trailing whitespace is ignored. Overflow/underflow will cause
81bf215546Sopenharmony_ci** numeric conversion to fail.
82bf215546Sopenharmony_ci**
83bf215546Sopenharmony_ci** If no property with this key is set (or the key is NULL) or the numeric
84bf215546Sopenharmony_ci** conversion fails, the default value is returned.
85bf215546Sopenharmony_ci**/
86bf215546Sopenharmony_ciint64_t property_get_int64(const char *key, int64_t default_value);
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci/* property_get_int32: returns the value of key truncated and coerced into an
89bf215546Sopenharmony_ci** int32_t. If the property is not set, then the default value is used.
90bf215546Sopenharmony_ci**
91bf215546Sopenharmony_ci** The numeric conversion is identical to strtoimax with the base inferred:
92bf215546Sopenharmony_ci** - All digits up to the first non-digit characters are read
93bf215546Sopenharmony_ci** - The longest consecutive prefix of digits is converted to a long
94bf215546Sopenharmony_ci**
95bf215546Sopenharmony_ci** Valid strings of digits are:
96bf215546Sopenharmony_ci** - An optional sign character + or -
97bf215546Sopenharmony_ci** - An optional prefix indicating the base (otherwise base 10 is assumed)
98bf215546Sopenharmony_ci**   -- 0 prefix is octal
99bf215546Sopenharmony_ci**   -- 0x / 0X prefix is hex
100bf215546Sopenharmony_ci**
101bf215546Sopenharmony_ci** Leading/trailing whitespace is ignored. Overflow/underflow will cause
102bf215546Sopenharmony_ci** numeric conversion to fail.
103bf215546Sopenharmony_ci**
104bf215546Sopenharmony_ci** If no property with this key is set (or the key is NULL) or the numeric
105bf215546Sopenharmony_ci** conversion fails, the default value is returned.
106bf215546Sopenharmony_ci**/
107bf215546Sopenharmony_ciint32_t property_get_int32(const char *key, int32_t default_value);
108bf215546Sopenharmony_ci
109bf215546Sopenharmony_ci/* property_set: returns 0 on success, < 0 on failure
110bf215546Sopenharmony_ci*/
111bf215546Sopenharmony_ciint property_set(const char *key, const char *value);
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ciint property_list(void (*propfn)(const char *key, const char *value, void *cookie), void *cookie);
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_ci#if defined(__BIONIC_FORTIFY)
116bf215546Sopenharmony_ci#define __property_get_err_str "property_get() called with too small of a buffer"
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_ci#if defined(__clang__)
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_ci/* Some projects use -Weverything; diagnose_if is clang-specific. */
121bf215546Sopenharmony_ci#pragma clang diagnostic push
122bf215546Sopenharmony_ci#pragma clang diagnostic ignored "-Wgcc-compat"
123bf215546Sopenharmony_ciint property_get(const char* key, char* value, const char* default_value)
124bf215546Sopenharmony_ci    __clang_error_if(__bos(value) != __BIONIC_FORTIFY_UNKNOWN_SIZE &&
125bf215546Sopenharmony_ci                         __bos(value) < PROPERTY_VALUE_MAX,
126bf215546Sopenharmony_ci                     __property_get_err_str);
127bf215546Sopenharmony_ci#pragma clang diagnostic pop
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci#else /* defined(__clang__) */
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ciextern int __property_get_real(const char *, char *, const char *)
132bf215546Sopenharmony_ci    __asm__(__USER_LABEL_PREFIX__ "property_get");
133bf215546Sopenharmony_ci__errordecl(__property_get_too_small_error, __property_get_err_str);
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci__BIONIC_FORTIFY_INLINE
136bf215546Sopenharmony_ciint property_get(const char *key, char *value, const char *default_value) {
137bf215546Sopenharmony_ci    size_t bos = __bos(value);
138bf215546Sopenharmony_ci    if (bos < PROPERTY_VALUE_MAX) {
139bf215546Sopenharmony_ci        __property_get_too_small_error();
140bf215546Sopenharmony_ci    }
141bf215546Sopenharmony_ci    return __property_get_real(key, value, default_value);
142bf215546Sopenharmony_ci}
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_ci#endif /* defined(__clang__) */
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_ci#undef __property_get_err_str
147bf215546Sopenharmony_ci#endif /* defined(__BIONIC_FORTIFY) */
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci#ifdef __cplusplus
150bf215546Sopenharmony_ci}
151bf215546Sopenharmony_ci#endif
152