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