Changeset c30a015 in mainline for uspace/lib
- Timestamp:
- 2012-03-03T10:32:47Z (13 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 7689590
- Parents:
- d0d7afb (diff), 0499235 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - Location:
- uspace/lib/c
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/generic/str.c
rd0d7afb rc30a015 259 259 260 260 return offset; 261 } 262 263 /** Get size of string with size limit. 264 * 265 * Get the number of bytes which are used by the string @a str 266 * (excluding the NULL-terminator), but no more than @max_size bytes. 267 * 268 * @param str String to consider. 269 * @param max_size Maximum number of bytes to measure. 270 * 271 * @return Number of bytes used by the string 272 * 273 */ 274 size_t str_nsize(const char *str, size_t max_size) 275 { 276 size_t size = 0; 277 278 while ((*str++ != 0) && (size < max_size)) 279 size++; 280 281 return size; 282 } 283 284 /** Get size of wide string with size limit. 285 * 286 * Get the number of bytes which are used by the wide string @a str 287 * (excluding the NULL-terminator), but no more than @max_size bytes. 288 * 289 * @param str Wide string to consider. 290 * @param max_size Maximum number of bytes to measure. 291 * 292 * @return Number of bytes used by the wide string 293 * 294 */ 295 size_t wstr_nsize(const wchar_t *str, size_t max_size) 296 { 297 return (wstr_nlength(str, max_size) * sizeof(wchar_t)); 261 298 } 262 299 -
uspace/lib/c/generic/sysinfo.c
rd0d7afb rc30a015 40 40 #include <bool.h> 41 41 42 /** Get sysinfo keys size 43 * 44 * @param path Sysinfo path. 45 * @param value Pointer to store the keys size. 46 * 47 * @return EOK if the keys were successfully read. 48 * 49 */ 50 static int sysinfo_get_keys_size(const char *path, size_t *size) 51 { 52 return (int) __SYSCALL3(SYS_SYSINFO_GET_KEYS_SIZE, (sysarg_t) path, 53 (sysarg_t) str_size(path), (sysarg_t) size); 54 } 55 56 /** Get sysinfo keys 57 * 58 * @param path Sysinfo path. 59 * @param value Pointer to store the keys size. 60 * 61 * @return Keys read from sysinfo or NULL if the 62 * sysinfo item has no subkeys. 63 * The returned non-NULL pointer should be 64 * freed by free(). 65 * 66 */ 67 char *sysinfo_get_keys(const char *path, size_t *size) 68 { 69 /* 70 * The size of the keys might change during time. 71 * Unfortunatelly we cannot allocate the buffer 72 * and transfer the keys as a single atomic operation. 73 */ 74 75 /* Get the keys size */ 76 int ret = sysinfo_get_keys_size(path, size); 77 if ((ret != EOK) || (size == 0)) { 78 /* 79 * Item with no subkeys. 80 */ 81 *size = 0; 82 return NULL; 83 } 84 85 char *data = malloc(*size); 86 if (data == NULL) { 87 *size = 0; 88 return NULL; 89 } 90 91 /* Get the data */ 92 size_t sz; 93 ret = __SYSCALL5(SYS_SYSINFO_GET_KEYS, (sysarg_t) path, 94 (sysarg_t) str_size(path), (sysarg_t) data, (sysarg_t) *size, 95 (sysarg_t) &sz); 96 if (ret == EOK) { 97 *size = sz; 98 return data; 99 } 100 101 free(data); 102 *size = 0; 103 return NULL; 104 } 105 42 106 /** Get sysinfo item type 43 107 * … … 70 134 /** Get sysinfo binary data size 71 135 * 72 * @param path 73 * @param value Pointer to store the binary data size.136 * @param path Sysinfo path. 137 * @param size Pointer to store the binary data size. 74 138 * 75 139 * @return EOK if the value was successfully read and … … 85 149 /** Get sysinfo binary data 86 150 * 87 * @param path 88 * @param value Pointer to store the binary data size.151 * @param path Sysinfo path. 152 * @param size Pointer to store the binary data size. 89 153 * 90 154 * @return Binary data read from sysinfo or NULL if the … … 134 198 } 135 199 200 /** Get sysinfo property 201 * 202 * @param path Sysinfo path. 203 * @param name Property name. 204 * @param size Pointer to store the binary data size. 205 * 206 * @return Property value read from sysinfo or NULL if the 207 * sysinfo item value type is not binary data. 208 * The returned non-NULL pointer should be 209 * freed by free(). 210 * 211 */ 212 void *sysinfo_get_property(const char *path, const char *name, size_t *size) 213 { 214 size_t total_size; 215 void *data = sysinfo_get_data(path, &total_size); 216 if ((data == NULL) || (total_size == 0)) { 217 *size = 0; 218 return NULL; 219 } 220 221 size_t pos = 0; 222 while (pos < total_size) { 223 /* Process each property with sanity checks */ 224 size_t cur_size = str_nsize(data + pos, total_size - pos); 225 if (((char *) data)[pos + cur_size] != 0) 226 break; 227 228 bool found = (str_cmp(data + pos, name) == 0); 229 230 pos += cur_size + 1; 231 if (pos >= total_size) 232 break; 233 234 /* Process value size */ 235 size_t value_size; 236 memcpy(&value_size, data + pos, sizeof(value_size)); 237 238 pos += sizeof(value_size); 239 if ((pos >= total_size) || (pos + value_size > total_size)) 240 break; 241 242 if (found) { 243 void *value = malloc(value_size); 244 if (value == NULL) 245 break; 246 247 memcpy(value, data + pos, value_size); 248 free(data); 249 250 *size = value_size; 251 return value; 252 } 253 254 pos += value_size; 255 } 256 257 free(data); 258 259 *size = 0; 260 return NULL; 261 } 262 136 263 /** @} 137 264 */ -
uspace/lib/c/include/str.h
rd0d7afb rc30a015 60 60 extern size_t str_size(const char *str); 61 61 extern size_t wstr_size(const wchar_t *str); 62 63 extern size_t str_nsize(const char *str, size_t max_size); 64 extern size_t wstr_nsize(const wchar_t *str, size_t max_size); 62 65 63 66 extern size_t str_lsize(const char *str, size_t max_len); -
uspace/lib/c/include/sysinfo.h
rd0d7afb rc30a015 40 40 #include <abi/sysinfo.h> 41 41 42 extern char *sysinfo_get_keys(const char *, size_t *); 42 43 extern sysinfo_item_val_type_t sysinfo_get_val_type(const char *); 43 44 extern int sysinfo_get_value(const char *, sysarg_t *); 44 45 extern void *sysinfo_get_data(const char *, size_t *); 46 extern void *sysinfo_get_property(const char *, const char *, size_t *); 45 47 46 48 #endif
Note:
See TracChangeset
for help on using the changeset viewer.