toolbox: make getprop print a sorted list of properties
This patch also factors out the dynarray_t/strlist_t code
from ls.c and moves it to dynarray.[hc].
Change-Id: Ifae2b364d7c2733aad5551ad3c78ae72f8ac31f4
diff --git a/toolbox/getprop.c b/toolbox/getprop.c
index fc80a4d..616644a 100644
--- a/toolbox/getprop.c
+++ b/toolbox/getprop.c
@@ -1,13 +1,34 @@
#include <stdio.h>
+#include <stdlib.h>
#include <cutils/properties.h>
#include <sys/system_properties.h>
+#include "dynarray.h"
-static void proplist(const char *key, const char *name,
- void *user __attribute__((unused)))
+static void record_prop(const char* key, const char* name, void* opaque)
{
- printf("[%s]: [%s]\n", key, name);
+ 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 __system_property_wait(prop_info *pi);
@@ -17,7 +38,7 @@
int n = 0;
if (argc == 1) {
- (void)property_list(proplist, NULL);
+ list_properties();
} else {
char value[PROPERTY_VALUE_MAX];
char *default_value;