diff options
author | Ethan Yonker <dees_troy@teamw.in> | 2016-01-11 05:26:51 +0100 |
---|---|---|
committer | Dees Troy <dees_troy@teamw.in> | 2016-01-15 01:27:45 +0100 |
commit | 6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b (patch) | |
tree | 6d971c658a3ca67fae74178fae46a250ad2ad604 | |
parent | Improve sdcard partitioning process (diff) | |
download | android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.gz android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.bz2 android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.lz android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.xz android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.tar.zst android_bootable_recovery-6bb26b5f14258c9fa0ed2b31d8cc0f25e0b24e1b.zip |
-rw-r--r-- | toolbox/Android.mk | 14 | ||||
-rw-r--r-- | toolbox/dynarray.c | 104 | ||||
-rw-r--r-- | toolbox/dynarray.h | 80 | ||||
-rw-r--r-- | toolbox/getprop.c | 50 | ||||
-rw-r--r-- | toolbox/setprop.c | 18 |
5 files changed, 266 insertions, 0 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk index d54bfa440..4698cd250 100644 --- a/toolbox/Android.mk +++ b/toolbox/Android.mk @@ -1,3 +1,4 @@ +TWRP_TOOLBOX_PATH := $(call my-dir) LOCAL_PATH := system/core/toolbox include $(CLEAR_VARS) @@ -212,6 +213,19 @@ ifneq (,$(filter $(PLATFORM_SDK_VERSION), 21 22 23)) LOCAL_WHOLE_STATIC_LIBRARIES := $(patsubst %,libtoolbox_%,$(BSD_TOOLS)) endif +ifeq ($(shell test $(PLATFORM_SDK_VERSION) -gt 22; echo $$?),0) + # Rule to make getprop and setprop in M trees where toybox normally + # provides these tools. Toybox does not allow for easy dynamic + # configuration, so we would have to include the entire toybox binary + # which takes up more space than is necessary so long as we are still + # including busybox. + LOCAL_SRC_FILES += \ + ../../../$(TWRP_TOOLBOX_PATH)/dynarray.c \ + ../../../$(TWRP_TOOLBOX_PATH)/getprop.c \ + ../../../$(TWRP_TOOLBOX_PATH)/setprop.c + OUR_TOOLS += getprop setprop +endif + LOCAL_MODULE := toolbox_recovery LOCAL_MODULE_STEM := toolbox LOCAL_MODULE_PATH := $(TARGET_RECOVERY_ROOT_OUT)/sbin diff --git a/toolbox/dynarray.c b/toolbox/dynarray.c new file mode 100644 index 000000000..3c35aa950 --- /dev/null +++ b/toolbox/dynarray.c @@ -0,0 +1,104 @@ +#include "dynarray.h" +#include <stdlib.h> +#include <limits.h> +#include <string.h> + +void +dynarray_init( dynarray_t *a ) +{ + a->count = a->capacity = 0; + a->items = NULL; +} + + +static void +dynarray_reserve_more( dynarray_t *a, int count ) +{ + int old_cap = a->capacity; + int new_cap = old_cap; + const int max_cap = INT_MAX/sizeof(void*); + void** new_items; + int new_count = a->count + count; + + if (count <= 0) + return; + + if (count > max_cap - a->count) + abort(); + + new_count = a->count + count; + + while (new_cap < new_count) { + old_cap = new_cap; + new_cap += (new_cap >> 2) + 4; + if (new_cap < old_cap || new_cap > max_cap) { + new_cap = max_cap; + } + } + new_items = realloc(a->items, new_cap*sizeof(void*)); + if (new_items == NULL) + abort(); + + a->items = new_items; + a->capacity = new_cap; +} + +void +dynarray_append( dynarray_t *a, void* item ) +{ + if (a->count >= a->capacity) + dynarray_reserve_more(a, 1); + + a->items[a->count++] = item; +} + +void +dynarray_done( dynarray_t *a ) +{ + free(a->items); + a->items = NULL; + a->count = a->capacity = 0; +} + +// string arrays + +void strlist_init( strlist_t *list ) +{ + dynarray_init(list); +} + +void strlist_append_b( strlist_t *list, const void* str, size_t slen ) +{ + char *copy = malloc(slen+1); + memcpy(copy, str, slen); + copy[slen] = '\0'; + dynarray_append(list, copy); +} + +void strlist_append_dup( strlist_t *list, const char *str) +{ + strlist_append_b(list, str, strlen(str)); +} + +void strlist_done( strlist_t *list ) +{ + STRLIST_FOREACH(list, string, free(string)); + dynarray_done(list); +} + +static int strlist_compare_strings(const void* a, const void* b) +{ + const char *sa = *(const char **)a; + const char *sb = *(const char **)b; + return strcmp(sa, sb); +} + +void strlist_sort( strlist_t *list ) +{ + if (list->count > 0) { + qsort(list->items, + (size_t)list->count, + sizeof(void*), + strlist_compare_strings); + } +} diff --git a/toolbox/dynarray.h b/toolbox/dynarray.h new file mode 100644 index 000000000..f73fb3b9c --- /dev/null +++ b/toolbox/dynarray.h @@ -0,0 +1,80 @@ +#ifndef DYNARRAY_H +#define DYNARRAY_H + +#include <stddef.h> + +/* simple dynamic array of pointers */ +typedef struct { + int count; + int capacity; + void** items; +} dynarray_t; + +#define DYNARRAY_INITIALIZER { 0, 0, NULL } + +void dynarray_init( dynarray_t *a ); +void dynarray_done( dynarray_t *a ); + +void dynarray_append( dynarray_t *a, void* item ); + +/* Used to iterate over a dynarray_t + * _array :: pointer to the array + * _item_type :: type of objects pointed to by the array + * _item :: name of a local variable defined within the loop + * with type '_item_type' + * _stmnt :: C statement that will be executed in each iteration. + * + * You case use 'break' and 'continue' within _stmnt + * + * This macro is only intended for simple uses. I.e. do not add or + * remove items from the array during iteration. + */ +#define DYNARRAY_FOREACH_TYPE(_array,_item_type,_item,_stmnt) \ + do { \ + int _nn_##__LINE__ = 0; \ + for (;_nn_##__LINE__ < (_array)->count; ++ _nn_##__LINE__) { \ + _item_type _item = (_item_type)(_array)->items[_nn_##__LINE__]; \ + _stmnt; \ + } \ + } while (0) + +#define DYNARRAY_FOREACH(_array,_item,_stmnt) \ + DYNARRAY_FOREACH_TYPE(_array,void *,_item,_stmnt) + +/* Simple dynamic string arrays + * + * NOTE: A strlist_t owns the strings it references. + */ +typedef dynarray_t strlist_t; + +#define STRLIST_INITIALIZER DYNARRAY_INITIALIZER + +/* Used to iterate over a strlist_t + * _list :: pointer to strlist_t object + * _string :: name of local variable name defined within the loop with + * type 'char*' + * _stmnt :: C statement executed in each iteration + * + * This macro is only intended for simple uses. Do not add or remove items + * to/from the list during iteration. + */ +#define STRLIST_FOREACH(_list,_string,_stmnt) \ + DYNARRAY_FOREACH_TYPE(_list,char *,_string,_stmnt) + +void strlist_init( strlist_t *list ); + +/* note: strlist_done will free all the strings owned by the list */ +void strlist_done( strlist_t *list ); + +/* append a new string made of the first 'slen' characters from 'str' + * followed by a trailing zero. + */ +void strlist_append_b( strlist_t *list, const void* str, size_t slen ); + +/* append the copy of a given input string to a strlist_t */ +void strlist_append_dup( strlist_t *list, const char *str); + +/* sort the strings in a given list (using strcmp) */ +void strlist_sort( strlist_t *list ); + +#endif /* DYNARRAY_H */
\ No newline at end of file diff --git a/toolbox/getprop.c b/toolbox/getprop.c new file mode 100644 index 000000000..dcc0ea030 --- /dev/null +++ b/toolbox/getprop.c @@ -0,0 +1,50 @@ +#include <stdio.h> +#include <stdlib.h> + +#include <cutils/properties.h> + +#include "dynarray.h" + +static void record_prop(const char* key, const char* name, void* opaque) +{ + strlist_t* list = opaque; + char temp[PROP_VALUE_MAX + PROP_NAME_MAX + 16]; + snprintf(temp, sizeof temp, "[%s]: [%s]", key, name); + strlist_append_dup(list, temp); +} + +static void list_properties(void) +{ + strlist_t list[1] = { STRLIST_INITIALIZER }; + + /* Record properties in the string list */ + (void)property_list(record_prop, list); + + /* Sort everything */ + strlist_sort(list); + + /* print everything */ + STRLIST_FOREACH(list, str, printf("%s\n", str)); + + /* voila */ + strlist_done(list); +} + +int getprop_main(int argc, char *argv[]) +{ + if (argc == 1) { + list_properties(); + } else { + char value[PROPERTY_VALUE_MAX]; + char *default_value; + if(argc > 2) { + default_value = argv[2]; + } else { + default_value = ""; + } + + property_get(argv[1], value, default_value); + printf("%s\n", value); + } + return 0; +} diff --git a/toolbox/setprop.c b/toolbox/setprop.c new file mode 100644 index 000000000..63ad2b426 --- /dev/null +++ b/toolbox/setprop.c @@ -0,0 +1,18 @@ +#include <stdio.h> + +#include <cutils/properties.h> + +int setprop_main(int argc, char *argv[]) +{ + if(argc != 3) { + fprintf(stderr,"usage: setprop <key> <value>\n"); + return 1; + } + + if(property_set(argv[1], argv[2])){ + fprintf(stderr,"could not set property\n"); + return 1; + } + + return 0; +} |